MediaWiki  master
benchmarkParse.php
Go to the documentation of this file.
1 <?php
25 require_once __DIR__ . '/../Maintenance.php';
26 
31 
38 class BenchmarkParse extends Maintenance {
40  private $templateTimestamp = null;
41 
42  private $clearLinkCache = false;
43 
47  private $linkCache;
48 
50  private $idCache = [];
51 
52  public function __construct() {
53  parent::__construct();
54  $this->addDescription( 'Benchmark parse operation' );
55  $this->addArg( 'title', 'The name of the page to parse' );
56  $this->addOption( 'warmup', 'Repeat the parse operation this number of times to warm the cache',
57  false, true );
58  $this->addOption( 'loops', 'Number of times to repeat parse operation post-warmup',
59  false, true );
60  $this->addOption( 'page-time',
61  'Use the version of the page which was current at the given time',
62  false, true );
63  $this->addOption( 'tpl-time',
64  'Use templates which were current at the given time (except that moves and ' .
65  'deletes are not handled properly)',
66  false, true );
67  $this->addOption( 'reset-linkcache', 'Reset the LinkCache after every parse.',
68  false, false );
69  }
70 
71  public function execute() {
72  if ( $this->hasOption( 'tpl-time' ) ) {
73  $this->templateTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'tpl-time' ) ) );
74  Hooks::register( 'BeforeParserFetchTemplateRevisionRecord', [ $this, 'onFetchTemplate' ] );
75  }
76 
77  $this->clearLinkCache = $this->hasOption( 'reset-linkcache' );
78  // Set as a member variable to avoid function calls when we're timing the parse
79  $this->linkCache = MediaWikiServices::getInstance()->getLinkCache();
80 
81  $title = Title::newFromText( $this->getArg( 0 ) );
82  if ( !$title ) {
83  $this->error( "Invalid title" );
84  exit( 1 );
85  }
86 
87  $revLookup = MediaWikiServices::getInstance()->getRevisionLookup();
88  if ( $this->hasOption( 'page-time' ) ) {
89  $pageTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'page-time' ) ) );
90  $id = $this->getRevIdForTime( $title, $pageTimestamp );
91  if ( !$id ) {
92  $this->error( "The page did not exist at that time" );
93  exit( 1 );
94  }
95 
96  $revision = $revLookup->getRevisionById( $id );
97  } else {
98  $revision = $revLookup->getRevisionByTitle( $title );
99  }
100 
101  if ( !$revision ) {
102  $this->error( "Unable to load revision, incorrect title?" );
103  exit( 1 );
104  }
105 
106  $warmup = $this->getOption( 'warmup', 1 );
107  for ( $i = 0; $i < $warmup; $i++ ) {
108  $this->runParser( $revision );
109  }
110 
111  $loops = $this->getOption( 'loops', 1 );
112  if ( $loops < 1 ) {
113  $this->fatalError( 'Invalid number of loops specified' );
114  }
115  $startUsage = getrusage();
116  $startTime = microtime( true );
117  for ( $i = 0; $i < $loops; $i++ ) {
118  $this->runParser( $revision );
119  }
120  $endUsage = getrusage();
121  $endTime = microtime( true );
122 
123  printf( "CPU time = %.3f s, wall clock time = %.3f s\n",
124  // CPU time
125  ( $endUsage['ru_utime.tv_sec'] + $endUsage['ru_utime.tv_usec'] * 1e-6
126  - $startUsage['ru_utime.tv_sec'] - $startUsage['ru_utime.tv_usec'] * 1e-6 ) / $loops,
127  // Wall clock time
128  ( $endTime - $startTime ) / $loops
129  );
130  }
131 
139  private function getRevIdForTime( Title $title, $timestamp ) {
140  $dbr = $this->getDB( DB_REPLICA );
141 
142  $id = $dbr->selectField(
143  [ 'revision', 'page' ],
144  'rev_id',
145  [
146  'page_namespace' => $title->getNamespace(),
147  'page_title' => $title->getDBkey(),
148  'rev_timestamp <= ' . $dbr->addQuotes( $timestamp )
149  ],
150  __METHOD__,
151  [ 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 1 ],
152  [ 'revision' => [ 'JOIN', 'rev_page=page_id' ] ]
153  );
154 
155  return $id;
156  }
157 
163  private function runParser( RevisionRecord $revision ) {
164  $content = $revision->getContent( SlotRecord::MAIN );
166 
167  $content->getParserOutput( $title, $revision->getId() );
168  if ( $this->clearLinkCache ) {
169  $this->linkCache->clear();
170  }
171  }
172 
183  private function onFetchTemplate(
184  ?LinkTarget $contextTitle,
185  LinkTarget $titleTarget,
186  bool &$skip,
187  ?RevisionRecord &$revRecord
188  ): bool {
189  $title = Title::castFromLinkTarget( $titleTarget );
190 
191  $pdbk = $title->getPrefixedDBkey();
192  if ( !isset( $this->idCache[$pdbk] ) ) {
193  $proposedId = $this->getRevIdForTime( $title, $this->templateTimestamp );
194  $this->idCache[$pdbk] = $proposedId;
195  }
196  if ( $this->idCache[$pdbk] !== false ) {
197  $revLookup = MediaWikiServices::getInstance()->getRevisionLookup();
198  $revRecord = $revLookup->getRevisionById( $this->idCache[$pdbk] );
199  }
200 
201  return true;
202  }
203 }
204 
205 $maintClass = BenchmarkParse::class;
206 require_once RUN_MAINTENANCE_IF_MAIN;
LinkCache
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:35
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:363
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:172
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:486
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1832
BenchmarkParse\onFetchTemplate
onFetchTemplate(?LinkTarget $contextTitle, LinkTarget $titleTarget, bool &$skip, ?RevisionRecord &$revRecord)
Hook into the parser's revision ID fetcher.
Definition: benchmarkParse.php:183
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
$dbr
$dbr
Definition: testCompression.php:54
BenchmarkParse
Maintenance script to benchmark how long it takes to parse a given title at an optionally specified t...
Definition: benchmarkParse.php:38
$maintClass
$maintClass
Definition: benchmarkParse.php:205
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
BenchmarkParse\$templateTimestamp
string $templateTimestamp
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: benchmarkParse.php:40
$title
$title
Definition: testCompression.php:38
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
BenchmarkParse\$linkCache
LinkCache $linkCache
Definition: benchmarkParse.php:47
$content
$content
Definition: router.php:76
Hooks\register
static register( $name, $callback)
Attach an event handler to a given hook in both legacy and non-legacy hook systems.
Definition: Hooks.php:48
Revision\RevisionRecord\getId
getId( $wikiId=self::LOCAL)
Get revision ID.
Definition: RevisionRecord.php:269
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1388
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:291
BenchmarkParse\execute
execute()
Do the actual work.
Definition: benchmarkParse.php:71
Revision\RevisionRecord\getPageAsLinkTarget
getPageAsLinkTarget()
Returns the title of the page this revision is associated with as a LinkTarget object.
Definition: RevisionRecord.php:345
Title
Represents a title within MediaWiki.
Definition: Title.php:46
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:298
BenchmarkParse\$clearLinkCache
$clearLinkCache
Definition: benchmarkParse.php:42
BenchmarkParse\__construct
__construct()
Default constructor.
Definition: benchmarkParse.php:52
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:462
Title\castFromLinkTarget
static castFromLinkTarget( $linkTarget)
Same as newFromLinkTarget, but if passed null, returns null.
Definition: Title.php:315
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:351
BenchmarkParse\runParser
runParser(RevisionRecord $revision)
Parse the text from a given Revision.
Definition: benchmarkParse.php:163
Revision\RevisionRecord\getContent
getContent( $role, $audience=self::FOR_PUBLIC, Authority $performer=null)
Returns the Content of the given slot of this revision.
Definition: RevisionRecord.php:156
BenchmarkParse\$idCache
array $idCache
Cache that maps a Title DB key to revision ID for the requested timestamp.
Definition: benchmarkParse.php:50
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
BenchmarkParse\getRevIdForTime
getRevIdForTime(Title $title, $timestamp)
Fetch the ID of the revision of a Title that occurred.
Definition: benchmarkParse.php:139