jenkins-bot has submitted this change and it was merged.

Change subject: Use the new limit report hooks
......................................................................


Use the new limit report hooks

Change Ie065c7b5 added an option to show profiling data at the bottom of
preview pages, and with it new hooks to gather and format this data in a
more structured way than is possible with ParserLimitReport. This change
adds support for the new hooks.

Depends-On: I7799616a602d90e1b8d3f0ece35811ca387bade7
Change-Id: Idffd2d78f9a0217c99c07cbbfc844d6daf0172f7
---
M Scribunto.i18n.php
M Scribunto.php
M common/Base.php
M common/Hooks.php
M engines/LuaSandbox/Engine.php
M engines/LuaStandalone/LuaStandaloneEngine.php
6 files changed, 270 insertions(+), 21 deletions(-)

Approvals:
  Tim Starling: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Scribunto.i18n.php b/Scribunto.i18n.php
index b1a1c08..e384c3b 100644
--- a/Scribunto.i18n.php
+++ b/Scribunto.i18n.php
@@ -66,6 +66,18 @@
        'scribunto-luastandalone-exited' => 'Lua error: Internal error: The 
interpreter exited with status $2.',
 
        'scribunto-module-with-errors-category' => 'Scribunto modules with 
errors',
+
+       'scribunto-limitreport-timeusage' => 'Lua time usage',
+       'scribunto-limitreport-timeusage-value' => '$1/$2 seconds',
+       'scribunto-limitreport-virtmemusage' => 'Lua virtual size',
+       'scribunto-limitreport-virtmemusage-value' => '$1/$2',
+       'scribunto-limitreport-estmemusage' => 'Lua estimated memory usage',
+       'scribunto-limitreport-estmemusage-value' => '$1',
+       'scribunto-limitreport-memusage' => 'Lua memory usage',
+       'scribunto-limitreport-memusage-value' => '$1/$2',
+       'scribunto-limitreport-profile' => 'Lua Profile',
+       'scribunto-limitreport-profile-ms' => '$1 ms',
+       'scribunto-limitreport-profile-percent' => '$1%',
 );
 
 /** Message documentation (Message documentation)
@@ -178,6 +190,26 @@
 
 See also:
 * {{msg-mw|scribunto-ignore-errors}}',
+       'scribunto-limitreport-timeusage' => 'Label for the "Lua time usage" 
row in the limit report table',
+       'scribunto-limitreport-timeusage-value' => 'Format for the "Lua time 
usage" value in the limit report table.
+* $1 is the usage in seconds
+* $2 is the maximum',
+       'scribunto-limitreport-virtmemusage' => 'Label for the "Lua virtual 
size" row in the limit report table',
+       'scribunto-limitreport-virtmemusage-value' => 'Format for the "Lua 
virtual size" value in the limit report table.
+* $1 is the usage
+* $2 is the maximum',
+       'scribunto-limitreport-estmemusage' => 'Label for the "Lua estimated 
memory usage" row in the limit report table',
+       'scribunto-limitreport-estmemusage-value' => 'Format for the "Lua 
estimated memory usage" value in the limit report table.
+* $1 is the value',
+       'scribunto-limitreport-memusage' => 'Label for the "Lua memory usage" 
row in the limit report table',
+       'scribunto-limitreport-memusage-value' => 'Format for the "Lua memory 
usage" value in the limit report table.
+* $1 is the usage
+* $2 is the maximum',
+       'scribunto-limitreport-profile' => 'Label for the "Lua Profile" row in 
the limit report table',
+       'scribunto-limitreport-profile-ms' => 'Text to format the milliseconds 
in the "Lua Profile" table
+* $1 is the time in milliseconds',
+       'scribunto-limitreport-profile-percent' => 'Text to format the time 
percentage in the "Lua Profile" table
+* $1 is the percentage',
 );
 
 /** Afrikaans (Afrikaans)
diff --git a/Scribunto.php b/Scribunto.php
index 5ead8af..9c13ede 100644
--- a/Scribunto.php
+++ b/Scribunto.php
@@ -48,7 +48,9 @@
 $wgHooks['SoftwareInfo'][] = 'ScribuntoHooks::getSoftwareInfo';
 
 $wgHooks['ParserFirstCallInit'][] = 'ScribuntoHooks::setupParserHook';
-$wgHooks['ParserLimitReport'][] = 'ScribuntoHooks::reportLimits';
+$wgHooks['ParserLimitReport']['scribunto'] = 'ScribuntoHooks::reportLimits';
+$wgHooks['ParserLimitReportPrepare'][] = 'ScribuntoHooks::reportLimitData';
+$wgHooks['ParserLimitReportFormat'][] = 'ScribuntoHooks::formatLimitData';
 $wgHooks['ParserClearState'][] = 'ScribuntoHooks::clearState';
 $wgHooks['ParserCloned'][] = 'ScribuntoHooks::parserCloned';
 
diff --git a/common/Base.php b/common/Base.php
index 78a5bf4..6041233 100644
--- a/common/Base.php
+++ b/common/Base.php
@@ -209,6 +209,39 @@
                wfRunHooks( 'ScribuntoExternalLibraryPaths', array( $engine, 
&$extraLibraryPaths ) );
                return array_merge( $coreLibraryPaths, $extraLibraryPaths );
        }
+
+       /**
+        * Get text for the limit report, old style
+        *
+        * @deprecated
+        * @return string
+        */
+       public function getLimitReport(){
+               return '';
+       }
+
+       /**
+        * Add limit report data to a ParserOutput object
+        *
+        * @param $output ParserOutput ParserOutput object in which to add 
limit data
+        * @return null
+        */
+       public function reportLimitData( ParserOutput $output ){
+       }
+
+       /**
+        * Format limit report data
+        *
+        * @param $key string
+        * @param &$value string
+        * @param &$report string
+        * @param $isHTML bool
+        * @param $localize bool
+        * @return bool
+        */
+       public function formatLimitData( $key, &$value, &$report, $isHTML, 
$localize ){
+               return true;
+       }
 }
 
 /**
diff --git a/common/Hooks.php b/common/Hooks.php
index 37e2839..bd3ae8f 100644
--- a/common/Hooks.php
+++ b/common/Hooks.php
@@ -174,7 +174,8 @@
 
        /**
         * Adds report of number of evaluations by the single wikitext page.
-        * 
+        *
+        * @deprecated
         * @param $parser Parser
         * @param $report
         * @return bool
@@ -188,6 +189,40 @@
        }
 
        /**
+        * Adds report of number of evaluations by the single wikitext page.
+        *
+        * @param $parser Parser
+        * @param $output ParserOutput
+        * @return bool
+        */
+       public static function reportLimitData( $parser, $output ) {
+               // Unhook the deprecated hook, since the new one exists.
+               global $wgHooks;
+               unset( $wgHooks['ParserLimitReport']['scribunto'] );
+
+               if ( Scribunto::isParserEnginePresent( $parser ) ) {
+                       $engine = Scribunto::getParserEngine( $parser );
+                       $engine->reportLimitData( $output );
+               }
+               return true;
+       }
+
+       /**
+        * Formats the limit report data
+        *
+        * @param $key string
+        * @param &$value string
+        * @param &$report string
+        * @param $isHTML bool
+        * @param $localize bool
+        * @return bool
+        */
+       public static function formatLimitData( $key, &$value, &$report, 
$isHTML, $localize ) {
+               $engine = Scribunto::newDefaultEngine();
+               return $engine->formatLimitData( $key, $value, $report, 
$isHTML, $localize );
+       }
+
+       /**
         * Adds the module namespaces.
         */
        public static function addCanonicalNamespaces( &$list ) {
diff --git a/engines/LuaSandbox/Engine.php b/engines/LuaSandbox/Engine.php
index bb8ea1a..ed82143 100644
--- a/engines/LuaSandbox/Engine.php
+++ b/engines/LuaSandbox/Engine.php
@@ -28,32 +28,38 @@
                }
        }
 
-       public function getLimitReport() {
+       private function getLimitReportData() {
+               $ret = array();
                $this->load();
-               $lang = Language::factory( 'en' );
 
                $t = $this->interpreter->getCPUUsage();
-               $s = 'Lua time usage: ' . sprintf( "%.3f", $t ) . "s\n" .
-                       'Lua memory usage: ' . $lang->formatSize( 
$this->interpreter->getPeakMemoryUsage() ) . "\n";
+               $ret['scribunto-limitreport-timeusage'] = array(
+                       sprintf( "%.3f", $t ),
+                       sprintf( "%.3f", $this->options['cpuLimit'] )
+               );
+               $ret['scribunto-limitreport-memusage'] = array(
+                       $this->interpreter->getPeakMemoryUsage(),
+                       $this->options['memoryLimit'],
+               );
                if ( $t < 1.0 ) {
-                       return $s;
+                       return $ret;
                }
-               $percentProfile = 
$this->interpreter->getProfilerFunctionReport( 
-                       Scribunto_LuaSandboxInterpreter::PERCENT );
-               if ( !count( $percentProfile ) ) {
-                       return $s;
-               }
-               $timeProfile = $this->interpreter->getProfilerFunctionReport( 
-                       Scribunto_LuaSandboxInterpreter::SECONDS );
 
-               $s .= "Lua Profile:\n";
-               $cumulativePercent = $cumulativeTime = 0;
+               $percentProfile = $this->interpreter->getProfilerFunctionReport(
+                       Scribunto_LuaSandboxInterpreter::PERCENT
+               );
+               if ( !count( $percentProfile ) ) {
+                       return $ret;
+               }
+               $timeProfile = $this->interpreter->getProfilerFunctionReport(
+                       Scribunto_LuaSandboxInterpreter::SECONDS
+               );
+
+               $lines = array();
+               $cumulativePercent = 0;
                $num = $otherTime = $otherPercent = 0;
-               $format = "    %-59s %8.0f ms %8.1f%%\n";
                foreach ( $percentProfile as $name => $percent ) {
                        $time = $timeProfile[$name] * 1000;
-                       $cumulativePercent += $percent;
-                       $cumulativeTime += $time;
                        $num++;
                        if ( $cumulativePercent <= 99 && $num <= 10 ) {
                                // Map some regularly appearing internal names
@@ -63,18 +69,118 @@
                                                $name = $m[2] . ' ' . $name;
                                        }
                                }
-                               $s .= sprintf( $format, $name, $time, $percent 
);
+                               $lines[] = array( $name, sprintf( '%.0f', $time 
), sprintf( '%.1f', $percent ) );
                        } else {
                                $otherTime += $time;
                                $otherPercent += $percent;
                        }
+                       $cumulativePercent += $percent;
                }
                if ( $otherTime ) {
-                       $s .= sprintf( $format, "[others]", $otherTime, 
$otherPercent );
+                       $lines[] = array( '[others]', sprintf( '%.0f', 
$otherTime ), sprintf( '%.1f', $otherPercent ) );
+               }
+               $ret['scribunto-limitreport-profile'] = $lines;
+               return $ret;
+       }
+
+       public function getLimitReport() {
+               $data = $this->getLimitReportData();
+               $lang = Language::factory( 'en' );
+
+               $t = $this->interpreter->getCPUUsage();
+               $s = 'Lua time usage: ' . sprintf( "%.3f", 
$data['scribunto-limitreport-timeusage'] ) . "s\n" .
+                       'Lua memory usage: ' . $lang->formatSize( 
$data['scribunto-limitreport-memusage'] ) . "\n";
+               if ( isset( $data['scribunto-limitreport-profile'] ) ) {
+                       $s .= "Lua Profile:\n";
+                       $format = "    %-59s %8.0f ms %8.1f%%\n";
+                       foreach ( $data['scribunto-limitreport-profile'] as 
$line ) {
+                               $s .= sprintf( $format, $line[0], $line[1], 
$line[2] );
+                       }
                }
                return $s;
        }
 
+       public function reportLimitData( ParserOutput $output ) {
+               $data = $this->getLimitReportData();
+               foreach ( $data as $k => $v ) {
+                       $output->setLimitReportData( $k, $v );
+               }
+       }
+
+       public function formatLimitData( $key, &$value, &$report, $isHTML, 
$localize ) {
+               global $wgLang;
+               $lang = $localize ? $wgLang : Language::factory( 'en' );
+               switch ( $key ) {
+                       case 'scribunto-limitreport-memusage':
+                               $value = array_map( array( $lang, 'formatSize' 
), $value );
+                               break;
+               }
+
+               if ( $key !== 'scribunto-limitreport-profile' ) {
+                       return true;
+               }
+
+               $keyMsg = wfMessage( 'scribunto-limitreport-profile' );
+               $msMsg = wfMessage( 'scribunto-limitreport-profile-ms' );
+               $percentMsg = wfMessage( 
'scribunto-limitreport-profile-percent' );
+               if ( !$localize ) {
+                       $keyMsg->inLanguage( 'en' )->useDatabase( false );
+                       $msMsg->inLanguage( 'en' )->useDatabase( false );
+                       $percentMsg->inLanguage( 'en' )->useDatabase( false );
+               }
+
+               // To avoid having to do actual work in Message::fetchMessage 
for each
+               // line in the loops below, call ->exists() here to populate 
->message.
+               $msMsg->exists();
+               $percentMsg->exists();
+
+               if ( $isHTML ) {
+                       $report .= Html::openElement( 'tr' ) .
+                               Html::rawElement( 'th', array( 'colspan' => 2 
), $keyMsg->parse() ) .
+                               Html::closeElement( 'tr' ) .
+                               Html::openElement( 'tr' ) .
+                               Html::openElement( 'td', array( 'colspan' => 2 
) ) .
+                               Html::openElement( 'table' );
+                       foreach ( $value as $line ) {
+                               $name = $line[0];
+                               $location = '';
+                               if ( preg_match( '/^(.*?) *<([^<>]+):(\d+)>$/', 
$name, $m ) ) {
+                                       $name = $m[1];
+                                       $title = Title::newFromText( $m[2] );
+                                       if ( $title && $title->getNamespace() 
=== NS_MODULE ) {
+                                               $location = '&lt;' . 
Linker::link( $title ) . ":{$m[3]}&gt;";
+                                       } else {
+                                               $location = htmlspecialchars( 
"<{$m[2]}:{$m[3]}>" );
+                                       }
+                               }
+                               $ms = clone $msMsg;
+                               $ms->params( $line[1] );
+                               $pct = clone $percentMsg;
+                               $pct->params( $line[2] );
+                               $report .= Html::openElement( 'tr' ) .
+                                       Html::element( 'td', null, $name ) .
+                                       Html::rawElement( 'td', null, $location 
) .
+                                       Html::rawElement( 'td', array( 'align' 
=> 'right' ), $ms->parse() ) .
+                                       Html::rawElement( 'td', array( 'align' 
=> 'right' ), $pct->parse() ) .
+                                       Html::closeElement( 'tr' );
+                       }
+                       $report .= Html::closeElement( 'table' ) .
+                               Html::closeElement( 'td' ) .
+                               Html::closeElement( 'tr' );
+               } else {
+                       $report .= $keyMsg->text() . ":\n";
+                       foreach ( $value as $line ) {
+                               $ms = clone $msMsg;
+                               $ms->params( $line[1] );
+                               $pct = clone $percentMsg;
+                               $pct->params( $line[2] );
+                               $report .= sprintf( "    %-59s %11s %11s\n", 
$line[0], $ms->text(), $pct->text() );
+                       }
+               }
+
+               return false;
+       }
+
        protected function getMwLuaLine( $lineNum ) {
                if ( !isset( $this->lineCache['mw.lua'] ) ) {
                        $this->lineCache['mw.lua'] = file( 
$this->getLuaLibDir() . '/mw.lua' );
diff --git a/engines/LuaStandalone/LuaStandaloneEngine.php 
b/engines/LuaStandalone/LuaStandaloneEngine.php
index 4fea92a..0f3597a 100644
--- a/engines/LuaStandalone/LuaStandaloneEngine.php
+++ b/engines/LuaStandalone/LuaStandaloneEngine.php
@@ -42,6 +42,47 @@
                        $lang->formatSize( $status['vsize'] - 
$this->initialStatus['vsize'] ) . "\n";
        }
 
+       function reportLimitData( ParserOutput $output ) {
+               try {
+                       $this->load();
+               } catch ( Exception $e ) {
+                       return;
+               }
+               if ( !$this->initialStatus ) {
+                       return;
+               }
+               $status = $this->interpreter->getStatus();
+               $output->setLimitReportData( 'scribunto-limitreport-timeusage',
+                       array(
+                               sprintf( "%.3f", $status['time'] / 
$this->getClockTick() ),
+                               sprintf( "%.3f", $this->options['cpuLimit'] )
+                       )
+               );
+               $output->setLimitReportData( 
'scribunto-limitreport-virtmemusage',
+                       array(
+                               $status['vsize'],
+                               $this->options['memoryLimit']
+                       )
+               );
+               $output->setLimitReportData( 
'scribunto-limitreport-estmemusage',
+                       $status['vsize'] - $this->initialStatus['vsize']
+               );
+       }
+
+       function formatLimitData( $key, &$value, &$report, $isHTML, $localize ) 
{
+               global $wgLang;
+               $lang = $localize ? $wgLang : Language::factory( 'en' );
+               switch ( $key ) {
+                       case 'scribunto-limitreport-virtmemusage':
+                               $value = array_map( array( $lang, 'formatSize' 
), $value );
+                               break;
+                       case 'scribunto-limitreport-estmemusage':
+                               $value = $lang->formatSize( $value );
+                               break;
+               }
+               return true;
+       }
+
        /**
         * @return mixed
         */

-- 
To view, visit https://gerrit.wikimedia.org/r/53804
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Idffd2d78f9a0217c99c07cbbfc844d6daf0172f7
Gerrit-PatchSet: 11
Gerrit-Project: mediawiki/extensions/Scribunto
Gerrit-Branch: master
Gerrit-Owner: Anomie <bjor...@wikimedia.org>
Gerrit-Reviewer: Aaron Schulz <asch...@wikimedia.org>
Gerrit-Reviewer: Anomie <bjor...@wikimedia.org>
Gerrit-Reviewer: Tim Starling <tstarl...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to