* bench(): Add support for setup function.
Demonstrated by converting bench_delete_truncate.php to use Benchmarker.
* bench(): Allow benchmarks to be named. Default remains (fn + args).
Useful for closures.
* Benchmarker: Support overriding the default count of 100.
Demonstrated in bench_delete_truncate.php to run 10x instead of
100x (previous: 1x).
Change-Id: Iac182eaf3053f5bf0e811cd23082f530629d8a4e
*/
abstract class Benchmarker extends Maintenance {
private $results;
*/
abstract class Benchmarker extends Maintenance {
private $results;
+ protected $defaultCount = 100;
public function __construct() {
parent::__construct();
public function __construct() {
parent::__construct();
}
public function bench( array $benchs ) {
}
public function bench( array $benchs ) {
- $bench_number = 0;
- $count = $this->getOption( 'count', 100 );
-
- foreach ( $benchs as $bench ) {
- // handle empty args
- if ( !array_key_exists( 'args', $bench ) ) {
+ $count = $this->getOption( 'count', $this->defaultCount );
+ foreach ( $benchs as $key => $bench ) {
+ // Default to no arguments
+ if ( !isset( $bench['args'] ) ) {
+ // Optional setup called outside time measure
+ if ( isset( $bench['setup'] ) ) {
+ call_user_func( $bench['setup'] );
+ }
$start = microtime( true );
for ( $i = 0; $i < $count; $i++ ) {
call_user_func_array( $bench['function'], $bench['args'] );
}
$delta = microtime( true ) - $start;
$start = microtime( true );
for ( $i = 0; $i < $count; $i++ ) {
call_user_func_array( $bench['function'], $bench['args'] );
}
$delta = microtime( true ) - $start;
- // function passed as a callback
- if ( is_array( $bench['function'] ) ) {
- $ret = get_class( $bench['function'][0] ) . '->' . $bench['function'][1];
- $bench['function'] = $ret;
+ // 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->results[$bench_number] = [
- 'function' => $bench['function'],
- 'arguments' => $bench['args'],
+ $this->results[] = [
+ 'name' => $name,
'count' => $count,
'total' => $delta,
'average' => $delta / $count,
'count' => $count,
'total' => $delta,
'average' => $delta / $count,
);
foreach ( $this->results as $res ) {
// show function with args
);
foreach ( $this->results as $res ) {
// show function with args
- $ret .= sprintf( "%s times: function %s(%s) :\n",
+ $ret .= sprintf( "%s times: %s\n",
- $res['function'],
- implode( ', ', $res['arguments'] )
);
$ret .= sprintf( " %6.2fms (%6.4fms each)\n",
$res['total'] * 1e3,
);
$ret .= sprintf( " %6.2fms (%6.4fms each)\n",
$res['total'] * 1e3,
* @ingroup Benchmark
*/
class BenchmarkDeleteTruncate extends Benchmarker {
* @ingroup Benchmark
*/
class BenchmarkDeleteTruncate extends Benchmarker {
+ protected $defaultCount = 10;
+
public function __construct() {
parent::__construct();
$this->addDescription( 'Benchmarks SQL DELETE vs SQL TRUNCATE.' );
public function __construct() {
parent::__construct();
$this->addDescription( 'Benchmarks SQL DELETE vs SQL TRUNCATE.' );
text varbinary(255) NOT NULL
);" );
text varbinary(255) NOT NULL
);" );
- $this->insertData( $dbw );
-
- $start = microtime( true );
-
- $this->delete( $dbw );
-
- $end = microtime( true );
-
- echo "Delete: " . sprintf( "%6.3fms", ( $end - $start ) * 1000 );
- echo "\r\n";
-
- $this->insertData( $dbw );
-
- $start = microtime( true );
-
- $this->truncate( $dbw );
-
- $end = microtime( true );
-
- echo "Truncate: " . sprintf( "%6.3fms", ( $end - $start ) * 1000 );
- echo "\r\n";
+ $this->bench( [
+ 'Delete' => [
+ 'setup' => function () use ( $dbw ) {
+ $this->insertData( $dbw );
+ },
+ 'function' => function () use ( $dbw ) {
+ $this->delete( $dbw );
+ }
+ ],
+ 'Truncate' => [
+ 'setup' => function () use ( $dbw ) {
+ $this->insertData( $dbw );
+ },
+ 'function' => function () use ( $dbw ) {
+ $this->truncate( $dbw );
+ }
+ ]
+ ] );
$dbw->dropTable( 'test' );
$dbw->dropTable( 'test' );
+
+ $this->output( $this->getFormattedResults() );