*/
abstract class Benchmarker extends Maintenance {
protected $defaultCount = 100;
+ private $lang;
public function __construct() {
parent::__construct();
$this->addOption( 'count', 'How many times to run a benchmark', false, true );
+ $this->addOption( 'verbose', 'Verbose logging of resource usage', false, false, 'v' );
}
public function bench( array $benchs ) {
+ $this->lang = Language::factory( 'en' );
+
$this->startBench();
$count = $this->getOption( 'count', $this->defaultCount );
+ $verbose = $this->hasOption( 'verbose' );
foreach ( $benchs as $key => $bench ) {
// Shortcut for simple functions
if ( is_callable( $bench ) ) {
$t = microtime( true );
call_user_func_array( $bench['function'], $bench['args'] );
$t = ( microtime( true ) - $t ) * 1000;
+ if ( $verbose ) {
+ $this->verboseRun( $i );
+ }
$times[] = $t;
}
'median' => $median,
'mean' => $mean,
'max' => $max,
+ 'usage' => [
+ 'mem' => memory_get_usage( true ),
+ 'mempeak' => memory_get_peak_usage( true ),
+ ],
] );
}
}
'times',
$res['count']
);
+
foreach ( [ 'total', 'min', 'median', 'mean', 'max' ] as $metric ) {
$ret .= sprintf( " %' 6s: %6.2fms\n",
$metric,
$res[$metric]
);
}
+
+ foreach ( [
+ 'mem' => 'Current memory usage',
+ 'mempeak' => 'Peak memory usage'
+ ] as $key => $label ) {
+ $ret .= sprintf( "%' 20s: %s\n",
+ $label,
+ $this->lang->formatSize( $res['usage'][$key] )
+ );
+ }
+
$this->output( "$ret\n" );
}
+
+ protected function verboseRun( $iteration ) {
+ $this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
+ $iteration,
+ $this->lang->formatSize( memory_get_usage( true ) ),
+ $this->lang->formatSize( memory_get_peak_usage( true ) )
+ ) );
+ }
}
*/
public function testDeprecatedModules( $name, $expected ) {
$modules = self::getModules();
- $rl = new ResourceLoaderFileModule( $modules[$name] );
- $rl->setName( $name );
+ $module = new ResourceLoaderFileModule( $modules[$name] );
+ $module->setName( $name );
$ctx = $this->getResourceLoaderContext();
- $this->assertEquals( $rl->getScript( $ctx ), $expected );
+ $this->assertEquals( $module->getScript( $ctx ), $expected );
+ }
+
+ /**
+ * @covers ResourceLoaderFileModule::getScript
+ */
+ public function testGetScript() {
+ $module = new ResourceLoaderFileModule( [
+ 'localBasePath' => __DIR__ . '/../../data/resourceloader',
+ 'scripts' => [ 'script-nosemi.js', 'script-comment.js' ],
+ ] );
+ $module->setName( 'testing' );
+ $ctx = $this->getResourceLoaderContext();
+ $this->assertEquals(
+ "/* eslint-disable */\nmw.foo()\n" .
+ "\n" .
+ "/* eslint-disable */\nmw.foo()\n// mw.bar();\n" .
+ "\n",
+ $module->getScript( $ctx ),
+ 'scripts are concatenated with a new-line'
+ );
}
/**
);
}
+ public static function provideBuildContentScripts() {
+ return [
+ [
+ "mw.foo()",
+ "mw.foo();\n",
+ ],
+ [
+ "mw.foo();",
+ "mw.foo();",
+ ],
+ [
+ "mw.foo();\n",
+ "mw.foo();\n",
+ ],
+ [
+ "mw.foo()\n",
+ "mw.foo()\n;\n",
+ ],
+ [
+ "mw.foo()\n// mw.bar();",
+ "mw.foo()\n// mw.bar();",
+ ],
+ [
+ "mw.foo()// mw.bar();",
+ "mw.foo()// mw.bar();",
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideBuildContentScripts
+ * @covers ResourceLoaderModule::buildContent
+ */
+ public function testBuildContentScripts( $raw, $build, $message = null ) {
+ $context = $this->getResourceLoaderContext();
+ $module = new ResourceLoaderTestModule( [
+ 'script' => $raw
+ ] );
+ $this->assertEquals( $raw, $module->getScript( $context ), 'Raw script' );
+ $this->assertEquals(
+ [ 'scripts' => $build ],
+ $module->getModuleContent( $context ),
+ $message
+ );
+ }
+
/**
* @covers ResourceLoaderModule::getRelativePaths
* @covers ResourceLoaderModule::expandRelativePaths