MediaWiki REL1_39
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 {
41 protected $defaultCount = 100;
42
43 public function __construct() {
44 parent::__construct();
45 $this->addOption( 'count', "How many times to run a benchmark. Default: {$this->defaultCount}", false, true );
46 $this->addOption( 'verbose', 'Verbose logging of resource usage', false, false, 'v' );
47 }
48
49 public function bench( array $benchs ) {
50 $this->startBench();
51 $count = $this->getOption( 'count', $this->defaultCount );
52 $verbose = $this->hasOption( 'verbose' );
53
54 // Normalise
55 $normBenchs = [];
56 foreach ( $benchs as $key => $bench ) {
57 // Shortcut for simple functions
58 if ( is_callable( $bench ) ) {
59 $bench = [ 'function' => $bench ];
60 }
61
62 // Default to no arguments
63 if ( !isset( $bench['args'] ) ) {
64 $bench['args'] = [];
65 }
66
67 // Name defaults to name of called function
68 if ( is_string( $key ) ) {
69 $name = $key;
70 } else {
71 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
72 if ( is_array( $bench['function'] ) ) {
73 $class = $bench['function'][0];
74 if ( is_object( $class ) ) {
75 $class = get_class( $class );
76 }
77 $name = $class . '::' . $bench['function'][1];
78 } else {
79 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
80 $name = strval( $bench['function'] );
81 }
82 $name = sprintf( "%s(%s)",
83 $name,
84 implode(
85 ', ',
86 array_map(
87 static function ( $a ) {
88 return var_export( $a, true );
89 },
90 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
91 $bench['args']
92 )
93 )
94 );
95 }
96
97 $normBenchs[$name] = $bench;
98 }
99
100 foreach ( $normBenchs as $name => $bench ) {
101 // Optional setup called outside time measure
102 if ( isset( $bench['setup'] ) ) {
103 call_user_func( $bench['setup'] );
104 }
105
106 // Run benchmarks
107 $stat = new RunningStat();
108 for ( $i = 0; $i < $count; $i++ ) {
109 // Setup outside of time measure for each loop
110 if ( isset( $bench['setupEach'] ) ) {
111 $bench['setupEach']();
112 }
113 $t = microtime( true );
114 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
115 call_user_func_array( $bench['function'], $bench['args'] );
116 $t = ( microtime( true ) - $t ) * 1000;
117 if ( $verbose ) {
118 $this->verboseRun( $i );
119 }
120 $stat->addObservation( $t );
121 }
122
123 $this->addResult( [
124 'name' => $name,
125 'count' => $stat->getCount(),
126 // Get rate per second from mean (in ms)
127 'rate' => $stat->getMean() == 0 ? INF : ( 1.0 / ( $stat->getMean() / 1000.0 ) ),
128 'total' => $stat->getMean() * $stat->getCount(),
129 'mean' => $stat->getMean(),
130 'max' => $stat->max,
131 'stddev' => $stat->getStdDev(),
132 'usage' => [
133 'mem' => memory_get_usage( true ),
134 'mempeak' => memory_get_peak_usage( true ),
135 ],
136 ] );
137 }
138 }
139
140 public function startBench() {
141 $this->output(
142 sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
143 phpversion(),
144 php_uname( 'm' ),
145 php_uname( 's' ),
146 php_uname( 'r' ),
147 php_uname( 'v' )
148 )
149 );
150 }
151
152 public function addResult( $res ) {
153 $ret = sprintf( "%s\n %' 6s: %d\n",
154 $res['name'],
155 'count',
156 $res['count']
157 );
158 $ret .= sprintf( " %' 6s: %8.1f/s\n",
159 'rate',
160 $res['rate']
161 );
162 foreach ( [ 'total', 'mean', 'max', 'stddev' ] as $metric ) {
163 $ret .= sprintf( " %' 6s: %8.2fms\n",
164 $metric,
165 $res[$metric]
166 );
167 }
168
169 foreach ( [
170 'mem' => 'Current memory usage',
171 'mempeak' => 'Peak memory usage'
172 ] as $key => $label ) {
173 $ret .= sprintf( "%' 20s: %s\n",
174 $label,
175 $this->formatSize( $res['usage'][$key] )
176 );
177 }
178
179 $this->output( "$ret\n" );
180 }
181
182 protected function verboseRun( $iteration ) {
183 $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
184 $iteration,
185 $this->formatSize( memory_get_usage( true ) ),
186 $this->formatSize( memory_get_peak_usage( true ) )
187 ) );
188 }
189
200 private function formatSize( $bytes ): string {
201 if ( $bytes >= ( 1024 ** 3 ) ) {
202 return number_format( $bytes / ( 1024 ** 3 ), 2 ) . ' GiB';
203 }
204 if ( $bytes >= ( 1024 ** 2 ) ) {
205 return number_format( $bytes / ( 1024 ** 2 ), 2 ) . ' MiB';
206 }
207 if ( $bytes >= 1024 ) {
208 return number_format( $bytes / 1024, 1 ) . ' KiB';
209 }
210 return $bytes . ' B';
211 }
212
218 protected function loadFile( $file ) {
219 $content = file_get_contents( $file );
220 // Detect GZIP compression header
221 if ( substr( $content, 0, 2 ) === "\037\213" ) {
222 $content = gzdecode( $content );
223 }
224 return $content;
225 }
226}
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:82
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.
$content
Definition router.php:76
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42