MediaWiki  REL1_22
Benchmarker.php
Go to the documentation of this file.
00001 <?php
00030 require_once __DIR__ . '/../Maintenance.php';
00031 
00037 abstract class Benchmarker extends Maintenance {
00038     private $results;
00039 
00040     public function __construct() {
00041         parent::__construct();
00042         $this->addOption( 'count', "How many time to run a benchmark", false, true );
00043     }
00044 
00045     public function bench( array $benchs ) {
00046         $bench_number = 0;
00047         $count = $this->getOption( 'count', 100 );
00048 
00049         foreach( $benchs as $bench ) {
00050             // handle empty args
00051             if( !array_key_exists( 'args', $bench ) ) {
00052                 $bench['args'] = array();
00053             }
00054 
00055             $bench_number++;
00056             $start = microtime( true );
00057             for( $i = 0; $i < $count; $i++ ) {
00058                 call_user_func_array( $bench['function'], $bench['args'] );
00059             }
00060             $delta = microtime( true ) - $start;
00061 
00062             // function passed as a callback
00063             if( is_array( $bench['function'] ) ) {
00064                 $ret = get_class( $bench['function'][0] ) . '->' . $bench['function'][1];
00065                 $bench['function'] = $ret;
00066             }
00067 
00068             $this->results[$bench_number] = array(
00069                 'function'  => $bench['function'],
00070                 'arguments' => $bench['args'],
00071                 'count'     => $count,
00072                 'delta'     => $delta,
00073                 'average'   => $delta / $count,
00074                 );
00075         }
00076     }
00077 
00078     public function getFormattedResults() {
00079         $ret = '';
00080         foreach( $this->results as $res ) {
00081             // show function with args
00082             $ret .= sprintf( "%s times: function %s(%s) :\n",
00083                 $res['count'],
00084                 $res['function'],
00085                 join( ', ', $res['arguments'] )
00086             );
00087             $ret .= sprintf( "   %6.2fms (%6.2fms each)\n",
00088                 $res['delta'] * 1000,
00089                 $res['average'] * 1000
00090             );
00091         }
00092         return $ret;
00093     }
00094 }