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