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