Merge "Show a warning in edit preview when a template loop is detected"
[lhc/web/wiklou.git] / maintenance / benchmarks / Benchmarker.php
1 <?php
2 /**
3 * @defgroup Benchmark Benchmark
4 * @ingroup Maintenance
5 */
6
7 /**
8 * Base code for benchmark scripts.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 * @ingroup Benchmark
27 */
28
29 require_once __DIR__ . '/../Maintenance.php';
30
31 /**
32 * Base class for benchmark scripts.
33 *
34 * @ingroup Benchmark
35 */
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 }