MediaWiki  1.31.0
Benchmarker.php
Go to the documentation of this file.
1 <?php
29 use Wikimedia\RunningStat;
30 
31 // @codeCoverageIgnoreStart
32 require_once __DIR__ . '/../Maintenance.php';
33 // @codeCoverageIgnoreEnd
34 
40 abstract 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  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  // Optional setup called outside time measure
68  if ( isset( $bench['setup'] ) ) {
69  call_user_func( $bench['setup'] );
70  }
71 
72  // Run benchmarks
73  $stat = new RunningStat();
74  for ( $i = 0; $i < $count; $i++ ) {
75  $t = microtime( true );
76  call_user_func_array( $bench['function'], $bench['args'] );
77  $t = ( microtime( true ) - $t ) * 1000;
78  if ( $verbose ) {
79  $this->verboseRun( $i );
80  }
81  $stat->addObservation( $t );
82  }
83 
84  // Name defaults to name of called function
85  if ( is_string( $key ) ) {
86  $name = $key;
87  } else {
88  if ( is_array( $bench['function'] ) ) {
89  $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
90  } else {
91  $name = strval( $bench['function'] );
92  }
93  $name = sprintf( "%s(%s)",
94  $name,
95  implode( ', ', $bench['args'] )
96  );
97  }
98 
99  $this->addResult( [
100  'name' => $name,
101  'count' => $stat->getCount(),
102  // Get rate per second from mean (in ms)
103  'rate' => $stat->getMean() == 0 ? INF : ( 1.0 / ( $stat->getMean() / 1000.0 ) ),
104  'total' => $stat->getMean() * $stat->getCount(),
105  'mean' => $stat->getMean(),
106  'max' => $stat->max,
107  'stddev' => $stat->getStdDev(),
108  'usage' => [
109  'mem' => memory_get_usage( true ),
110  'mempeak' => memory_get_peak_usage( true ),
111  ],
112  ] );
113  }
114  }
115 
116  public function startBench() {
117  $this->output(
118  sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
119  phpversion(),
120  php_uname( 'm' ),
121  php_uname( 's' ),
122  php_uname( 'r' ),
123  php_uname( 'v' )
124  )
125  );
126  }
127 
128  public function addResult( $res ) {
129  $ret = sprintf( "%s\n %' 6s: %d\n",
130  $res['name'],
131  'count',
132  $res['count']
133  );
134  $ret .= sprintf( " %' 6s: %8.1f/s\n",
135  'rate',
136  $res['rate']
137  );
138  foreach ( [ 'total', 'mean', 'max', 'stddev' ] as $metric ) {
139  $ret .= sprintf( " %' 6s: %8.2fms\n",
140  $metric,
141  $res[$metric]
142  );
143  }
144 
145  foreach ( [
146  'mem' => 'Current memory usage',
147  'mempeak' => 'Peak memory usage'
148  ] as $key => $label ) {
149  $ret .= sprintf( "%' 20s: %s\n",
150  $label,
151  $this->lang->formatSize( $res['usage'][$key] )
152  );
153  }
154 
155  $this->output( "$ret\n" );
156  }
157 
158  protected function verboseRun( $iteration ) {
159  $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
160  $iteration,
161  $this->lang->formatSize( memory_get_usage( true ) ),
162  $this->lang->formatSize( memory_get_peak_usage( true ) )
163  ) );
164  }
165 }
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$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:50
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:128
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:219
Benchmarker\startBench
startBench()
Definition: Benchmarker.php:116
Benchmarker
Base class for benchmark scripts.
Definition: Benchmarker.php:40
$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:1987
Benchmarker\$defaultCount
$defaultCount
Definition: Benchmarker.php:41
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:254
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:158
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:388
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:69
Benchmarker\__construct
__construct()
Default constructor.
Definition: Benchmarker.php:44
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:240
Benchmarker\$lang
$lang
Definition: Benchmarker.php:42
array
the array() calling protocol came about after MediaWiki 1.4rc1.