mediawiki.searchSuggest: Show full article title as a tooltip for each suggestion
[lhc/web/wiklou.git] / maintenance / benchmarks / benchmarkParse.php
1 <?php
2 /**
3 * Benchmark script for parse operations
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Tim Starling <tstarling@wikimedia.org>
22 * @ingroup Benchmark
23 */
24
25 require __DIR__ . '/../Maintenance.php';
26
27 /**
28 * Maintenance script to benchmark how long it takes to parse a given title at an optionally
29 * specified timestamp
30 *
31 * @since 1.23
32 */
33 class BenchmarkParse extends Maintenance {
34 /** @var string MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS) */
35 private $templateTimestamp = null;
36
37 /** @var array Cache that maps a Title DB key to revision ID for the requested timestamp */
38 private $idCache = array();
39
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 );
52 }
53
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' ) );
58 }
59
60 $title = Title::newFromText( $this->getArg() );
61 if ( !$title ) {
62 $this->error( "Invalid title" );
63 exit( 1 );
64 }
65
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 );
72 }
73
74 $revision = Revision::newFromId( $id );
75 } else {
76 $revision = Revision::newFromTitle( $title );
77 }
78
79 if ( !$revision ) {
80 $this->error( "Unable to load revision, incorrect title?" );
81 exit( 1 );
82 }
83
84 if ( !$this->hasOption( 'cold' ) ) {
85 $this->runParser( $revision );
86 }
87
88 $startUsage = getrusage();
89 $startTime = microtime( true );
90 $this->runParser( $revision );
91 $endUsage = getrusage();
92 $endTime = microtime( true );
93
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 );
100 }
101
102 /**
103 * Fetch the ID of the revision of a Title that occurred
104 *
105 * @param Title $title
106 * @param string $timestamp
107 * @return bool|string Revision ID, or false if not found or error
108 */
109 function getRevIdForTime( Title $title, $timestamp ) {
110 $dbr = wfGetDB( DB_SLAVE );
111
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 )
119 ),
120 __METHOD__,
121 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 1 ),
122 array( 'revision' => array( 'INNER JOIN', 'rev_page=page_id' ) )
123 );
124
125 return $id;
126 }
127
128 /**
129 * Parse the text from a given Revision
130 *
131 * @param Revision $revision
132 */
133 function runParser( Revision $revision ) {
134 $content = $revision->getContent();
135 $content->getParserOutput( $revision->getTitle(), $revision->getId() );
136 }
137
138 /**
139 * Hook into the parser's revision ID fetcher. Make sure that the parser only
140 * uses revisions around the specified timestamp.
141 *
142 * @param Parser $parser
143 * @param Title $title
144 * @param bool &$skip
145 * @param string|bool &$id
146 * @return bool
147 */
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;
153 }
154 if ( $this->idCache[$pdbk] !== false ) {
155 $id = $this->idCache[$pdbk];
156 }
157
158 return true;
159 }
160 }
161
162 $maintClass = 'BenchmarkParse';
163 require RUN_MAINTENANCE_IF_MAIN;