MediaWiki  master
benchmarkTitleValue.php
Go to the documentation of this file.
1 <?php
22 
23 require_once __DIR__ . '/Benchmarker.php';
24 
31 
35  private $titleFormatter;
39  private $titleParser;
40 
44  private $dbKey = 'FooBar';
48  private $titleValue;
52  private $title;
53 
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  'parseTitleValue cached' => [
87  'function' => [ $this, 'parseTitleValue' ],
88  'setup' => [ $this, 'randomize' ],
89  ],
90  'parseTitle cached' => [
91  'function' => [ $this, 'parseTitle' ],
92  'setup' => [ $this, 'randomize' ],
93  ],
94  'parseTitleValue no cache' => [
95  'function' => [ $this, 'parseTitleValue' ],
96  'setupEach' => [ $this, 'randomize' ],
97  ],
98  'parseTitle no cache' => [
99  'function' => [ $this, 'parseTitle' ],
100  'setupEach' => [ $this, 'randomize' ],
101  ],
102  ] );
103  }
104 
108  protected function randomize() {
109  $this->dbKey = ucfirst( wfRandomString( 10 ) );
110  }
111 
112  protected function constructTitleValue() {
113  return new TitleValue( NS_CATEGORY, $this->dbKey );
114  }
115 
116  protected function constructTitle() {
117  return Title::makeTitle( NS_CATEGORY, $this->dbKey );
118  }
119 
120  protected function constructTitleSafe() {
121  return Title::makeTitleSafe( NS_CATEGORY, $this->dbKey );
122  }
123 
124  protected function getPrefixedTextTitleValue() {
125  // This is really showing TitleFormatter aka MediaWikiTitleCodec perf
126  return $this->titleFormatter->getPrefixedText( $this->titleValue );
127  }
128 
129  protected function getPrefixedTextTitle() {
130  return $this->title->getPrefixedText();
131  }
132 
133  protected function parseTitleValue() {
134  // This is really showing TitleParser aka MediaWikiTitleCodec perf
135  $this->titleParser->parseTitle( 'Category:' . $this->dbKey, NS_MAIN );
136  }
137 
138  protected function parseTitle() {
139  Title::newFromText( 'Category:' . $this->dbKey );
140  }
141 }
142 
143 $maintClass = BenchmarkTitleValue::class;
144 require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
const NS_MAIN
Definition: Defines.php:60
TitleFormatter $titleFormatter
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36
bench(array $benchs)
Definition: Benchmarker.php:50
Maintenance script that benchmarks TitleValue vs Title.
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
addDescription( $text)
Set the description text.
const NS_CATEGORY
Definition: Defines.php:74
randomize()
Use a different dbKey each time to avoid influence of Title caches.
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:613
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
Base class for benchmark scripts.
Definition: Benchmarker.php:40
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316