Fixed a typo in r107054
[lhc/web/wiklou.git] / maintenance / benchmarks / Benchmarker.php
1 <?php
2 /**
3 * @defgroup Benchmark Benchmark
4 */
5
6 /**
7 * Create a doxygen subgroup of Maintenance for benchmarks
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @todo Report PHP version, OS ..
25 * @file
26 * @ingroup Maintenance Benchmark
27 */
28
29 require_once( dirname( __FILE__ ) . '/../Maintenance.php' );
30 abstract class Benchmarker extends Maintenance {
31 private $results;
32
33 public function __construct() {
34 parent::__construct();
35 $this->addOption( 'count', "How many time to run a benchmark", false, true );
36 }
37
38 public function bench( array $benchs ) {
39 $bench_number = 0;
40 $count = $this->getOption( 'count', 100 );
41
42 foreach( $benchs as $bench ) {
43 // handle empty args
44 if(!array_key_exists( 'args', $bench )) {
45 $bench['args'] = array();
46 }
47
48 $bench_number++;
49 $start = wfTime();
50 for( $i=0; $i<$count; $i++ ) {
51 call_user_func_array( $bench['function'], $bench['args'] );
52 }
53 $delta = wfTime() - $start;
54
55 // function passed as a callback
56 if( is_array( $bench['function'] ) ) {
57 $ret = get_class( $bench['function'][0] ). '->' . $bench['function'][1];
58 $bench['function'] = $ret;
59 }
60
61 $this->results[$bench_number] = array(
62 'function' => $bench['function'],
63 'arguments' => $bench['args'],
64 'count' => $count,
65 'delta' => $delta,
66 'average' => $delta / $count,
67 );
68 }
69 }
70
71 public function getFormattedResults( ) {
72 $ret = '';
73 foreach( $this->results as $res ) {
74 // show function with args
75 $ret .= sprintf( "%s times: function %s(%s) :\n",
76 $res['count'],
77 $res['function'],
78 join( ', ', $res['arguments'] )
79 );
80 $ret .= sprintf( " %6.2fms (%6.2fms each)\n",
81 $res['delta'] * 1000,
82 $res['average'] * 1000
83 );
84 }
85 return $ret;
86 }
87 }