Merge "Fix reset interwiki table between tests"
[lhc/web/wiklou.git] / maintenance / benchmarks / benchmarkTitleValue.php
1 <?php
2 /**
3 * Copyright (C) 2018 Kunal Mehta <legoktm@member.fsf.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21 use MediaWiki\MediaWikiServices;
22
23 require_once __DIR__ . '/Benchmarker.php';
24
25 /**
26 * Maintenance script that benchmarks TitleValue vs Title.
27 *
28 * @ingroup Benchmark
29 */
30 class BenchmarkTitleValue extends Benchmarker {
31
32 /**
33 * @var TitleFormatter
34 */
35 private $titleFormatter;
36 /**
37 * @var TitleParser
38 */
39 private $titleParser;
40
41 /**
42 * @var string
43 */
44 private $dbKey = 'FooBar';
45 /**
46 * @var TitleValue
47 */
48 private $titleValue;
49 /**
50 * @var Title
51 */
52 private $title;
53
54 /**
55 * @var string
56 */
57 private $toParse;
58
59 public function __construct() {
60 parent::__construct();
61 $this->addDescription( 'Benchmark TitleValue vs Title.' );
62 }
63
64 public function execute() {
65 $this->titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
66 $this->titleParser = MediaWikiServices::getInstance()->getTitleParser();
67 $this->titleValue = $this->constructTitleValue();
68 $this->title = $this->constructTitle();
69 $this->toParse = 'Category:FooBar';
70 $this->bench( [
71 [
72 'function' => [ $this, 'constructTitleValue' ],
73 ],
74 [
75 'function' => [ $this, 'constructTitle' ],
76 ],
77 [
78 'function' => [ $this, 'constructTitleSafe' ],
79 ],
80 [
81 'function' => [ $this, 'getPrefixedTextTitleValue' ],
82 ],
83 [
84 'function' => [ $this, 'getPrefixedTextTitle' ],
85 ],
86 [
87 'function' => [ $this, 'parseTitleValue' ],
88 'setup' => [ $this, 'randomize' ],
89 ],
90 [
91 'function' => [ $this, 'parseTitle' ],
92 'setup' => [ $this, 'randomize' ],
93 ],
94 ] );
95 }
96
97 /**
98 * Use a different dbKey each time to avoid influence of Title caches
99 */
100 protected function randomize() {
101 $this->dbKey = ucfirst( wfRandomString( 10 ) );
102 }
103
104 protected function constructTitleValue() {
105 return new TitleValue( NS_CATEGORY, $this->dbKey );
106 }
107
108 protected function constructTitle() {
109 return Title::makeTitle( NS_CATEGORY, $this->dbKey );
110 }
111
112 protected function constructTitleSafe() {
113 return Title::makeTitleSafe( NS_CATEGORY, $this->dbKey );
114 }
115
116 protected function getPrefixedTextTitleValue() {
117 // This is really showing TitleFormatter aka MediaWikiTitleCodec perf
118 return $this->titleFormatter->getPrefixedText( $this->titleValue );
119 }
120
121 protected function getPrefixedTextTitle() {
122 return $this->title->getPrefixedText();
123 }
124
125 protected function parseTitleValue() {
126 // This is really showing TitleParser aka MediaWikiTitleCodec perf
127 $this->titleParser->parseTitle( 'Category:' . $this->dbKey, NS_MAIN );
128 }
129
130 protected function parseTitle() {
131 Title::newFromText( 'Category:' . $this->dbKey );
132 }
133 }
134
135 $maintClass = BenchmarkTitleValue::class;
136 require_once RUN_MAINTENANCE_IF_MAIN;