MediaWiki  1.29.2
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 
39  public function __construct() {
40  parent::__construct();
41  $this->addOption( 'count', 'How many times to run a benchmark', false, true );
42  }
43 
44  public function bench( array $benchs ) {
45  $this->startBench();
46  $count = $this->getOption( 'count', $this->defaultCount );
47  foreach ( $benchs as $key => $bench ) {
48  // Default to no arguments
49  if ( !isset( $bench['args'] ) ) {
50  $bench['args'] = [];
51  }
52 
53  // Optional setup called outside time measure
54  if ( isset( $bench['setup'] ) ) {
55  call_user_func( $bench['setup'] );
56  }
57 
58  // Run benchmarks
59  $times = [];
60  for ( $i = 0; $i < $count; $i++ ) {
61  $t = microtime( true );
62  call_user_func_array( $bench['function'], $bench['args'] );
63  $t = ( microtime( true ) - $t ) * 1000;
64  $times[] = $t;
65  }
66 
67  // Collect metrics
68  sort( $times, SORT_NUMERIC );
69  $min = $times[0];
70  $max = end( $times );
71  if ( $count % 2 ) {
72  $median = $times[ ( $count - 1 ) / 2 ];
73  } else {
74  $median = ( $times[$count / 2] + $times[$count / 2 - 1] ) / 2;
75  }
76  $total = array_sum( $times );
77  $mean = $total / $count;
78 
79  // Name defaults to name of called function
80  if ( is_string( $key ) ) {
81  $name = $key;
82  } else {
83  if ( is_array( $bench['function'] ) ) {
84  $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
85  } else {
86  $name = strval( $bench['function'] );
87  }
88  $name = sprintf( "%s(%s)",
89  $name,
90  implode( ', ', $bench['args'] )
91  );
92  }
93 
94  $this->addResult( [
95  'name' => $name,
96  'count' => $count,
97  'total' => $total,
98  'min' => $min,
99  'median' => $median,
100  'mean' => $mean,
101  'max' => $max,
102  ] );
103  }
104  }
105 
106  public function startBench() {
107  $this->output(
108  sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
109  phpversion(),
110  php_uname( 'm' ),
111  php_uname( 's' ),
112  php_uname( 'r' ),
113  php_uname( 'v' )
114  )
115  );
116  }
117 
118  public function addResult( $res ) {
119  $ret = sprintf( "%s\n %' 6s: %d\n",
120  $res['name'],
121  'times',
122  $res['count']
123  );
124  foreach ( [ 'total', 'min', 'median', 'mean', 'max' ] as $metric ) {
125  $ret .= sprintf( " %' 6s: %6.2fms\n",
126  $metric,
127  $res[$metric]
128  );
129  }
130  $this->output( "$ret\n" );
131  }
132 }
$res
$res
Definition: database.txt:21
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
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:44
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:118
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:106
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:1956
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
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
$t
$t
Definition: testCompression.php:67
Benchmarker\__construct
__construct()
Default constructor.
Definition: Benchmarker.php:39
array
the array() calling protocol came about after MediaWiki 1.4rc1.