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