From: Timo Tijhof Date: Wed, 19 Apr 2017 23:22:50 +0000 (-0700) Subject: benchmarks: Add setup, bench naming, and custom count default X-Git-Tag: 1.31.0-rc.0~3456 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=dcc8b7e4cee8c08a6bce8f795ebf1b07cece73e6;p=lhc%2Fweb%2Fwiklou.git benchmarks: Add setup, bench naming, and custom count default * 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 --- diff --git a/maintenance/benchmarks/Benchmarker.php b/maintenance/benchmarks/Benchmarker.php index 70dc1f4494..7d8280d20e 100644 --- a/maintenance/benchmarks/Benchmarker.php +++ b/maintenance/benchmarks/Benchmarker.php @@ -35,6 +35,7 @@ require_once __DIR__ . '/../Maintenance.php'; */ abstract class Benchmarker extends Maintenance { private $results; + protected $defaultCount = 100; public function __construct() { parent::__construct(); @@ -42,31 +43,40 @@ abstract class Benchmarker extends Maintenance { } 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'] ) ) { $bench['args'] = []; } - $bench_number++; + // 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; - // 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, @@ -84,10 +94,9 @@ abstract class Benchmarker extends Maintenance { ); foreach ( $this->results as $res ) { // show function with args - $ret .= sprintf( "%s times: function %s(%s) :\n", + $ret .= sprintf( "%s times: %s\n", $res['count'], - $res['function'], - implode( ', ', $res['arguments'] ) + $res['name'] ); $ret .= sprintf( " %6.2fms (%6.4fms each)\n", $res['total'] * 1e3, diff --git a/maintenance/benchmarks/bench_delete_truncate.php b/maintenance/benchmarks/bench_delete_truncate.php index 042f8bde21..de655b1a86 100644 --- a/maintenance/benchmarks/bench_delete_truncate.php +++ b/maintenance/benchmarks/bench_delete_truncate.php @@ -32,6 +32,8 @@ use Wikimedia\Rdbms\IMaintainableDatabase; * @ingroup Benchmark */ class BenchmarkDeleteTruncate extends Benchmarker { + protected $defaultCount = 10; + public function __construct() { parent::__construct(); $this->addDescription( 'Benchmarks SQL DELETE vs SQL TRUNCATE.' ); @@ -46,29 +48,28 @@ class BenchmarkDeleteTruncate extends Benchmarker { 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' ); + + $this->output( $this->getFormattedResults() ); } /**