Debian Codegraph @

maintenance/benchmarks/benchmarkParse.php

raw symbols SCIP
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 <[email protected]>
22 * @ingroup Benchmark
23 */
24
25 // @codeCoverageIgnoreStart
26 require_once __DIR__ . '/../Maintenance.php';
27 // @codeCoverageIgnoreEnd
28
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;
36
37 /**
38 * Maintenance script to benchmark how long it takes to parse a given title at an optionally
39 * specified timestamp
40 *
41 * @since 1.23
42 */
43 class BenchmarkParse extends Maintenance {
44 /** @var string MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS) */
45 private $templateTimestamp = null;
46
47 /** @var bool */
48 private $clearLinkCache = false;
49
50 /**
51 * @var LinkCache
52 */
53 private $linkCache;
54
55 /** @var array Cache that maps a Title DB key to revision ID for the requested timestamp */
56 private $idCache = [];
57
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',
63 false, true );
64 $this->addOption( 'loops', 'Number of times to repeat parse operation post-warmup',
65 false, true );
66 $this->addOption( 'page-time',
67 'Use the version of the page which was current at the given time',
68 false, true );
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)',
72 false, true );
73 $this->addOption( 'reset-linkcache', 'Reset the LinkCache after every parse.',
74 false, false );
75 }
76
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' ] );
82 }
83
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();
87
88 $title = Title::newFromText( $this->getArg( 0 ) );
89 if ( !$title ) {
90 $this->fatalError( "Invalid title" );
91 }
92
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 );
97 if ( !$id ) {
98 $this->fatalError( "The page did not exist at that time" );
99 }
100
101 $revision = $revLookup->getRevisionById( (int)$id );
102 } else {
103 $revision = $revLookup->getRevisionByTitle( $title );
104 }
105
106 if ( !$revision ) {
107 $this->fatalError( "Unable to load revision, incorrect title?" );
108 }
109
110 $warmup = $this->getOption( 'warmup', 1 );
111 for ( $i = 0; $i < $warmup; $i++ ) {
112 $this->runParser( $revision );
113 }
114
115 $loops = $this->getOption( 'loops', 1 );
116 if ( $loops < 1 ) {
117 $this->fatalError( 'Invalid number of loops specified' );
118 }
119 $startUsage = getrusage();
120 $startTime = microtime( true );
121 for ( $i = 0; $i < $loops; $i++ ) {
122 $this->runParser( $revision );
123 }
124 $endUsage = getrusage();
125 $endTime = microtime( true );
126
127 printf( "CPU time = %.3f s, wall clock time = %.3f s\n",
128 // CPU time
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,
131 // Wall clock time
132 ( $endTime - $startTime ) / $loops
133 );
134 }
135
136 /**
137 * Fetch the ID of the revision of a Title that occurred
138 *
139 * @param Title $title
140 * @param string $timestamp
141 * @return bool|string Revision ID, or false if not found or error
142 */
143 private function getRevIdForTime( Title $title, $timestamp ) {
144 $dbr = $this->getReplicaDB();
145
146 $id = $dbr->newSelectQueryBuilder()
147 ->select( 'rev_id' )
148 ->from( 'revision' )
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();
154
155 return $id;
156 }
157
158 /**
159 * Parse the text from a given RevisionRecord
160 *
161 * @param RevisionRecord $revision
162 */
163 private function runParser( RevisionRecord $revision ) {
164 $content = $revision->getContent( SlotRecord::MAIN );
165 $contentRenderer = $this->getServiceContainer()->getContentRenderer();
166 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable getId does not return null here
167 $contentRenderer->getParserOutput( $content, $revision->getPage(), $revision->getId() );
168 if ( $this->clearLinkCache ) {
169 $this->linkCache->clear();
170 }
171 }
172
173 /**
174 * Hook into the parser's revision ID fetcher. Make sure that the parser only
175 * uses revisions around the specified timestamp.
176 *
177 * @param ?LinkTarget $contextTitle
178 * @param LinkTarget $titleTarget
179 * @param bool &$skip
180 * @param ?RevisionRecord &$revRecord
181 * @return bool
182 */
183 private function onFetchTemplate(
184 ?LinkTarget $contextTitle,
185 LinkTarget $titleTarget,
186 bool &$skip,
187 ?RevisionRecord &$revRecord
188 ): bool {
189 $title = Title::newFromLinkTarget( $titleTarget );
190
191 $pdbk = $title->getPrefixedDBkey();
192 if ( !isset( $this->idCache[$pdbk] ) ) {
193 $proposedId = $this->getRevIdForTime( $title, $this->templateTimestamp );
194 $this->idCache[$pdbk] = $proposedId;
195 }
196 if ( $this->idCache[$pdbk] !== false ) {
197 $revLookup = $this->getServiceContainer()->getRevisionLookup();
198 $revRecord = $revLookup->getRevisionById( $this->idCache[$pdbk] );
199 }
200
201 return true;
202 }
203 }
204
205 // @codeCoverageIgnoreStart
206 $maintClass = BenchmarkParse::class;
207 require_once RUN_MAINTENANCE_IF_MAIN;
208 // @codeCoverageIgnoreEnd