3 * @defgroup Benchmark Benchmark
8 * Base code for benchmark scripts.
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.
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.
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
29 use Wikimedia\RunningStat
;
31 // @codeCoverageIgnoreStart
32 require_once __DIR__
. '/../Maintenance.php';
33 // @codeCoverageIgnoreEnd
36 * Base class for benchmark scripts.
40 abstract class Benchmarker
extends Maintenance
{
41 protected $defaultCount = 100;
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' );
50 public function bench( array $benchs ) {
51 $this->lang
= Language
::factory( 'en' );
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 ];
62 // Default to no arguments
63 if ( !isset( $bench['args'] ) ) {
67 // Optional setup called outside time measure
68 if ( isset( $bench['setup'] ) ) {
69 call_user_func( $bench['setup'] );
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;
79 $this->verboseRun( $i );
81 $stat->addObservation( $t );
84 // Name defaults to name of called function
85 if ( is_string( $key ) ) {
88 if ( is_array( $bench['function'] ) ) {
89 $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
91 $name = strval( $bench['function'] );
93 $name = sprintf( "%s(%s)",
95 implode( ', ', $bench['args'] )
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(),
107 'stddev' => $stat->getStdDev(),
109 'mem' => memory_get_usage( true ),
110 'mempeak' => memory_get_peak_usage( true ),
116 public function startBench() {
118 sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
128 public function addResult( $res ) {
129 $ret = sprintf( "%s\n %' 6s: %d\n",
134 $ret .= sprintf( " %' 6s: %8.1f/s\n",
138 foreach ( [ 'total', 'mean', 'max', 'stddev' ] as $metric ) {
139 $ret .= sprintf( " %' 6s: %8.2fms\n",
146 'mem' => 'Current memory usage',
147 'mempeak' => 'Peak memory usage'
148 ] as $key => $label ) {
149 $ret .= sprintf( "%' 20s: %s\n",
151 $this->lang
->formatSize( $res['usage'][$key] )
155 $this->output( "$ret\n" );
158 protected function verboseRun( $iteration ) {
159 $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
161 $this->lang
->formatSize( memory_get_usage( true ) ),
162 $this->lang
->formatSize( memory_get_peak_usage( true ) )