MediaWiki REL1_37
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 if ( is_array( $bench['function'] ) ) {
72 $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
73 } else {
74 $name = strval( $bench['function'] );
75 }
76 $name = sprintf( "%s(%s)",
77 $name,
78 implode( ', ', $bench['args'] )
79 );
80 }
81
82 $normBenchs[$name] = $bench;
83 }
84
85 foreach ( $normBenchs as $name => $bench ) {
86 // Optional setup called outside time measure
87 if ( isset( $bench['setup'] ) ) {
88 call_user_func( $bench['setup'] );
89 }
90
91 // Run benchmarks
92 $stat = new RunningStat();
93 for ( $i = 0; $i < $count; $i++ ) {
94 // Setup outside of time measure for each loop
95 if ( isset( $bench['setupEach'] ) ) {
96 $bench['setupEach']();
97 }
98 $t = microtime( true );
99 call_user_func_array( $bench['function'], $bench['args'] );
100 $t = ( microtime( true ) - $t ) * 1000;
101 if ( $verbose ) {
102 $this->verboseRun( $i );
103 }
104 $stat->addObservation( $t );
105 }
106
107 $this->addResult( [
108 'name' => $name,
109 'count' => $stat->getCount(),
110 // Get rate per second from mean (in ms)
111 'rate' => $stat->getMean() == 0 ? INF : ( 1.0 / ( $stat->getMean() / 1000.0 ) ),
112 'total' => $stat->getMean() * $stat->getCount(),
113 'mean' => $stat->getMean(),
114 'max' => $stat->max,
115 'stddev' => $stat->getStdDev(),
116 'usage' => [
117 'mem' => memory_get_usage( true ),
118 'mempeak' => memory_get_peak_usage( true ),
119 ],
120 ] );
121 }
122 }
123
124 public function startBench() {
125 $this->output(
126 sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
127 phpversion(),
128 php_uname( 'm' ),
129 php_uname( 's' ),
130 php_uname( 'r' ),
131 php_uname( 'v' )
132 )
133 );
134 }
135
136 public function addResult( $res ) {
137 $ret = sprintf( "%s\n %' 6s: %d\n",
138 $res['name'],
139 'count',
140 $res['count']
141 );
142 $ret .= sprintf( " %' 6s: %8.1f/s\n",
143 'rate',
144 $res['rate']
145 );
146 foreach ( [ 'total', 'mean', 'max', 'stddev' ] as $metric ) {
147 $ret .= sprintf( " %' 6s: %8.2fms\n",
148 $metric,
149 $res[$metric]
150 );
151 }
152
153 foreach ( [
154 'mem' => 'Current memory usage',
155 'mempeak' => 'Peak memory usage'
156 ] as $key => $label ) {
157 $ret .= sprintf( "%' 20s: %s\n",
158 $label,
159 $this->formatSize( $res['usage'][$key] )
160 );
161 }
162
163 $this->output( "$ret\n" );
164 }
165
166 protected function verboseRun( $iteration ) {
167 $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
168 $iteration,
169 $this->formatSize( memory_get_usage( true ) ),
170 $this->formatSize( memory_get_peak_usage( true ) )
171 ) );
172 }
173
184 private function formatSize( $bytes ): string {
185 if ( $bytes >= ( 1024 ** 3 ) ) {
186 return number_format( $bytes / ( 1024 ** 3 ), 2 ) . ' GiB';
187 }
188 if ( $bytes >= ( 1024 ** 2 ) ) {
189 return number_format( $bytes / ( 1024 ** 2 ), 2 ) . ' MiB';
190 }
191 if ( $bytes >= 1024 ) {
192 return number_format( $bytes / 1024, 1 ) . ' KiB';
193 }
194 return $bytes . ' B';
195 }
196
202 protected function loadFile( $file ) {
203 $content = file_get_contents( $file );
204 // Detect GZIP compression header
205 if ( substr( $content, 0, 2 ) === "\037\213" ) {
206 $content = gzdecode( $content );
207 }
208 return $content;
209 }
210}
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:88
Base class for benchmark scripts.
__construct()
Default constructor.
verboseRun( $iteration)
formatSize( $bytes)
Format an amount of bytes into short human-readable string.
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