benchmarkParse: Add option to clear link cache
authorKunal Mehta <legoktm@member.fsf.org>
Thu, 15 Sep 2016 06:55:35 +0000 (23:55 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Thu, 15 Sep 2016 06:57:16 +0000 (23:57 -0700)
For performance testing the lookup of links (e.g. LinkHolderArray),
allow resetting the LinkCache after every parse.

Change-Id: I75c2cad9a8cf1b41d192708773194f799673ce83

maintenance/benchmarks/benchmarkParse.php

index 884e307..1753250 100644 (file)
@@ -24,6 +24,8 @@
 
 require __DIR__ . '/../Maintenance.php';
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Maintenance script to benchmark how long it takes to parse a given title at an optionally
  * specified timestamp
@@ -34,6 +36,13 @@ class BenchmarkParse extends Maintenance {
        /** @var string MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS) */
        private $templateTimestamp = null;
 
+       private $clearLinkCache = false;
+
+       /**
+        * @var LinkCache
+        */
+       private $linkCache;
+
        /** @var array Cache that maps a Title DB key to revision ID for the requested timestamp */
        private $idCache = [];
 
@@ -52,6 +61,8 @@ class BenchmarkParse extends Maintenance {
                        'Use templates which were current at the given time (except that moves and ' .
                        'deletes are not handled properly)',
                        false, true );
+               $this->addOption( 'reset-linkcache', 'Reset the LinkCache after every parse.',
+                       false, false );
        }
 
        function execute() {
@@ -60,6 +71,10 @@ class BenchmarkParse extends Maintenance {
                        Hooks::register( 'BeforeParserFetchTemplateAndtitle', [ $this, 'onFetchTemplate' ] );
                }
 
+               $this->clearLinkCache = $this->hasOption( 'reset-linkcache' );
+               // Set as a member variable to avoid function calls when we're timing the parse
+               $this->linkCache = MediaWikiServices::getInstance()->getLinkCache();
+
                $title = Title::newFromText( $this->getArg() );
                if ( !$title ) {
                        $this->error( "Invalid title" );
@@ -144,6 +159,9 @@ class BenchmarkParse extends Maintenance {
        function runParser( Revision $revision ) {
                $content = $revision->getContent();
                $content->getParserOutput( $revision->getTitle(), $revision->getId() );
+               if ( $this->clearLinkCache ) {
+                       $this->linkCache->clear();
+               }
        }
 
        /**