Skip to content

Commit

Permalink
feat: db name configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 committed Nov 5, 2024
1 parent 354e59e commit 07aae0b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ wget https://raw.githubusercontent.com/SergiX44/php-benchmark-script/master/rand
# io: file read/write/zip/unzip
wget https://raw.githubusercontent.com/SergiX44/php-benchmark-script/master/io.bench.php
# mysql: selects, updates, deletes, transactions, ....
# you need to pass the args --mysql_user=xxx --mysql_password=xxx --mysql_host=xxx
# you need to pass the args --mysql_user=xxx --mysql_password=xxx --mysql_host=xxx (optional: --mysql_port=xxx --mysql_database=xxx)
wget https://raw.githubusercontent.com/SergiX44/php-benchmark-script/master/mysql.bench.php
```

Expand Down
34 changes: 19 additions & 15 deletions bench.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
$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 = array_merge($defaultArgs, get_args($defaultArgs));
$setupHooks = [];
$cleanupHooks = [];

$additionalBenchmarks = loadAdditionalBenchmarks();
$args = array_merge($defaultArgs, get_args($defaultArgs));

/** @var array<string, callable> $benchmarks */
// the benchmarks!
$benchmarks = [
Expand Down Expand Up @@ -273,7 +270,6 @@
$lf = $isCli ? PHP_EOL : '<br>';
$w = 55;
$multiplier = $args['multiplier'];
$additionalBenchmarks = loadAdditionalBenchmarks();
$extraLines = [];
$currentBenchmark = null;

Expand All @@ -299,12 +295,13 @@
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);
}

printLine('', '', '-', STR_PAD_BOTH);

$stopwatch = new StopWatch();

foreach ($benchmarks as $name => $benchmark) {
Expand All @@ -322,18 +319,18 @@
}
}

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

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

printLine('', '', '-');
foreach ($cleanupHooks as $hook) {
$hook($args);
}

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 Down Expand Up @@ -395,7 +392,7 @@ function get_args($expectedArgs)

// cast the type to the original type if needed
foreach ($expectedArgs as $key => $value) {
if (isset($args[$key]) && $value !== null) {
if (isset($args[$key]) && $value !== null) {
settype($args[$key], gettype($value));
}
}
Expand Down Expand Up @@ -456,7 +453,8 @@ function runBenchmark($stopwatch, $benchmark, $multiplier = 1)
return number_format($time, 4) . ' s';
}

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

if (!empty($endStr)) {
Expand All @@ -476,4 +474,10 @@ function teardown(callable $hook)
{
global $cleanupHooks;
$cleanupHooks[] = $hook;
}

function pushArgs($args)
{
global $defaultArgs;
$defaultArgs = array_merge($defaultArgs, $args);
}
70 changes: 48 additions & 22 deletions mysql.bench.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

$initialRowCount = 1000;
$mysqli = null;
$dbName = null;

setup(function ($args) use (&$mysqli, $initialRowCount) {
pushArgs([
'mysql_host' => '127.0.0.1',
'mysql_user' => null,
'mysql_password' => null,
'mysql_port' => 3306,
'mysql_database' => null,
]);

setup(function ($args) use (&$mysqli, $initialRowCount, &$dbName) {
if (!extension_loaded('mysqli')) {
print('The mysqli extension is not loaded' . PHP_EOL);
return;
Expand All @@ -22,25 +31,42 @@
return;
}

// drop database
$mysqli->query("DROP DATABASE IF EXISTS `bench_test`");
// create database
$mysqli->query("CREATE DATABASE IF NOT EXISTS `bench_test`");
$mysqli->select_db('bench_test');
$mysqli->query("CREATE TABLE IF NOT EXISTS `bench_test`.`test` (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255))");
$dbName = $args['mysql_database'] ?? 'bench_test';

// check if database exists
$result = $mysqli->query("SELECT schema_name FROM information_schema.schemata WHERE schema_name = '$dbName'");

// if exists, delete all tables
if ($result->num_rows > 0) {
$result = $mysqli->query("SELECT table_name FROM information_schema.tables WHERE table_schema = '$dbName'");
while ($row = $result->fetch_assoc()) {
$mysqli->query("DROP TABLE IF EXISTS `$dbName`.`{$row['table_name']}`");
}
} else {
$mysqli->query("CREATE DATABASE IF NOT EXISTS `$dbName`");
}

$mysqli->select_db($dbName);
$mysqli->query("CREATE TABLE IF NOT EXISTS `$dbName`.`test` (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255))");

for ($i = 0; $i < $initialRowCount; $i++) {
$values[] = "('test$i')";
}
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES " . implode(',', $values));
$r = $mysqli->query("INSERT INTO `$dbName`.`test` (name) VALUES " . implode(',', $values));
if (!$r) {
printf("Mysql Error (%s) %s\n", $mysqli->errno, $mysqli->error);
}
});

teardown(function () use (&$mysqli) {
teardown(function () use (&$mysqli, $dbName) {
if ($mysqli === null) {
return;
}

$mysqli->query("DROP DATABASE IF EXISTS `bench_test`");
$result = $mysqli->query("SELECT table_name FROM information_schema.tables WHERE table_schema = '$dbName'");
while ($row = $result->fetch_assoc()) {
$mysqli->query("DROP TABLE IF EXISTS `$dbName`.`{$row['table_name']}`");
}
$mysqli->close();
});

Expand Down Expand Up @@ -76,7 +102,7 @@
$count = $count * $multiplier;
$time = StopWatch::time();
for ($i = 0; $i < $count; $i++) {
$mysqli->query("SELECT * FROM `bench_test`.`test`");
$mysqli->query("SELECT * FROM `test`");
}
extraStat('q/s', round($count / (StopWatch::time() - $time)));
return $i;
Expand All @@ -88,7 +114,7 @@

$count = $count * $multiplier;
for ($i = 0; $i < $count; $i++) {
$result = $mysqli->query("SELECT * FROM `bench_test`.`test`");
$result = $mysqli->query("SELECT * FROM `test`");
while ($row = $result->fetch_assoc()) {
}
$result->close();
Expand All @@ -103,7 +129,7 @@
$count = $count * $multiplier;
$time = StopWatch::time();
for ($i = 0; $i < $count; $i++) {
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES ('test')");
$mysqli->query("INSERT INTO `test` (name) VALUES ('test')");
}
extraStat('q/s', round($count / (StopWatch::time() - $time)));
return $i;
Expand All @@ -118,7 +144,7 @@
for ($i = 0; $i < $count; $i++) {
$values[] = "('test$i')";
}
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES " . implode(',', $values));
$mysqli->query("INSERT INTO `test` (name) VALUES " . implode(',', $values));
return $i;
},
'update' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
Expand All @@ -129,7 +155,7 @@
$count = $count * $multiplier;
$time = StopWatch::time();
for ($i = 0; $i < $count; $i++) {
$mysqli->query("UPDATE `bench_test`.`test` SET name = 'test' WHERE id = '$i'");
$mysqli->query("UPDATE `test` SET name = 'test' WHERE id = '$i'");
}
extraStat('q/s', round($count / (StopWatch::time() - $time)));
return $i;
Expand All @@ -139,16 +165,16 @@
return INF;
}

$mysqli->query("CREATE INDEX idx ON `bench_test`.`test` (id)");
$mysqli->query("CREATE INDEX idx ON `test` (id)");

$count = $count * $multiplier;
$time = StopWatch::time();
for ($i = 0; $i < $count; $i++) {
$mysqli->query("UPDATE `bench_test`.`test` SET name = 'test' WHERE id = '$i'");
$mysqli->query("UPDATE `test` SET name = 'test' WHERE id = '$i'");
}
extraStat('q/s', round($count / (StopWatch::time() - $time)));

$mysqli->query("DROP INDEX idx ON `bench_test`.`test`");
$mysqli->query("DROP INDEX idx ON `test`");
return $i;
},
'transaction_insert' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
Expand All @@ -160,7 +186,7 @@
$time = StopWatch::time();
for ($i = 0; $i < $count; $i++) {
$mysqli->begin_transaction();
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES ('test')");
$mysqli->query("INSERT INTO `test` (name) VALUES ('test')");
$mysqli->commit();
}
extraStat('t/s', round($count / (StopWatch::time() - $time)));
Expand Down Expand Up @@ -209,8 +235,8 @@
return INF;
}

$mysqli->query("CREATE INDEX idx_name ON `bench_test`.`test` (name)");
$mysqli->query("DROP INDEX idx_name ON `bench_test`.`test`");
$mysqli->query("CREATE INDEX idx_name ON `test` (name)");
$mysqli->query("DROP INDEX idx_name ON `test`");
return 1;
},
'delete' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
Expand All @@ -221,7 +247,7 @@
$count = $count * $multiplier;
$time = StopWatch::time();
for ($i = 0; $i < $count; $i++) {
$mysqli->query("DELETE FROM `bench_test`.`test` WHERE id = '$i'");
$mysqli->query("DELETE FROM `test` WHERE id = '$i'");
}
extraStat('q/s', round($count / (StopWatch::time() - $time)));
return $i;
Expand Down

0 comments on commit 07aae0b

Please sign in to comment.