MediaWiki master
benchmarkParse.php
Go to the documentation of this file.
1<?php
11// @codeCoverageIgnoreStart
12require_once __DIR__ . '/../Maintenance.php';
13// @codeCoverageIgnoreEnd
14
22use Wikimedia\Timestamp\TimestampFormat as TS;
23
32 private $templateTimestamp = null;
33
35 private $clearLinkCache = false;
36
40 private $linkCache;
41
43 private $idCache = [];
44
45 public function __construct() {
46 parent::__construct();
47 $this->addDescription( 'Benchmark parse operation' );
48 $this->addArg( 'title', 'The name of the page to parse' );
49 $this->addOption( 'warmup', 'Repeat the parse operation this number of times to warm the cache',
50 false, true );
51 $this->addOption( 'loops', 'Number of times to repeat parse operation post-warmup',
52 false, true );
53 $this->addOption( 'page-time',
54 'Use the version of the page which was current at the given time',
55 false, true );
56 $this->addOption( 'tpl-time',
57 'Use templates which were current at the given time (except that moves and ' .
58 'deletes are not handled properly)',
59 false, true );
60 $this->addOption( 'reset-linkcache', 'Reset the LinkCache after every parse.',
61 false, false );
62 }
63
64 public function execute() {
65 if ( $this->hasOption( 'tpl-time' ) ) {
66 $this->templateTimestamp = wfTimestamp( TS::MW, strtotime( $this->getOption( 'tpl-time' ) ) );
67 $hookContainer = $this->getHookContainer();
68 $hookContainer->register( 'BeforeParserFetchTemplateRevisionRecord', [ $this, 'onFetchTemplate' ] );
69 }
70
71 $this->clearLinkCache = $this->hasOption( 'reset-linkcache' );
72 // Set as a member variable to avoid function calls when we're timing the parse
73 $this->linkCache = $this->getServiceContainer()->getLinkCache();
74
75 $title = Title::newFromText( $this->getArg( 0 ) );
76 if ( !$title ) {
77 $this->fatalError( "Invalid title" );
78 }
79
80 $revLookup = $this->getServiceContainer()->getRevisionLookup();
81 if ( $this->hasOption( 'page-time' ) ) {
82 $pageTimestamp = wfTimestamp( TS::MW, strtotime( $this->getOption( 'page-time' ) ) );
83 $id = $this->getRevIdForTime( $title, $pageTimestamp );
84 if ( !$id ) {
85 $this->fatalError( "The page did not exist at that time" );
86 }
87
88 $revision = $revLookup->getRevisionById( (int)$id );
89 } else {
90 $revision = $revLookup->getRevisionByTitle( $title );
91 }
92
93 if ( !$revision ) {
94 $this->fatalError( "Unable to load revision, incorrect title?" );
95 }
96
97 $warmup = $this->getOption( 'warmup', 1 );
98 for ( $i = 0; $i < $warmup; $i++ ) {
99 $this->runParser( $revision );
100 }
101
102 $loops = $this->getOption( 'loops', 1 );
103 if ( $loops < 1 ) {
104 $this->fatalError( 'Invalid number of loops specified' );
105 }
106 $startUsage = getrusage();
107 $startTime = microtime( true );
108 for ( $i = 0; $i < $loops; $i++ ) {
109 $this->runParser( $revision );
110 }
111 $endUsage = getrusage();
112 $endTime = microtime( true );
113
114 printf( "CPU time = %.3f s, wall clock time = %.3f s\n",
115 // CPU time
116 ( $endUsage['ru_utime.tv_sec'] + $endUsage['ru_utime.tv_usec'] * 1e-6
117 - $startUsage['ru_utime.tv_sec'] - $startUsage['ru_utime.tv_usec'] * 1e-6 ) / $loops,
118 // Wall clock time
119 ( $endTime - $startTime ) / $loops
120 );
121 }
122
130 private function getRevIdForTime( Title $title, $timestamp ) {
131 $dbr = $this->getReplicaDB();
132
133 $id = $dbr->newSelectQueryBuilder()
134 ->select( 'rev_id' )
135 ->from( 'revision' )
136 ->join( 'page', null, 'rev_page=page_id' )
137 ->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey() ] )
138 ->andWhere( $dbr->expr( 'rev_timestamp', '<=', $timestamp ) )
139 ->orderBy( 'rev_timestamp', SelectQueryBuilder::SORT_DESC )
140 ->caller( __METHOD__ )->fetchField();
141
142 return $id;
143 }
144
148 private function runParser( RevisionRecord $revision ) {
149 $content = $revision->getContent( SlotRecord::MAIN );
150 $contentRenderer = $this->getServiceContainer()->getContentRenderer();
151 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable - $content is not null here
152 $contentRenderer->getParserOutput( $content, $revision->getPage(), $revision );
153 if ( $this->clearLinkCache ) {
154 $this->linkCache->clear();
155 }
156 }
157
169 private function onFetchTemplate(
170 ?LinkTarget $contextTitle,
171 LinkTarget $titleTarget,
172 bool &$skip,
173 ?RevisionRecord &$revRecord
174 ): bool {
175 $title = Title::newFromLinkTarget( $titleTarget );
176
177 $pdbk = $title->getPrefixedDBkey();
178 if ( !isset( $this->idCache[$pdbk] ) ) {
179 $proposedId = $this->getRevIdForTime( $title, $this->templateTimestamp );
180 $this->idCache[$pdbk] = $proposedId;
181 }
182 if ( $this->idCache[$pdbk] !== false ) {
183 $revLookup = $this->getServiceContainer()->getRevisionLookup();
184 $revRecord = $revLookup->getRevisionById( $this->idCache[$pdbk] );
185 }
186
187 return true;
188 }
189}
190
191// @codeCoverageIgnoreStart
192$maintClass = BenchmarkParse::class;
193require_once RUN_MAINTENANCE_IF_MAIN;
194// @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.
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.
getReplicaDB(string|false $virtualDomain=false)
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Page existence and metadata cache.
Definition LinkCache.php:54
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.
Value object representing a content slot associated with a page revision.
Represents a title within MediaWiki.
Definition Title.php:69
getNamespace()
Get the namespace index, i.e.
Definition Title.php:1037
getDBkey()
Get the main part with underscores.
Definition Title.php:1028
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1845
Build SELECT queries with a fluent interface.
Represents the target of a wiki link.