From fa3f8e1fd520db1fb69c332d0b5336ec5c027e3d Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 19 Apr 2017 16:46:13 -0700 Subject: [PATCH] benchmarks: Report more metrics (min/max/median) Add minimum, maximum, median to the report in addition to the mean (average) which was already there. Based on benchmarkTidy.php from I254793fc5. Example output: > Delete > times: 10 > total: 7.47ms > min: 0.53ms > median: 0.74ms > mean: 0.75ms > max: 1.21ms > > Truncate > times: 10 > total: 72.38ms > min: 1.37ms > median: 8.32ms > mean: 7.24ms > max: 15.73ms Change-Id: Ifd3064a3621e07f55505490403189cb47022c6c7 --- maintenance/benchmarks/Benchmarker.php | 73 ++++++++++++------- maintenance/benchmarks/bench_HTTP_HTTPS.php | 1 - .../bench_Wikimedia_base_convert.php | 2 - .../benchmarks/bench_delete_truncate.php | 2 - maintenance/benchmarks/bench_if_switch.php | 1 - .../benchmarks/bench_strtr_str_replace.php | 1 - .../benchmarks/bench_utf8_title_check.php | 1 - maintenance/benchmarks/bench_wfIsWindows.php | 1 - 8 files changed, 48 insertions(+), 34 deletions(-) diff --git a/maintenance/benchmarks/Benchmarker.php b/maintenance/benchmarks/Benchmarker.php index 7d8280d20e..638e47eb31 100644 --- a/maintenance/benchmarks/Benchmarker.php +++ b/maintenance/benchmarks/Benchmarker.php @@ -34,7 +34,6 @@ require_once __DIR__ . '/../Maintenance.php'; * @ingroup Benchmark */ abstract class Benchmarker extends Maintenance { - private $results; protected $defaultCount = 100; public function __construct() { @@ -43,6 +42,7 @@ abstract class Benchmarker extends Maintenance { } public function bench( array $benchs ) { + $this->startBench(); $count = $this->getOption( 'count', $this->defaultCount ); foreach ( $benchs as $key => $bench ) { // Default to no arguments @@ -54,11 +54,27 @@ abstract class Benchmarker extends Maintenance { if ( isset( $bench['setup'] ) ) { call_user_func( $bench['setup'] ); } - $start = microtime( true ); + + // Run benchmarks + $times = []; for ( $i = 0; $i < $count; $i++ ) { + $t = microtime( true ); call_user_func_array( $bench['function'], $bench['args'] ); + $t = ( microtime( true ) - $t ) * 1000; + $times[] = $t; } - $delta = microtime( true ) - $start; + + // Collect metrics + sort( $times, SORT_NUMERIC ); + $min = $times[0]; + $max = end( $times ); + if ( $count % 2 ) { + $median = $times[ ( $count - 1 ) / 2 ]; + } else { + $median = ( $times[$count / 2] + $times[$count / 2 - 1] ) / 2; + } + $total = array_sum( $times ); + $mean = $total / $count; // Name defaults to name of called function if ( is_string( $key ) ) { @@ -75,35 +91,42 @@ abstract class Benchmarker extends Maintenance { ); } - $this->results[] = [ + $this->addResult( [ 'name' => $name, 'count' => $count, - 'total' => $delta, - 'average' => $delta / $count, - ]; + 'total' => $total, + 'min' => $min, + 'median' => $median, + 'mean' => $mean, + 'max' => $max, + ] ); } } - public function getFormattedResults() { - $ret = sprintf( "Running PHP version %s (%s) on %s %s %s\n\n", - phpversion(), - php_uname( 'm' ), - php_uname( 's' ), - php_uname( 'r' ), - php_uname( 'v' ) + public function startBench() { + $this->output( + sprintf( "Running PHP version %s (%s) on %s %s %s\n\n", + phpversion(), + php_uname( 'm' ), + php_uname( 's' ), + php_uname( 'r' ), + php_uname( 'v' ) + ) ); - foreach ( $this->results as $res ) { - // show function with args - $ret .= sprintf( "%s times: %s\n", - $res['count'], - $res['name'] - ); - $ret .= sprintf( " %6.2fms (%6.4fms each)\n", - $res['total'] * 1e3, - $res['average'] * 1e3 + } + + public function addResult( $res ) { + $ret = sprintf( "%s\n %' 6s: %d\n", + $res['name'], + 'times', + $res['count'] + ); + foreach ( [ 'total', 'min', 'median', 'mean', 'max' ] as $metric ) { + $ret .= sprintf( " %' 6s: %6.2fms\n", + $metric, + $res[$metric] ); } - - return $ret; + $this->output( "$ret\n" ); } } diff --git a/maintenance/benchmarks/bench_HTTP_HTTPS.php b/maintenance/benchmarks/bench_HTTP_HTTPS.php index 1be50bd438..0e3cd73d0c 100644 --- a/maintenance/benchmarks/bench_HTTP_HTTPS.php +++ b/maintenance/benchmarks/bench_HTTP_HTTPS.php @@ -42,7 +42,6 @@ class BenchHttpHttps extends Benchmarker { [ 'function' => [ $this, 'getHTTP' ] ], [ 'function' => [ $this, 'getHTTPS' ] ], ] ); - $this->output( $this->getFormattedResults() ); } private function doRequest( $proto ) { diff --git a/maintenance/benchmarks/bench_Wikimedia_base_convert.php b/maintenance/benchmarks/bench_Wikimedia_base_convert.php index bc83a24f14..86bcc8a3ac 100644 --- a/maintenance/benchmarks/bench_Wikimedia_base_convert.php +++ b/maintenance/benchmarks/bench_Wikimedia_base_convert.php @@ -60,8 +60,6 @@ class BenchWikimediaBaseConvert extends Benchmarker { 'args' => [ $number, $inbase, $outbase, 0, true, 'gmp' ] ], ] ); - - $this->output( $this->getFormattedResults() ); } protected static function makeRandomNumber( $base, $length ) { diff --git a/maintenance/benchmarks/bench_delete_truncate.php b/maintenance/benchmarks/bench_delete_truncate.php index de655b1a86..0a999ecca1 100644 --- a/maintenance/benchmarks/bench_delete_truncate.php +++ b/maintenance/benchmarks/bench_delete_truncate.php @@ -68,8 +68,6 @@ class BenchmarkDeleteTruncate extends Benchmarker { ] ); $dbw->dropTable( 'test' ); - - $this->output( $this->getFormattedResults() ); } /** diff --git a/maintenance/benchmarks/bench_if_switch.php b/maintenance/benchmarks/bench_if_switch.php index 32c3932266..843ef7cd59 100644 --- a/maintenance/benchmarks/bench_if_switch.php +++ b/maintenance/benchmarks/bench_if_switch.php @@ -42,7 +42,6 @@ class BenchIfSwitch extends Benchmarker { [ 'function' => [ $this, 'doElseIf' ] ], [ 'function' => [ $this, 'doSwitch' ] ], ] ); - $this->output( $this->getFormattedResults() ); } // bench function 1 diff --git a/maintenance/benchmarks/bench_strtr_str_replace.php b/maintenance/benchmarks/bench_strtr_str_replace.php index 276e666f22..55c7159b8f 100644 --- a/maintenance/benchmarks/bench_strtr_str_replace.php +++ b/maintenance/benchmarks/bench_strtr_str_replace.php @@ -51,7 +51,6 @@ class BenchStrtrStrReplace extends Benchmarker { [ 'function' => [ $this, 'benchstrtr_indirect' ] ], [ 'function' => [ $this, 'benchstr_replace_indirect' ] ], ] ); - $this->output( $this->getFormattedResults() ); } protected function benchstrtr() { diff --git a/maintenance/benchmarks/bench_utf8_title_check.php b/maintenance/benchmarks/bench_utf8_title_check.php index 9ba162307b..3091de6208 100644 --- a/maintenance/benchmarks/bench_utf8_title_check.php +++ b/maintenance/benchmarks/bench_utf8_title_check.php @@ -86,7 +86,6 @@ class BenchUtf8TitleCheck extends Benchmarker { ]; } $this->bench( $benchmarks ); - $this->output( $this->getFormattedResults() ); } protected function use_regexp( $s ) { diff --git a/maintenance/benchmarks/bench_wfIsWindows.php b/maintenance/benchmarks/bench_wfIsWindows.php index f26dbb276c..960ef0e8ee 100644 --- a/maintenance/benchmarks/bench_wfIsWindows.php +++ b/maintenance/benchmarks/bench_wfIsWindows.php @@ -42,7 +42,6 @@ class BenchWfIsWindows extends Benchmarker { [ 'function' => [ $this, 'wfIsWindows' ] ], [ 'function' => [ $this, 'wfIsWindowsCached' ] ], ] ); - print $this->getFormattedResults(); } protected static function is_win() { -- 2.20.1