MediaWiki  1.23.0
benchmarkParse.php
Go to the documentation of this file.
1 <?php
25 require __DIR__ . '/../Maintenance.php';
26 
33 class BenchmarkParse extends Maintenance {
35  private $templateTimestamp = null;
36 
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 
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 
133  function runParser( Revision $revision ) {
134  $content = $revision->getContent();
135  $content->getParserOutput( $revision->getTitle(), $revision->getId() );
136  }
137 
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';
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
Revision\newFromId
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:88
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:227
$timestamp
if( $limit) $timestamp
Definition: importImages.php:104
Revision\getContent
getContent( $audience=self::FOR_PUBLIC, User $user=null)
Fetch revision content if it's available to the specified audience.
Definition: Revision.php:999
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
e
in this case you re responsible for computing and outputting the entire conflict i e
Definition: hooks.txt:1038
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
Revision\getId
getId()
Get revision ID.
Definition: Revision.php:699
$dbr
$dbr
Definition: testCompression.php:48
Revision
Definition: Revision.php:26
BenchmarkParse
Maintenance script to benchmark how long it takes to parse a given title at an optionally specified t...
Definition: benchmarkParse.php:33
$maintClass
$maintClass
Definition: benchmarkParse.php:160
$parser
do that in ParserLimitReportFormat instead $parser
Definition: hooks.txt:1956
BenchmarkParse\$templateTimestamp
string $templateTimestamp
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS) *.
Definition: benchmarkParse.php:34
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
Revision\getTitle
getTitle()
Returns the title of the page associated with this entry or null.
Definition: Revision.php:756
BenchmarkParse\onFetchTemplate
onFetchTemplate(Parser $parser, Title $title, &$skip, &$id)
Hook into the parser's revision ID fetcher.
Definition: benchmarkParse.php:146
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2431
Revision\newFromTitle
static newFromTitle( $title, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given title.
Definition: Revision.php:106
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
BenchmarkParse\runParser
runParser(Revision $revision)
Parse the text from a given Revision.
Definition: benchmarkParse.php:131
Hooks\register
static register( $name, $callback)
Attach an event handler to a given hook.
Definition: Hooks.php:55
BenchmarkParse\execute
execute()
Do the actual work.
Definition: benchmarkParse.php:52
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Title
Represents a title within MediaWiki.
Definition: Title.php:35
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:207
BenchmarkParse\__construct
__construct()
Default constructor.
Definition: benchmarkParse.php:38
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:246
BenchmarkParse\$idCache
array $idCache
Cache that maps a Title DB key to revision ID for the requested timestamp *.
Definition: benchmarkParse.php:36
BenchmarkParse\getRevIdForTime
getRevIdForTime(Title $title, $timestamp)
Fetch the ID of the revision of a Title that occurred.
Definition: benchmarkParse.php:107