3 * Benchmark script for parse operations
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Tim Starling <tstarling@wikimedia.org>
25 // @codeCoverageIgnoreStart
26 require_once __DIR__
. '/../Maintenance.php';
27 // @codeCoverageIgnoreEnd
29 use MediaWiki\Cache\LinkCache
;
30 use MediaWiki\Linker\LinkTarget
;
31 use MediaWiki\Maintenance\Maintenance
;
32 use MediaWiki\Revision\RevisionRecord
;
33 use MediaWiki\Revision\SlotRecord
;
34 use MediaWiki\Title\Title
;
35 use Wikimedia\Rdbms\SelectQueryBuilder
;
38 * Maintenance script to benchmark how long it takes to parse a given title at an optionally
43 class BenchmarkParse
extends Maintenance
{
44 /** @var string MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS) */
45 private $templateTimestamp = null;
48 private $clearLinkCache = false;
55 /** @var array Cache that maps a Title DB key to revision ID for the requested timestamp */
56 private $idCache = [];
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',
64 $this->addOption( 'loops', 'Number of times to repeat parse operation post-warmup',
66 $this->addOption( 'page-time',
67 'Use the version of the page which was current at the given time',
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)',
73 $this->addOption( 'reset-linkcache', 'Reset the LinkCache after every parse.',
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' ] );
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();
88 $title = Title
::newFromText( $this->getArg( 0 ) );
90 $this->fatalError( "Invalid title" );
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 );
98 $this->fatalError( "The page did not exist at that time" );
101 $revision = $revLookup->getRevisionById( (int)$id );
103 $revision = $revLookup->getRevisionByTitle( $title );
107 $this->fatalError( "Unable to load revision, incorrect title?" );
110 $warmup = $this->getOption( 'warmup', 1 );
111 for ( $i = 0; $i < $warmup; $i++
) {
112 $this->runParser( $revision );
115 $loops = $this->getOption( 'loops', 1 );
117 $this->fatalError( 'Invalid number of loops specified' );
119 $startUsage = getrusage();
120 $startTime = microtime( true );
121 for ( $i = 0; $i < $loops; $i++
) {
122 $this->runParser( $revision );
124 $endUsage = getrusage();
125 $endTime = microtime( true );
127 printf( "CPU time = %.3f s, wall clock time = %.3f s\n",
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,
132 ( $endTime - $startTime ) / $loops
137 * Fetch the ID of the revision of a Title that occurred
139 * @param Title $title
140 * @param string $timestamp
141 * @return bool|string Revision ID, or false if not found or error
143 private function getRevIdForTime( Title
$title, $timestamp ) {
144 $dbr = $this->getReplicaDB();
146 $id = $dbr->newSelectQueryBuilder()
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();
159 * Parse the text from a given RevisionRecord
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();
172 * Hook into the parser's revision ID fetcher. Make sure that the parser only
173 * uses revisions around the specified timestamp.
175 * @param ?LinkTarget $contextTitle
176 * @param LinkTarget $titleTarget
178 * @param ?RevisionRecord &$revRecord
181 private function onFetchTemplate(
182 ?LinkTarget
$contextTitle,
183 LinkTarget
$titleTarget,
185 ?RevisionRecord
&$revRecord
187 $title = Title
::newFromLinkTarget( $titleTarget );
189 $pdbk = $title->getPrefixedDBkey();
190 if ( !isset( $this->idCache
[$pdbk] ) ) {
191 $proposedId = $this->getRevIdForTime( $title, $this->templateTimestamp
);
192 $this->idCache
[$pdbk] = $proposedId;
194 if ( $this->idCache
[$pdbk] !== false ) {
195 $revLookup = $this->getServiceContainer()->getRevisionLookup();
196 $revRecord = $revLookup->getRevisionById( $this->idCache
[$pdbk] );
203 // @codeCoverageIgnoreStart
204 $maintClass = BenchmarkParse
::class;
205 require_once RUN_MAINTENANCE_IF_MAIN
;
206 // @codeCoverageIgnoreEnd