Merge "Import: Handle uploads with sha1 starting with 0 properly"
[mediawiki.git] / maintenance / benchmarks / benchmarkParse.php
blobb850e638d040e03fab688e7c5718c342523eda43
1 <?php
2 /**
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
20 * @file
21 * @author Tim Starling <tstarling@wikimedia.org>
22 * @ingroup Benchmark
25 require __DIR__ . '/../Maintenance.php';
27 /**
28 * Maintenance script to benchmark how long it takes to parse a given title at an optionally
29 * specified timestamp
31 * @since 1.23
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 = array();
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',
45 false, true );
46 $this->addOption( 'loops', 'Number of times to repeat parse operation post-warmup',
47 false, true );
48 $this->addOption( 'page-time',
49 'Use the version of the page which was current at the given time',
50 false, true );
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)',
54 false, true );
57 function execute() {
58 if ( $this->hasOption( 'tpl-time' ) ) {
59 $this->templateTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'tpl-time' ) ) );
60 Hooks::register( 'BeforeParserFetchTemplateAndtitle', array( $this, 'onFetchTemplate' ) );
63 $title = Title::newFromText( $this->getArg() );
64 if ( !$title ) {
65 $this->error( "Invalid title" );
66 exit( 1 );
69 if ( $this->hasOption( 'page-time' ) ) {
70 $pageTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'page-time' ) ) );
71 $id = $this->getRevIdForTime( $title, $pageTimestamp );
72 if ( !$id ) {
73 $this->error( "The page did not exist at that time" );
74 exit( 1 );
77 $revision = Revision::newFromId( $id );
78 } else {
79 $revision = Revision::newFromTitle( $title );
82 if ( !$revision ) {
83 $this->error( "Unable to load revision, incorrect title?" );
84 exit( 1 );
87 $warmup = $this->getOption( 'warmup', 1 );
88 for ( $i = 0; $i < $warmup; $i++ ) {
89 $this->runParser( $revision );
92 $loops = $this->getOption( 'loops', 1 );
93 if ( $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",
105 // CPU time
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,
108 // Wall clock time
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 array( 'revision', 'page' ),
125 'rev_id',
126 array(
127 'page_namespace' => $title->getNamespace(),
128 'page_title' => $title->getDBkey(),
129 'rev_timestamp <= ' . $dbr->addQuotes( $timestamp )
131 __METHOD__,
132 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 1 ),
133 array( 'revision' => array( 'INNER JOIN', 'rev_page=page_id' ) )
136 return $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
155 * @param bool &$skip
156 * @param string|bool &$id
157 * @return bool
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];
169 return true;
173 $maintClass = 'BenchmarkParse';
174 require RUN_MAINTENANCE_IF_MAIN;