9bfebced5d10ea9f6b5d2fd46f7a0431439a064b
[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 use Wikimedia\RunningStat;
30
31 // @codeCoverageIgnoreStart
32 require_once __DIR__ . '/../Maintenance.php';
33 // @codeCoverageIgnoreEnd
34
35 /**
36 * Base class for benchmark scripts.
37 *
38 * @ingroup Benchmark
39 */
40 abstract class Benchmarker extends Maintenance {
41 protected $defaultCount = 100;
42 private $lang;
43
44 public function __construct() {
45 parent::__construct();
46 $this->addOption( 'count', 'How many times to run a benchmark', false, true );
47 $this->addOption( 'verbose', 'Verbose logging of resource usage', false, false, 'v' );
48 }
49
50 public function bench( array $benchs ) {
51 $this->lang = Language::factory( 'en' );
52
53 $this->startBench();
54 $count = $this->getOption( 'count', $this->defaultCount );
55 $verbose = $this->hasOption( 'verbose' );
56
57 // Normalise
58 $normBenchs = [];
59 foreach ( $benchs as $key => $bench ) {
60 // Shortcut for simple functions
61 if ( is_callable( $bench ) ) {
62 $bench = [ 'function' => $bench ];
63 }
64
65 // Default to no arguments
66 if ( !isset( $bench['args'] ) ) {
67 $bench['args'] = [];
68 }
69
70 // Name defaults to name of called function
71 if ( is_string( $key ) ) {
72 $name = $key;
73 } else {
74 if ( is_array( $bench['function'] ) ) {
75 $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
76 } else {
77 $name = strval( $bench['function'] );
78 }
79 $name = sprintf( "%s(%s)",
80 $name,
81 implode( ', ', $bench['args'] )
82 );
83 }
84
85 $normBenchs[$name] = $bench;
86 }
87
88 foreach ( $normBenchs as $name => $bench ) {
89 // Optional setup called outside time measure
90 if ( isset( $bench['setup'] ) ) {
91 call_user_func( $bench['setup'] );
92 }
93
94 // Run benchmarks
95 $stat = new RunningStat();
96 for ( $i = 0; $i < $count; $i++ ) {
97 $t = microtime( true );
98 call_user_func_array( $bench['function'], $bench['args'] );
99 $t = ( microtime( true ) - $t ) * 1000;
100 if ( $verbose ) {
101 $this->verboseRun( $i );
102 }
103 $stat->addObservation( $t );
104 }
105
106 $this->addResult( [
107 'name' => $name,
108 'count' => $stat->getCount(),
109 // Get rate per second from mean (in ms)
110 'rate' => $stat->getMean() == 0 ? INF : ( 1.0 / ( $stat->getMean() / 1000.0 ) ),
111 'total' => $stat->getMean() * $stat->getCount(),
112 'mean' => $stat->getMean(),
113 'max' => $stat->max,
114 'stddev' => $stat->getStdDev(),
115 'usage' => [
116 'mem' => memory_get_usage( true ),
117 'mempeak' => memory_get_peak_usage( true ),
118 ],
119 ] );
120 }
121 }
122
123 public function startBench() {
124 $this->output(
125 sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
126 phpversion(),
127 php_uname( 'm' ),
128 php_uname( 's' ),
129 php_uname( 'r' ),
130 php_uname( 'v' )
131 )
132 );
133 }
134
135 public function addResult( $res ) {
136 $ret = sprintf( "%s\n %' 6s: %d\n",
137 $res['name'],
138 'count',
139 $res['count']
140 );
141 $ret .= sprintf( " %' 6s: %8.1f/s\n",
142 'rate',
143 $res['rate']
144 );
145 foreach ( [ 'total', 'mean', 'max', 'stddev' ] as $metric ) {
146 $ret .= sprintf( " %' 6s: %8.2fms\n",
147 $metric,
148 $res[$metric]
149 );
150 }
151
152 foreach ( [
153 'mem' => 'Current memory usage',
154 'mempeak' => 'Peak memory usage'
155 ] as $key => $label ) {
156 $ret .= sprintf( "%' 20s: %s\n",
157 $label,
158 $this->lang->formatSize( $res['usage'][$key] )
159 );
160 }
161
162 $this->output( "$ret\n" );
163 }
164
165 protected function verboseRun( $iteration ) {
166 $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
167 $iteration,
168 $this->lang->formatSize( memory_get_usage( true ) ),
169 $this->lang->formatSize( memory_get_peak_usage( true ) )
170 ) );
171 }
172
173 /**
174 * @since 1.32
175 * @param string $file Path to file (maybe compressed with gzip)
176 * @return string Contents of file
177 */
178 protected function loadFile( $file ) {
179 $content = file_get_contents( $file );
180 // Detect GZIP compression header
181 if ( substr( $content, 0, 2 ) === "\037\213" ) {
182 $content = gzdecode( $content );
183 }
184 return $content;
185 }
186 }