MediaWiki REL1_33
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 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 // Setup outside of time measure for each loop
98 if ( isset( $bench['setupEach'] ) ) {
99 $bench['setupEach']();
100 }
101 $t = microtime( true );
102 call_user_func_array( $bench['function'], $bench['args'] );
103 $t = ( microtime( true ) - $t ) * 1000;
104 if ( $verbose ) {
105 $this->verboseRun( $i );
106 }
107 $stat->addObservation( $t );
108 }
109
110 $this->addResult( [
111 'name' => $name,
112 'count' => $stat->getCount(),
113 // Get rate per second from mean (in ms)
114 'rate' => $stat->getMean() == 0 ? INF : ( 1.0 / ( $stat->getMean() / 1000.0 ) ),
115 'total' => $stat->getMean() * $stat->getCount(),
116 'mean' => $stat->getMean(),
117 'max' => $stat->max,
118 'stddev' => $stat->getStdDev(),
119 'usage' => [
120 'mem' => memory_get_usage( true ),
121 'mempeak' => memory_get_peak_usage( true ),
122 ],
123 ] );
124 }
125 }
126
127 public function startBench() {
128 $this->output(
129 sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
130 phpversion(),
131 php_uname( 'm' ),
132 php_uname( 's' ),
133 php_uname( 'r' ),
134 php_uname( 'v' )
135 )
136 );
137 }
138
139 public function addResult( $res ) {
140 $ret = sprintf( "%s\n %' 6s: %d\n",
141 $res['name'],
142 'count',
143 $res['count']
144 );
145 $ret .= sprintf( " %' 6s: %8.1f/s\n",
146 'rate',
147 $res['rate']
148 );
149 foreach ( [ 'total', 'mean', 'max', 'stddev' ] as $metric ) {
150 $ret .= sprintf( " %' 6s: %8.2fms\n",
151 $metric,
152 $res[$metric]
153 );
154 }
155
156 foreach ( [
157 'mem' => 'Current memory usage',
158 'mempeak' => 'Peak memory usage'
159 ] as $key => $label ) {
160 $ret .= sprintf( "%' 20s: %s\n",
161 $label,
162 $this->lang->formatSize( $res['usage'][$key] )
163 );
164 }
165
166 $this->output( "$ret\n" );
167 }
168
169 protected function verboseRun( $iteration ) {
170 $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
171 $iteration,
172 $this->lang->formatSize( memory_get_usage( true ) ),
173 $this->lang->formatSize( memory_get_peak_usage( true ) )
174 ) );
175 }
176
182 protected function loadFile( $file ) {
183 $content = file_get_contents( $file );
184 // Detect GZIP compression header
185 if ( substr( $content, 0, 2 ) === "\037\213" ) {
186 $content = gzdecode( $content );
187 }
188 return $content;
189 }
190}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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 exists.
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.
$res
Definition database.txt:21
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2003
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:271
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$content
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition router.php:42