Cast the return value of getExtraUserToggles() to an array in case it's not in the...
[mediawiki.git] / maintenance / benchmarks / Benchmarker.php
blob57fb8759bfebf8c98dc50f8e771b76817f4d34b9
1 <?php
2 /**
3 * @defgroup Benchmark Benchmark
4 */
6 /**
7 * Create a doxygen subgroup of Maintenance for benchmarks
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.
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.
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
24 * @todo Report PHP version, OS ..
25 * @file
26 * @ingroup Maintenance Benchmark
29 require_once( dirname( __FILE__ ) . '/../Maintenance.php' );
30 abstract class Benchmarker extends Maintenance {
31 private $results;
33 public function __construct() {
34 parent::__construct();
35 $this->addOption( 'count', "How many time to run a benchmark", false, true );
38 public function bench( array $benchs ) {
39 $bench_number = 0;
40 $count = $this->getOption( 'count', 100 );
42 foreach( $benchs as $bench ) {
43 // handle empty args
44 if(!array_key_exists( 'args', $bench )) {
45 $bench['args'] = array();
48 $bench_number++;
49 $start = wfTime();
50 for( $i=0; $i<$count; $i++ ) {
51 call_user_func_array( $bench['function'], $bench['args'] );
53 $delta = wfTime() - $start;
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;
61 $this->results[$bench_number] = array(
62 'function' => $bench['function'],
63 'arguments' => $bench['args'],
64 'count' => $count,
65 'delta' => $delta,
66 'average' => $delta / $count,
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'] )
80 $ret .= sprintf( " %6.2fms (%6.2fms each)\n",
81 $res['delta'] * 1000,
82 $res['average'] * 1000
85 return $ret;