Skip to content

Commit 07aae0b

Browse files
committed
feat: db name configurable
1 parent 354e59e commit 07aae0b

File tree

3 files changed

+68
-38
lines changed

3 files changed

+68
-38
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ wget https://raw.githubusercontent.com/SergiX44/php-benchmark-script/master/rand
3434
# io: file read/write/zip/unzip
3535
wget https://raw.githubusercontent.com/SergiX44/php-benchmark-script/master/io.bench.php
3636
# mysql: selects, updates, deletes, transactions, ....
37-
# you need to pass the args --mysql_user=xxx --mysql_password=xxx --mysql_host=xxx
37+
# you need to pass the args --mysql_user=xxx --mysql_password=xxx --mysql_host=xxx (optional: --mysql_port=xxx --mysql_database=xxx)
3838
wget https://raw.githubusercontent.com/SergiX44/php-benchmark-script/master/mysql.bench.php
3939
```
4040

bench.php

+19-15
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
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,
1511
];
16-
17-
$args = array_merge($defaultArgs, get_args($defaultArgs));
1812
$setupHooks = [];
1913
$cleanupHooks = [];
2014

15+
$additionalBenchmarks = loadAdditionalBenchmarks();
16+
$args = array_merge($defaultArgs, get_args($defaultArgs));
17+
2118
/** @var array<string, callable> $benchmarks */
2219
// the benchmarks!
2320
$benchmarks = [
@@ -273,7 +270,6 @@
273270
$lf = $isCli ? PHP_EOL : '<br>';
274271
$w = 55;
275272
$multiplier = $args['multiplier'];
276-
$additionalBenchmarks = loadAdditionalBenchmarks();
277273
$extraLines = [];
278274
$currentBenchmark = null;
279275

@@ -299,12 +295,13 @@
299295
printLine('XDebug extension', extension_loaded('xdebug') ? 'enabled' : 'disabled');
300296
printLine('Difficulty multiplier', "{$multiplier}x");
301297
printLine('Started at', $now->format('d/m/Y H:i:s.v'));
302-
printLine('', '', '-', STR_PAD_BOTH);
303298

304299
foreach ($setupHooks as $hook) {
305300
$hook($args);
306301
}
307302

303+
printLine('', '', '-', STR_PAD_BOTH);
304+
308305
$stopwatch = new StopWatch();
309306

310307
foreach ($benchmarks as $name => $benchmark) {
@@ -322,18 +319,18 @@
322319
}
323320
}
324321

325-
foreach ($cleanupHooks as $hook) {
326-
$hook($args);
327-
}
328-
329-
if (!empty($extraLines)) {
322+
if (!empty($extraLines)) {
330323
printLine('Extra', '', '-', STR_PAD_BOTH);
331324
foreach ($extraLines as $line) {
332325
printLine($line[0], $line[1]);
333326
}
334327
}
335328

336329
printLine('', '', '-');
330+
foreach ($cleanupHooks as $hook) {
331+
$hook($args);
332+
}
333+
337334
printLine('Total time', number_format($stopwatch->totalTime, 4) . ' s');
338335
printLine('Peak memory usage', round(memory_get_peak_usage(true) / 1024 / 1024, 2) . ' MiB');
339336
echo $isCli ? '' : '</pre>';
@@ -395,7 +392,7 @@ function get_args($expectedArgs)
395392

396393
// cast the type to the original type if needed
397394
foreach ($expectedArgs as $key => $value) {
398-
if (isset($args[$key]) && $value !== null) {
395+
if (isset($args[$key]) && $value !== null) {
399396
settype($args[$key], gettype($value));
400397
}
401398
}
@@ -456,7 +453,8 @@ function runBenchmark($stopwatch, $benchmark, $multiplier = 1)
456453
return number_format($time, 4) . ' s';
457454
}
458455

459-
function printLine($str, $endStr = '', $pad = '.', $mode = STR_PAD_RIGHT) {
456+
function printLine($str, $endStr = '', $pad = '.', $mode = STR_PAD_RIGHT)
457+
{
460458
global $lf, $w;
461459

462460
if (!empty($endStr)) {
@@ -476,4 +474,10 @@ function teardown(callable $hook)
476474
{
477475
global $cleanupHooks;
478476
$cleanupHooks[] = $hook;
477+
}
478+
479+
function pushArgs($args)
480+
{
481+
global $defaultArgs;
482+
$defaultArgs = array_merge($defaultArgs, $args);
479483
}

mysql.bench.php

+48-22
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
$initialRowCount = 1000;
44
$mysqli = null;
5+
$dbName = null;
56

6-
setup(function ($args) use (&$mysqli, $initialRowCount) {
7+
pushArgs([
8+
'mysql_host' => '127.0.0.1',
9+
'mysql_user' => null,
10+
'mysql_password' => null,
11+
'mysql_port' => 3306,
12+
'mysql_database' => null,
13+
]);
14+
15+
setup(function ($args) use (&$mysqli, $initialRowCount, &$dbName) {
716
if (!extension_loaded('mysqli')) {
817
print('The mysqli extension is not loaded' . PHP_EOL);
918
return;
@@ -22,25 +31,42 @@
2231
return;
2332
}
2433

25-
// drop database
26-
$mysqli->query("DROP DATABASE IF EXISTS `bench_test`");
27-
// create database
28-
$mysqli->query("CREATE DATABASE IF NOT EXISTS `bench_test`");
29-
$mysqli->select_db('bench_test');
30-
$mysqli->query("CREATE TABLE IF NOT EXISTS `bench_test`.`test` (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255))");
34+
$dbName = $args['mysql_database'] ?? 'bench_test';
35+
36+
// check if database exists
37+
$result = $mysqli->query("SELECT schema_name FROM information_schema.schemata WHERE schema_name = '$dbName'");
38+
39+
// if exists, delete all tables
40+
if ($result->num_rows > 0) {
41+
$result = $mysqli->query("SELECT table_name FROM information_schema.tables WHERE table_schema = '$dbName'");
42+
while ($row = $result->fetch_assoc()) {
43+
$mysqli->query("DROP TABLE IF EXISTS `$dbName`.`{$row['table_name']}`");
44+
}
45+
} else {
46+
$mysqli->query("CREATE DATABASE IF NOT EXISTS `$dbName`");
47+
}
48+
49+
$mysqli->select_db($dbName);
50+
$mysqli->query("CREATE TABLE IF NOT EXISTS `$dbName`.`test` (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255))");
3151

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

38-
teardown(function () use (&$mysqli) {
61+
teardown(function () use (&$mysqli, $dbName) {
3962
if ($mysqli === null) {
4063
return;
4164
}
4265

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

@@ -76,7 +102,7 @@
76102
$count = $count * $multiplier;
77103
$time = StopWatch::time();
78104
for ($i = 0; $i < $count; $i++) {
79-
$mysqli->query("SELECT * FROM `bench_test`.`test`");
105+
$mysqli->query("SELECT * FROM `test`");
80106
}
81107
extraStat('q/s', round($count / (StopWatch::time() - $time)));
82108
return $i;
@@ -88,7 +114,7 @@
88114

89115
$count = $count * $multiplier;
90116
for ($i = 0; $i < $count; $i++) {
91-
$result = $mysqli->query("SELECT * FROM `bench_test`.`test`");
117+
$result = $mysqli->query("SELECT * FROM `test`");
92118
while ($row = $result->fetch_assoc()) {
93119
}
94120
$result->close();
@@ -103,7 +129,7 @@
103129
$count = $count * $multiplier;
104130
$time = StopWatch::time();
105131
for ($i = 0; $i < $count; $i++) {
106-
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES ('test')");
132+
$mysqli->query("INSERT INTO `test` (name) VALUES ('test')");
107133
}
108134
extraStat('q/s', round($count / (StopWatch::time() - $time)));
109135
return $i;
@@ -118,7 +144,7 @@
118144
for ($i = 0; $i < $count; $i++) {
119145
$values[] = "('test$i')";
120146
}
121-
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES " . implode(',', $values));
147+
$mysqli->query("INSERT INTO `test` (name) VALUES " . implode(',', $values));
122148
return $i;
123149
},
124150
'update' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
@@ -129,7 +155,7 @@
129155
$count = $count * $multiplier;
130156
$time = StopWatch::time();
131157
for ($i = 0; $i < $count; $i++) {
132-
$mysqli->query("UPDATE `bench_test`.`test` SET name = 'test' WHERE id = '$i'");
158+
$mysqli->query("UPDATE `test` SET name = 'test' WHERE id = '$i'");
133159
}
134160
extraStat('q/s', round($count / (StopWatch::time() - $time)));
135161
return $i;
@@ -139,16 +165,16 @@
139165
return INF;
140166
}
141167

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

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

151-
$mysqli->query("DROP INDEX idx ON `bench_test`.`test`");
177+
$mysqli->query("DROP INDEX idx ON `test`");
152178
return $i;
153179
},
154180
'transaction_insert' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
@@ -160,7 +186,7 @@
160186
$time = StopWatch::time();
161187
for ($i = 0; $i < $count; $i++) {
162188
$mysqli->begin_transaction();
163-
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES ('test')");
189+
$mysqli->query("INSERT INTO `test` (name) VALUES ('test')");
164190
$mysqli->commit();
165191
}
166192
extraStat('t/s', round($count / (StopWatch::time() - $time)));
@@ -209,8 +235,8 @@
209235
return INF;
210236
}
211237

212-
$mysqli->query("CREATE INDEX idx_name ON `bench_test`.`test` (name)");
213-
$mysqli->query("DROP INDEX idx_name ON `bench_test`.`test`");
238+
$mysqli->query("CREATE INDEX idx_name ON `test` (name)");
239+
$mysqli->query("DROP INDEX idx_name ON `test`");
214240
return 1;
215241
},
216242
'delete' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
@@ -221,7 +247,7 @@
221247
$count = $count * $multiplier;
222248
$time = StopWatch::time();
223249
for ($i = 0; $i < $count; $i++) {
224-
$mysqli->query("DELETE FROM `bench_test`.`test` WHERE id = '$i'");
250+
$mysqli->query("DELETE FROM `test` WHERE id = '$i'");
225251
}
226252
extraStat('q/s', round($count / (StopWatch::time() - $time)));
227253
return $i;

0 commit comments

Comments
 (0)