mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-12-02 03:46:17 +00:00
1365e35d89
Apparently microtime is a bit unreliable for this test for some reason, so let's use getrusage() to measure actual CPU time instead. Bug: T125045 Change-Id: Ia1ab6b043b99abb4fc6c2989ad09a24d97dd09c3
87 lines
2.3 KiB
PHP
87 lines
2.3 KiB
PHP
<?php
|
|
|
|
class Scribunto_LuaSandboxTests extends Scribunto_LuaEngineTestBase {
|
|
protected static $moduleName = 'SandboxTests';
|
|
|
|
public static function suite( $className ) {
|
|
return self::makeSuite( $className, 'LuaSandbox' );
|
|
}
|
|
|
|
protected function getTestModules() {
|
|
return parent::getTestModules() + array(
|
|
'SandboxTests' => __DIR__ . '/SandboxTests.lua',
|
|
);
|
|
}
|
|
|
|
public function testArgumentParsingTime() {
|
|
if ( !wfGetRusage() ) {
|
|
$this->markTestSkipped( "getrusage is not available" );
|
|
}
|
|
|
|
$engine = $this->getEngine();
|
|
if ( !is_callable( array( $engine->getInterpreter()->sandbox, 'pauseUsageTimer' ) ) ) {
|
|
$this->markTestSkipped( "LuaSandbox::pauseUsageTimer is not available" );
|
|
}
|
|
|
|
$parser = $engine->getParser();
|
|
$pp = $parser->getPreprocessor();
|
|
$frame = $pp->newFrame();
|
|
|
|
$parser->setHook( 'scribuntodelay', function () {
|
|
$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 );
|
|
|
|
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'
|
|
);
|
|
}
|
|
}
|