MediaWiki  1.30.0
Benchmarker.php
Go to the documentation of this file.
1 <?php
29 require_once __DIR__ . '/../Maintenance.php';
30 
36 abstract class Benchmarker extends Maintenance {
37  protected $defaultCount = 100;
38  private $lang;
39 
40  public function __construct() {
41  parent::__construct();
42  $this->addOption( 'count', 'How many times to run a benchmark', false, true );
43  $this->addOption( 'verbose', 'Verbose logging of resource usage', false, false, 'v' );
44  }
45 
46  public function bench( array $benchs ) {
47  $this->lang = Language::factory( 'en' );
48 
49  $this->startBench();
50  $count = $this->getOption( 'count', $this->defaultCount );
51  $verbose = $this->hasOption( 'verbose' );
52  foreach ( $benchs as $key => $bench ) {
53  // Shortcut for simple functions
54  if ( is_callable( $bench ) ) {
55  $bench = [ 'function' => $bench ];
56  }
57 
58  // Default to no arguments
59  if ( !isset( $bench['args'] ) ) {
60  $bench['args'] = [];
61  }
62 
63  // Optional setup called outside time measure
64  if ( isset( $bench['setup'] ) ) {
65  call_user_func( $bench['setup'] );
66  }
67 
68  // Run benchmarks
69  $times = [];
70  for ( $i = 0; $i < $count; $i++ ) {
71  $t = microtime( true );
72  call_user_func_array( $bench['function'], $bench['args'] );
73  $t = ( microtime( true ) - $t ) * 1000;
74  if ( $verbose ) {
75  $this->verboseRun( $i );
76  }
77  $times[] = $t;
78  }
79 
80  // Collect metrics
81  sort( $times, SORT_NUMERIC );
82  $min = $times[0];
83  $max = end( $times );
84  if ( $count % 2 ) {
85  $median = $times[ ( $count - 1 ) / 2 ];
86  } else {
87  $median = ( $times[$count / 2] + $times[$count / 2 - 1] ) / 2;
88  }
89  $total = array_sum( $times );
90  $mean = $total / $count;
91 
92  // Name defaults to name of called function
93  if ( is_string( $key ) ) {
94  $name = $key;
95  } else {
96  if ( is_array( $bench['function'] ) ) {
97  $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
98  } else {
99  $name = strval( $bench['function'] );
100  }
101  $name = sprintf( "%s(%s)",
102  $name,
103  implode( ', ', $bench['args'] )
104  );
105  }
106 
107  $this->addResult( [
108  'name' => $name,
109  'count' => $count,
110  'total' => $total,
111  'min' => $min,
112  'median' => $median,
113  'mean' => $mean,
114  'max' => $max,
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  'times',
139  $res['count']
140  );
141 
142  foreach ( [ 'total', 'min', 'median', 'mean', 'max' ] as $metric ) {
143  $ret .= sprintf( " %' 6s: %6.2fms\n",
144  $metric,
145  $res[$metric]
146  );
147  }
148 
149  foreach ( [
150  'mem' => 'Current memory usage',
151  'mempeak' => 'Peak memory usage'
152  ] as $key => $label ) {
153  $ret .= sprintf( "%' 20s: %s\n",
154  $label,
155  $this->lang->formatSize( $res['usage'][$key] )
156  );
157  }
158 
159  $this->output( "$ret\n" );
160  }
161 
162  protected function verboseRun( $iteration ) {
163  $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
164  $iteration,
165  $this->lang->formatSize( memory_get_usage( true ) ),
166  $this->lang->formatSize( memory_get_peak_usage( true ) )
167  ) );
168  }
169 }
$res
$res
Definition: database.txt:21
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
Benchmarker\bench
bench(array $benchs)
Definition: Benchmarker.php:46
php
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:35
Benchmarker\addResult
addResult( $res)
Definition: Benchmarker.php:135
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
Benchmarker\startBench
startBench()
Definition: Benchmarker.php:123
Benchmarker
Base class for benchmark scripts.
Definition: Benchmarker.php:36
$ret
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:1965
Benchmarker\$defaultCount
$defaultCount
Definition: Benchmarker.php:37
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
as
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
Definition: distributors.txt:9
Benchmarker\verboseRun
verboseRun( $iteration)
Definition: Benchmarker.php:162
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:183
$t
$t
Definition: testCompression.php:67
Benchmarker\__construct
__construct()
Default constructor.
Definition: Benchmarker.php:40
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:236
Benchmarker\$lang
$lang
Definition: Benchmarker.php:38
array
the array() calling protocol came about after MediaWiki 1.4rc1.