8
8
$ defaultArgs = [
9
9
// Increase the multiplier if you want to benchmark longer
10
10
'multiplier ' => 1.0 ,
11
+ 'mysql_host ' => '127.0.0.1 ' ,
12
+ 'mysql_user ' => null ,
13
+ 'mysql_password ' => null ,
14
+ 'mysql_port ' => 3306 ,
11
15
];
12
16
13
- $ args = get_args ($ defaultArgs );
14
- $ args = array_merge ($ defaultArgs , $ args );
17
+ $ args = array_merge ($ defaultArgs , get_args ($ defaultArgs ));
18
+ $ setupHooks = [];
19
+ $ cleanupHooks = [];
15
20
16
21
/** @var array<string, callable> $benchmarks */
17
22
// the benchmarks!
269
274
$ w = 55 ;
270
275
$ multiplier = $ args ['multiplier ' ];
271
276
$ additionalBenchmarks = loadAdditionalBenchmarks ();
272
-
273
- $ p = function ($ str , $ endStr = '' , $ pad = '. ' , $ mode = STR_PAD_RIGHT ) use ($ w , $ lf ) {
274
- if (!empty ($ endStr )) {
275
- $ endStr = " $ endStr " ;
276
- }
277
- $ length = max (0 , $ w - strlen ($ endStr ));
278
- echo str_pad ($ str , $ length , $ pad , $ mode ) . $ endStr . $ lf ;
279
- };
277
+ $ extraLines = [];
278
+ $ currentBenchmark = null ;
280
279
281
280
echo $ isCli ? '' : '<pre> ' ;
282
- $ p ('' , '' , '- ' );
281
+ printLine ('' , '' , '- ' );
283
282
printf ('|%s|%s ' , str_pad (sprintf ("PHP BENCHMARK SCRIPT v.%s by @SergiX44 " , $ V ), $ w - 2 , ' ' , STR_PAD_BOTH ), $ lf );
284
- $ p ('' , '' , '- ' );
285
- $ p ('PHP ' , PHP_VERSION );
286
- $ p ('Platform ' , PHP_OS );
287
- $ p ('Arch ' , php_uname ('m ' ));
283
+ printLine ('' , '' , '- ' );
284
+ printLine ('PHP ' , PHP_VERSION );
285
+ printLine ('Platform ' , PHP_OS );
286
+ printLine ('Arch ' , php_uname ('m ' ));
288
287
if ($ isCli ) {
289
- $ p ('Server ' , gethostname ());
288
+ printLine ('Server ' , gethostname ());
290
289
} else {
291
290
$ name = @$ _SERVER ['SERVER_NAME ' ] ?: 'null ' ;
292
291
$ addr = @$ _SERVER ['SERVER_ADDR ' ] ?: 'null ' ;
293
- $ p ('Server ' , "{$ name }@ {$ addr }" );
292
+ printLine ('Server ' , "{$ name }@ {$ addr }" );
294
293
}
295
- $ p ('Max memory usage ' , ini_get ('memory_limit ' ));
294
+ printLine ('Max memory usage ' , ini_get ('memory_limit ' ));
296
295
$ opStatus = function_exists ('opcache_get_status ' ) ? opcache_get_status () : false ;
297
- $ p ('OPCache status ' , is_array ($ opStatus ) && @$ opStatus ['opcache_enabled ' ] ? 'enabled ' : 'disabled ' );
298
- $ p ('OPCache JIT ' , is_array ($ opStatus ) && @$ opStatus ['jit ' ]['enabled ' ] ? 'enabled ' : 'disabled/unavailable ' );
299
- $ p ('PCRE JIT ' , ini_get ('pcre.jit ' ) ? 'enabled ' : 'disabled ' );
300
- $ p ('XDebug extension ' , extension_loaded ('xdebug ' ) ? 'enabled ' : 'disabled ' );
301
- $ p ('Difficulty multiplier ' , "{$ multiplier }x " );
302
- $ p ('Started at ' , $ now ->format ('d/m/Y H:i:s.v ' ));
303
- $ p ('' , '' , '- ' , STR_PAD_BOTH );
296
+ printLine ('OPCache status ' , is_array ($ opStatus ) && @$ opStatus ['opcache_enabled ' ] ? 'enabled ' : 'disabled ' );
297
+ printLine ('OPCache JIT ' , is_array ($ opStatus ) && @$ opStatus ['jit ' ]['enabled ' ] ? 'enabled ' : 'disabled/unavailable ' );
298
+ printLine ('PCRE JIT ' , ini_get ('pcre.jit ' ) ? 'enabled ' : 'disabled ' );
299
+ printLine ('XDebug extension ' , extension_loaded ('xdebug ' ) ? 'enabled ' : 'disabled ' );
300
+ printLine ('Difficulty multiplier ' , "{$ multiplier }x " );
301
+ printLine ('Started at ' , $ now ->format ('d/m/Y H:i:s.v ' ));
302
+ printLine ('' , '' , '- ' , STR_PAD_BOTH );
303
+
304
+ foreach ($ setupHooks as $ hook ) {
305
+ $ hook ($ args );
306
+ }
304
307
305
308
$ stopwatch = new StopWatch ();
306
309
307
310
foreach ($ benchmarks as $ name => $ benchmark ) {
308
- $ time = runBenchmark ($ stopwatch , $ name , $ benchmark , $ multiplier );
309
- $ p ($ name , $ time );
311
+ $ currentBenchmark = $ name ;
312
+ $ time = runBenchmark ($ stopwatch , $ benchmark , $ multiplier );
313
+ printLine ($ name , $ time );
310
314
}
311
315
312
316
if (!empty ($ additionalBenchmarks )) {
313
- $ p ('Additional Benchmarks ' , '' , '- ' , STR_PAD_BOTH );
317
+ printLine ('Additional Benchmarks ' , '' , '- ' , STR_PAD_BOTH );
314
318
foreach ($ additionalBenchmarks as $ name => $ benchmark ) {
315
- $ time = runBenchmark ($ stopwatch , $ name , $ benchmark , $ multiplier );
316
- $ p ($ name , $ time );
319
+ $ currentBenchmark = $ name ;
320
+ $ time = runBenchmark ($ stopwatch , $ benchmark , $ multiplier );
321
+ printLine ($ name , $ time );
317
322
}
318
323
}
319
324
320
- $ p ('' , '' , '- ' );
321
- $ p ('Total time ' , number_format ($ stopwatch ->totalTime , 4 ) . ' s ' );
322
- $ p ('Peak memory usage ' , round (memory_get_peak_usage (true ) / 1024 / 1024 , 2 ) . ' MiB ' );
325
+ foreach ($ cleanupHooks as $ hook ) {
326
+ $ hook ($ args );
327
+ }
328
+
329
+ if (!empty ($ extraLines )) {
330
+ printLine ('Extra ' , '' , '- ' , STR_PAD_BOTH );
331
+ foreach ($ extraLines as $ line ) {
332
+ printLine ($ line [0 ], $ line [1 ]);
333
+ }
334
+ }
323
335
336
+ printLine ('' , '' , '- ' );
337
+ printLine ('Total time ' , number_format ($ stopwatch ->totalTime , 4 ) . ' s ' );
338
+ printLine ('Peak memory usage ' , round (memory_get_peak_usage (true ) / 1024 / 1024 , 2 ) . ' MiB ' );
324
339
echo $ isCli ? '' : '</pre> ' ;
325
340
326
341
@@ -339,15 +354,15 @@ class StopWatch
339
354
*/
340
355
public function start ()
341
356
{
342
- return $ this ->start = $ this -> t ();
357
+ return $ this ->start = self :: time ();
343
358
}
344
359
345
360
/**
346
361
* @return float
347
362
*/
348
363
public function stop ()
349
364
{
350
- $ time = $ this -> t () - $ this ->start ;
365
+ $ time = self :: time () - $ this ->start ;
351
366
$ this ->totalTime += $ time ;
352
367
353
368
return $ time ;
@@ -356,7 +371,7 @@ public function stop()
356
371
/**
357
372
* @return float
358
373
*/
359
- private function t ()
374
+ public static function time ()
360
375
{
361
376
return function_exists ('hrtime ' ) ? hrtime (true ) / 1e9 : microtime (true );
362
377
}
@@ -380,7 +395,7 @@ function get_args($expectedArgs)
380
395
381
396
// cast the type to the original type if needed
382
397
foreach ($ expectedArgs as $ key => $ value ) {
383
- if (isset ($ args [$ key ])) {
398
+ if (isset ($ args [$ key ]) && $ value !== null ) {
384
399
settype ($ args [$ key ], gettype ($ value ));
385
400
}
386
401
}
@@ -416,7 +431,13 @@ function loadAdditionalBenchmarks()
416
431
return $ benchmarks ;
417
432
}
418
433
419
- function runBenchmark ($ stopwatch , $ name , $ benchmark , $ multiplier = 1 )
434
+ function extraStat ($ name , $ value )
435
+ {
436
+ global $ extraLines , $ currentBenchmark ;
437
+ $ extraLines [] = ["$ currentBenchmark:: $ name " , $ value ];
438
+ }
439
+
440
+ function runBenchmark ($ stopwatch , $ benchmark , $ multiplier = 1 )
420
441
{
421
442
$ r = null ;
422
443
try {
@@ -433,4 +454,26 @@ function runBenchmark($stopwatch, $name, $benchmark, $multiplier = 1)
433
454
}
434
455
435
456
return number_format ($ time , 4 ) . ' s ' ;
457
+ }
458
+
459
+ function printLine ($ str , $ endStr = '' , $ pad = '. ' , $ mode = STR_PAD_RIGHT ) {
460
+ global $ lf , $ w ;
461
+
462
+ if (!empty ($ endStr )) {
463
+ $ endStr = " $ endStr " ;
464
+ }
465
+ $ length = max (0 , $ w - strlen ($ endStr ));
466
+ echo str_pad ($ str , $ length , $ pad , $ mode ) . $ endStr . $ lf ;
467
+ }
468
+
469
+ function setup (callable $ hook )
470
+ {
471
+ global $ setupHooks ;
472
+ $ setupHooks [] = $ hook ;
473
+ }
474
+
475
+ function teardown (callable $ hook )
476
+ {
477
+ global $ cleanupHooks ;
478
+ $ cleanupHooks [] = $ hook ;
436
479
}
0 commit comments