2013-02-05 00:44:44 +00:00
|
|
|
<?php
|
|
|
|
|
2016-05-17 14:52:05 +00:00
|
|
|
// @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps
|
2013-02-05 00:44:44 +00:00
|
|
|
class Scribunto_LuaSandboxTests extends Scribunto_LuaEngineTestBase {
|
|
|
|
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-02-11 18:30:24 +00:00
|
|
|
$ru = wfGetRusage();
|
|
|
|
$endTime = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6 + 0.5;
|
|
|
|
|
|
|
|
// Waste CPU cycles
|
|
|
|
do {
|
|
|
|
$ru = wfGetRusage();
|
|
|
|
$t = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
|
|
|
|
} 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,
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
|
|
|
$u0 = $engine->getInterpreter()->getCPUUsage();
|
|
|
|
$frame->expand(
|
|
|
|
$pp->preprocessToObj(
|
|
|
|
'{{#invoke:TestArgumentParsingTime|f|<scribuntodelay/>}}'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertLessThan( 0.25, $engine->getInterpreter()->getCPUUsage() - $u0,
|
|
|
|
'Argument access time was not counted'
|
|
|
|
);
|
|
|
|
|
|
|
|
$u0 = $engine->getInterpreter()->getCPUUsage();
|
|
|
|
$frame->expand(
|
|
|
|
$pp->preprocessToObj(
|
|
|
|
'{{#invoke:TestArgumentParsingTime|f2|<scribuntodelay/>}}'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertLessThan( 0.25, $engine->getInterpreter()->getCPUUsage() - $u0,
|
|
|
|
'Unused arguments not counted in preprocess'
|
|
|
|
);
|
|
|
|
|
|
|
|
$u0 = $engine->getInterpreter()->getCPUUsage();
|
|
|
|
$frame->expand(
|
|
|
|
$pp->preprocessToObj(
|
|
|
|
'{{#invoke:TestArgumentParsingTime|f3}}'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertGreaterThan( 0.25, $engine->getInterpreter()->getCPUUsage() - $u0,
|
|
|
|
'Recursive argument access time was counted'
|
|
|
|
);
|
|
|
|
}
|
2013-02-05 00:44:44 +00:00
|
|
|
}
|