Index: trunk/phase3/maintenance/benchmarks/Benchmarker.php |
— | — | @@ -0,0 +1,72 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Create a doxygen subgroup of Maintenance for benchmarks |
| 5 | + * @defgroup Benchmark |
| 6 | + * @ingroup Maintenance |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * TODO: report PHP version, OS .. |
| 11 | + * @file |
| 12 | + * @ingroup Benchmark |
| 13 | + */ |
| 14 | + |
| 15 | +require_once( dirname( __FILE__ ) . '/../Maintenance.php' ); |
| 16 | +abstract class Benchmarker extends Maintenance { |
| 17 | + private $results; |
| 18 | + |
| 19 | + public function __construct() { |
| 20 | + parent::__construct(); |
| 21 | + $this->addOption( 'count', "How many time to run a benchmark", false, true ); |
| 22 | + } |
| 23 | + |
| 24 | + public function bench( array $benchs ) { |
| 25 | + $bench_number = 0; |
| 26 | + $count = $this->getOption( 'count' ); |
| 27 | + |
| 28 | + foreach( $benchs as $bench ) { |
| 29 | + // handle empty args |
| 30 | + if(!array_key_exists( 'args', $bench )) { |
| 31 | + $bench['args'] = array(); |
| 32 | + } |
| 33 | + |
| 34 | + $bench_number++; |
| 35 | + $start = wfTime(); |
| 36 | + for( $i=0; $i<$count; $i++ ) { |
| 37 | + call_user_func_array( $bench['function'], $bench['args'] ); |
| 38 | + } |
| 39 | + $delta = wfTime() - $start; |
| 40 | + |
| 41 | + // function passed as a callback |
| 42 | + if( is_array( $bench['function'] ) ) { |
| 43 | + $ret = get_class( $bench['function'][0] ). '->' . $bench['function'][1]; |
| 44 | + $bench['function'] = $ret; |
| 45 | + } |
| 46 | + |
| 47 | + $this->results[$bench_number] = array( |
| 48 | + 'function' => $bench['function'], |
| 49 | + 'arguments' => $bench['args'], |
| 50 | + 'count' => $count, |
| 51 | + 'delta' => $delta, |
| 52 | + 'average' => $delta / $count, |
| 53 | + ); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public function getFormattedResults( ) { |
| 58 | + $ret = ''; |
| 59 | + foreach( $this->results as $res ) { |
| 60 | + // show function with args |
| 61 | + $ret .= sprintf( "%s times: function %s(%s) :\n", |
| 62 | + $res['count'], |
| 63 | + $res['function'], |
| 64 | + join( ', ', $res['arguments'] ) |
| 65 | + ); |
| 66 | + $ret .= sprintf( " %6.2fms (%6.2fms each)\n", |
| 67 | + $res['delta'] * 1000, |
| 68 | + $res['average'] * 1000 |
| 69 | + ); |
| 70 | + } |
| 71 | + return $ret; |
| 72 | + } |
| 73 | +} |
Index: trunk/phase3/maintenance/benchmarks/bench_wfIsWindows.php |
— | — | @@ -0,0 +1,46 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * This come from r75429 message |
| 5 | + * @author Platonides |
| 6 | + */ |
| 7 | + |
| 8 | +require_once( dirname( __FILE__ ) . '/Benchmarker.php' ); |
| 9 | +class bench_wfIsWindows extends Benchmarker { |
| 10 | + |
| 11 | + public function __construct() { |
| 12 | + parent::__construct(); |
| 13 | + } |
| 14 | + |
| 15 | + public function execute() { |
| 16 | + $this->bench( array( |
| 17 | + array( 'function' => array( $this, 'wfIsWindows' ) ), |
| 18 | + array( 'function' => array( $this, 'wfIsWindowsCached' ) ), |
| 19 | + )); |
| 20 | + print $this->getFormattedResults(); |
| 21 | + } |
| 22 | + |
| 23 | + static function is_win() { |
| 24 | + return substr( php_uname(), 0, 7 == 'Windows' ); |
| 25 | + } |
| 26 | + |
| 27 | + // bench function 1 |
| 28 | + function wfIsWindows() { |
| 29 | + if( self::is_win() ) { |
| 30 | + return true; |
| 31 | + } else { |
| 32 | + return false; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // bench function 2 |
| 37 | + function wfIsWindowsCached() { |
| 38 | + static $isWindows = null; |
| 39 | + if( $isWindows == null ) { |
| 40 | + $isWindows = self::is_win(); |
| 41 | + } |
| 42 | + return $isWindows; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +$maintClass = 'bench_wfIsWindows'; |
| 47 | +require_once( DO_MAINTENANCE ); |