benchmarks: Add setup, bench naming, and custom count default
[lhc/web/wiklou.git] / maintenance / benchmarks / Benchmarker.php
1 <?php
2 /**
3 * @defgroup Benchmark Benchmark
4 * @ingroup Maintenance
5 */
6
7 /**
8 * Base code for benchmark scripts.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 * @ingroup Benchmark
27 */
28
29 require_once __DIR__ . '/../Maintenance.php';
30
31 /**
32 * Base class for benchmark scripts.
33 *
34 * @ingroup Benchmark
35 */
36 abstract class Benchmarker extends Maintenance {
37 private $results;
38 protected $defaultCount = 100;
39
40 public function __construct() {
41 parent::__construct();
42 $this->addOption( 'count', 'How many times to run a benchmark', false, true );
43 }
44
45 public function bench( array $benchs ) {
46 $count = $this->getOption( 'count', $this->defaultCount );
47 foreach ( $benchs as $key => $bench ) {
48 // Default to no arguments
49 if ( !isset( $bench['args'] ) ) {
50 $bench['args'] = [];
51 }
52
53 // Optional setup called outside time measure
54 if ( isset( $bench['setup'] ) ) {
55 call_user_func( $bench['setup'] );
56 }
57 $start = microtime( true );
58 for ( $i = 0; $i < $count; $i++ ) {
59 call_user_func_array( $bench['function'], $bench['args'] );
60 }
61 $delta = microtime( true ) - $start;
62
63 // Name defaults to name of called function
64 if ( is_string( $key ) ) {
65 $name = $key;
66 } else {
67 if ( is_array( $bench['function'] ) ) {
68 $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
69 } else {
70 $name = strval( $bench['function'] );
71 }
72 $name = sprintf( "%s(%s)",
73 $name,
74 implode( ', ', $bench['args'] )
75 );
76 }
77
78 $this->results[] = [
79 'name' => $name,
80 'count' => $count,
81 'total' => $delta,
82 'average' => $delta / $count,
83 ];
84 }
85 }
86
87 public function getFormattedResults() {
88 $ret = sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
89 phpversion(),
90 php_uname( 'm' ),
91 php_uname( 's' ),
92 php_uname( 'r' ),
93 php_uname( 'v' )
94 );
95 foreach ( $this->results as $res ) {
96 // show function with args
97 $ret .= sprintf( "%s times: %s\n",
98 $res['count'],
99 $res['name']
100 );
101 $ret .= sprintf( " %6.2fms (%6.4fms each)\n",
102 $res['total'] * 1e3,
103 $res['average'] * 1e3
104 );
105 }
106
107 return $ret;
108 }
109 }