3 require __DIR__
. '/../Maintenance.php';
5 class BenchmarkParse
extends Maintenance
{
6 /** @var string MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS) */
7 private $templateTimestamp;
9 private $idCache = array();
11 function __construct() {
12 parent
::__construct();
13 $this->addDescription( 'Benchmark parse operation' );
14 $this->addArg( 'title', 'The name of the page to parse' );
15 $this->addOption( 'cold', 'Don\'t repeat the parse operation to warm the cache' );
16 $this->addOption( 'page-time',
17 'Use the version of the page which was current at the given time',
19 $this->addOption( 'tpl-time',
20 'Use templates which were current at the given time (except that moves and deletes are not handled properly)',
27 if ( $this->hasOption( 'tpl-time' ) ) {
28 $this->templateTimestamp
= wfTimestamp( TS_MW
, strtotime( $this->getOption( 'tpl-time' ) ) );
29 Hooks
::register( 'BeforeParserFetchTemplateAndtitle', array( $this, 'onFetchTemplate' ) );
31 $title = Title
::newFromText( $this->getArg() );
33 $this->error( "Invalid title" );
36 if ( $this->hasOption( 'page-time' ) ) {
37 $pageTimestamp = wfTimestamp( TS_MW
, strtotime( $this->getOption( 'page-time' ) ) );
38 $id = $this->getRevIdForTime( $title, $pageTimestamp );
40 $this->error( "The page did not exist at that time" );
44 $revision = Revision
::newFromId( $id );
46 $revision = Revision
::newFromTitle( $title );
49 $this->error( "Unable to load revision, incorrect title?" );
52 if ( !$this->hasOption( 'cold' ) ) {
53 $this->runParser( $revision );
55 $startUsage = getrusage();
56 $startTime = microtime( true );
57 $this->runParser( $revision );
58 $endUsage = getrusage();
59 $endTime = microtime( true );
61 printf( "CPU time = %.3f s, wall clock time = %.3f s\n",
62 $endUsage['ru_utime.tv_sec'] +
$endUsage['ru_utime.tv_usec'] * 1e-6
63 - $startUsage['ru_utime.tv_sec'] - $startUsage['ru_utime.tv_usec'] * 1e-6,
64 $endTime - $startTime );
69 * @param string $timestamp
72 function getRevIdForTime( $title, $timestamp ) {
73 $dbr = wfGetDB( DB_SLAVE
);
74 $id = $dbr->selectField(
75 array( 'revision', 'page' ),
78 'page_namespace' => $title->getNamespace(),
79 'page_title' => $title->getDBkey(),
81 'rev_timestamp <= ' . $dbr->addQuotes( $timestamp )
84 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 1 ) );
90 * @param Revision $revision
92 function runParser( $revision ) {
93 $content = $revision->getContent();
94 $content->getParserOutput( $revision->getTitle(), $revision->getId() );
98 * @param Parser $parser
104 function onFetchTemplate( $parser, $title, &$skip, &$id ) {
105 $pdbk = $title->getPrefixedDBkey();
106 if ( !isset( $this->idCache
[$pdbk] ) ) {
107 $proposedId = $this->getRevIdForTime( $title, $this->templateTimestamp
);
108 $this->idCache
[$pdbk] = $proposedId;
110 if ( $this->idCache
[$pdbk] !== false ) {
111 $id = $this->idCache
[$pdbk];
118 $maintClass = 'BenchmarkParse';
119 require RUN_MAINTENANCE_IF_MAIN
;