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 require_once __DIR__
. '/../Maintenance.php';
32 * Base class for benchmark scripts.
36 abstract class Benchmarker
extends Maintenance
{
37 protected $defaultCount = 100;
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' );
46 public function bench( array $benchs ) {
47 $this->lang
= Language
::factory( 'en' );
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 ];
58 // Default to no arguments
59 if ( !isset( $bench['args'] ) ) {
63 // Optional setup called outside time measure
64 if ( isset( $bench['setup'] ) ) {
65 call_user_func( $bench['setup'] );
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;
75 $this->verboseRun( $i );
81 sort( $times, SORT_NUMERIC
);
85 $median = $times[ ( $count - 1 ) / 2 ];
87 $median = ( $times[$count / 2] +
$times[$count / 2 - 1] ) / 2;
89 $total = array_sum( $times );
90 $mean = $total / $count;
92 // Name defaults to name of called function
93 if ( is_string( $key ) ) {
96 if ( is_array( $bench['function'] ) ) {
97 $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
99 $name = strval( $bench['function'] );
101 $name = sprintf( "%s(%s)",
103 implode( ', ', $bench['args'] )
116 'mem' => memory_get_usage( true ),
117 'mempeak' => memory_get_peak_usage( true ),
123 public function startBench() {
125 sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
135 public function addResult( $res ) {
136 $ret = sprintf( "%s\n %' 6s: %d\n",
142 foreach ( [ 'total', 'min', 'median', 'mean', 'max' ] as $metric ) {
143 $ret .= sprintf( " %' 6s: %6.2fms\n",
150 'mem' => 'Current memory usage',
151 'mempeak' => 'Peak memory usage'
152 ] as $key => $label ) {
153 $ret .= sprintf( "%' 20s: %s\n",
155 $this->lang
->formatSize( $res['usage'][$key] )
159 $this->output( "$ret\n" );
162 protected function verboseRun( $iteration ) {
163 $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
165 $this->lang
->formatSize( memory_get_usage( true ) ),
166 $this->lang
->formatSize( memory_get_peak_usage( true ) )