Merge "Add 3D filetype for STL files"
[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
39 public function __construct() {
40 parent::__construct();
41 $this->addOption( 'count', 'How many times to run a benchmark', false, true );
42 }
43
44 public function bench( array $benchs ) {
45 $this->startBench();
46 $count = $this->getOption( 'count', $this->defaultCount );
47 foreach ( $benchs as $key => $bench ) {
48 // Shortcut for simple functions
49 if ( is_callable( $bench ) ) {
50 $bench = [ 'function' => $bench ];
51 }
52
53 // Default to no arguments
54 if ( !isset( $bench['args'] ) ) {
55 $bench['args'] = [];
56 }
57
58 // Optional setup called outside time measure
59 if ( isset( $bench['setup'] ) ) {
60 call_user_func( $bench['setup'] );
61 }
62
63 // Run benchmarks
64 $times = [];
65 for ( $i = 0; $i < $count; $i++ ) {
66 $t = microtime( true );
67 call_user_func_array( $bench['function'], $bench['args'] );
68 $t = ( microtime( true ) - $t ) * 1000;
69 $times[] = $t;
70 }
71
72 // Collect metrics
73 sort( $times, SORT_NUMERIC );
74 $min = $times[0];
75 $max = end( $times );
76 if ( $count % 2 ) {
77 $median = $times[ ( $count - 1 ) / 2 ];
78 } else {
79 $median = ( $times[$count / 2] + $times[$count / 2 - 1] ) / 2;
80 }
81 $total = array_sum( $times );
82 $mean = $total / $count;
83
84 // Name defaults to name of called function
85 if ( is_string( $key ) ) {
86 $name = $key;
87 } else {
88 if ( is_array( $bench['function'] ) ) {
89 $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
90 } else {
91 $name = strval( $bench['function'] );
92 }
93 $name = sprintf( "%s(%s)",
94 $name,
95 implode( ', ', $bench['args'] )
96 );
97 }
98
99 $this->addResult( [
100 'name' => $name,
101 'count' => $count,
102 'total' => $total,
103 'min' => $min,
104 'median' => $median,
105 'mean' => $mean,
106 'max' => $max,
107 ] );
108 }
109 }
110
111 public function startBench() {
112 $this->output(
113 sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
114 phpversion(),
115 php_uname( 'm' ),
116 php_uname( 's' ),
117 php_uname( 'r' ),
118 php_uname( 'v' )
119 )
120 );
121 }
122
123 public function addResult( $res ) {
124 $ret = sprintf( "%s\n %' 6s: %d\n",
125 $res['name'],
126 'times',
127 $res['count']
128 );
129 foreach ( [ 'total', 'min', 'median', 'mean', 'max' ] as $metric ) {
130 $ret .= sprintf( " %' 6s: %6.2fms\n",
131 $metric,
132 $res[$metric]
133 );
134 }
135 $this->output( "$ret\n" );
136 }
137 }