Skip to content

Commit e23947e

Browse files
authored
Merge pull request #7 from sergix44/mysql-tests
Mysql bench
2 parents 35a3960 + af8e5be commit e23947e

File tree

3 files changed

+321
-43
lines changed

3 files changed

+321
-43
lines changed

.github/workflows/php.yml

+11-5
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,23 @@ jobs:
2525
extensions: opcache
2626
coverage: none
2727

28+
- uses: shogo82148/actions-setup-mysql@v1
29+
with:
30+
distribution: "mariadb"
31+
mysql-version: "11.4"
32+
root-password: "root"
33+
2834
- name: Run (1)
29-
run: php -d opcache.enable=1 -d opcache.enable_cli=1 -d pcre.jit=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=32M bench.php
35+
run: php -d opcache.enable=1 -d opcache.enable_cli=1 -d pcre.jit=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=32M bench.php --mysql_user=root --mysql_password=root
3036

3137
- name: Run (2)
32-
run: php -d opcache.enable=1 -d opcache.enable_cli=1 -d pcre.jit=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=32M bench.php
38+
run: php -d opcache.enable=1 -d opcache.enable_cli=1 -d pcre.jit=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=32M bench.php --mysql_user=root --mysql_password=root
3339

3440
- name: Run (3)
35-
run: php -d opcache.enable=1 -d opcache.enable_cli=1 -d pcre.jit=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=32M bench.php
41+
run: php -d opcache.enable=1 -d opcache.enable_cli=1 -d pcre.jit=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=32M bench.php --mysql_user=root --mysql_password=root
3642

3743
- name: Run (4)
38-
run: php -d opcache.enable=1 -d opcache.enable_cli=1 -d pcre.jit=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=32M bench.php
44+
run: php -d opcache.enable=1 -d opcache.enable_cli=1 -d pcre.jit=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=32M bench.php --mysql_user=root --mysql_password=root
3945

4046
- name: Run (5)
41-
run: php -d opcache.enable=1 -d opcache.enable_cli=1 -d pcre.jit=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=32M bench.php
47+
run: php -d opcache.enable=1 -d opcache.enable_cli=1 -d pcre.jit=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=32M bench.php --mysql_user=root --mysql_password=root

bench.php

+81-38
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
$defaultArgs = [
99
// Increase the multiplier if you want to benchmark longer
1010
'multiplier' => 1.0,
11+
'mysql_host' => '127.0.0.1',
12+
'mysql_user' => null,
13+
'mysql_password' => null,
14+
'mysql_port' => 3306,
1115
];
1216

13-
$args = get_args($defaultArgs);
14-
$args = array_merge($defaultArgs, $args);
17+
$args = array_merge($defaultArgs, get_args($defaultArgs));
18+
$setupHooks = [];
19+
$cleanupHooks = [];
1520

1621
/** @var array<string, callable> $benchmarks */
1722
// the benchmarks!
@@ -269,58 +274,68 @@
269274
$w = 55;
270275
$multiplier = $args['multiplier'];
271276
$additionalBenchmarks = loadAdditionalBenchmarks();
272-
273-
$p = function ($str, $endStr = '', $pad = '.', $mode = STR_PAD_RIGHT) use ($w, $lf) {
274-
if (!empty($endStr)) {
275-
$endStr = " $endStr";
276-
}
277-
$length = max(0, $w - strlen($endStr));
278-
echo str_pad($str, $length, $pad, $mode) . $endStr . $lf;
279-
};
277+
$extraLines = [];
278+
$currentBenchmark = null;
280279

281280
echo $isCli ? '' : '<pre>';
282-
$p('', '', '-');
281+
printLine('', '', '-');
283282
printf('|%s|%s', str_pad(sprintf("PHP BENCHMARK SCRIPT v.%s by @SergiX44", $V), $w - 2, ' ', STR_PAD_BOTH), $lf);
284-
$p('', '', '-');
285-
$p('PHP', PHP_VERSION);
286-
$p('Platform', PHP_OS);
287-
$p('Arch', php_uname('m'));
283+
printLine('', '', '-');
284+
printLine('PHP', PHP_VERSION);
285+
printLine('Platform', PHP_OS);
286+
printLine('Arch', php_uname('m'));
288287
if ($isCli) {
289-
$p('Server', gethostname());
288+
printLine('Server', gethostname());
290289
} else {
291290
$name = @$_SERVER['SERVER_NAME'] ?: 'null';
292291
$addr = @$_SERVER['SERVER_ADDR'] ?: 'null';
293-
$p('Server', "{$name}@{$addr}");
292+
printLine('Server', "{$name}@{$addr}");
294293
}
295-
$p('Max memory usage', ini_get('memory_limit'));
294+
printLine('Max memory usage', ini_get('memory_limit'));
296295
$opStatus = function_exists('opcache_get_status') ? opcache_get_status() : false;
297-
$p('OPCache status', is_array($opStatus) && @$opStatus['opcache_enabled'] ? 'enabled' : 'disabled');
298-
$p('OPCache JIT', is_array($opStatus) && @$opStatus['jit']['enabled'] ? 'enabled' : 'disabled/unavailable');
299-
$p('PCRE JIT', ini_get('pcre.jit') ? 'enabled' : 'disabled');
300-
$p('XDebug extension', extension_loaded('xdebug') ? 'enabled' : 'disabled');
301-
$p('Difficulty multiplier', "{$multiplier}x");
302-
$p('Started at', $now->format('d/m/Y H:i:s.v'));
303-
$p('', '', '-', STR_PAD_BOTH);
296+
printLine('OPCache status', is_array($opStatus) && @$opStatus['opcache_enabled'] ? 'enabled' : 'disabled');
297+
printLine('OPCache JIT', is_array($opStatus) && @$opStatus['jit']['enabled'] ? 'enabled' : 'disabled/unavailable');
298+
printLine('PCRE JIT', ini_get('pcre.jit') ? 'enabled' : 'disabled');
299+
printLine('XDebug extension', extension_loaded('xdebug') ? 'enabled' : 'disabled');
300+
printLine('Difficulty multiplier', "{$multiplier}x");
301+
printLine('Started at', $now->format('d/m/Y H:i:s.v'));
302+
printLine('', '', '-', STR_PAD_BOTH);
303+
304+
foreach ($setupHooks as $hook) {
305+
$hook($args);
306+
}
304307

305308
$stopwatch = new StopWatch();
306309

307310
foreach ($benchmarks as $name => $benchmark) {
308-
$time = runBenchmark($stopwatch, $name, $benchmark, $multiplier);
309-
$p($name, $time);
311+
$currentBenchmark = $name;
312+
$time = runBenchmark($stopwatch, $benchmark, $multiplier);
313+
printLine($name, $time);
310314
}
311315

312316
if (!empty($additionalBenchmarks)) {
313-
$p('Additional Benchmarks', '', '-', STR_PAD_BOTH);
317+
printLine('Additional Benchmarks', '', '-', STR_PAD_BOTH);
314318
foreach ($additionalBenchmarks as $name => $benchmark) {
315-
$time = runBenchmark($stopwatch, $name, $benchmark, $multiplier);
316-
$p($name, $time);
319+
$currentBenchmark = $name;
320+
$time = runBenchmark($stopwatch, $benchmark, $multiplier);
321+
printLine($name, $time);
317322
}
318323
}
319324

320-
$p('', '', '-');
321-
$p('Total time', number_format($stopwatch->totalTime, 4) . ' s');
322-
$p('Peak memory usage', round(memory_get_peak_usage(true) / 1024 / 1024, 2) . ' MiB');
325+
foreach ($cleanupHooks as $hook) {
326+
$hook($args);
327+
}
328+
329+
if (!empty($extraLines)) {
330+
printLine('Extra', '', '-', STR_PAD_BOTH);
331+
foreach ($extraLines as $line) {
332+
printLine($line[0], $line[1]);
333+
}
334+
}
323335

336+
printLine('', '', '-');
337+
printLine('Total time', number_format($stopwatch->totalTime, 4) . ' s');
338+
printLine('Peak memory usage', round(memory_get_peak_usage(true) / 1024 / 1024, 2) . ' MiB');
324339
echo $isCli ? '' : '</pre>';
325340

326341

@@ -339,15 +354,15 @@ class StopWatch
339354
*/
340355
public function start()
341356
{
342-
return $this->start = $this->t();
357+
return $this->start = self::time();
343358
}
344359

345360
/**
346361
* @return float
347362
*/
348363
public function stop()
349364
{
350-
$time = $this->t() - $this->start;
365+
$time = self::time() - $this->start;
351366
$this->totalTime += $time;
352367

353368
return $time;
@@ -356,7 +371,7 @@ public function stop()
356371
/**
357372
* @return float
358373
*/
359-
private function t()
374+
public static function time()
360375
{
361376
return function_exists('hrtime') ? hrtime(true) / 1e9 : microtime(true);
362377
}
@@ -380,7 +395,7 @@ function get_args($expectedArgs)
380395

381396
// cast the type to the original type if needed
382397
foreach ($expectedArgs as $key => $value) {
383-
if (isset($args[$key])) {
398+
if (isset($args[$key]) && $value !== null) {
384399
settype($args[$key], gettype($value));
385400
}
386401
}
@@ -416,7 +431,13 @@ function loadAdditionalBenchmarks()
416431
return $benchmarks;
417432
}
418433

419-
function runBenchmark($stopwatch, $name, $benchmark, $multiplier = 1)
434+
function extraStat($name, $value)
435+
{
436+
global $extraLines, $currentBenchmark;
437+
$extraLines[] = ["$currentBenchmark::$name", $value];
438+
}
439+
440+
function runBenchmark($stopwatch, $benchmark, $multiplier = 1)
420441
{
421442
$r = null;
422443
try {
@@ -433,4 +454,26 @@ function runBenchmark($stopwatch, $name, $benchmark, $multiplier = 1)
433454
}
434455

435456
return number_format($time, 4) . ' s';
457+
}
458+
459+
function printLine($str, $endStr = '', $pad = '.', $mode = STR_PAD_RIGHT) {
460+
global $lf, $w;
461+
462+
if (!empty($endStr)) {
463+
$endStr = " $endStr";
464+
}
465+
$length = max(0, $w - strlen($endStr));
466+
echo str_pad($str, $length, $pad, $mode) . $endStr . $lf;
467+
}
468+
469+
function setup(callable $hook)
470+
{
471+
global $setupHooks;
472+
$setupHooks[] = $hook;
473+
}
474+
475+
function teardown(callable $hook)
476+
{
477+
global $cleanupHooks;
478+
$cleanupHooks[] = $hook;
436479
}

0 commit comments

Comments
 (0)