3 * @defgroup Benchmark Benchmark
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 ..
26 * @ingroup Maintenance Benchmark
29 require_once( dirname( __FILE__
) . '/../Maintenance.php' );
30 abstract class Benchmarker
extends Maintenance
{
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 ) {
40 $count = $this->getOption( 'count', 100 );
42 foreach( $benchs as $bench ) {
44 if(!array_key_exists( 'args', $bench )) {
45 $bench['args'] = array();
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'],
66 'average' => $delta / $count,
71 public function getFormattedResults( ) {
73 foreach( $this->results
as $res ) {
74 // show function with args
75 $ret .= sprintf( "%s times: function %s(%s) :\n",
78 join( ', ', $res['arguments'] )
80 $ret .= sprintf( " %6.2fms (%6.2fms each)\n",
82 $res['average'] * 1000