Anomie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/53804


Change subject: Use the new ParserPrepareLimitReport hook
......................................................................

Use the new ParserPrepareLimitReport hook

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

Note this will work fine without Ie065c7b5, in that case the new hook
will never be called so it will use the existing behavior.

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, 167 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Scribunto 
refs/changes/04/53804/1

diff --git a/Scribunto.i18n.php b/Scribunto.i18n.php
index d83cb8f..43f7d0d 100644
--- a/Scribunto.i18n.php
+++ b/Scribunto.i18n.php
@@ -61,6 +61,19 @@
        'scribunto-luastandalone-gone' => 'Lua error: Internal error: The 
interpreter has already exited.',
        'scribunto-luastandalone-signal' => 'Lua error: Internal error: The 
interpreter has terminated with signal "$2".',
        'scribunto-luastandalone-exited' => 'Lua error: Internal error: The 
interpreter exited with status $2.',
+
+       'scribunto-limitreport-timeusage' => 'Lua time usage',
+       'scribunto-limitreport-timeusage-value' => '$1s/$2s',
+       'scribunto-limitreport-virtmemusage' => 'Lua virtual size',
+       'scribunto-limitreport-virtmemusage-value' => '$1/$2',
+       'scribunto-limitreport-estmemusage' => 'Lua estimated memory usage',
+       'scribunto-limitreport-memusage' => 'Lua memory usage',
+       'scribunto-limitreport-memusage-value' => '$1/$2',
+       'scribunto-limitreport-profile' => 'Lua Profile',
+       'scribunto-limitreport-profile-top' => '<table>',
+       'scribunto-limitreport-profile-value' => '<tr><td>$1</td><td>$2 
ms</td><td>$3%</td></tr>',
+       'scribunto-limitreport-profile-bottom' => '</table>',
+       'scribunto-limitreport-profile-value-text' => '$4 $2 ms $3%',
 );
 
 /** Message documentation (Message documentation)
@@ -126,6 +139,33 @@
 * $2 is an exit status.',
        'scribunto-luastandalone-exited' => 'Exception message. Parameters:
 * $2 is an exit status.',
+
+       '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-top' => 'HTML to open the "Lua Profile" 
table in the limit report table',
+       'scribunto-limitreport-profile-value' => 'HTML for each row in the "Lua 
Profile" table in the limit report table.
+* $1 is the name
+* $2 is the time in milliseconds
+* $3 is the percentage',
+       'scribunto-limitreport-profile-bottom' => 'HTML to close the "Lua 
Profile" table in the limit report table',
+       'scribunto-limitreport-profile-value-text' => 'Text for each row in the 
"Lua Profile" table in the NewPP limit report comment.
+* $4 is the name ($1 has wikitext formatting)
+* $2 is the time in milliseconds
+* $3 is the percentage',
 );
 
 /** Belarusian (Taraškievica orthography) (беларуская (тарашкевіца)‎)
diff --git a/Scribunto.php b/Scribunto.php
index 0e6763f..dcb3eba 100644
--- a/Scribunto.php
+++ b/Scribunto.php
@@ -46,7 +46,8 @@
 $wgAutoloadClasses['ScribuntoContent'] = $dir.'common/ScribuntoContent.php';
 
 $wgHooks['ParserFirstCallInit'][] = 'ScribuntoHooks::setupParserHook';
-$wgHooks['ParserLimitReport'][] = 'ScribuntoHooks::reportLimits';
+$wgHooks['ParserPrepareLimitReport'][] = 'ScribuntoHooks::reportLimitData';
+$wgHooks['ParserLimitReport']['scribunto'] = 'ScribuntoHooks::reportLimits';
 $wgHooks['ParserClearState'][] = 'ScribuntoHooks::clearState';
 $wgHooks['ParserCloned'][] = 'ScribuntoHooks::parserCloned';
 
diff --git a/common/Base.php b/common/Base.php
index 74d6238..866f2ed 100644
--- a/common/Base.php
+++ b/common/Base.php
@@ -196,6 +196,15 @@
                wfRunHooks( 'ScribuntoExternalLibraryPaths', array( $engine, 
&$extraLibraryPaths ) );
                return array_merge( $coreLibraryPaths, $extraLibraryPaths );
        }
+
+       /**
+        * 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 ){
+       }
 }
 
 /**
diff --git a/common/Hooks.php b/common/Hooks.php
index 3e44585..1d71336 100644
--- a/common/Hooks.php
+++ b/common/Hooks.php
@@ -162,7 +162,8 @@
 
        /**
         * Adds report of number of evaluations by the single wikitext page.
-        * 
+        *
+        * @deprecated
         * @param $parser Parser
         * @param $report
         * @return bool
@@ -176,6 +177,25 @@
        }
 
        /**
+        * 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 );
+                       $report .= $engine->reportLimitData( $output );
+               }
+               return true;
+       }
+
+       /**
         * Adds the module namespaces.
         */
        public static function addCanonicalNamespaces( &$list ) {
diff --git a/engines/LuaSandbox/Engine.php b/engines/LuaSandbox/Engine.php
index 1f957db..0d022f4 100644
--- a/engines/LuaSandbox/Engine.php
+++ b/engines/LuaSandbox/Engine.php
@@ -29,13 +29,12 @@
                        Scribunto_LuaSandboxInterpreter::SECONDS );
 
                $s .= "Lua Profile:\n";
-               $cumulativePercent = $cumulativeTime = 0;
+               $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
@@ -57,6 +56,76 @@
                return $s;
        }
 
+       public function reportLimitData( ParserOutput $output ) {
+               global $wgLang;
+
+               $this->load();
+
+               $t = $this->interpreter->getCPUUsage();
+               $output->setLimitReportData( 'scribunto-limitreport-timeusage',
+                       array(
+                               sprintf( "%.3f", $t ),
+                               sprintf( "%.3f", $this->options['cpuLimit'] )
+                       )
+               );
+               $output->setLimitReportData( 'scribunto-limitreport-memusage',
+                       array(
+                               $wgLang->formatSize( 
$this->interpreter->getPeakMemoryUsage() ),
+                               $wgLang->formatSize( 
$this->options['memoryLimit'] ),
+                       )
+               );
+               if ( $t < 1.0 ) {
+                       return;
+               }
+
+               $percentProfile = $this->interpreter->getProfilerFunctionReport(
+                       Scribunto_LuaSandboxInterpreter::PERCENT
+               );
+               if ( !count( $percentProfile ) ) {
+                       return;
+               }
+               $timeProfile = $this->interpreter->getProfilerFunctionReport(
+                       Scribunto_LuaSandboxInterpreter::SECONDS
+               );
+
+               $lines = array();
+               $cumulativePercent = 0;
+               $num = $otherTime = $otherPercent = 0;
+               foreach ( $percentProfile as $name => $percent ) {
+                       $time = $timeProfile[$name] * 1000;
+                       $num++;
+                       if ( $cumulativePercent <= 99 && $num <= 10 ) {
+                               // Map some regularly appearing internal names
+                               if ( preg_match( '/^<mw.lua:(\d+)>$/', $name, 
$m ) ) {
+                                       $line = $this->getMwLuaLine( $m[1] );
+                                       if ( preg_match( 
'/^\s*(local\s+)?function ([a-zA-Z0-9_.]*)/', $line, $m ) ) {
+                                               $name = $m[2] . ' ' . $name;
+                                       }
+                               }
+                               $wikiname = preg_replace( 
'/<(Module:[^>]+):(\d+)>$/', '<[[$1]]:$2>', $name );
+                               $lines[] = array(
+                                       htmlspecialchars( $wikiname ),
+                                       sprintf( "%8.0f", $time ),
+                                       sprintf( "%8.1f", $percent ),
+                                       htmlspecialchars( sprintf( "%-59s", 
$name ) ),
+                               );
+                       } else {
+                               $otherTime += $time;
+                               $otherPercent += $percent;
+                       }
+                       $cumulativePercent += $percent;
+               }
+               if ( $otherTime ) {
+                       $lines[] = array(
+                               "[others]",
+                               sprintf( "%8.0f", $otherTime ),
+                               sprintf( "%8.1f", $otherPercent ),
+                               sprintf( "%-59s", "[others]" ),
+                       );
+               }
+               $output->setLimitReportData( 'scribunto-limitreport-profile', 
$lines, array( 'multiline' ) );
+       }
+
        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 f656111..c2239aa 100644
--- a/engines/LuaStandalone/LuaStandaloneEngine.php
+++ b/engines/LuaStandalone/LuaStandaloneEngine.php
@@ -38,6 +38,30 @@
                        $lang->formatSize( $status['vsize'] - 
$this->initialStatus['vsize'] ) . "\n";
        }
 
+       function reportLimitData( ParserOutput $output ) {
+               global $wgLang;
+               $this->load();
+               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(
+                               $wgLang->formatSize( $status['vsize'] ),
+                               $wgLang->formatSize( 
$this->options['memoryLimit'] )
+                       )
+               );
+               $output->setLimitReportData( 
'scribunto-limitreport-estmemusage',
+                       $wgLang->formatSize( $status['vsize'] - 
$this->initialStatus['vsize'] )
+               );
+       }
+
        /**
         * @return mixed
         */

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idffd2d78f9a0217c99c07cbbfc844d6daf0172f7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Scribunto
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to