Debian Codegraph @

maintenance/benchmarks/benchmarkTitleValue.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright (C) 2018 Kunal Mehta <[email protected]>
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\Title\Title;
22 use MediaWiki\Title\TitleFormatter;
23 use MediaWiki\Title\TitleParser;
24 use MediaWiki\Title\TitleValue;
25
26 // @codeCoverageIgnoreStart
27 require_once __DIR__ . '/../includes/Benchmarker.php';
28 // @codeCoverageIgnoreEnd
29
30 /**
31 * Maintenance script that benchmarks TitleValue vs Title.
32 *
33 * @ingroup Benchmark
34 */
35 class BenchmarkTitleValue extends Benchmarker {
36
37 /**
38 * @var TitleFormatter
39 */
40 private $titleFormatter;
41 /**
42 * @var TitleParser
43 */
44 private $titleParser;
45
46 /**
47 * @var string
48 */
49 private $dbKey = 'FooBar';
50 /**
51 * @var TitleValue
52 */
53 private $titleValue;
54 /**
55 * @var Title
56 */
57 private $title;
58
59 /**
60 * @var string
61 */
62 private $toParse;
63
64 public function __construct() {
65 parent::__construct();
66 $this->addDescription( 'Benchmark TitleValue vs Title.' );
67 }
68
69 public function execute() {
70 $this->titleFormatter = $this->getServiceContainer()->getTitleFormatter();
71 $this->titleParser = $this->getServiceContainer()->getTitleParser();
72 $this->titleValue = $this->constructTitleValue();
73 $this->title = $this->constructTitle();
74 $this->toParse = 'Category:FooBar';
75 $this->bench( [
76 [
77 'function' => [ $this, 'constructTitleValue' ],
78 ],
79 [
80 'function' => [ $this, 'constructTitle' ],
81 ],
82 [
83 'function' => [ $this, 'constructTitleSafe' ],
84 ],
85 [
86 'function' => [ $this, 'getPrefixedTextTitleValue' ],
87 ],
88 [
89 'function' => [ $this, 'getPrefixedTextTitle' ],
90 ],
91 'parseTitleValue cached' => [
92 'function' => [ $this, 'parseTitleValue' ],
93 'setup' => [ $this, 'randomize' ],
94 ],
95 'parseTitle cached' => [
96 'function' => [ $this, 'parseTitle' ],
97 'setup' => [ $this, 'randomize' ],
98 ],
99 'parseTitleValue no cache' => [
100 'function' => [ $this, 'parseTitleValue' ],
101 'setupEach' => [ $this, 'randomize' ],
102 ],
103 'parseTitle no cache' => [
104 'function' => [ $this, 'parseTitle' ],
105 'setupEach' => [ $this, 'randomize' ],
106 ],
107 ] );
108 }
109
110 /**
111 * Use a different dbKey each time to avoid influence of Title caches
112 */
113 protected function randomize() {
114 $this->dbKey = ucfirst( wfRandomString( 10 ) );
115 }
116
117 protected function constructTitleValue() {
118 return new TitleValue( NS_CATEGORY, $this->dbKey );
119 }
120
121 protected function constructTitle() {
122 return Title::makeTitle( NS_CATEGORY, $this->dbKey );
123 }
124
125 protected function constructTitleSafe() {
126 return Title::makeTitleSafe( NS_CATEGORY, $this->dbKey );
127 }
128
129 protected function getPrefixedTextTitleValue() {
130 // This is really showing TitleFormatter aka MediaWikiTitleCodec perf
131 return $this->titleFormatter->getPrefixedText( $this->titleValue );
132 }
133
134 protected function getPrefixedTextTitle() {
135 return $this->title->getPrefixedText();
136 }
137
138 protected function parseTitleValue() {
139 // This is really showing TitleParser aka MediaWikiTitleCodec perf
140 $this->titleParser->parseTitle( 'Category:' . $this->dbKey, NS_MAIN );
141 }
142
143 protected function parseTitle() {
144 Title::newFromText( 'Category:' . $this->dbKey );
145 }
146 }
147
148 // @codeCoverageIgnoreStart
149 $maintClass = BenchmarkTitleValue::class;
150 require_once RUN_MAINTENANCE_IF_MAIN;
151 // @codeCoverageIgnoreEnd