Skip to content

Commit 1e3a56d

Browse files
committed
Lests also test mysql
1 parent c36a4fb commit 1e3a56d

File tree

3 files changed

+85
-43
lines changed

3 files changed

+85
-43
lines changed

.github/workflows/php.yml

Lines changed: 11 additions & 5 deletions
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

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -274,38 +274,32 @@
274274
$w = 55;
275275
$multiplier = $args['multiplier'];
276276
$additionalBenchmarks = loadAdditionalBenchmarks();
277-
278-
$p = function ($str, $endStr = '', $pad = '.', $mode = STR_PAD_RIGHT) use ($w, $lf) {
279-
if (!empty($endStr)) {
280-
$endStr = " $endStr";
281-
}
282-
$length = max(0, $w - strlen($endStr));
283-
echo str_pad($str, $length, $pad, $mode) . $endStr . $lf;
284-
};
277+
$extraLines = [];
278+
$currentBenchmark = null;
285279

286280
echo $isCli ? '' : '<pre>';
287-
$p('', '', '-');
281+
printLine('', '', '-');
288282
printf('|%s|%s', str_pad(sprintf("PHP BENCHMARK SCRIPT v.%s by @SergiX44", $V), $w - 2, ' ', STR_PAD_BOTH), $lf);
289-
$p('', '', '-');
290-
$p('PHP', PHP_VERSION);
291-
$p('Platform', PHP_OS);
292-
$p('Arch', php_uname('m'));
283+
printLine('', '', '-');
284+
printLine('PHP', PHP_VERSION);
285+
printLine('Platform', PHP_OS);
286+
printLine('Arch', php_uname('m'));
293287
if ($isCli) {
294-
$p('Server', gethostname());
288+
printLine('Server', gethostname());
295289
} else {
296290
$name = @$_SERVER['SERVER_NAME'] ?: 'null';
297291
$addr = @$_SERVER['SERVER_ADDR'] ?: 'null';
298-
$p('Server', "{$name}@{$addr}");
292+
printLine('Server', "{$name}@{$addr}");
299293
}
300-
$p('Max memory usage', ini_get('memory_limit'));
294+
printLine('Max memory usage', ini_get('memory_limit'));
301295
$opStatus = function_exists('opcache_get_status') ? opcache_get_status() : false;
302-
$p('OPCache status', is_array($opStatus) && @$opStatus['opcache_enabled'] ? 'enabled' : 'disabled');
303-
$p('OPCache JIT', is_array($opStatus) && @$opStatus['jit']['enabled'] ? 'enabled' : 'disabled/unavailable');
304-
$p('PCRE JIT', ini_get('pcre.jit') ? 'enabled' : 'disabled');
305-
$p('XDebug extension', extension_loaded('xdebug') ? 'enabled' : 'disabled');
306-
$p('Difficulty multiplier', "{$multiplier}x");
307-
$p('Started at', $now->format('d/m/Y H:i:s.v'));
308-
$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);
309303

310304
foreach ($setupHooks as $hook) {
311305
$hook($args);
@@ -314,28 +308,36 @@
314308
$stopwatch = new StopWatch();
315309

316310
foreach ($benchmarks as $name => $benchmark) {
311+
$currentBenchmark = $name;
317312
$time = runBenchmark($stopwatch, $benchmark, $multiplier);
318-
$p($name, $time);
313+
printLine($name, $time);
319314
}
320315

321316
if (!empty($additionalBenchmarks)) {
322-
$p('Additional Benchmarks', '', '-', STR_PAD_BOTH);
317+
printLine('Additional Benchmarks', '', '-', STR_PAD_BOTH);
323318
foreach ($additionalBenchmarks as $name => $benchmark) {
319+
$currentBenchmark = $name;
324320
$time = runBenchmark($stopwatch, $benchmark, $multiplier);
325-
$p($name, $time);
321+
printLine($name, $time);
326322
}
327323
}
328324

329-
$p('', '', '-');
330-
$p('Total time', number_format($stopwatch->totalTime, 4) . ' s');
331-
$p('Peak memory usage', round(memory_get_peak_usage(true) / 1024 / 1024, 2) . ' MiB');
332-
333-
echo $isCli ? '' : '</pre>';
334-
335325
foreach ($cleanupHooks as $hook) {
336326
$hook($args);
337327
}
338328

329+
if (!empty($extraLines)) {
330+
printLine('Extra', '', '-', STR_PAD_BOTH);
331+
foreach ($extraLines as $line) {
332+
printLine($line[0], $line[1]);
333+
}
334+
}
335+
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');
339+
echo $isCli ? '' : '</pre>';
340+
339341

340342
class StopWatch
341343
{
@@ -352,15 +354,15 @@ class StopWatch
352354
*/
353355
public function start()
354356
{
355-
return $this->start = $this->t();
357+
return $this->start = self::time();
356358
}
357359

358360
/**
359361
* @return float
360362
*/
361363
public function stop()
362364
{
363-
$time = $this->t() - $this->start;
365+
$time = self::time() - $this->start;
364366
$this->totalTime += $time;
365367

366368
return $time;
@@ -369,7 +371,7 @@ public function stop()
369371
/**
370372
* @return float
371373
*/
372-
private function t()
374+
public static function time()
373375
{
374376
return function_exists('hrtime') ? hrtime(true) / 1e9 : microtime(true);
375377
}
@@ -429,6 +431,12 @@ function loadAdditionalBenchmarks()
429431
return $benchmarks;
430432
}
431433

434+
function extraStat($name, $value)
435+
{
436+
global $extraLines, $currentBenchmark;
437+
$extraLines[] = ["$currentBenchmark::$name", $value];
438+
}
439+
432440
function runBenchmark($stopwatch, $benchmark, $multiplier = 1)
433441
{
434442
$r = null;
@@ -448,6 +456,16 @@ function runBenchmark($stopwatch, $benchmark, $multiplier = 1)
448456
return number_format($time, 4) . ' s';
449457
}
450458

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+
451469
function setup(callable $hook)
452470
{
453471
global $setupHooks;

db/mysql.bench.php renamed to mysql.bench.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@
5959
}
6060

6161
$count = $count * $multiplier;
62+
$time = StopWatch::time();
6263
for ($i = 0; $i < $count; $i++) {
6364
$mysqli->query("SELECT VERSION()");
6465
}
66+
67+
extraStat('q/s', round($count / (StopWatch::time() - $time)));
68+
6569
return $i;
6670
},
6771
'select_all' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
@@ -70,9 +74,11 @@
7074
}
7175

7276
$count = $count * $multiplier;
77+
$time = StopWatch::time();
7378
for ($i = 0; $i < $count; $i++) {
7479
$mysqli->query("SELECT * FROM `bench_test`.`test`");
7580
}
81+
extraStat('q/s', round($count / (StopWatch::time() - $time)));
7682
return $i;
7783
},
7884
'select_cursor' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
@@ -95,9 +101,11 @@
95101
}
96102

97103
$count = $count * $multiplier;
104+
$time = StopWatch::time();
98105
for ($i = 0; $i < $count; $i++) {
99106
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES ('test')");
100107
}
108+
extraStat('q/s', round($count / (StopWatch::time() - $time)));
101109
return $i;
102110
},
103111
'bulk_insert' => function ($multiplier = 1, $count = 100000) use (&$mysqli) {
@@ -113,15 +121,17 @@
113121
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES " . implode(',', $values));
114122
return $i;
115123
},
116-
'update' => function ($multiplier = 1, $count = 100) use (&$mysqli) {
124+
'update' => function ($multiplier = 1, $count = 50) use (&$mysqli) {
117125
if ($mysqli === null) {
118126
return INF;
119127
}
120128

121129
$count = $count * $multiplier;
130+
$time = StopWatch::time();
122131
for ($i = 0; $i < $count; $i++) {
123132
$mysqli->query("UPDATE `bench_test`.`test` SET name = 'test' WHERE id % 2 = 0");
124133
}
134+
extraStat('q/s', round($count / (StopWatch::time() - $time)));
125135
return $i;
126136
},
127137
'transaction_insert' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
@@ -130,14 +140,16 @@
130140
}
131141

132142
$count = $count * $multiplier;
143+
$time = StopWatch::time();
133144
for ($i = 0; $i < $count; $i++) {
134145
$mysqli->begin_transaction();
135146
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES ('test')");
136147
$mysqli->commit();
137148
}
149+
extraStat('t/s', round($count / (StopWatch::time() - $time)));
138150
return $i;
139151
},
140-
'aes_encrypt' => function ($multiplier = 1, $count = 10000) use (&$mysqli) {
152+
'aes_encrypt' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
141153
if ($mysqli === null) {
142154
return INF;
143155
}
@@ -147,14 +159,16 @@
147159

148160
$data = str_repeat('a', 16);
149161
$count = $count * $multiplier;
162+
$time = StopWatch::time();
150163
for ($i = 0; $i < $count; $i++) {
151164
$stmt->execute();
152165
$stmt->get_result()->fetch_assoc();
153166
}
167+
extraStat('q/s', round($count / (StopWatch::time() - $time)));
154168
$stmt->close();
155169
return $i;
156170
},
157-
'aes_decrypt' => function ($multiplier = 1, $count = 10000) use (&$mysqli) {
171+
'aes_decrypt' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
158172
if ($mysqli === null) {
159173
return INF;
160174
}
@@ -164,10 +178,12 @@
164178

165179
$data = str_repeat('a', 16);
166180
$count = $count * $multiplier;
181+
$time = StopWatch::time();
167182
for ($i = 0; $i < $count; $i++) {
168183
$stmt->execute();
169184
$stmt->get_result()->fetch_assoc();
170185
}
186+
extraStat('q/s', round($count / (StopWatch::time() - $time)));
171187
$stmt->close();
172188
return $i;
173189
},
@@ -186,9 +202,11 @@
186202
}
187203

188204
$count = $count * $multiplier;
205+
$time = StopWatch::time();
189206
for ($i = 0; $i < $count; $i++) {
190207
$mysqli->query("DELETE FROM `bench_test`.`test` WHERE id % 2 = 0");
191208
}
209+
extraStat('q/s', round($count / (StopWatch::time() - $time)));
192210
return $i;
193211
},
194212
];

0 commit comments

Comments
 (0)