MediaWiki master
benchmarkParse.php
Go to the documentation of this file.
1<?php
25// @codeCoverageIgnoreStart
26require_once __DIR__ . '/../Maintenance.php';
27// @codeCoverageIgnoreEnd
28
36
45 private $templateTimestamp = null;
46
48 private $clearLinkCache = false;
49
53 private $linkCache;
54
56 private $idCache = [];
57
58 public function __construct() {
59 parent::__construct();
60 $this->addDescription( 'Benchmark parse operation' );
61 $this->addArg( 'title', 'The name of the page to parse' );
62 $this->addOption( 'warmup', 'Repeat the parse operation this number of times to warm the cache',
63 false, true );
64 $this->addOption( 'loops', 'Number of times to repeat parse operation post-warmup',
65 false, true );
66 $this->addOption( 'page-time',
67 'Use the version of the page which was current at the given time',
68 false, true );
69 $this->addOption( 'tpl-time',
70 'Use templates which were current at the given time (except that moves and ' .
71 'deletes are not handled properly)',
72 false, true );
73 $this->addOption( 'reset-linkcache', 'Reset the LinkCache after every parse.',
74 false, false );
75 }
76
77 public function execute() {
78 if ( $this->hasOption( 'tpl-time' ) ) {
79 $this->templateTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'tpl-time' ) ) );
80 $hookContainer = $this->getHookContainer();
81 $hookContainer->register( 'BeforeParserFetchTemplateRevisionRecord', [ $this, 'onFetchTemplate' ] );
82 }
83
84 $this->clearLinkCache = $this->hasOption( 'reset-linkcache' );
85 // Set as a member variable to avoid function calls when we're timing the parse
86 $this->linkCache = $this->getServiceContainer()->getLinkCache();
87
88 $title = Title::newFromText( $this->getArg( 0 ) );
89 if ( !$title ) {
90 $this->fatalError( "Invalid title" );
91 }
92
93 $revLookup = $this->getServiceContainer()->getRevisionLookup();
94 if ( $this->hasOption( 'page-time' ) ) {
95 $pageTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'page-time' ) ) );
96 $id = $this->getRevIdForTime( $title, $pageTimestamp );
97 if ( !$id ) {
98 $this->fatalError( "The page did not exist at that time" );
99 }
100
101 $revision = $revLookup->getRevisionById( (int)$id );
102 } else {
103 $revision = $revLookup->getRevisionByTitle( $title );
104 }
105
106 if ( !$revision ) {
107 $this->fatalError( "Unable to load revision, incorrect title?" );
108 }
109
110 $warmup = $this->getOption( 'warmup', 1 );
111 for ( $i = 0; $i < $warmup; $i++ ) {
112 $this->runParser( $revision );
113 }
114
115 $loops = $this->getOption( 'loops', 1 );
116 if ( $loops < 1 ) {
117 $this->fatalError( 'Invalid number of loops specified' );
118 }
119 $startUsage = getrusage();
120 $startTime = microtime( true );
121 for ( $i = 0; $i < $loops; $i++ ) {
122 $this->runParser( $revision );
123 }
124 $endUsage = getrusage();
125 $endTime = microtime( true );
126
127 printf( "CPU time = %.3f s, wall clock time = %.3f s\n",
128 // CPU time
129 ( $endUsage['ru_utime.tv_sec'] + $endUsage['ru_utime.tv_usec'] * 1e-6
130 - $startUsage['ru_utime.tv_sec'] - $startUsage['ru_utime.tv_usec'] * 1e-6 ) / $loops,
131 // Wall clock time
132 ( $endTime - $startTime ) / $loops
133 );
134 }
135
143 private function getRevIdForTime( Title $title, $timestamp ) {
144 $dbr = $this->getReplicaDB();
145
146 $id = $dbr->newSelectQueryBuilder()
147 ->select( 'rev_id' )
148 ->from( 'revision' )
149 ->join( 'page', null, 'rev_page=page_id' )
150 ->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey() ] )
151 ->andWhere( $dbr->expr( 'rev_timestamp', '<=', $timestamp ) )
152 ->orderBy( 'rev_timestamp', SelectQueryBuilder::SORT_DESC )
153 ->caller( __METHOD__ )->fetchField();
154
155 return $id;
156 }
157
161 private function runParser( RevisionRecord $revision ) {
162 $content = $revision->getContent( SlotRecord::MAIN );
163 $contentRenderer = $this->getServiceContainer()->getContentRenderer();
164 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable getId does not return null here
165 $contentRenderer->getParserOutput( $content, $revision->getPage(), $revision->getId() );
166 if ( $this->clearLinkCache ) {
167 $this->linkCache->clear();
168 }
169 }
170
181 private function onFetchTemplate(
182 ?LinkTarget $contextTitle,
183 LinkTarget $titleTarget,
184 bool &$skip,
185 ?RevisionRecord &$revRecord
186 ): bool {
187 $title = Title::newFromLinkTarget( $titleTarget );
188
189 $pdbk = $title->getPrefixedDBkey();
190 if ( !isset( $this->idCache[$pdbk] ) ) {
191 $proposedId = $this->getRevIdForTime( $title, $this->templateTimestamp );
192 $this->idCache[$pdbk] = $proposedId;
193 }
194 if ( $this->idCache[$pdbk] !== false ) {
195 $revLookup = $this->getServiceContainer()->getRevisionLookup();
196 $revRecord = $revLookup->getRevisionById( $this->idCache[$pdbk] );
197 }
198
199 return true;
200 }
201}
202
203// @codeCoverageIgnoreStart
204$maintClass = BenchmarkParse::class;
205require_once RUN_MAINTENANCE_IF_MAIN;
206// @codeCoverageIgnoreEnd
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:52
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.
getArg( $argId=0, $default=null)
Get an argument.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getHookContainer()
Get a HookContainer, for running extension hooks or for hook metadata.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Page revision base class.
getContent( $role, $audience=self::FOR_PUBLIC, ?Authority $performer=null)
Returns the Content of the given slot of this revision.
getPage()
Returns the page this revision belongs to.
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:78
getNamespace()
Get the namespace index, i.e.
Definition Title.php:1040
getDBkey()
Get the main part with underscores.
Definition Title.php:1031
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1843
Build SELECT queries with a fluent interface.
Represents the target of a wiki link.