MediaWiki master
benchmarkParse.php
Go to the documentation of this file.
1<?php
25require_once __DIR__ . '/../Maintenance.php';
26
33
42 private $templateTimestamp = null;
43
44 private $clearLinkCache = false;
45
49 private $linkCache;
50
52 private $idCache = [];
53
54 public function __construct() {
55 parent::__construct();
56 $this->addDescription( 'Benchmark parse operation' );
57 $this->addArg( 'title', 'The name of the page to parse' );
58 $this->addOption( 'warmup', 'Repeat the parse operation this number of times to warm the cache',
59 false, true );
60 $this->addOption( 'loops', 'Number of times to repeat parse operation post-warmup',
61 false, true );
62 $this->addOption( 'page-time',
63 'Use the version of the page which was current at the given time',
64 false, true );
65 $this->addOption( 'tpl-time',
66 'Use templates which were current at the given time (except that moves and ' .
67 'deletes are not handled properly)',
68 false, true );
69 $this->addOption( 'reset-linkcache', 'Reset the LinkCache after every parse.',
70 false, false );
71 }
72
73 public function execute() {
74 if ( $this->hasOption( 'tpl-time' ) ) {
75 $this->templateTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'tpl-time' ) ) );
76 $hookContainer = $this->getHookContainer();
77 $hookContainer->register( 'BeforeParserFetchTemplateRevisionRecord', [ $this, 'onFetchTemplate' ] );
78 }
79
80 $this->clearLinkCache = $this->hasOption( 'reset-linkcache' );
81 // Set as a member variable to avoid function calls when we're timing the parse
82 $this->linkCache = $this->getServiceContainer()->getLinkCache();
83
84 $title = Title::newFromText( $this->getArg( 0 ) );
85 if ( !$title ) {
86 $this->fatalError( "Invalid title" );
87 }
88
89 $revLookup = $this->getServiceContainer()->getRevisionLookup();
90 if ( $this->hasOption( 'page-time' ) ) {
91 $pageTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'page-time' ) ) );
92 $id = $this->getRevIdForTime( $title, $pageTimestamp );
93 if ( !$id ) {
94 $this->fatalError( "The page did not exist at that time" );
95 }
96
97 $revision = $revLookup->getRevisionById( (int)$id );
98 } else {
99 $revision = $revLookup->getRevisionByTitle( $title );
100 }
101
102 if ( !$revision ) {
103 $this->fatalError( "Unable to load revision, incorrect title?" );
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->getReplicaDB();
141
142 $id = $dbr->newSelectQueryBuilder()
143 ->select( 'rev_id' )
144 ->from( 'revision' )
145 ->join( 'page', null, 'rev_page=page_id' )
146 ->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey() ] )
147 ->andWhere( $dbr->expr( 'rev_timestamp', '<=', $timestamp ) )
148 ->orderBy( 'rev_timestamp', SelectQueryBuilder::SORT_DESC )
149 ->caller( __METHOD__ )->fetchField();
150
151 return $id;
152 }
153
159 private function runParser( RevisionRecord $revision ) {
160 $content = $revision->getContent( SlotRecord::MAIN );
161 $contentRenderer = $this->getServiceContainer()->getContentRenderer();
162 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable getId does not return null here
163 $contentRenderer->getParserOutput( $content, $revision->getPage(), $revision->getId() );
164 if ( $this->clearLinkCache ) {
165 $this->linkCache->clear();
166 }
167 }
168
179 private function onFetchTemplate(
180 ?LinkTarget $contextTitle,
181 LinkTarget $titleTarget,
182 bool &$skip,
183 ?RevisionRecord &$revRecord
184 ): bool {
185 $title = Title::newFromLinkTarget( $titleTarget );
186
187 $pdbk = $title->getPrefixedDBkey();
188 if ( !isset( $this->idCache[$pdbk] ) ) {
189 $proposedId = $this->getRevIdForTime( $title, $this->templateTimestamp );
190 $this->idCache[$pdbk] = $proposedId;
191 }
192 if ( $this->idCache[$pdbk] !== false ) {
193 $revLookup = $this->getServiceContainer()->getRevisionLookup();
194 $revRecord = $revLookup->getRevisionById( $this->idCache[$pdbk] );
195 }
196
197 return true;
198 }
199}
200
201$maintClass = BenchmarkParse::class;
202require_once RUN_MAINTENANCE_IF_MAIN;
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.
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.
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition LinkCache.php:53
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:78
getNamespace()
Get the namespace index, i.e.
Definition Title.php:1044
getDBkey()
Get the main part with underscores.
Definition Title.php:1035
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1849
Build SELECT queries with a fluent interface.
Represents the target of a wiki link.