Refactor out duplicate code
[lhc/web/wiklou.git] / maintenance / benchmarks / benchmarkHooks.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Maintenance
20 */
21
22 require_once( dirname( __FILE__ ) . '/Benchmarker.php' );
23
24 class BenchmarkHooks extends Benchmarker {
25
26 public function __construct() {
27 parent::__construct();
28 $this->mDescription = "Benchmark MediaWiki Hooks.";
29 }
30
31 public function execute() {
32 global $wgHooks;
33 $wgHooks['Test'] = array();
34
35 $time = $this->benchHooks();
36 $this->output( 'Empty hook: ' . $time . "\n" );
37
38 $wgHooks['Test'][] = array( $this, 'test' );
39 $time = $this->benchHooks();
40 $this->output( 'Loaded (one) hook: ' . $time . "\n" );
41
42 for( $i = 0; $i < 9; $i++ ) {
43 $wgHooks['Test'][] = array( $this, 'test' );
44 }
45 $time = $this->benchHooks();
46 $this->output( 'Loaded (ten) hook: ' . $time . "\n" );
47 $this->output( "\n" );
48 }
49
50 private function benchHooks( $trials = 1 ) {
51 $start = wfTime();
52 for ( $i = 0; $i < $trials; $i++ ) {
53 wfRunHooks( 'Test' );
54 }
55 $delta = wfTime() - $start;
56 $pertrial = $delta / $trials;
57 return sprintf( "Took %6.2fms",
58 $pertrial );
59 }
60
61 public function test() {
62 return true;
63 }
64 }
65
66 $maintClass = "BenchmarkHooks";
67 require_once( RUN_MAINTENANCE_IF_MAIN );