MediaWiki 1.41.2
benchmarkParse.php
Go to the documentation of this file.
1<?php
25require_once __DIR__ . '/../Maintenance.php';
26
32
41 private $templateTimestamp = null;
42
43 private $clearLinkCache = false;
44
48 private $linkCache;
49
51 private $idCache = [];
52
53 public function __construct() {
54 parent::__construct();
55 $this->addDescription( 'Benchmark parse operation' );
56 $this->addArg( 'title', 'The name of the page to parse' );
57 $this->addOption( 'warmup', 'Repeat the parse operation this number of times to warm the cache',
58 false, true );
59 $this->addOption( 'loops', 'Number of times to repeat parse operation post-warmup',
60 false, true );
61 $this->addOption( 'page-time',
62 'Use the version of the page which was current at the given time',
63 false, true );
64 $this->addOption( 'tpl-time',
65 'Use templates which were current at the given time (except that moves and ' .
66 'deletes are not handled properly)',
67 false, true );
68 $this->addOption( 'reset-linkcache', 'Reset the LinkCache after every parse.',
69 false, false );
70 }
71
72 public function execute() {
73 if ( $this->hasOption( 'tpl-time' ) ) {
74 $this->templateTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'tpl-time' ) ) );
75 $hookContainer = $this->getHookContainer();
76 $hookContainer->register( 'BeforeParserFetchTemplateRevisionRecord', [ $this, 'onFetchTemplate' ] );
77 }
78
79 $this->clearLinkCache = $this->hasOption( 'reset-linkcache' );
80 // Set as a member variable to avoid function calls when we're timing the parse
81 $this->linkCache = $this->getServiceContainer()->getLinkCache();
82
83 $title = Title::newFromText( $this->getArg( 0 ) );
84 if ( !$title ) {
85 $this->fatalError( "Invalid title" );
86 }
87
88 $revLookup = $this->getServiceContainer()->getRevisionLookup();
89 if ( $this->hasOption( 'page-time' ) ) {
90 $pageTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'page-time' ) ) );
91 $id = $this->getRevIdForTime( $title, $pageTimestamp );
92 if ( !$id ) {
93 $this->fatalError( "The page did not exist at that time" );
94 }
95
96 $revision = $revLookup->getRevisionById( (int)$id );
97 } else {
98 $revision = $revLookup->getRevisionByTitle( $title );
99 }
100
101 if ( !$revision ) {
102 $this->fatalError( "Unable to load revision, incorrect title?" );
103 }
104
105 $warmup = $this->getOption( 'warmup', 1 );
106 for ( $i = 0; $i < $warmup; $i++ ) {
107 $this->runParser( $revision );
108 }
109
110 $loops = $this->getOption( 'loops', 1 );
111 if ( $loops < 1 ) {
112 $this->fatalError( 'Invalid number of loops specified' );
113 }
114 $startUsage = getrusage();
115 $startTime = microtime( true );
116 for ( $i = 0; $i < $loops; $i++ ) {
117 $this->runParser( $revision );
118 }
119 $endUsage = getrusage();
120 $endTime = microtime( true );
121
122 printf( "CPU time = %.3f s, wall clock time = %.3f s\n",
123 // CPU time
124 ( $endUsage['ru_utime.tv_sec'] + $endUsage['ru_utime.tv_usec'] * 1e-6
125 - $startUsage['ru_utime.tv_sec'] - $startUsage['ru_utime.tv_usec'] * 1e-6 ) / $loops,
126 // Wall clock time
127 ( $endTime - $startTime ) / $loops
128 );
129 }
130
138 private function getRevIdForTime( Title $title, $timestamp ) {
139 $dbr = $this->getDB( DB_REPLICA );
140
141 $id = $dbr->newSelectQueryBuilder()
142 ->select( 'rev_id' )
143 ->from( 'revision' )
144 ->join( 'page', null, 'rev_page=page_id' )
145 ->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey() ] )
146 ->andWhere( 'rev_timestamp <= ' . $dbr->addQuotes( $timestamp ) )
147 ->orderBy( 'rev_timestamp', SelectQueryBuilder::SORT_DESC )
148 ->caller( __METHOD__ )->fetchField();
149
150 return $id;
151 }
152
158 private function runParser( RevisionRecord $revision ) {
159 $content = $revision->getContent( SlotRecord::MAIN );
160 $contentRenderer = $this->getServiceContainer()->getContentRenderer();
161 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable getId does not return null here
162 $contentRenderer->getParserOutput( $content, $revision->getPage(), $revision->getId() );
163 if ( $this->clearLinkCache ) {
164 $this->linkCache->clear();
165 }
166 }
167
178 private function onFetchTemplate(
179 ?LinkTarget $contextTitle,
180 LinkTarget $titleTarget,
181 bool &$skip,
182 ?RevisionRecord &$revRecord
183 ): bool {
184 $title = Title::newFromLinkTarget( $titleTarget );
185
186 $pdbk = $title->getPrefixedDBkey();
187 if ( !isset( $this->idCache[$pdbk] ) ) {
188 $proposedId = $this->getRevIdForTime( $title, $this->templateTimestamp );
189 $this->idCache[$pdbk] = $proposedId;
190 }
191 if ( $this->idCache[$pdbk] !== false ) {
192 $revLookup = $this->getServiceContainer()->getRevisionLookup();
193 $revRecord = $revLookup->getRevisionById( $this->idCache[$pdbk] );
194 }
195
196 return true;
197 }
198}
199
200$maintClass = BenchmarkParse::class;
201require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
$maintClass
Maintenance script to benchmark how long it takes to parse a given title at an optionally specified t...
__construct()
Default constructor.
execute()
Do the actual work.
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition LinkCache.php:45
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getHookContainer()
Get a HookContainer, for running extension hooks or for hook metadata.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Page revision base class.
getPage()
Returns the page this revision belongs to.
getContent( $role, $audience=self::FOR_PUBLIC, Authority $performer=null)
Returns the Content of the given slot of this revision.
getId( $wikiId=self::LOCAL)
Get revision ID.
Value object representing a content slot associated with a page revision.
Represents a title within MediaWiki.
Definition Title.php:76
getNamespace()
Get the namespace index, i.e.
Definition Title.php:1058
getDBkey()
Get the main part with underscores.
Definition Title.php:1049
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1873
Build SELECT queries with a fluent interface.
Represents the target of a wiki link.
const DB_REPLICA
Definition defines.php:26
$content
Definition router.php:76