X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=maintenance%2Fbenchmarks%2FbenchmarkHooks.php;h=d49fa1d43b02f1a4f601182ffae97e17e2539d3b;hp=c59ce0d983986956e7b484bc929d11f8d6292d4f;hb=89539f2aa1b158fdcc703ad053e2580cb97a6385;hpb=f43fa6f4f0e2cb60b8543daad661b48a3e0653a9 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 ); } /**