3 * Copyright (C) 2018 Kunal Mehta <legoktm@member.fsf.org>
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.
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.
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.
21 use MediaWiki\MediaWikiServices
;
23 require_once __DIR__
. '/Benchmarker.php';
26 * Maintenance script that benchmarks TitleValue vs Title.
30 class BenchmarkTitleValue
extends Benchmarker
{
35 private $titleFormatter;
44 private $dbKey = 'FooBar';
59 public function __construct() {
60 parent
::__construct();
61 $this->addDescription( 'Benchmark TitleValue vs Title.' );
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';
72 'function' => [ $this, 'constructTitleValue' ],
75 'function' => [ $this, 'constructTitle' ],
78 'function' => [ $this, 'constructTitleSafe' ],
81 'function' => [ $this, 'getPrefixedTextTitleValue' ],
84 'function' => [ $this, 'getPrefixedTextTitle' ],
86 'parseTitleValue cached' => [
87 'function' => [ $this, 'parseTitleValue' ],
88 'setup' => [ $this, 'randomize' ],
90 'parseTitle cached' => [
91 'function' => [ $this, 'parseTitle' ],
92 'setup' => [ $this, 'randomize' ],
94 'parseTitleValue no cache' => [
95 'function' => [ $this, 'parseTitleValue' ],
96 'setupEach' => [ $this, 'randomize' ],
98 'parseTitle no cache' => [
99 'function' => [ $this, 'parseTitle' ],
100 'setupEach' => [ $this, 'randomize' ],
106 * Use a different dbKey each time to avoid influence of Title caches
108 protected function randomize() {
109 $this->dbKey
= ucfirst( wfRandomString( 10 ) );
112 protected function constructTitleValue() {
113 return new TitleValue( NS_CATEGORY
, $this->dbKey
);
116 protected function constructTitle() {
117 return Title
::makeTitle( NS_CATEGORY
, $this->dbKey
);
120 protected function constructTitleSafe() {
121 return Title
::makeTitleSafe( NS_CATEGORY
, $this->dbKey
);
124 protected function getPrefixedTextTitleValue() {
125 // This is really showing TitleFormatter aka MediaWikiTitleCodec perf
126 return $this->titleFormatter
->getPrefixedText( $this->titleValue
);
129 protected function getPrefixedTextTitle() {
130 return $this->title
->getPrefixedText();
133 protected function parseTitleValue() {
134 // This is really showing TitleParser aka MediaWikiTitleCodec perf
135 $this->titleParser
->parseTitle( 'Category:' . $this->dbKey
, NS_MAIN
);
138 protected function parseTitle() {
139 Title
::newFromText( 'Category:' . $this->dbKey
);
143 $maintClass = BenchmarkTitleValue
::class;
144 require_once RUN_MAINTENANCE_IF_MAIN
;