MediaWiki REL1_30
Benchmarker.php
Go to the documentation of this file.
1<?php
29require_once __DIR__ . '/../Maintenance.php';
30
36abstract 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}
Base class for benchmark scripts.
__construct()
Default constructor.
verboseRun( $iteration)
addResult( $res)
bench(array $benchs)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
hasOption( $name)
Checks to see if a particular param 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
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
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:1975