Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaSandbox/SandboxTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaSandbox;
4
5 use MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon\LuaEngineUnitTestBase;
6
7 /**
8 * @covers \MediaWiki\Extension\Scribunto\Engines\LuaSandbox\LuaSandboxEngine
9 */
10 class SandboxTest extends LuaEngineUnitTestBase {
11 /** @inheritDoc */
12 protected static $moduleName = 'SandboxTests';
13
14 public static function suite( $className ) {
15 return self::makeSuite( $className, 'LuaSandbox' );
16 }
17
18 protected function getTestModules() {
19 return parent::getTestModules() + [
20 'SandboxTests' => __DIR__ . '/SandboxTests.lua',
21 ];
22 }
23
24 public function testArgumentParsingTime() {
25 $engine = $this->getEngine();
26 $parser = $engine->getParser();
27 $pp = $parser->getPreprocessor();
28 $frame = $pp->newFrame();
29
30 $parser->setHook( 'scribuntodelay', function () {
31 $endTime = $this->getRuTime() + 0.5;
32
33 // Waste CPU cycles
34 do {
35 $t = $this->getRuTime();
36 } while ( $t < $endTime );
37
38 return "ok";
39 } );
40 $this->extraModules['Module:TestArgumentParsingTime'] = '
41 return {
42 f = function ( frame )
43 return frame.args[1]
44 end,
45 f2 = function ( frame )
46 return frame:preprocess( "{{#invoke:TestArgumentParsingTime|f|}}" )
47 end,
48 f3 = function ( frame )
49 return frame:preprocess( "{{#invoke:TestArgumentParsingTime|f|<scribuntodelay/>}}" )
50 end,
51 }
52 ';
53
54 // Below we assert that the CPU time counted by LuaSandbox is $delta less than
55 // the CPU time actually spent.
56 // That way we can make sure that the time spent in the parser hook (which
57 // must be more than delta) is not taken into account.
58 $delta = 0.25;
59
60 $u0 = $engine->getInterpreter()->getCPUUsage();
61 $uTimeBefore = $this->getRuTime();
62 $frame->expand(
63 $pp->preprocessToObj(
64 '{{#invoke:TestArgumentParsingTime|f|<scribuntodelay/>}}'
65 )
66 );
67 $threshold = $this->getRuTime() - $uTimeBefore - $delta;
68 $this->assertLessThan( $threshold, $engine->getInterpreter()->getCPUUsage() - $u0,
69 'Argument access time was not counted'
70 );
71
72 $uTimeBefore = $this->getRuTime();
73 $u0 = $engine->getInterpreter()->getCPUUsage();
74 $frame->expand(
75 $pp->preprocessToObj(
76 '{{#invoke:TestArgumentParsingTime|f2|<scribuntodelay/>}}'
77 )
78 );
79 $threshold = $this->getRuTime() - $uTimeBefore - $delta;
80 $this->assertLessThan( $threshold, $engine->getInterpreter()->getCPUUsage() - $u0,
81 'Unused arguments not counted in preprocess'
82 );
83
84 $uTimeBefore = $this->getRuTime();
85 $u0 = $engine->getInterpreter()->getCPUUsage();
86 $frame->expand(
87 $pp->preprocessToObj(
88 '{{#invoke:TestArgumentParsingTime|f3}}'
89 )
90 );
91 $threshold = $this->getRuTime() - $uTimeBefore - $delta;
92 // If the underlying node is extremely slow, this test might produce false positives
93 $this->assertGreaterThan( $threshold, $engine->getInterpreter()->getCPUUsage() - $u0,
94 'Recursive argument access time was counted'
95 );
96 }
97
98 private function getRuTime() {
99 // RUSAGE_SELF = 0
100 $ru = getrusage( 0 );
101 return $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6 +
102 $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
103 }
104
105 }