maintenance: Add unit test for Benchmarker class
[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 // @codeCoverageIgnoreStart
30 require_once __DIR__ . '/../Maintenance.php';
31 // @codeCoverageIgnoreEnd
32
33 /**
34 * Base class for benchmark scripts.
35 *
36 * @ingroup Benchmark
37 */
38 abstract class Benchmarker extends Maintenance {
39 protected $defaultCount = 100;
40 private $lang;
41
42 public function __construct() {
43 parent::__construct();
44 $this->addOption( 'count', 'How many times to run a benchmark', false, true );
45 $this->addOption( 'verbose', 'Verbose logging of resource usage', false, false, 'v' );
46 }
47
48 public function bench( array $benchs ) {
49 $this->lang = Language::factory( 'en' );
50
51 $this->startBench();
52 $count = $this->getOption( 'count', $this->defaultCount );
53 $verbose = $this->hasOption( 'verbose' );
54 foreach ( $benchs as $key => $bench ) {
55 // Shortcut for simple functions
56 if ( is_callable( $bench ) ) {
57 $bench = [ 'function' => $bench ];
58 }
59
60 // Default to no arguments
61 if ( !isset( $bench['args'] ) ) {
62 $bench['args'] = [];
63 }
64
65 // Optional setup called outside time measure
66 if ( isset( $bench['setup'] ) ) {
67 call_user_func( $bench['setup'] );
68 }
69
70 // Run benchmarks
71 $times = [];
72 for ( $i = 0; $i < $count; $i++ ) {
73 $t = microtime( true );
74 call_user_func_array( $bench['function'], $bench['args'] );
75 $t = ( microtime( true ) - $t ) * 1000;
76 if ( $verbose ) {
77 $this->verboseRun( $i );
78 }
79 $times[] = $t;
80 }
81
82 // Collect metrics
83 sort( $times, SORT_NUMERIC );
84 $min = $times[0];
85 $max = end( $times );
86 if ( $count % 2 ) {
87 $median = $times[ ( $count - 1 ) / 2 ];
88 } else {
89 $median = ( $times[$count / 2] + $times[$count / 2 - 1] ) / 2;
90 }
91 $total = array_sum( $times );
92 $mean = $total / $count;
93
94 // Name defaults to name of called function
95 if ( is_string( $key ) ) {
96 $name = $key;
97 } else {
98 if ( is_array( $bench['function'] ) ) {
99 $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
100 } else {
101 $name = strval( $bench['function'] );
102 }
103 $name = sprintf( "%s(%s)",
104 $name,
105 implode( ', ', $bench['args'] )
106 );
107 }
108
109 $this->addResult( [
110 'name' => $name,
111 'count' => $count,
112 'total' => $total,
113 'min' => $min,
114 'median' => $median,
115 'mean' => $mean,
116 'max' => $max,
117 'usage' => [
118 'mem' => memory_get_usage( true ),
119 'mempeak' => memory_get_peak_usage( true ),
120 ],
121 ] );
122 }
123 }
124
125 public function startBench() {
126 $this->output(
127 sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
128 phpversion(),
129 php_uname( 'm' ),
130 php_uname( 's' ),
131 php_uname( 'r' ),
132 php_uname( 'v' )
133 )
134 );
135 }
136
137 public function addResult( $res ) {
138 $ret = sprintf( "%s\n %' 6s: %d\n",
139 $res['name'],
140 'times',
141 $res['count']
142 );
143
144 foreach ( [ 'total', 'min', 'median', 'mean', 'max' ] as $metric ) {
145 $ret .= sprintf( " %' 6s: %6.2fms\n",
146 $metric,
147 $res[$metric]
148 );
149 }
150
151 foreach ( [
152 'mem' => 'Current memory usage',
153 'mempeak' => 'Peak memory usage'
154 ] as $key => $label ) {
155 $ret .= sprintf( "%' 20s: %s\n",
156 $label,
157 $this->lang->formatSize( $res['usage'][$key] )
158 );
159 }
160
161 $this->output( "$ret\n" );
162 }
163
164 protected function verboseRun( $iteration ) {
165 $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
166 $iteration,
167 $this->lang->formatSize( memory_get_usage( true ) ),
168 $this->lang->formatSize( memory_get_peak_usage( true ) )
169 ) );
170 }
171 }