MediaWiki  REL1_23
benchmarkParse.php
Go to the documentation of this file.
00001 <?php
00025 require __DIR__ . '/../Maintenance.php';
00026 
00033 class BenchmarkParse extends Maintenance {
00035     private $templateTimestamp = null;
00036 
00038     private $idCache = array();
00039 
00040     function __construct() {
00041         parent::__construct();
00042         $this->addDescription( 'Benchmark parse operation' );
00043         $this->addArg( 'title', 'The name of the page to parse' );
00044         $this->addOption( 'cold', 'Don\'t repeat the parse operation to warm the cache' );
00045         $this->addOption( 'page-time',
00046             'Use the version of the page which was current at the given time',
00047             false, true );
00048         $this->addOption( 'tpl-time',
00049             'Use templates which were current at the given time (except that moves and ' .
00050                 'deletes are not handled properly)',
00051             false, true );
00052     }
00053 
00054     function execute() {
00055         if ( $this->hasOption( 'tpl-time' ) ) {
00056             $this->templateTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'tpl-time' ) ) );
00057             Hooks::register( 'BeforeParserFetchTemplateAndtitle', array( $this, 'onFetchTemplate' ) );
00058         }
00059 
00060         $title = Title::newFromText( $this->getArg() );
00061         if ( !$title ) {
00062             $this->error( "Invalid title" );
00063             exit( 1 );
00064         }
00065 
00066         if ( $this->hasOption( 'page-time' ) ) {
00067             $pageTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'page-time' ) ) );
00068             $id = $this->getRevIdForTime( $title, $pageTimestamp );
00069             if ( !$id ) {
00070                 $this->error( "The page did not exist at that time" );
00071                 exit( 1 );
00072             }
00073 
00074             $revision = Revision::newFromId( $id );
00075         } else {
00076             $revision = Revision::newFromTitle( $title );
00077         }
00078 
00079         if ( !$revision ) {
00080             $this->error( "Unable to load revision, incorrect title?" );
00081             exit( 1 );
00082         }
00083 
00084         if ( !$this->hasOption( 'cold' ) ) {
00085             $this->runParser( $revision );
00086         }
00087 
00088         $startUsage = getrusage();
00089         $startTime = microtime( true );
00090         $this->runParser( $revision );
00091         $endUsage = getrusage();
00092         $endTime = microtime( true );
00093 
00094         printf( "CPU time = %.3f s, wall clock time = %.3f s\n",
00095             // CPU time
00096             $endUsage['ru_utime.tv_sec'] + $endUsage['ru_utime.tv_usec'] * 1e-6
00097                 - $startUsage['ru_utime.tv_sec'] - $startUsage['ru_utime.tv_usec'] * 1e-6,
00098             // Wall clock time
00099             $endTime - $startTime );
00100     }
00101 
00109     function getRevIdForTime( Title $title, $timestamp ) {
00110         $dbr = wfGetDB( DB_SLAVE );
00111 
00112         $id = $dbr->selectField(
00113             array( 'revision', 'page' ),
00114             'rev_id',
00115             array(
00116                 'page_namespace' => $title->getNamespace(),
00117                 'page_title' => $title->getDBkey(),
00118                 'rev_timestamp <= ' . $dbr->addQuotes( $timestamp )
00119             ),
00120             __METHOD__,
00121             array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 1 ),
00122             array( 'revision' => array( 'INNER JOIN', 'rev_page=page_id' ) )
00123         );
00124 
00125         return $id;
00126     }
00127 
00133     function runParser( Revision $revision ) {
00134         $content = $revision->getContent();
00135         $content->getParserOutput( $revision->getTitle(), $revision->getId() );
00136     }
00137 
00148     function onFetchTemplate( Parser $parser, Title $title, &$skip, &$id ) {
00149         $pdbk = $title->getPrefixedDBkey();
00150         if ( !isset( $this->idCache[$pdbk] ) ) {
00151             $proposedId = $this->getRevIdForTime( $title, $this->templateTimestamp );
00152             $this->idCache[$pdbk] = $proposedId;
00153         }
00154         if ( $this->idCache[$pdbk] !== false ) {
00155             $id = $this->idCache[$pdbk];
00156         }
00157 
00158         return true;
00159     }
00160 }
00161 
00162 $maintClass = 'BenchmarkParse';
00163 require RUN_MAINTENANCE_IF_MAIN;