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 // @codeCoverageIgnoreStart
30 require_once __DIR__
. '/../Maintenance.php';
31 // @codeCoverageIgnoreEnd
34 * Base class for benchmark scripts.
38 abstract class Benchmarker
extends Maintenance
{
39 protected $defaultCount = 100;
42 public function __construct() {
43 parent
::__construct();
44 $this->addOption( 'count', 'How many times to run a benchmark', false, true );
45 $this->addOption( 'verbose', 'Verbose logging of resource usage', false, false, 'v' );
48 public function bench( array $benchs ) {
49 $this->lang
= Language
::factory( 'en' );
52 $count = $this->getOption( 'count', $this->defaultCount
);
53 $verbose = $this->hasOption( 'verbose' );
54 foreach ( $benchs as $key => $bench ) {
55 // Shortcut for simple functions
56 if ( is_callable( $bench ) ) {
57 $bench = [ 'function' => $bench ];
60 // Default to no arguments
61 if ( !isset( $bench['args'] ) ) {
65 // Optional setup called outside time measure
66 if ( isset( $bench['setup'] ) ) {
67 call_user_func( $bench['setup'] );
72 for ( $i = 0; $i < $count; $i++
) {
73 $t = microtime( true );
74 call_user_func_array( $bench['function'], $bench['args'] );
75 $t = ( microtime( true ) - $t ) * 1000;
77 $this->verboseRun( $i );
83 sort( $times, SORT_NUMERIC
);
87 $median = $times[ ( $count - 1 ) / 2 ];
89 $median = ( $times[$count / 2] +
$times[$count / 2 - 1] ) / 2;
91 $total = array_sum( $times );
92 $mean = $total / $count;
94 // Name defaults to name of called function
95 if ( is_string( $key ) ) {
98 if ( is_array( $bench['function'] ) ) {
99 $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
101 $name = strval( $bench['function'] );
103 $name = sprintf( "%s(%s)",
105 implode( ', ', $bench['args'] )
118 'mem' => memory_get_usage( true ),
119 'mempeak' => memory_get_peak_usage( true ),
125 public function startBench() {
127 sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
137 public function addResult( $res ) {
138 $ret = sprintf( "%s\n %' 6s: %d\n",
144 foreach ( [ 'total', 'min', 'median', 'mean', 'max' ] as $metric ) {
145 $ret .= sprintf( " %' 6s: %6.2fms\n",
152 'mem' => 'Current memory usage',
153 'mempeak' => 'Peak memory usage'
154 ] as $key => $label ) {
155 $ret .= sprintf( "%' 20s: %s\n",
157 $this->lang
->formatSize( $res['usage'][$key] )
161 $this->output( "$ret\n" );
164 protected function verboseRun( $iteration ) {
165 $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
167 $this->lang
->formatSize( memory_get_usage( true ) ),
168 $this->lang
->formatSize( memory_get_peak_usage( true ) )