Merge "Make update.php file executable"
[mediawiki.git] / maintenance / benchmarks / benchmarkParse.php
blobb81f9fd4246fd69ef00165079e46a1057750e8a5
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( 'cold', 'Don\'t repeat the parse operation to warm the cache' );
45 $this->addOption( 'page-time',
46 'Use the version of the page which was current at the given time',
47 false, true );
48 $this->addOption( 'tpl-time',
49 'Use templates which were current at the given time (except that moves and ' .
50 'deletes are not handled properly)',
51 false, true );
54 function execute() {
55 if ( $this->hasOption( 'tpl-time' ) ) {
56 $this->templateTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'tpl-time' ) ) );
57 Hooks::register( 'BeforeParserFetchTemplateAndtitle', array( $this, 'onFetchTemplate' ) );
60 $title = Title::newFromText( $this->getArg() );
61 if ( !$title ) {
62 $this->error( "Invalid title" );
63 exit( 1 );
66 if ( $this->hasOption( 'page-time' ) ) {
67 $pageTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'page-time' ) ) );
68 $id = $this->getRevIdForTime( $title, $pageTimestamp );
69 if ( !$id ) {
70 $this->error( "The page did not exist at that time" );
71 exit( 1 );
74 $revision = Revision::newFromId( $id );
75 } else {
76 $revision = Revision::newFromTitle( $title );
79 if ( !$revision ) {
80 $this->error( "Unable to load revision, incorrect title?" );
81 exit( 1 );
84 if ( !$this->hasOption( 'cold' ) ) {
85 $this->runParser( $revision );
88 $startUsage = getrusage();
89 $startTime = microtime( true );
90 $this->runParser( $revision );
91 $endUsage = getrusage();
92 $endTime = microtime( true );
94 printf( "CPU time = %.3f s, wall clock time = %.3f s\n",
95 // CPU time
96 $endUsage['ru_utime.tv_sec'] + $endUsage['ru_utime.tv_usec'] * 1e-6
97 - $startUsage['ru_utime.tv_sec'] - $startUsage['ru_utime.tv_usec'] * 1e-6,
98 // Wall clock time
99 $endTime - $startTime );
103 * Fetch the ID of the revision of a Title that occurred
105 * @param Title $title
106 * @param string $timestamp
107 * @return bool|string Revision ID, or false if not found or error
109 function getRevIdForTime( Title $title, $timestamp ) {
110 $dbr = wfGetDB( DB_SLAVE );
112 $id = $dbr->selectField(
113 array( 'revision', 'page' ),
114 'rev_id',
115 array(
116 'page_namespace' => $title->getNamespace(),
117 'page_title' => $title->getDBkey(),
118 'rev_timestamp <= ' . $dbr->addQuotes( $timestamp )
120 __METHOD__,
121 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 1 ),
122 array( 'revision' => array( 'INNER JOIN', 'rev_page=page_id' ) )
125 return $id;
129 * Parse the text from a given Revision
131 * @param Revision $revision
133 function runParser( Revision $revision ) {
134 $content = $revision->getContent();
135 $content->getParserOutput( $revision->getTitle(), $revision->getId() );
139 * Hook into the parser's revision ID fetcher. Make sure that the parser only
140 * uses revisions around the specified timestamp.
142 * @param Parser $parser
143 * @param Title $title
144 * @param bool &$skip
145 * @param string|bool &$id
146 * @return bool
148 function onFetchTemplate( Parser $parser, Title $title, &$skip, &$id ) {
149 $pdbk = $title->getPrefixedDBkey();
150 if ( !isset( $this->idCache[$pdbk] ) ) {
151 $proposedId = $this->getRevIdForTime( $title, $this->templateTimestamp );
152 $this->idCache[$pdbk] = $proposedId;
154 if ( $this->idCache[$pdbk] !== false ) {
155 $id = $this->idCache[$pdbk];
158 return true;
162 $maintClass = 'BenchmarkParse';
163 require RUN_MAINTENANCE_IF_MAIN;