From 74aca41874f60e4e83f484d7d06ca27801f0c10f Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 3 May 2017 20:17:54 -0700 Subject: [PATCH] benchmarks: Convert benchmarkHooks to use Benchmarker Also change benchmarkPurge to *not* use Benchmarker since it doesn't use bench() or any other Benchmarker method. Change-Id: I5d8ace161ecf1e05d69abf2f242e3684ffe48fa0 --- maintenance/benchmarks/Benchmarker.php | 5 ++ maintenance/benchmarks/benchmarkHooks.php | 60 +++++++++-------------- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/maintenance/benchmarks/Benchmarker.php b/maintenance/benchmarks/Benchmarker.php index 638e47eb31..0039d20632 100644 --- a/maintenance/benchmarks/Benchmarker.php +++ b/maintenance/benchmarks/Benchmarker.php @@ -45,6 +45,11 @@ abstract class Benchmarker extends Maintenance { $this->startBench(); $count = $this->getOption( 'count', $this->defaultCount ); foreach ( $benchs as $key => $bench ) { + // Shortcut for simple functions + if ( is_callable( $bench ) ) { + $bench = [ 'function' => $bench ]; + } + // Default to no arguments if ( !isset( $bench['args'] ) ) { $bench['args'] = []; diff --git a/maintenance/benchmarks/benchmarkHooks.php b/maintenance/benchmarks/benchmarkHooks.php index c59ce0d983..d49fa1d43b 100644 --- a/maintenance/benchmarks/benchmarkHooks.php +++ b/maintenance/benchmarks/benchmarkHooks.php @@ -29,50 +29,36 @@ require_once __DIR__ . '/Benchmarker.php'; * @ingroup Benchmark */ class BenchmarkHooks extends Benchmarker { + protected $defaultCount = 10; + public function __construct() { parent::__construct(); $this->addDescription( 'Benchmark MediaWiki Hooks.' ); } public function execute() { - global $wgHooks; - $wgHooks['Test'] = []; - - $time = $this->benchHooks(); - $this->output( 'Empty hook: ' . $time . "\n" ); - - $wgHooks['Test'][] = [ $this, 'test' ]; - $time = $this->benchHooks(); - $this->output( 'Loaded (one) hook: ' . $time . "\n" ); - - for ( $i = 0; $i < 9; $i++ ) { - $wgHooks['Test'][] = [ $this, 'test' ]; - } - $time = $this->benchHooks(); - $this->output( 'Loaded (ten) hook: ' . $time . "\n" ); - - for ( $i = 0; $i < 90; $i++ ) { - $wgHooks['Test'][] = [ $this, 'test' ]; + $cases = [ + 'Loaded 0 hooks' => 0, + 'Loaded 1 hook' => 1, + 'Loaded 10 hooks' => 10, + 'Loaded 100 hooks' => 100, + ]; + $benches = []; + foreach ( $cases as $label => $load ) { + $benches[$label] = [ + 'setup' => function () use ( $load ) { + global $wgHooks; + $wgHooks['Test'] = []; + for ( $i = 1; $i <= $load; $i++ ) { + $wgHooks['Test'][] = [ $this, 'test' ]; + } + }, + 'function' => function () { + Hooks::run( 'Test' ); + } + ]; } - $time = $this->benchHooks(); - $this->output( 'Loaded (one hundred) hook: ' . $time . "\n" ); - $this->output( "\n" ); - } - - /** - * @param int $trials - * @return string - */ - private function benchHooks( $trials = 10 ) { - $start = microtime( true ); - for ( $i = 0; $i < $trials; $i++ ) { - Hooks::run( 'Test' ); - } - $delta = microtime( true ) - $start; - $pertrial = $delta / $trials; - - return sprintf( "Took %6.3fms", - $pertrial * 1000 ); + $this->bench( $benches ); } /** -- 2.20.1