274274$ w = 55 ;
275275$ multiplier = $ args ['multiplier ' ];
276276$ additionalBenchmarks = loadAdditionalBenchmarks ();
277-
278- $ p = function ($ str , $ endStr = '' , $ pad = '. ' , $ mode = STR_PAD_RIGHT ) use ($ w , $ lf ) {
279- if (!empty ($ endStr )) {
280- $ endStr = " $ endStr " ;
281- }
282- $ length = max (0 , $ w - strlen ($ endStr ));
283- echo str_pad ($ str , $ length , $ pad , $ mode ) . $ endStr . $ lf ;
284- };
277+ $ extraLines = [];
278+ $ currentBenchmark = null ;
285279
286280echo $ isCli ? '' : '<pre> ' ;
287- $ p ('' , '' , '- ' );
281+ printLine ('' , '' , '- ' );
288282printf ('|%s|%s ' , str_pad (sprintf ("PHP BENCHMARK SCRIPT v.%s by @SergiX44 " , $ V ), $ w - 2 , ' ' , STR_PAD_BOTH ), $ lf );
289- $ p ('' , '' , '- ' );
290- $ p ('PHP ' , PHP_VERSION );
291- $ p ('Platform ' , PHP_OS );
292- $ p ('Arch ' , php_uname ('m ' ));
283+ printLine ('' , '' , '- ' );
284+ printLine ('PHP ' , PHP_VERSION );
285+ printLine ('Platform ' , PHP_OS );
286+ printLine ('Arch ' , php_uname ('m ' ));
293287if ($ isCli ) {
294- $ p ('Server ' , gethostname ());
288+ printLine ('Server ' , gethostname ());
295289} else {
296290 $ name = @$ _SERVER ['SERVER_NAME ' ] ?: 'null ' ;
297291 $ addr = @$ _SERVER ['SERVER_ADDR ' ] ?: 'null ' ;
298- $ p ('Server ' , "{$ name }@ {$ addr }" );
292+ printLine ('Server ' , "{$ name }@ {$ addr }" );
299293}
300- $ p ('Max memory usage ' , ini_get ('memory_limit ' ));
294+ printLine ('Max memory usage ' , ini_get ('memory_limit ' ));
301295$ opStatus = function_exists ('opcache_get_status ' ) ? opcache_get_status () : false ;
302- $ p ('OPCache status ' , is_array ($ opStatus ) && @$ opStatus ['opcache_enabled ' ] ? 'enabled ' : 'disabled ' );
303- $ p ('OPCache JIT ' , is_array ($ opStatus ) && @$ opStatus ['jit ' ]['enabled ' ] ? 'enabled ' : 'disabled/unavailable ' );
304- $ p ('PCRE JIT ' , ini_get ('pcre.jit ' ) ? 'enabled ' : 'disabled ' );
305- $ p ('XDebug extension ' , extension_loaded ('xdebug ' ) ? 'enabled ' : 'disabled ' );
306- $ p ('Difficulty multiplier ' , "{$ multiplier }x " );
307- $ p ('Started at ' , $ now ->format ('d/m/Y H:i:s.v ' ));
308- $ 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 );
309303
310304foreach ($ setupHooks as $ hook ) {
311305 $ hook ($ args );
314308$ stopwatch = new StopWatch ();
315309
316310foreach ($ benchmarks as $ name => $ benchmark ) {
311+ $ currentBenchmark = $ name ;
317312 $ time = runBenchmark ($ stopwatch , $ benchmark , $ multiplier );
318- $ p ($ name , $ time );
313+ printLine ($ name , $ time );
319314}
320315
321316if (!empty ($ additionalBenchmarks )) {
322- $ p ('Additional Benchmarks ' , '' , '- ' , STR_PAD_BOTH );
317+ printLine ('Additional Benchmarks ' , '' , '- ' , STR_PAD_BOTH );
323318 foreach ($ additionalBenchmarks as $ name => $ benchmark ) {
319+ $ currentBenchmark = $ name ;
324320 $ time = runBenchmark ($ stopwatch , $ benchmark , $ multiplier );
325- $ p ($ name , $ time );
321+ printLine ($ name , $ time );
326322 }
327323}
328324
329- $ p ('' , '' , '- ' );
330- $ p ('Total time ' , number_format ($ stopwatch ->totalTime , 4 ) . ' s ' );
331- $ p ('Peak memory usage ' , round (memory_get_peak_usage (true ) / 1024 / 1024 , 2 ) . ' MiB ' );
332-
333- echo $ isCli ? '' : '</pre> ' ;
334-
335325foreach ($ cleanupHooks as $ hook ) {
336326 $ hook ($ args );
337327}
338328
329+ if (!empty ($ extraLines )) {
330+ printLine ('Extra ' , '' , '- ' , STR_PAD_BOTH );
331+ foreach ($ extraLines as $ line ) {
332+ printLine ($ line [0 ], $ line [1 ]);
333+ }
334+ }
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 ' );
339+ echo $ isCli ? '' : '</pre> ' ;
340+
339341
340342class StopWatch
341343{
@@ -352,15 +354,15 @@ class StopWatch
352354 */
353355 public function start ()
354356 {
355- return $ this ->start = $ this -> t ();
357+ return $ this ->start = self :: time ();
356358 }
357359
358360 /**
359361 * @return float
360362 */
361363 public function stop ()
362364 {
363- $ time = $ this -> t () - $ this ->start ;
365+ $ time = self :: time () - $ this ->start ;
364366 $ this ->totalTime += $ time ;
365367
366368 return $ time ;
@@ -369,7 +371,7 @@ public function stop()
369371 /**
370372 * @return float
371373 */
372- private function t ()
374+ public static function time ()
373375 {
374376 return function_exists ('hrtime ' ) ? hrtime (true ) / 1e9 : microtime (true );
375377 }
@@ -429,6 +431,12 @@ function loadAdditionalBenchmarks()
429431 return $ benchmarks ;
430432}
431433
434+ function extraStat ($ name , $ value )
435+ {
436+ global $ extraLines , $ currentBenchmark ;
437+ $ extraLines [] = ["$ currentBenchmark:: $ name " , $ value ];
438+ }
439+
432440function runBenchmark ($ stopwatch , $ benchmark , $ multiplier = 1 )
433441{
434442 $ r = null ;
@@ -448,6 +456,16 @@ function runBenchmark($stopwatch, $benchmark, $multiplier = 1)
448456 return number_format ($ time , 4 ) . ' s ' ;
449457}
450458
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+
451469function setup (callable $ hook )
452470{
453471 global $ setupHooks ;
0 commit comments