Skip to content

Commit c36a4fb

Browse files
committed
Add mysql tests
1 parent 35a3960 commit c36a4fb

File tree

2 files changed

+225
-6
lines changed

2 files changed

+225
-6
lines changed

bench.php

Lines changed: 31 additions & 6 deletions
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!
@@ -302,17 +307,21 @@
302307
$p('Started at', $now->format('d/m/Y H:i:s.v'));
303308
$p('', '', '-', STR_PAD_BOTH);
304309

310+
foreach ($setupHooks as $hook) {
311+
$hook($args);
312+
}
313+
305314
$stopwatch = new StopWatch();
306315

307316
foreach ($benchmarks as $name => $benchmark) {
308-
$time = runBenchmark($stopwatch, $name, $benchmark, $multiplier);
317+
$time = runBenchmark($stopwatch, $benchmark, $multiplier);
309318
$p($name, $time);
310319
}
311320

312321
if (!empty($additionalBenchmarks)) {
313322
$p('Additional Benchmarks', '', '-', STR_PAD_BOTH);
314323
foreach ($additionalBenchmarks as $name => $benchmark) {
315-
$time = runBenchmark($stopwatch, $name, $benchmark, $multiplier);
324+
$time = runBenchmark($stopwatch, $benchmark, $multiplier);
316325
$p($name, $time);
317326
}
318327
}
@@ -323,6 +332,10 @@
323332

324333
echo $isCli ? '' : '</pre>';
325334

335+
foreach ($cleanupHooks as $hook) {
336+
$hook($args);
337+
}
338+
326339

327340
class StopWatch
328341
{
@@ -380,7 +393,7 @@ function get_args($expectedArgs)
380393

381394
// cast the type to the original type if needed
382395
foreach ($expectedArgs as $key => $value) {
383-
if (isset($args[$key])) {
396+
if (isset($args[$key]) && $value !== null) {
384397
settype($args[$key], gettype($value));
385398
}
386399
}
@@ -416,7 +429,7 @@ function loadAdditionalBenchmarks()
416429
return $benchmarks;
417430
}
418431

419-
function runBenchmark($stopwatch, $name, $benchmark, $multiplier = 1)
432+
function runBenchmark($stopwatch, $benchmark, $multiplier = 1)
420433
{
421434
$r = null;
422435
try {
@@ -433,4 +446,16 @@ function runBenchmark($stopwatch, $name, $benchmark, $multiplier = 1)
433446
}
434447

435448
return number_format($time, 4) . ' s';
449+
}
450+
451+
function setup(callable $hook)
452+
{
453+
global $setupHooks;
454+
$setupHooks[] = $hook;
455+
}
456+
457+
function teardown(callable $hook)
458+
{
459+
global $cleanupHooks;
460+
$cleanupHooks[] = $hook;
436461
}

db/mysql.bench.php

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?php
2+
3+
$initialRowCount = 1000;
4+
$mysqli = null;
5+
6+
setup(function ($args) use (&$mysqli, $initialRowCount) {
7+
if (!extension_loaded('mysqli')) {
8+
print('The mysqli extension is not loaded' . PHP_EOL);
9+
return;
10+
}
11+
12+
if ($args['mysql_host'] === null || $args['mysql_user'] === null || $args['mysql_password'] === null) {
13+
print('The --mysql_host=, --mysql_user=, and --mysql_password= arguments are required' . PHP_EOL);
14+
return;
15+
}
16+
17+
$mysqli = new mysqli($args['mysql_host'], $args['mysql_user'], $args['mysql_password'], null,
18+
isset($args['mysql_port']) ? $args['mysql_port'] : 3306);
19+
20+
if ($mysqli->connect_error) {
21+
printf("Mysql Connect Error (%s) %s\n", $mysqli->connect_errno, $mysqli->connect_error);
22+
return;
23+
}
24+
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))");
31+
32+
for ($i = 0; $i < $initialRowCount; $i++) {
33+
$values[] = "('test$i')";
34+
}
35+
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES " . implode(',', $values));
36+
});
37+
38+
teardown(function () use (&$mysqli) {
39+
if ($mysqli === null) {
40+
return;
41+
}
42+
43+
$mysqli->query("DROP DATABASE IF EXISTS `bench_test`");
44+
$mysqli->close();
45+
});
46+
47+
48+
return [
49+
'ping' => function () use (&$mysqli) {
50+
if ($mysqli === null) {
51+
return INF;
52+
}
53+
$mysqli->ping();
54+
return 1;
55+
},
56+
'select_version' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
57+
if ($mysqli === null) {
58+
return INF;
59+
}
60+
61+
$count = $count * $multiplier;
62+
for ($i = 0; $i < $count; $i++) {
63+
$mysqli->query("SELECT VERSION()");
64+
}
65+
return $i;
66+
},
67+
'select_all' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
68+
if ($mysqli === null) {
69+
return INF;
70+
}
71+
72+
$count = $count * $multiplier;
73+
for ($i = 0; $i < $count; $i++) {
74+
$mysqli->query("SELECT * FROM `bench_test`.`test`");
75+
}
76+
return $i;
77+
},
78+
'select_cursor' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
79+
if ($mysqli === null) {
80+
return INF;
81+
}
82+
83+
$count = $count * $multiplier;
84+
for ($i = 0; $i < $count; $i++) {
85+
$result = $mysqli->query("SELECT * FROM `bench_test`.`test`");
86+
while ($row = $result->fetch_assoc()) {
87+
}
88+
$result->close();
89+
}
90+
return $i;
91+
},
92+
'seq_insert' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
93+
if ($mysqli === null) {
94+
return INF;
95+
}
96+
97+
$count = $count * $multiplier;
98+
for ($i = 0; $i < $count; $i++) {
99+
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES ('test')");
100+
}
101+
return $i;
102+
},
103+
'bulk_insert' => function ($multiplier = 1, $count = 100000) use (&$mysqli) {
104+
if ($mysqli === null) {
105+
return INF;
106+
}
107+
108+
$count = $count * $multiplier;
109+
$values = [];
110+
for ($i = 0; $i < $count; $i++) {
111+
$values[] = "('test$i')";
112+
}
113+
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES " . implode(',', $values));
114+
return $i;
115+
},
116+
'update' => function ($multiplier = 1, $count = 100) use (&$mysqli) {
117+
if ($mysqli === null) {
118+
return INF;
119+
}
120+
121+
$count = $count * $multiplier;
122+
for ($i = 0; $i < $count; $i++) {
123+
$mysqli->query("UPDATE `bench_test`.`test` SET name = 'test' WHERE id % 2 = 0");
124+
}
125+
return $i;
126+
},
127+
'transaction_insert' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
128+
if ($mysqli === null) {
129+
return INF;
130+
}
131+
132+
$count = $count * $multiplier;
133+
for ($i = 0; $i < $count; $i++) {
134+
$mysqli->begin_transaction();
135+
$mysqli->query("INSERT INTO `bench_test`.`test` (name) VALUES ('test')");
136+
$mysqli->commit();
137+
}
138+
return $i;
139+
},
140+
'aes_encrypt' => function ($multiplier = 1, $count = 10000) use (&$mysqli) {
141+
if ($mysqli === null) {
142+
return INF;
143+
}
144+
145+
$stmt = $mysqli->prepare("SELECT AES_ENCRYPT(?, 'key')");
146+
$stmt->bind_param('s', $data);
147+
148+
$data = str_repeat('a', 16);
149+
$count = $count * $multiplier;
150+
for ($i = 0; $i < $count; $i++) {
151+
$stmt->execute();
152+
$stmt->get_result()->fetch_assoc();
153+
}
154+
$stmt->close();
155+
return $i;
156+
},
157+
'aes_decrypt' => function ($multiplier = 1, $count = 10000) use (&$mysqli) {
158+
if ($mysqli === null) {
159+
return INF;
160+
}
161+
162+
$stmt = $mysqli->prepare("SELECT AES_DECRYPT(?, 'key')");
163+
$stmt->bind_param('s', $data);
164+
165+
$data = str_repeat('a', 16);
166+
$count = $count * $multiplier;
167+
for ($i = 0; $i < $count; $i++) {
168+
$stmt->execute();
169+
$stmt->get_result()->fetch_assoc();
170+
}
171+
$stmt->close();
172+
return $i;
173+
},
174+
'indexes' => function ($multiplier = 1, $count = 100) use (&$mysqli) {
175+
if ($mysqli === null) {
176+
return INF;
177+
}
178+
179+
$mysqli->query("CREATE INDEX idx_name ON `bench_test`.`test` (name)");
180+
$mysqli->query("DROP INDEX idx_name ON `bench_test`.`test`");
181+
return 1;
182+
},
183+
'delete' => function ($multiplier = 1, $count = 100) use (&$mysqli) {
184+
if ($mysqli === null) {
185+
return INF;
186+
}
187+
188+
$count = $count * $multiplier;
189+
for ($i = 0; $i < $count; $i++) {
190+
$mysqli->query("DELETE FROM `bench_test`.`test` WHERE id % 2 = 0");
191+
}
192+
return $i;
193+
},
194+
];

0 commit comments

Comments
 (0)