X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/87219ebd28426c6d21cb545233ee52f5f7af7dfd..18a6620945d02687fbcfc4c27355d952fd748b41:/maintenance/benchmarks/Benchmarker.php diff --git a/maintenance/benchmarks/Benchmarker.php b/maintenance/benchmarks/Benchmarker.php new file mode 100644 index 00000000..66789ea4 --- /dev/null +++ b/maintenance/benchmarks/Benchmarker.php @@ -0,0 +1,72 @@ +addOption( 'count', "How many time to run a benchmark", false, true ); + } + + public function bench( array $benchs ) { + $bench_number = 0; + $count = $this->getOption( 'count', 100 ); + + foreach( $benchs as $bench ) { + // handle empty args + if(!array_key_exists( 'args', $bench )) { + $bench['args'] = array(); + } + + $bench_number++; + $start = wfTime(); + for( $i=0; $i<$count; $i++ ) { + call_user_func_array( $bench['function'], $bench['args'] ); + } + $delta = wfTime() - $start; + + // function passed as a callback + if( is_array( $bench['function'] ) ) { + $ret = get_class( $bench['function'][0] ). '->' . $bench['function'][1]; + $bench['function'] = $ret; + } + + $this->results[$bench_number] = array( + 'function' => $bench['function'], + 'arguments' => $bench['args'], + 'count' => $count, + 'delta' => $delta, + 'average' => $delta / $count, + ); + } + } + + public function getFormattedResults( ) { + $ret = ''; + foreach( $this->results as $res ) { + // show function with args + $ret .= sprintf( "%s times: function %s(%s) :\n", + $res['count'], + $res['function'], + join( ', ', $res['arguments'] ) + ); + $ret .= sprintf( " %6.2fms (%6.2fms each)\n", + $res['delta'] * 1000, + $res['average'] * 1000 + ); + } + return $ret; + } +}