2013-02-05 00:44:44 +00:00
|
|
|
<?php
|
|
|
|
|
2018-05-27 00:50:43 +00:00
|
|
|
/**
|
|
|
|
* @covers Scribunto_LuaSandboxEngine
|
|
|
|
*/
|
2018-03-10 13:15:42 +00:00
|
|
|
class Scribunto_LuaSandboxTest extends Scribunto_LuaEngineTestBase {
|
2013-02-05 00:44:44 +00:00
|
|
|
protected static $moduleName = 'SandboxTests';
|
|
|
|
|
|
|
|
public static function suite( $className ) {
|
|
|
|
return self::makeSuite( $className, 'LuaSandbox' );
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
protected function getTestModules() {
|
2017-06-15 17:19:00 +00:00
|
|
|
return parent::getTestModules() + [
|
2013-02-05 00:44:44 +00:00
|
|
|
'SandboxTests' => __DIR__ . '/SandboxTests.lua',
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2013-02-05 00:44:44 +00:00
|
|
|
}
|
2013-03-06 23:28:24 +00:00
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testArgumentParsingTime() {
|
2016-02-11 18:30:24 +00:00
|
|
|
if ( !wfGetRusage() ) {
|
|
|
|
$this->markTestSkipped( "getrusage is not available" );
|
|
|
|
}
|
|
|
|
|
2013-03-06 23:28:24 +00:00
|
|
|
$engine = $this->getEngine();
|
|
|
|
$parser = $engine->getParser();
|
|
|
|
$pp = $parser->getPreprocessor();
|
|
|
|
$frame = $pp->newFrame();
|
|
|
|
|
|
|
|
$parser->setHook( 'scribuntodelay', function () {
|
2016-08-19 13:46:30 +00:00
|
|
|
$endTime = $this->getRuTime() + 0.5;
|
2016-02-11 18:30:24 +00:00
|
|
|
|
|
|
|
// Waste CPU cycles
|
|
|
|
do {
|
2016-08-19 13:46:30 +00:00
|
|
|
$t = $this->getRuTime();
|
2016-02-11 18:30:24 +00:00
|
|
|
} while ( $t < $endTime );
|
|
|
|
|
2013-03-06 23:28:24 +00:00
|
|
|
return "ok";
|
|
|
|
} );
|
|
|
|
$this->extraModules['Module:TestArgumentParsingTime'] = '
|
|
|
|
return {
|
|
|
|
f = function ( frame )
|
|
|
|
return frame.args[1]
|
|
|
|
end,
|
|
|
|
f2 = function ( frame )
|
|
|
|
return frame:preprocess( "{{#invoke:TestArgumentParsingTime|f|}}" )
|
|
|
|
end,
|
|
|
|
f3 = function ( frame )
|
|
|
|
return frame:preprocess( "{{#invoke:TestArgumentParsingTime|f|<scribuntodelay/>}}" )
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
2016-08-19 13:46:30 +00:00
|
|
|
// Below we assert that the CPU time counted by LuaSandbox is $delta less than
|
|
|
|
// the CPU time actually spent.
|
|
|
|
// That way we can make sure that the time spent in the parser hook (which
|
|
|
|
// must be more than delta) is not taken into account.
|
|
|
|
$delta = 0.25;
|
|
|
|
|
2013-03-06 23:28:24 +00:00
|
|
|
$u0 = $engine->getInterpreter()->getCPUUsage();
|
2016-08-19 13:46:30 +00:00
|
|
|
$uTimeBefore = $this->getRuTime();
|
2013-03-06 23:28:24 +00:00
|
|
|
$frame->expand(
|
|
|
|
$pp->preprocessToObj(
|
|
|
|
'{{#invoke:TestArgumentParsingTime|f|<scribuntodelay/>}}'
|
|
|
|
)
|
|
|
|
);
|
2016-08-19 13:46:30 +00:00
|
|
|
$threshold = $this->getRuTime() - $uTimeBefore - $delta;
|
|
|
|
$this->assertLessThan( $threshold, $engine->getInterpreter()->getCPUUsage() - $u0,
|
2013-03-06 23:28:24 +00:00
|
|
|
'Argument access time was not counted'
|
|
|
|
);
|
|
|
|
|
2016-08-19 13:46:30 +00:00
|
|
|
$uTimeBefore = $this->getRuTime();
|
2013-03-06 23:28:24 +00:00
|
|
|
$u0 = $engine->getInterpreter()->getCPUUsage();
|
|
|
|
$frame->expand(
|
|
|
|
$pp->preprocessToObj(
|
|
|
|
'{{#invoke:TestArgumentParsingTime|f2|<scribuntodelay/>}}'
|
|
|
|
)
|
|
|
|
);
|
2016-08-19 13:46:30 +00:00
|
|
|
$threshold = $this->getRuTime() - $uTimeBefore - $delta;
|
|
|
|
$this->assertLessThan( $threshold, $engine->getInterpreter()->getCPUUsage() - $u0,
|
2013-03-06 23:28:24 +00:00
|
|
|
'Unused arguments not counted in preprocess'
|
|
|
|
);
|
|
|
|
|
2016-08-19 13:46:30 +00:00
|
|
|
$uTimeBefore = $this->getRuTime();
|
2013-03-06 23:28:24 +00:00
|
|
|
$u0 = $engine->getInterpreter()->getCPUUsage();
|
|
|
|
$frame->expand(
|
|
|
|
$pp->preprocessToObj(
|
|
|
|
'{{#invoke:TestArgumentParsingTime|f3}}'
|
|
|
|
)
|
|
|
|
);
|
2016-08-19 13:46:30 +00:00
|
|
|
$threshold = $this->getRuTime() - $uTimeBefore - $delta;
|
|
|
|
// If the underlying node is extremely slow, this test might produce false positives
|
|
|
|
$this->assertGreaterThan( $threshold, $engine->getInterpreter()->getCPUUsage() - $u0,
|
2013-03-06 23:28:24 +00:00
|
|
|
'Recursive argument access time was counted'
|
|
|
|
);
|
|
|
|
}
|
2016-08-19 13:46:30 +00:00
|
|
|
|
|
|
|
private function getRuTime() {
|
|
|
|
$ru = wfGetRusage();
|
|
|
|
return $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6 +
|
|
|
|
$ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
|
|
|
|
}
|
|
|
|
|
2013-02-05 00:44:44 +00:00
|
|
|
}
|