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 require __DIR__
. '/../Maintenance.php';
28 * Maintenance script to benchmark how long it takes to parse a given title at an optionally
33 class BenchmarkParse
extends Maintenance
{
34 /** @var string MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS) */
35 private $templateTimestamp = null;
37 /** @var array Cache that maps a Title DB key to revision ID for the requested timestamp */
38 private $idCache = [];
40 function __construct() {
41 parent
::__construct();
42 $this->addDescription( 'Benchmark parse operation' );
43 $this->addArg( 'title', 'The name of the page to parse' );
44 $this->addOption( 'warmup', 'Repeat the parse operation this number of times to warm the cache',
46 $this->addOption( 'loops', 'Number of times to repeat parse operation post-warmup',
48 $this->addOption( 'page-time',
49 'Use the version of the page which was current at the given time',
51 $this->addOption( 'tpl-time',
52 'Use templates which were current at the given time (except that moves and ' .
53 'deletes are not handled properly)',
58 if ( $this->hasOption( 'tpl-time' ) ) {
59 $this->templateTimestamp
= wfTimestamp( TS_MW
, strtotime( $this->getOption( 'tpl-time' ) ) );
60 Hooks
::register( 'BeforeParserFetchTemplateAndtitle', [ $this, 'onFetchTemplate' ] );
63 $title = Title
::newFromText( $this->getArg() );
65 $this->error( "Invalid title" );
69 if ( $this->hasOption( 'page-time' ) ) {
70 $pageTimestamp = wfTimestamp( TS_MW
, strtotime( $this->getOption( 'page-time' ) ) );
71 $id = $this->getRevIdForTime( $title, $pageTimestamp );
73 $this->error( "The page did not exist at that time" );
77 $revision = Revision
::newFromId( $id );
79 $revision = Revision
::newFromTitle( $title );
83 $this->error( "Unable to load revision, incorrect title?" );
87 $warmup = $this->getOption( 'warmup', 1 );
88 for ( $i = 0; $i < $warmup; $i++
) {
89 $this->runParser( $revision );
92 $loops = $this->getOption( 'loops', 1 );
94 $this->error( 'Invalid number of loops specified', true );
96 $startUsage = getrusage();
97 $startTime = microtime( true );
98 for ( $i = 0; $i < $loops; $i++
) {
99 $this->runParser( $revision );
101 $endUsage = getrusage();
102 $endTime = microtime( true );
104 printf( "CPU time = %.3f s, wall clock time = %.3f s\n",
106 ( $endUsage['ru_utime.tv_sec'] +
$endUsage['ru_utime.tv_usec'] * 1e-6
107 - $startUsage['ru_utime.tv_sec'] - $startUsage['ru_utime.tv_usec'] * 1e-6 ) / $loops,
109 ( $endTime - $startTime ) / $loops
114 * Fetch the ID of the revision of a Title that occurred
116 * @param Title $title
117 * @param string $timestamp
118 * @return bool|string Revision ID, or false if not found or error
120 function getRevIdForTime( Title
$title, $timestamp ) {
121 $dbr = $this->getDB( DB_SLAVE
);
123 $id = $dbr->selectField(
124 [ 'revision', 'page' ],
127 'page_namespace' => $title->getNamespace(),
128 'page_title' => $title->getDBkey(),
129 'rev_timestamp <= ' . $dbr->addQuotes( $timestamp )
132 [ 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 1 ],
133 [ 'revision' => [ 'INNER JOIN', 'rev_page=page_id' ] ]
140 * Parse the text from a given Revision
142 * @param Revision $revision
144 function runParser( Revision
$revision ) {
145 $content = $revision->getContent();
146 $content->getParserOutput( $revision->getTitle(), $revision->getId() );
150 * Hook into the parser's revision ID fetcher. Make sure that the parser only
151 * uses revisions around the specified timestamp.
153 * @param Parser $parser
154 * @param Title $title
156 * @param string|bool &$id
159 function onFetchTemplate( Parser
$parser, Title
$title, &$skip, &$id ) {
160 $pdbk = $title->getPrefixedDBkey();
161 if ( !isset( $this->idCache
[$pdbk] ) ) {
162 $proposedId = $this->getRevIdForTime( $title, $this->templateTimestamp
);
163 $this->idCache
[$pdbk] = $proposedId;
165 if ( $this->idCache
[$pdbk] !== false ) {
166 $id = $this->idCache
[$pdbk];
173 $maintClass = 'BenchmarkParse';
174 require RUN_MAINTENANCE_IF_MAIN
;