Merge "Remove "@author Aaron Schulz" annotations"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 27 Jun 2017 22:39:10 +0000 (22:39 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 27 Jun 2017 22:39:10 +0000 (22:39 +0000)
maintenance/benchmarks/Benchmarker.php
tests/phpunit/data/resourceloader/script-comment.js [new file with mode: 0644]
tests/phpunit/data/resourceloader/script-nosemi.js [new file with mode: 0644]
tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php
tests/phpunit/includes/resourceloader/ResourceLoaderModuleTest.php

index 0039d20..832da4d 100644 (file)
@@ -35,15 +35,20 @@ require_once __DIR__ . '/../Maintenance.php';
  */
 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 ) ) {
@@ -66,6 +71,9 @@ abstract class Benchmarker extends Maintenance {
                                $t = microtime( true );
                                call_user_func_array( $bench['function'], $bench['args'] );
                                $t = ( microtime( true ) - $t ) * 1000;
+                               if ( $verbose ) {
+                                       $this->verboseRun( $i );
+                               }
                                $times[] = $t;
                        }
 
@@ -104,6 +112,10 @@ abstract class Benchmarker extends Maintenance {
                                'median' => $median,
                                'mean' => $mean,
                                'max' => $max,
+                               'usage' => [
+                                       'mem' => memory_get_usage( true ),
+                                       'mempeak' => memory_get_peak_usage( true ),
+                               ],
                        ] );
                }
        }
@@ -126,12 +138,32 @@ abstract class Benchmarker extends Maintenance {
                        '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 ) )
+               ) );
+       }
 }
diff --git a/tests/phpunit/data/resourceloader/script-comment.js b/tests/phpunit/data/resourceloader/script-comment.js
new file mode 100644 (file)
index 0000000..46b60f9
--- /dev/null
@@ -0,0 +1,3 @@
+/* eslint-disable */
+mw.foo()
+// mw.bar();
diff --git a/tests/phpunit/data/resourceloader/script-nosemi.js b/tests/phpunit/data/resourceloader/script-nosemi.js
new file mode 100644 (file)
index 0000000..2b1550f
--- /dev/null
@@ -0,0 +1,2 @@
+/* eslint-disable */
+mw.foo()
index 9750ea4..ded56e9 100644 (file)
@@ -132,10 +132,30 @@ class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase {
         */
        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'
+               );
        }
 
        /**
index 17861d8..6057b97 100644 (file)
@@ -94,6 +94,52 @@ class ResourceLoaderModuleTest extends ResourceLoaderTestCase {
                );
        }
 
+       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