[MediaWiki-commits] [Gerrit] mediawiki/core[master]: benchmarks: Add setup, bench naming, and custom count default
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/349122 ) Change subject: benchmarks: Add setup, bench naming, and custom count default .. 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 --- M maintenance/benchmarks/Benchmarker.php M maintenance/benchmarks/bench_delete_truncate.php 2 files changed, 48 insertions(+), 38 deletions(-) Approvals: Tim Starling: Looks good to me, approved jenkins-bot: Verified diff --git a/maintenance/benchmarks/Benchmarker.php b/maintenance/benchmarks/Benchmarker.php index 70dc1f4..7d8280d 100644 --- a/maintenance/benchmarks/Benchmarker.php +++ b/maintenance/benchmarks/Benchmarker.php @@ -35,6 +35,7 @@ */ abstract class Benchmarker extends Maintenance { private $results; + protected $defaultCount = 100; public function __construct() { parent::__construct(); @@ -42,31 +43,40 @@ } 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 @@ ); 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 042f8bd..de655b1 100644 --- a/maintenance/benchmarks/bench_delete_truncate.php +++
[MediaWiki-commits] [Gerrit] mediawiki/core[master]: benchmarks: Add setup, bench naming, and custom count default
Krinkle has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/349122 ) Change subject: benchmarks: Add setup, bench naming, and custom count default .. 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 --- M maintenance/benchmarks/Benchmarker.php M maintenance/benchmarks/bench_delete_truncate.php 2 files changed, 48 insertions(+), 38 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/22/349122/1 diff --git a/maintenance/benchmarks/Benchmarker.php b/maintenance/benchmarks/Benchmarker.php index 70dc1f4..7d8280d 100644 --- a/maintenance/benchmarks/Benchmarker.php +++ b/maintenance/benchmarks/Benchmarker.php @@ -35,6 +35,7 @@ */ abstract class Benchmarker extends Maintenance { private $results; + protected $defaultCount = 100; public function __construct() { parent::__construct(); @@ -42,31 +43,40 @@ } 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 @@ ); 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 042f8bd..de655b1 100644 --- a/maintenance/benchmarks/bench_delete_truncate.php +++