benchmarks: Convert benchmarkHooks to use Benchmarker
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 4 May 2017 03:17:54 +0000 (20:17 -0700)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 4 May 2017 03:19:32 +0000 (20:19 -0700)
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
maintenance/benchmarks/benchmarkHooks.php

index 638e47e..0039d20 100644 (file)
@@ -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'] = [];
index c59ce0d..d49fa1d 100644 (file)
@@ -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 );
        }
 
        /**