Skip to content

Commit

Permalink
Merge pull request #7 from sergix44/mysql-tests
Browse files Browse the repository at this point in the history
Mysql bench
  • Loading branch information
sergix44 authored Jun 15, 2024
2 parents 35a3960 + af8e5be commit e23947e
Show file tree
Hide file tree
Showing 3 changed files with 321 additions and 43 deletions.
16 changes: 11 additions & 5 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@ jobs:
extensions: opcache
coverage: none

- uses: shogo82148/actions-setup-mysql@v1
with:
distribution: "mariadb"
mysql-version: "11.4"
root-password: "root"

- name: Run (1)
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
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

- name: Run (2)
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
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

- name: Run (3)
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
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

- name: Run (4)
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
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

- name: Run (5)
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
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
119 changes: 81 additions & 38 deletions bench.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
$defaultArgs = [
// Increase the multiplier if you want to benchmark longer
'multiplier' => 1.0,
'mysql_host' => '127.0.0.1',
'mysql_user' => null,
'mysql_password' => null,
'mysql_port' => 3306,
];

$args = get_args($defaultArgs);
$args = array_merge($defaultArgs, $args);
$args = array_merge($defaultArgs, get_args($defaultArgs));
$setupHooks = [];
$cleanupHooks = [];

/** @var array<string, callable> $benchmarks */
// the benchmarks!
Expand Down Expand Up @@ -269,58 +274,68 @@
$w = 55;
$multiplier = $args['multiplier'];
$additionalBenchmarks = loadAdditionalBenchmarks();

$p = function ($str, $endStr = '', $pad = '.', $mode = STR_PAD_RIGHT) use ($w, $lf) {
if (!empty($endStr)) {
$endStr = " $endStr";
}
$length = max(0, $w - strlen($endStr));
echo str_pad($str, $length, $pad, $mode) . $endStr . $lf;
};
$extraLines = [];
$currentBenchmark = null;

echo $isCli ? '' : '<pre>';
$p('', '', '-');
printLine('', '', '-');
printf('|%s|%s', str_pad(sprintf("PHP BENCHMARK SCRIPT v.%s by @SergiX44", $V), $w - 2, ' ', STR_PAD_BOTH), $lf);
$p('', '', '-');
$p('PHP', PHP_VERSION);
$p('Platform', PHP_OS);
$p('Arch', php_uname('m'));
printLine('', '', '-');
printLine('PHP', PHP_VERSION);
printLine('Platform', PHP_OS);
printLine('Arch', php_uname('m'));
if ($isCli) {
$p('Server', gethostname());
printLine('Server', gethostname());
} else {
$name = @$_SERVER['SERVER_NAME'] ?: 'null';
$addr = @$_SERVER['SERVER_ADDR'] ?: 'null';
$p('Server', "{$name}@{$addr}");
printLine('Server', "{$name}@{$addr}");
}
$p('Max memory usage', ini_get('memory_limit'));
printLine('Max memory usage', ini_get('memory_limit'));
$opStatus = function_exists('opcache_get_status') ? opcache_get_status() : false;
$p('OPCache status', is_array($opStatus) && @$opStatus['opcache_enabled'] ? 'enabled' : 'disabled');
$p('OPCache JIT', is_array($opStatus) && @$opStatus['jit']['enabled'] ? 'enabled' : 'disabled/unavailable');
$p('PCRE JIT', ini_get('pcre.jit') ? 'enabled' : 'disabled');
$p('XDebug extension', extension_loaded('xdebug') ? 'enabled' : 'disabled');
$p('Difficulty multiplier', "{$multiplier}x");
$p('Started at', $now->format('d/m/Y H:i:s.v'));
$p('', '', '-', STR_PAD_BOTH);
printLine('OPCache status', is_array($opStatus) && @$opStatus['opcache_enabled'] ? 'enabled' : 'disabled');
printLine('OPCache JIT', is_array($opStatus) && @$opStatus['jit']['enabled'] ? 'enabled' : 'disabled/unavailable');
printLine('PCRE JIT', ini_get('pcre.jit') ? 'enabled' : 'disabled');
printLine('XDebug extension', extension_loaded('xdebug') ? 'enabled' : 'disabled');
printLine('Difficulty multiplier', "{$multiplier}x");
printLine('Started at', $now->format('d/m/Y H:i:s.v'));
printLine('', '', '-', STR_PAD_BOTH);

foreach ($setupHooks as $hook) {
$hook($args);
}

$stopwatch = new StopWatch();

foreach ($benchmarks as $name => $benchmark) {
$time = runBenchmark($stopwatch, $name, $benchmark, $multiplier);
$p($name, $time);
$currentBenchmark = $name;
$time = runBenchmark($stopwatch, $benchmark, $multiplier);
printLine($name, $time);
}

if (!empty($additionalBenchmarks)) {
$p('Additional Benchmarks', '', '-', STR_PAD_BOTH);
printLine('Additional Benchmarks', '', '-', STR_PAD_BOTH);
foreach ($additionalBenchmarks as $name => $benchmark) {
$time = runBenchmark($stopwatch, $name, $benchmark, $multiplier);
$p($name, $time);
$currentBenchmark = $name;
$time = runBenchmark($stopwatch, $benchmark, $multiplier);
printLine($name, $time);
}
}

$p('', '', '-');
$p('Total time', number_format($stopwatch->totalTime, 4) . ' s');
$p('Peak memory usage', round(memory_get_peak_usage(true) / 1024 / 1024, 2) . ' MiB');
foreach ($cleanupHooks as $hook) {
$hook($args);
}

if (!empty($extraLines)) {
printLine('Extra', '', '-', STR_PAD_BOTH);
foreach ($extraLines as $line) {
printLine($line[0], $line[1]);
}
}

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


Expand All @@ -339,15 +354,15 @@ class StopWatch
*/
public function start()
{
return $this->start = $this->t();
return $this->start = self::time();
}

/**
* @return float
*/
public function stop()
{
$time = $this->t() - $this->start;
$time = self::time() - $this->start;
$this->totalTime += $time;

return $time;
Expand All @@ -356,7 +371,7 @@ public function stop()
/**
* @return float
*/
private function t()
public static function time()
{
return function_exists('hrtime') ? hrtime(true) / 1e9 : microtime(true);
}
Expand All @@ -380,7 +395,7 @@ function get_args($expectedArgs)

// cast the type to the original type if needed
foreach ($expectedArgs as $key => $value) {
if (isset($args[$key])) {
if (isset($args[$key]) && $value !== null) {
settype($args[$key], gettype($value));
}
}
Expand Down Expand Up @@ -416,7 +431,13 @@ function loadAdditionalBenchmarks()
return $benchmarks;
}

function runBenchmark($stopwatch, $name, $benchmark, $multiplier = 1)
function extraStat($name, $value)
{
global $extraLines, $currentBenchmark;
$extraLines[] = ["$currentBenchmark::$name", $value];
}

function runBenchmark($stopwatch, $benchmark, $multiplier = 1)
{
$r = null;
try {
Expand All @@ -433,4 +454,26 @@ function runBenchmark($stopwatch, $name, $benchmark, $multiplier = 1)
}

return number_format($time, 4) . ' s';
}

function printLine($str, $endStr = '', $pad = '.', $mode = STR_PAD_RIGHT) {
global $lf, $w;

if (!empty($endStr)) {
$endStr = " $endStr";
}
$length = max(0, $w - strlen($endStr));
echo str_pad($str, $length, $pad, $mode) . $endStr . $lf;
}

function setup(callable $hook)
{
global $setupHooks;
$setupHooks[] = $hook;
}

function teardown(callable $hook)
{
global $cleanupHooks;
$cleanupHooks[] = $hook;
}
Loading

0 comments on commit e23947e

Please sign in to comment.