| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
require_once __DIR__ . '/../Maintenance.php'; |
| 27 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class BenchmarkParse extends Maintenance { |
| 44 |
|
| 45 |
private $templateTimestamp = null; |
| 46 |
|
| 47 |
|
| 48 |
private $clearLinkCache = false; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private $linkCache; |
| 54 |
|
| 55 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 132 |
( $endTime - $startTime ) / $loops |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 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 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private function runParser( RevisionRecord $revision ) { |
| 164 |
$content = $revision->getContent( SlotRecord::MAIN ); |
| 165 |
$contentRenderer = $this->getServiceContainer()->getContentRenderer(); |
| 166 |
|
| 167 |
$contentRenderer->getParserOutput( $content, $revision->getPage(), $revision->getId() ); |
| 168 |
if ( $this->clearLinkCache ) { |
| 169 |
$this->linkCache->clear(); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 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 |
|
| 206 |
$maintClass = BenchmarkParse::class; |
| 207 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 208 |
|