$benchmarks */
19 | // the benchmarks!
20 | $benchmarks = [
21 | 'math' => function ($multiplier = 1, $count = 200000) {
22 | $x = 0;
23 | $count = $count * $multiplier;
24 | for ($i = 0; $i < $count; $i++) {
25 | $x += $i + $i;
26 | $x += $i * $i;
27 | $x += $i ** $i;
28 | $x += $i / (($i + 1) * 2);
29 | $x += $i % (($i + 1) * 2);
30 | abs($i);
31 | acos($i);
32 | acosh($i);
33 | asin($i);
34 | asinh($i);
35 | atan2($i, $i);
36 | atan($i);
37 | atanh($i);
38 | ceil($i);
39 | cos($i);
40 | cosh($i);
41 | decbin($i);
42 | dechex($i);
43 | decoct($i);
44 | deg2rad($i);
45 | exp($i);
46 | expm1($i);
47 | floor($i);
48 | fmod($i, $i);
49 | hypot($i, $i);
50 | is_infinite($i);
51 | is_finite($i);
52 | is_nan($i);
53 | log10($i);
54 | log1p($i);
55 | log($i);
56 | pi();
57 | pow($i, $i);
58 | rad2deg($i);
59 | sin($i);
60 | sinh($i);
61 | sqrt($i);
62 | tan($i);
63 | tanh($i);
64 | }
65 |
66 | return $i;
67 | },
68 | 'loops' => function ($multiplier = 1, $count = 20000000) {
69 | $count = $count * $multiplier;
70 | for ($i = 0; $i < $count; ++$i) {
71 | $i;
72 | }
73 | $i = 0;
74 | while ($i < $count) {
75 | ++$i;
76 | }
77 | return $i;
78 | },
79 | 'ifelse' => function ($multiplier = 1, $count = 10000000) {
80 | $a = 0;
81 | $b = 0;
82 | $count = $count * $multiplier;
83 | for ($i = 0; $i < $count; $i++) {
84 | $k = $i % 4;
85 | if ($k === 0) {
86 | $i;
87 | } elseif ($k === 1) {
88 | $a = $i;
89 | } elseif ($k === 2) {
90 | $b = $i;
91 | } else {
92 | $i;
93 | }
94 | }
95 | return $a - $b;
96 | },
97 | 'switch' => function ($multiplier = 1, $count = 10000000) {
98 | $a = 0;
99 | $b = 0;
100 | $count = $count * $multiplier;
101 | for ($i = 0; $i < $count; $i++) {
102 | switch ($i % 4) {
103 | case 0:
104 | $i;
105 | break;
106 | case 1:
107 | $a = $i;
108 | break;
109 | case 2:
110 | $b = $i;
111 | break;
112 | default:
113 | break;
114 | }
115 | }
116 | return $a - $b;
117 | },
118 | 'string' => function ($multiplier = 1, $count = 50000) {
119 | $string = 'the quick brown fox jumps over the lazy dog ';
120 | $count = $count * $multiplier;
121 | for ($i = 0; $i < $count; $i++) {
122 | addslashes($string);
123 | bin2hex($string);
124 | chunk_split($string);
125 | convert_uudecode(convert_uuencode($string));
126 | count_chars($string);
127 | explode(' ', $string);
128 | htmlentities($string);
129 | md5($string);
130 | metaphone($string);
131 | ord($string);
132 | rtrim($string);
133 | sha1($string);
134 | soundex($string);
135 | str_getcsv($string);
136 | str_ireplace('fox', 'cat', $string);
137 | str_pad($string, 50);
138 | str_repeat($string, 10);
139 | str_replace('fox', 'cat', $string);
140 | str_rot13($string);
141 | str_shuffle($string);
142 | str_word_count($string);
143 | strip_tags($string);
144 | strpos($string, 'fox');
145 | strlen($string);
146 | strtolower($string);
147 | strtoupper($string);
148 | substr_count($string, 'the');
149 | trim($string);
150 | ucfirst($string);
151 | ucwords($string);
152 | }
153 | return $string;
154 | },
155 | 'array' => function ($multiplier = 1, $count = 50000) {
156 | $a = range(0, 100);
157 | $count = $count * $multiplier;
158 | for ($i = 0; $i < $count; $i++) {
159 | array_keys($a);
160 | array_values($a);
161 | array_flip($a);
162 | array_map(function ($e) {
163 | }, $a);
164 | array_walk($a, function ($e, $i) {
165 | });
166 | array_reverse($a);
167 | array_sum($a);
168 | array_merge($a, [101, 102, 103]);
169 | array_replace($a, [1, 2, 3]);
170 | array_chunk($a, 2);
171 | }
172 | return $a;
173 | },
174 | 'regex' => function ($multiplier = 1, $count = 1000000) {
175 | for ($i = 0; $i < $count * $multiplier; $i++) {
176 | preg_match("#http[s]?://\w+[^\s\[\]\<]+#",
177 | 'this is a link to https://google.com which is a really popular site');
178 | preg_replace("#(^|\s)(http[s]?://\w+[^\s\[\]\<]+)#i", '\1\2',
179 | 'this is a link to https://google.com which is a really popular site');
180 | }
181 | return $i;
182 | },
183 | 'is_{type}' => function ($multiplier = 1, $count = 2500000) {
184 | $o = new stdClass();
185 | $count = $count * $multiplier;
186 | for ($i = 0; $i < $count; $i++) {
187 | is_array([1]);
188 | is_array('1');
189 | is_int(1);
190 | is_int('abc');
191 | is_string('foo');
192 | is_string(123);
193 | is_bool(true);
194 | is_bool(5);
195 | is_numeric('hi');
196 | is_numeric('123');
197 | is_float(1.3);
198 | is_float(0);
199 | is_object($o);
200 | is_object('hi');
201 | }
202 | return $o;
203 | },
204 | 'hash' => function ($multiplier = 1, $count = 10000) {
205 | $count = $count * $multiplier;
206 | for ($i = 0; $i < $count; $i++) {
207 | md5($i);
208 | sha1($i);
209 | hash('sha256', $i);
210 | hash('sha512', $i);
211 | hash('ripemd160', $i);
212 | hash('crc32', $i);
213 | hash('crc32b', $i);
214 | hash('adler32', $i);
215 | hash('fnv132', $i);
216 | hash('fnv164', $i);
217 | hash('joaat', $i);
218 | hash('haval128,3', $i);
219 | hash('haval160,3', $i);
220 | hash('haval192,3', $i);
221 | hash('haval224,3', $i);
222 | hash('haval256,3', $i);
223 | hash('haval128,4', $i);
224 | hash('haval160,4', $i);
225 | hash('haval192,4', $i);
226 | hash('haval224,4', $i);
227 | hash('haval256,4', $i);
228 | hash('haval128,5', $i);
229 | hash('haval160,5', $i);
230 | hash('haval192,5', $i);
231 | hash('haval224,5', $i);
232 | hash('haval256,5', $i);
233 | }
234 | return $i;
235 | },
236 | 'json' => function ($multiplier = 1, $count = 100000) {
237 | $data = [
238 | 'foo' => 'bar',
239 | 'bar' => 'baz',
240 | 'baz' => 'qux',
241 | 'qux' => 'quux',
242 | 'quux' => 'corge',
243 | 'corge' => 'grault',
244 | 'grault' => 'garply',
245 | 'garply' => 'waldo',
246 | 'waldo' => 'fred',
247 | 'fred' => 'plugh',
248 | 'plugh' => 'xyzzy',
249 | 'xyzzy' => 'thud',
250 | 'thud' => 'end',
251 | ];
252 | $count = $count * $multiplier;
253 | for ($i = 0; $i < $count; $i++) {
254 | json_encode($data);
255 | json_decode(json_encode($data));
256 | }
257 | return $data;
258 | },
259 | ];
260 |
261 | $mtime = microtime(true);
262 | // workaround for https://www.php.net/manual/en/datetime.createfromformat.php#128901
263 | if (fmod($mtime, 1) === .0000) {
264 | $mtime += .0001;
265 | }
266 | $now = DateTime::createFromFormat('U.u', $mtime);
267 |
268 | $V = '2.0';
269 | $isCli = PHP_SAPI === 'cli';
270 | $lf = $isCli ? PHP_EOL : '
';
271 | $w = 55;
272 | $multiplier = $args['multiplier'];
273 | $extraLines = [];
274 | $currentBenchmark = null;
275 |
276 | echo $isCli ? '' : '';
277 | printLine('', '', '-');
278 | printf('|%s|%s', str_pad(sprintf("PHP BENCHMARK SCRIPT v.%s by @SergiX44", $V), $w - 2, ' ', STR_PAD_BOTH), $lf);
279 | printLine('', '', '-');
280 | printLine('PHP', PHP_VERSION);
281 | printLine('Platform', PHP_OS);
282 | printLine('Arch', php_uname('m'));
283 | if ($isCli) {
284 | printLine('Server', gethostname());
285 | } else {
286 | $name = @$_SERVER['SERVER_NAME'] ?: 'null';
287 | $addr = @$_SERVER['SERVER_ADDR'] ?: 'null';
288 | printLine('Server', "{$name}@{$addr}");
289 | }
290 | printLine('Max memory usage', ini_get('memory_limit'));
291 | $opStatus = function_exists('opcache_get_status') ? opcache_get_status() : false;
292 | printLine('OPCache status', is_array($opStatus) && @$opStatus['opcache_enabled'] ? 'enabled' : 'disabled');
293 | printLine('OPCache JIT', is_array($opStatus) && @$opStatus['jit']['enabled'] ? 'enabled' : 'disabled/unavailable');
294 | printLine('PCRE JIT', ini_get('pcre.jit') ? 'enabled' : 'disabled');
295 | printLine('XDebug extension', extension_loaded('xdebug') ? 'enabled' : 'disabled');
296 | printLine('Difficulty multiplier', "{$multiplier}x");
297 | printLine('Started at', $now->format('d/m/Y H:i:s.v'));
298 |
299 | foreach ($setupHooks as $hook) {
300 | $hook($args);
301 | }
302 |
303 | printLine('', '', '-', STR_PAD_BOTH);
304 |
305 | $stopwatch = new StopWatch();
306 |
307 | foreach ($benchmarks as $name => $benchmark) {
308 | $currentBenchmark = $name;
309 | $time = runBenchmark($stopwatch, $benchmark, $multiplier);
310 | printLine($name, $time);
311 | }
312 |
313 | if (!empty($additionalBenchmarks)) {
314 | printLine('Additional Benchmarks', '', '-', STR_PAD_BOTH);
315 | foreach ($additionalBenchmarks as $name => $benchmark) {
316 | $currentBenchmark = $name;
317 | $time = runBenchmark($stopwatch, $benchmark, $multiplier);
318 | printLine($name, $time);
319 | }
320 | }
321 |
322 | if (!empty($extraLines)) {
323 | printLine('Extra', '', '-', STR_PAD_BOTH);
324 | foreach ($extraLines as $line) {
325 | printLine($line[0], $line[1]);
326 | }
327 | }
328 |
329 | printLine('', '', '-');
330 | foreach ($cleanupHooks as $hook) {
331 | $hook($args);
332 | }
333 |
334 | printLine('Total time', number_format($stopwatch->totalTime, 4) . ' s');
335 | printLine('Peak memory usage', round(memory_get_peak_usage(true) / 1024 / 1024, 2) . ' MiB');
336 | echo $isCli ? '' : '
';
337 |
338 |
339 | class StopWatch
340 | {
341 | /**
342 | * @var float
343 | */
344 | public $totalTime = .0;
345 |
346 | private $start;
347 |
348 |
349 | /**
350 | * @return float
351 | */
352 | public function start()
353 | {
354 | return $this->start = self::time();
355 | }
356 |
357 | /**
358 | * @return float
359 | */
360 | public function stop()
361 | {
362 | $time = self::time() - $this->start;
363 | $this->totalTime += $time;
364 |
365 | return $time;
366 | }
367 |
368 | /**
369 | * @return float
370 | */
371 | public static function time()
372 | {
373 | return function_exists('hrtime') ? hrtime(true) / 1e9 : microtime(true);
374 | }
375 | }
376 |
377 | function get_args($expectedArgs)
378 | {
379 | $args = [];
380 |
381 | if (PHP_SAPI === 'cli') {
382 | $cleanedArgs = array_map(function ($arg) {
383 | return strpos($arg, '--') !== 0 ? null : str_replace('--', '', $arg);
384 | }, $GLOBALS['argv']);
385 |
386 | parse_str(implode('&', array_filter($cleanedArgs)), $args);
387 | } else {
388 | parse_str($_SERVER['QUERY_STRING'], $args);
389 | }
390 |
391 | $args = array_intersect_key($args, array_flip(array_keys($expectedArgs)));
392 |
393 | // cast the type to the original type if needed
394 | foreach ($expectedArgs as $key => $value) {
395 | if (isset($args[$key]) && $value !== null) {
396 | settype($args[$key], gettype($value));
397 | }
398 | }
399 |
400 | return $args;
401 | }
402 |
403 | function loadAdditionalBenchmarks()
404 | {
405 | $benchmarks = [];
406 | $benchFiles = glob(__DIR__ . '/./*.bench.php');
407 | foreach ($benchFiles as $benchFile) {
408 | $benchName = basename($benchFile, '.bench.php');
409 | $newBenchmark = require $benchFile;
410 | if (is_callable($newBenchmark)) {
411 | $benchmarks[$benchName] = $newBenchmark;
412 | continue;
413 | }
414 |
415 | if (is_array($newBenchmark)) {
416 | $newBenchmark = array_filter($newBenchmark, 'is_callable');
417 | $newBenchmark = array_combine(array_map(function ($name) use ($benchName) {
418 | return "{$benchName}::{$name}";
419 | }, array_keys($newBenchmark)), $newBenchmark);
420 |
421 | $benchmarks = array_merge($benchmarks, $newBenchmark);
422 | continue;
423 | }
424 |
425 | throw new RuntimeException("Invalid benchmark file: {$benchFile}");
426 | }
427 |
428 | return $benchmarks;
429 | }
430 |
431 | function extraStat($name, $value)
432 | {
433 | global $extraLines, $currentBenchmark;
434 | $extraLines[] = ["$currentBenchmark::$name", $value];
435 | }
436 |
437 | function runBenchmark($stopwatch, $benchmark, $multiplier = 1)
438 | {
439 | $r = null;
440 | try {
441 | $stopwatch->start();
442 | $r = $benchmark($multiplier);
443 | } catch (Exception $e) {
444 | return 'ERROR: ' . $e->getMessage();
445 | } finally {
446 | $time = $stopwatch->stop();
447 | }
448 |
449 | if ($r === INF) {
450 | return 'SKIPPED';
451 | }
452 |
453 | return number_format($time, 4) . ' s';
454 | }
455 |
456 | function printLine($str, $endStr = '', $pad = '.', $mode = STR_PAD_RIGHT)
457 | {
458 | global $lf, $w;
459 |
460 | if (!empty($endStr)) {
461 | $endStr = " $endStr";
462 | }
463 | $length = max(0, $w - strlen($endStr));
464 | echo str_pad($str, $length, $pad, $mode) . $endStr . $lf;
465 | }
466 |
467 | function setup(callable $hook)
468 | {
469 | global $setupHooks;
470 | $setupHooks[] = $hook;
471 | }
472 |
473 | function teardown(callable $hook)
474 | {
475 | global $cleanupHooks;
476 | $cleanupHooks[] = $hook;
477 | }
478 |
479 | function pushArgs($args)
480 | {
481 | global $defaultArgs;
482 | $defaultArgs = array_merge($defaultArgs, $args);
483 | }
--------------------------------------------------------------------------------
/io.bench.php:
--------------------------------------------------------------------------------
1 | function($multiplier = 1, $count = 1000) {
5 | file_put_contents('test.txt', "test");
6 | $count = $count * $multiplier;
7 | for ($i = 0; $i < $count; $i++) {
8 | file_get_contents('test.txt');
9 | }
10 | unlink('test.txt');
11 | return $i;
12 | },
13 | 'file_write' => function($multiplier = 1, $count = 1000) {
14 | $count = $count * $multiplier;
15 | for ($i = 0; $i < $count; $i++) {
16 | file_put_contents('test.txt', "test $i");
17 | }
18 | unlink('test.txt');
19 | return $i;
20 | },
21 | 'file_zip' => function($multiplier = 1, $count = 1000) {
22 | file_put_contents('test.txt', "test");
23 | $count = $count * $multiplier;
24 | for ($i = 0; $i < $count; $i++) {
25 | $zip = new ZipArchive();
26 | $zip->open('test.zip', ZipArchive::CREATE);
27 | $zip->addFile('test.txt');
28 | $zip->close();
29 | }
30 | unlink('test.txt');
31 | unlink('test.zip');
32 | return $i;
33 | },
34 | 'file_unzip' => function($multiplier = 1, $count = 1000) {
35 | file_put_contents('test.txt', "test");
36 | $zip = new ZipArchive();
37 | $zip->open('test.zip', ZipArchive::CREATE);
38 | $zip->addFile('test.txt');
39 | $zip->close();
40 | $count = $count * $multiplier;
41 | for ($i = 0; $i < $count; $i++) {
42 | $zip = new ZipArchive();
43 | $zip->open('test.zip');
44 | $zip->extractTo('test');
45 | $zip->close();
46 | }
47 | unlink('test.txt');
48 | unlink('test.zip');
49 | unlink('test/test.txt');
50 | rmdir('test');
51 | return $i;
52 | },
53 |
54 | ];
--------------------------------------------------------------------------------
/mysql.bench.php:
--------------------------------------------------------------------------------
1 | '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) {
16 | if (!extension_loaded('mysqli')) {
17 | print('The mysqli extension is not loaded' . PHP_EOL);
18 | return;
19 | }
20 |
21 | if ($args['mysql_host'] === null || $args['mysql_user'] === null || $args['mysql_password'] === null) {
22 | print('The --mysql_host=, --mysql_user=, and --mysql_password= arguments are required' . PHP_EOL);
23 | return;
24 | }
25 |
26 | $mysqli = new mysqli($args['mysql_host'], $args['mysql_user'], $args['mysql_password'], null,
27 | isset($args['mysql_port']) ? $args['mysql_port'] : 3306);
28 |
29 | if ($mysqli->connect_error) {
30 | printf("Mysql Connect Error (%s) %s\n", $mysqli->connect_errno, $mysqli->connect_error);
31 | return;
32 | }
33 |
34 | $dbName = isset($args['mysql_database']) ? $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))");
51 |
52 | for ($i = 0; $i < $initialRowCount; $i++) {
53 | $values[] = "('test$i')";
54 | }
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 | }
59 | });
60 |
61 | teardown(function () use (&$mysqli, $dbName) {
62 | if ($mysqli === null) {
63 | return;
64 | }
65 |
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 | }
70 | $mysqli->close();
71 | });
72 |
73 |
74 | return [
75 | 'ping' => function () use (&$mysqli) {
76 | if ($mysqli === null) {
77 | return INF;
78 | }
79 | $mysqli->ping();
80 | return 1;
81 | },
82 | 'select_version' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
83 | if ($mysqli === null) {
84 | return INF;
85 | }
86 |
87 | $count = $count * $multiplier;
88 | $time = StopWatch::time();
89 | for ($i = 0; $i < $count; $i++) {
90 | $mysqli->query("SELECT VERSION()");
91 | }
92 |
93 | extraStat('q/s', round($count / (StopWatch::time() - $time)));
94 |
95 | return $i;
96 | },
97 | 'select_all' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
98 | if ($mysqli === null) {
99 | return INF;
100 | }
101 |
102 | $count = $count * $multiplier;
103 | $time = StopWatch::time();
104 | for ($i = 0; $i < $count; $i++) {
105 | $mysqli->query("SELECT * FROM `test`");
106 | }
107 | extraStat('q/s', round($count / (StopWatch::time() - $time)));
108 | return $i;
109 | },
110 | 'select_cursor' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
111 | if ($mysqli === null) {
112 | return INF;
113 | }
114 |
115 | $count = $count * $multiplier;
116 | for ($i = 0; $i < $count; $i++) {
117 | $result = $mysqli->query("SELECT * FROM `test`");
118 | while ($row = $result->fetch_assoc()) {
119 | }
120 | $result->close();
121 | }
122 | return $i;
123 | },
124 | 'seq_insert' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
125 | if ($mysqli === null) {
126 | return INF;
127 | }
128 |
129 | $count = $count * $multiplier;
130 | $time = StopWatch::time();
131 | for ($i = 0; $i < $count; $i++) {
132 | $mysqli->query("INSERT INTO `test` (name) VALUES ('test')");
133 | }
134 | extraStat('q/s', round($count / (StopWatch::time() - $time)));
135 | return $i;
136 | },
137 | 'bulk_insert' => function ($multiplier = 1, $count = 100000) use (&$mysqli) {
138 | if ($mysqli === null) {
139 | return INF;
140 | }
141 |
142 | $count = $count * $multiplier;
143 | $values = [];
144 | for ($i = 0; $i < $count; $i++) {
145 | $values[] = "('test$i')";
146 | }
147 | $mysqli->query("INSERT INTO `test` (name) VALUES " . implode(',', $values));
148 | return $i;
149 | },
150 | 'update' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
151 | if ($mysqli === null) {
152 | return INF;
153 | }
154 |
155 | $count = $count * $multiplier;
156 | $time = StopWatch::time();
157 | for ($i = 0; $i < $count; $i++) {
158 | $mysqli->query("UPDATE `test` SET name = 'test' WHERE id = '$i'");
159 | }
160 | extraStat('q/s', round($count / (StopWatch::time() - $time)));
161 | return $i;
162 | },
163 | 'update_with_index' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
164 | if ($mysqli === null) {
165 | return INF;
166 | }
167 |
168 | $mysqli->query("CREATE INDEX idx ON `test` (id)");
169 |
170 | $count = $count * $multiplier;
171 | $time = StopWatch::time();
172 | for ($i = 0; $i < $count; $i++) {
173 | $mysqli->query("UPDATE `test` SET name = 'test' WHERE id = '$i'");
174 | }
175 | extraStat('q/s', round($count / (StopWatch::time() - $time)));
176 |
177 | $mysqli->query("DROP INDEX idx ON `test`");
178 | return $i;
179 | },
180 | 'transaction_insert' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
181 | if ($mysqli === null) {
182 | return INF;
183 | }
184 |
185 | $count = $count * $multiplier;
186 | $time = StopWatch::time();
187 | for ($i = 0; $i < $count; $i++) {
188 | $mysqli->begin_transaction();
189 | $mysqli->query("INSERT INTO `test` (name) VALUES ('test')");
190 | $mysqli->commit();
191 | }
192 | extraStat('t/s', round($count / (StopWatch::time() - $time)));
193 | return $i;
194 | },
195 | 'aes_encrypt' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
196 | if ($mysqli === null) {
197 | return INF;
198 | }
199 |
200 | $stmt = $mysqli->prepare("SELECT AES_ENCRYPT(?, 'key')");
201 | $stmt->bind_param('s', $data);
202 |
203 | $data = str_repeat('a', 16);
204 | $count = $count * $multiplier;
205 | $time = StopWatch::time();
206 | for ($i = 0; $i < $count; $i++) {
207 | $stmt->execute();
208 | $stmt->get_result()->fetch_assoc();
209 | }
210 | extraStat('q/s', round($count / (StopWatch::time() - $time)));
211 | $stmt->close();
212 | return $i;
213 | },
214 | 'aes_decrypt' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
215 | if ($mysqli === null) {
216 | return INF;
217 | }
218 |
219 | $stmt = $mysqli->prepare("SELECT AES_DECRYPT(?, 'key')");
220 | $stmt->bind_param('s', $data);
221 |
222 | $data = str_repeat('a', 16);
223 | $count = $count * $multiplier;
224 | $time = StopWatch::time();
225 | for ($i = 0; $i < $count; $i++) {
226 | $stmt->execute();
227 | $stmt->get_result()->fetch_assoc();
228 | }
229 | extraStat('q/s', round($count / (StopWatch::time() - $time)));
230 | $stmt->close();
231 | return $i;
232 | },
233 | 'indexes' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
234 | if ($mysqli === null) {
235 | return INF;
236 | }
237 |
238 | $mysqli->query("CREATE INDEX idx_name ON `test` (name)");
239 | $mysqli->query("DROP INDEX idx_name ON `test`");
240 | return 1;
241 | },
242 | 'delete' => function ($multiplier = 1, $count = 1000) use (&$mysqli) {
243 | if ($mysqli === null) {
244 | return INF;
245 | }
246 |
247 | $count = $count * $multiplier;
248 | $time = StopWatch::time();
249 | for ($i = 0; $i < $count; $i++) {
250 | $mysqli->query("DELETE FROM `test` WHERE id = '$i'");
251 | }
252 | extraStat('q/s', round($count / (StopWatch::time() - $time)));
253 | return $i;
254 | },
255 | ];
--------------------------------------------------------------------------------
/rand.bench.php:
--------------------------------------------------------------------------------
1 | */
4 | return [
5 | 'rand' => function($multiplier = 1, $count = 1000000) {
6 | $count = $count * $multiplier;
7 | for ($i = 0; $i < $count; $i++) {
8 | rand(0, $i);
9 | }
10 | return $i;
11 | },
12 | 'mt_rand' => function($multiplier = 1, $count = 1000000) {
13 | $count = $count * $multiplier;
14 | for ($i = 0; $i < $count; $i++) {
15 | mt_rand(0, $i);
16 | }
17 | return $i;
18 | },
19 | 'random_int' => function($multiplier = 1, $count = 1000000) {
20 | if (!function_exists('random_int')) {
21 | return INF;
22 | }
23 |
24 | $count = $count * $multiplier;
25 | for ($i = 0; $i < $count; $i++) {
26 | random_int(0, $i);
27 | }
28 | return $i;
29 | },
30 | 'random_bytes' => function($multiplier = 1, $count = 1000000) {
31 | if (!function_exists('random_bytes')) {
32 | return INF;
33 | }
34 |
35 | $count = $count * $multiplier;
36 | for ($i = 0; $i < $count; $i++) {
37 | random_bytes(32);
38 | }
39 | return $i;
40 | },
41 | 'openssl_random_pseudo_bytes' => function($multiplier = 1, $count = 1000000) {
42 | if (!function_exists('openssl_random_pseudo_bytes')) {
43 | return INF;
44 | }
45 |
46 | $count = $count * $multiplier;
47 | for ($i = 0; $i < $count; $i++) {
48 | openssl_random_pseudo_bytes(32);
49 | }
50 | return $i;
51 | },
52 | ];
53 |
--------------------------------------------------------------------------------