MediaWiki master
Benchmarker.php
Go to the documentation of this file.
1<?php
29use Wikimedia\RunningStat;
30
31// @codeCoverageIgnoreStart
32require_once __DIR__ . '/../Maintenance.php';
33// @codeCoverageIgnoreEnd
34
40abstract class Benchmarker extends Maintenance {
42 protected $defaultCount = 100;
43
44 public function __construct() {
45 parent::__construct();
46 $this->addOption( 'count', "How many times to run a benchmark. Default: {$this->defaultCount}", false, true );
47 $this->addOption( 'verbose', 'Verbose logging of resource usage', false, false, 'v' );
48 }
49
50 public function bench( array $benchs ) {
51 $this->startBench();
52 $count = $this->getOption( 'count', $this->defaultCount );
53 $verbose = $this->hasOption( 'verbose' );
54
55 $normBenchs = [];
56 $shortNames = [];
57
58 // Normalise
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 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
75 if ( is_array( $bench['function'] ) ) {
76 $class = $bench['function'][0];
77 if ( is_object( $class ) ) {
78 $class = get_class( $class );
79 }
80 $name = $class . '::' . $bench['function'][1];
81 } else {
82 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
83 $name = strval( $bench['function'] );
84 }
85 $argsText = implode(
86 ', ',
87 array_map(
88 static function ( $a ) {
89 return var_export( $a, true );
90 },
91 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
92 $bench['args']
93 )
94 );
95 $index = $shortNames[$name] = ( $shortNames[$name] ?? 0 ) + 1;
96 $shorten = strlen( $argsText ) > 80 || str_contains( $argsText, "\n" );
97 if ( !$shorten ) {
98 $name = "$name($argsText)";
99 }
100 if ( $shorten || $index > 1 ) {
101 $name = "$name@$index";
102 }
103 }
104
105 $normBenchs[$name] = $bench;
106 }
107
108 foreach ( $normBenchs as $name => $bench ) {
109 // Optional setup called outside time measure
110 if ( isset( $bench['setup'] ) ) {
111 call_user_func( $bench['setup'] );
112 }
113
114 // Run benchmarks
115 $stat = new RunningStat();
116 for ( $i = 0; $i < $count; $i++ ) {
117 // Setup outside of time measure for each loop
118 if ( isset( $bench['setupEach'] ) ) {
119 $bench['setupEach']();
120 }
121 $t = microtime( true );
122 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
123 call_user_func_array( $bench['function'], $bench['args'] );
124 $t = ( microtime( true ) - $t ) * 1000;
125 if ( $verbose ) {
126 $this->verboseRun( $i );
127 }
128 $stat->addObservation( $t );
129 }
130
131 $this->addResult( [
132 'name' => $name,
133 'count' => $stat->getCount(),
134 // Get rate per second from mean (in ms)
135 'rate' => $stat->getMean() == 0 ? INF : ( 1.0 / ( $stat->getMean() / 1000.0 ) ),
136 'total' => $stat->getMean() * $stat->getCount(),
137 'mean' => $stat->getMean(),
138 'max' => $stat->max,
139 'stddev' => $stat->getStdDev(),
140 'usage' => [
141 'mem' => memory_get_usage( true ),
142 'mempeak' => memory_get_peak_usage( true ),
143 ],
144 ] );
145 }
146 }
147
148 public function startBench() {
149 $this->output(
150 sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
151 phpversion(),
152 php_uname( 'm' ),
153 php_uname( 's' ),
154 php_uname( 'r' ),
155 php_uname( 'v' )
156 )
157 );
158 }
159
160 public function addResult( $res ) {
161 $ret = sprintf( "%s\n %' 6s: %d\n",
162 $res['name'],
163 'count',
164 $res['count']
165 );
166 $ret .= sprintf( " %' 6s: %8.1f/s\n",
167 'rate',
168 $res['rate']
169 );
170 foreach ( [ 'total', 'mean', 'max', 'stddev' ] as $metric ) {
171 $ret .= sprintf( " %' 6s: %8.2fms\n",
172 $metric,
173 $res[$metric]
174 );
175 }
176
177 foreach ( [
178 'mem' => 'Current memory usage',
179 'mempeak' => 'Peak memory usage'
180 ] as $key => $label ) {
181 $ret .= sprintf( "%' 20s: %s\n",
182 $label,
183 $this->formatSize( $res['usage'][$key] )
184 );
185 }
186
187 $this->output( "$ret\n" );
188 }
189
190 protected function verboseRun( $iteration ) {
191 $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
192 $iteration,
193 $this->formatSize( memory_get_usage( true ) ),
194 $this->formatSize( memory_get_peak_usage( true ) )
195 ) );
196 }
197
208 private function formatSize( $bytes ): string {
209 if ( $bytes >= ( 1024 ** 3 ) ) {
210 return number_format( $bytes / ( 1024 ** 3 ), 2 ) . ' GiB';
211 }
212 if ( $bytes >= ( 1024 ** 2 ) ) {
213 return number_format( $bytes / ( 1024 ** 2 ), 2 ) . ' MiB';
214 }
215 if ( $bytes >= 1024 ) {
216 return number_format( $bytes / 1024, 1 ) . ' KiB';
217 }
218 return $bytes . ' B';
219 }
220
226 protected function loadFile( $file ) {
227 $content = file_get_contents( $file );
228 // Detect GZIP compression header
229 if ( str_starts_with( $content, "\037\213" ) ) {
230 $content = gzdecode( $content );
231 }
232 return $content;
233 }
234}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Base class for benchmark scripts.
__construct()
Default constructor.
verboseRun( $iteration)
addResult( $res)
bench(array $benchs)
loadFile( $file)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.