508ea80aa4bd17359a90a28bd073a33de01e8c48
[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 Benchmark
20 */
21
22 require_once( __DIR__ . '/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
48 for( $i = 0; $i < 90; $i++ ) {
49 $wgHooks['Test'][] = array( $this, 'test' );
50 }
51 $time = $this->benchHooks();
52 $this->output( 'Loaded (one hundred) hook: ' . $time . "\n" );
53 $this->output( "\n" );
54 }
55
56 /**
57 * @param $trials int
58 * @return string
59 */
60 private function benchHooks( $trials = 10 ) {
61 $start = wfTime();
62 for ( $i = 0; $i < $trials; $i++ ) {
63 wfRunHooks( 'Test' );
64 }
65 $delta = wfTime() - $start;
66 $pertrial = $delta / $trials;
67 return sprintf( "Took %6.2fs",
68 $pertrial );
69 }
70
71 /**
72 * @return bool
73 */
74 public function test() {
75 return true;
76 }
77 }
78
79 $maintClass = 'BenchmarkHooks';
80 require_once( RUN_MAINTENANCE_IF_MAIN );