MediaWiki  1.29.2
benchmarkParse.php
Go to the documentation of this file.
1 <?php
25 require __DIR__ . '/../Maintenance.php';
26 
28 
35 class BenchmarkParse extends Maintenance {
37  private $templateTimestamp = null;
38 
39  private $clearLinkCache = false;
40 
44  private $linkCache;
45 
47  private $idCache = [];
48 
49  function __construct() {
50  parent::__construct();
51  $this->addDescription( 'Benchmark parse operation' );
52  $this->addArg( 'title', 'The name of the page to parse' );
53  $this->addOption( 'warmup', 'Repeat the parse operation this number of times to warm the cache',
54  false, true );
55  $this->addOption( 'loops', 'Number of times to repeat parse operation post-warmup',
56  false, true );
57  $this->addOption( 'page-time',
58  'Use the version of the page which was current at the given time',
59  false, true );
60  $this->addOption( 'tpl-time',
61  'Use templates which were current at the given time (except that moves and ' .
62  'deletes are not handled properly)',
63  false, true );
64  $this->addOption( 'reset-linkcache', 'Reset the LinkCache after every parse.',
65  false, false );
66  }
67 
68  function execute() {
69  if ( $this->hasOption( 'tpl-time' ) ) {
70  $this->templateTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'tpl-time' ) ) );
71  Hooks::register( 'BeforeParserFetchTemplateAndtitle', [ $this, 'onFetchTemplate' ] );
72  }
73 
74  $this->clearLinkCache = $this->hasOption( 'reset-linkcache' );
75  // Set as a member variable to avoid function calls when we're timing the parse
76  $this->linkCache = MediaWikiServices::getInstance()->getLinkCache();
77 
78  $title = Title::newFromText( $this->getArg() );
79  if ( !$title ) {
80  $this->error( "Invalid title" );
81  exit( 1 );
82  }
83 
84  if ( $this->hasOption( 'page-time' ) ) {
85  $pageTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'page-time' ) ) );
86  $id = $this->getRevIdForTime( $title, $pageTimestamp );
87  if ( !$id ) {
88  $this->error( "The page did not exist at that time" );
89  exit( 1 );
90  }
91 
92  $revision = Revision::newFromId( $id );
93  } else {
94  $revision = Revision::newFromTitle( $title );
95  }
96 
97  if ( !$revision ) {
98  $this->error( "Unable to load revision, incorrect title?" );
99  exit( 1 );
100  }
101 
102  $warmup = $this->getOption( 'warmup', 1 );
103  for ( $i = 0; $i < $warmup; $i++ ) {
104  $this->runParser( $revision );
105  }
106 
107  $loops = $this->getOption( 'loops', 1 );
108  if ( $loops < 1 ) {
109  $this->error( 'Invalid number of loops specified', true );
110  }
111  $startUsage = getrusage();
112  $startTime = microtime( true );
113  for ( $i = 0; $i < $loops; $i++ ) {
114  $this->runParser( $revision );
115  }
116  $endUsage = getrusage();
117  $endTime = microtime( true );
118 
119  printf( "CPU time = %.3f s, wall clock time = %.3f s\n",
120  // CPU time
121  ( $endUsage['ru_utime.tv_sec'] + $endUsage['ru_utime.tv_usec'] * 1e-6
122  - $startUsage['ru_utime.tv_sec'] - $startUsage['ru_utime.tv_usec'] * 1e-6 ) / $loops,
123  // Wall clock time
124  ( $endTime - $startTime ) / $loops
125  );
126  }
127 
135  function getRevIdForTime( Title $title, $timestamp ) {
136  $dbr = $this->getDB( DB_REPLICA );
137 
138  $id = $dbr->selectField(
139  [ 'revision', 'page' ],
140  'rev_id',
141  [
142  'page_namespace' => $title->getNamespace(),
143  'page_title' => $title->getDBkey(),
144  'rev_timestamp <= ' . $dbr->addQuotes( $timestamp )
145  ],
146  __METHOD__,
147  [ 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 1 ],
148  [ 'revision' => [ 'INNER JOIN', 'rev_page=page_id' ] ]
149  );
150 
151  return $id;
152  }
153 
159  function runParser( Revision $revision ) {
160  $content = $revision->getContent();
161  $content->getParserOutput( $revision->getTitle(), $revision->getId() );
162  if ( $this->clearLinkCache ) {
163  $this->linkCache->clear();
164  }
165  }
166 
177  function onFetchTemplate( Parser $parser, Title $title, &$skip, &$id ) {
178  $pdbk = $title->getPrefixedDBkey();
179  if ( !isset( $this->idCache[$pdbk] ) ) {
180  $proposedId = $this->getRevIdForTime( $title, $this->templateTimestamp );
181  $this->idCache[$pdbk] = $proposedId;
182  }
183  if ( $this->idCache[$pdbk] !== false ) {
184  $id = $this->idCache[$pdbk];
185  }
186 
187  return true;
188  }
189 }
190 
191 $maintClass = 'BenchmarkParse';
LinkCache
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:34
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:265
Revision\newFromId
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:116
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
Revision\getContent
getContent( $audience=self::FOR_PUBLIC, User $user=null)
Fetch revision content if it's available to the specified audience.
Definition: Revision.php:1057
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1994
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
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:735
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Revision
Definition: Revision.php:33
Revision\newFromTitle
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition: Revision.php:134
BenchmarkParse
Maintenance script to benchmark how long it takes to parse a given title at an optionally specified t...
Definition: benchmarkParse.php:35
$maintClass
$maintClass
Definition: benchmarkParse.php:191
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
$content
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1049
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
BenchmarkParse\$templateTimestamp
string $templateTimestamp
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: benchmarkParse.php:37
$parser
do that in ParserLimitReportFormat instead $parser
Definition: hooks.txt:2536
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
BenchmarkParse\$linkCache
LinkCache $linkCache
Definition: benchmarkParse.php:44
Revision\getTitle
getTitle()
Returns the title of the page associated with this entry or null.
Definition: Revision.php:809
BenchmarkParse\onFetchTemplate
onFetchTemplate(Parser $parser, Title $title, &$skip, &$id)
Hook into the parser's revision ID fetcher.
Definition: benchmarkParse.php:177
e
in this case you re responsible for computing and outputting the entire conflict i e
Definition: hooks.txt:1398
BenchmarkParse\runParser
runParser(Revision $revision)
Parse the text from a given Revision.
Definition: benchmarkParse.php:159
Hooks\register
static register( $name, $callback)
Attach an event handler to a given hook.
Definition: Hooks.php:49
BenchmarkParse\execute
execute()
Do the actual work.
Definition: benchmarkParse.php:68
Title
Represents a title within MediaWiki.
Definition: Title.php:39
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:267
BenchmarkParse\$clearLinkCache
$clearLinkCache
Definition: benchmarkParse.php:39
BenchmarkParse\__construct
__construct()
Default constructor.
Definition: benchmarkParse.php:49
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:236
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:306
BenchmarkParse\$idCache
array $idCache
Cache that maps a Title DB key to revision ID for the requested timestamp.
Definition: benchmarkParse.php:47
BenchmarkParse\getRevIdForTime
getRevIdForTime(Title $title, $timestamp)
Fetch the ID of the revision of a Title that occurred.
Definition: benchmarkParse.php:135
array
the array() calling protocol came about after MediaWiki 1.4rc1.