$this->startBench();
$count = $this->getOption( 'count', $this->defaultCount );
$verbose = $this->hasOption( 'verbose' );
+
+ // Normalise
+ $normBenchs = [];
foreach ( $benchs as $key => $bench ) {
// Shortcut for simple functions
if ( is_callable( $bench ) ) {
$bench['args'] = [];
}
+ // Name defaults to name of called function
+ if ( is_string( $key ) ) {
+ $name = $key;
+ } else {
+ if ( is_array( $bench['function'] ) ) {
+ $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
+ } else {
+ $name = strval( $bench['function'] );
+ }
+ $name = sprintf( "%s(%s)",
+ $name,
+ implode( ', ', $bench['args'] )
+ );
+ }
+
+ $normBenchs[$name] = $bench;
+ }
+
+ foreach ( $normBenchs as $name => $bench ) {
// Optional setup called outside time measure
if ( isset( $bench['setup'] ) ) {
call_user_func( $bench['setup'] );
// Run benchmarks
$stat = new RunningStat();
for ( $i = 0; $i < $count; $i++ ) {
+ // Setup outside of time measure for each loop
+ if ( isset( $bench['setupEach'] ) ) {
+ $bench['setupEach']();
+ }
$t = microtime( true );
call_user_func_array( $bench['function'], $bench['args'] );
$t = ( microtime( true ) - $t ) * 1000;
$stat->addObservation( $t );
}
- // Name defaults to name of called function
- if ( is_string( $key ) ) {
- $name = $key;
- } else {
- if ( is_array( $bench['function'] ) ) {
- $name = get_class( $bench['function'][0] ) . '::' . $bench['function'][1];
- } else {
- $name = strval( $bench['function'] );
- }
- $name = sprintf( "%s(%s)",
- $name,
- implode( ', ', $bench['args'] )
- );
- }
-
$this->addResult( [
'name' => $name,
'count' => $stat->getCount(),
$this->lang->formatSize( memory_get_peak_usage( true ) )
) );
}
+
+ /**
+ * @since 1.32
+ * @param string $file Path to file (maybe compressed with gzip)
+ * @return string Contents of file
+ */
+ protected function loadFile( $file ) {
+ $content = file_get_contents( $file );
+ // Detect GZIP compression header
+ if ( substr( $content, 0, 2 ) === "\037\213" ) {
+ $content = gzdecode( $content );
+ }
+ return $content;
+ }
}