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