X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=maintenance%2Fbenchmarks%2FBenchmarker.php;h=832da4db79177c54aa9485a5b6daec8cdda293a3;hp=638e47eb31d9adfe9e1addf3f8d5ff2e067e9c13;hb=6c9a2923fe1ee3a65cb027be5e781772f2b12fbd;hpb=cf35ff756c89ca6d6e003b440076df5ebe7ccef7 diff --git a/maintenance/benchmarks/Benchmarker.php b/maintenance/benchmarks/Benchmarker.php index 638e47eb31..832da4db79 100644 --- a/maintenance/benchmarks/Benchmarker.php +++ b/maintenance/benchmarks/Benchmarker.php @@ -35,16 +35,26 @@ require_once __DIR__ . '/../Maintenance.php'; */ abstract class Benchmarker extends Maintenance { protected $defaultCount = 100; + private $lang; public function __construct() { parent::__construct(); $this->addOption( 'count', 'How many times to run a benchmark', false, true ); + $this->addOption( 'verbose', 'Verbose logging of resource usage', false, false, 'v' ); } public function bench( array $benchs ) { + $this->lang = Language::factory( 'en' ); + $this->startBench(); $count = $this->getOption( 'count', $this->defaultCount ); + $verbose = $this->hasOption( 'verbose' ); foreach ( $benchs as $key => $bench ) { + // Shortcut for simple functions + if ( is_callable( $bench ) ) { + $bench = [ 'function' => $bench ]; + } + // Default to no arguments if ( !isset( $bench['args'] ) ) { $bench['args'] = []; @@ -61,6 +71,9 @@ abstract class Benchmarker extends Maintenance { $t = microtime( true ); call_user_func_array( $bench['function'], $bench['args'] ); $t = ( microtime( true ) - $t ) * 1000; + if ( $verbose ) { + $this->verboseRun( $i ); + } $times[] = $t; } @@ -99,6 +112,10 @@ abstract class Benchmarker extends Maintenance { 'median' => $median, 'mean' => $mean, 'max' => $max, + 'usage' => [ + 'mem' => memory_get_usage( true ), + 'mempeak' => memory_get_peak_usage( true ), + ], ] ); } } @@ -121,12 +138,32 @@ abstract class Benchmarker extends Maintenance { 'times', $res['count'] ); + foreach ( [ 'total', 'min', 'median', 'mean', 'max' ] as $metric ) { $ret .= sprintf( " %' 6s: %6.2fms\n", $metric, $res[$metric] ); } + + foreach ( [ + 'mem' => 'Current memory usage', + 'mempeak' => 'Peak memory usage' + ] as $key => $label ) { + $ret .= sprintf( "%' 20s: %s\n", + $label, + $this->lang->formatSize( $res['usage'][$key] ) + ); + } + $this->output( "$ret\n" ); } + + protected function verboseRun( $iteration ) { + $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n", + $iteration, + $this->lang->formatSize( memory_get_usage( true ) ), + $this->lang->formatSize( memory_get_peak_usage( true ) ) + ) ); + } }