resourceloader: Add CSSMin benchmarks
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 4 May 2017 04:00:14 +0000 (21:00 -0700)
committerKrinkle <krinklemail@gmail.com>
Thu, 4 May 2017 04:13:42 +0000 (04:13 +0000)
Usage:

 # Run default benchmark
 $ php maintenance/benchmarks/benchmarkCSSMin.php

 # Use custom file
 $ php maintenance/benchmarks/benchmarkCSSMin.php --file resources/lib/qunitjs/qunit.css

 # Debug the output for inspection (no benchmark)
 $ php maintenance/benchmarks/benchmarkCSSMin.php --out

Change-Id: I70d118131d0ff16d1a811b2de1328ea622b7ca69

autoload.php
maintenance/benchmarks/benchmarkCSSMin.php [new file with mode: 0644]
maintenance/benchmarks/cssmin/circle.svg [new file with mode: 0644]
maintenance/benchmarks/cssmin/styles.css [new file with mode: 0644]
maintenance/benchmarks/cssmin/wiki.png [new file with mode: 0644]

index 1141c39..e5161f1 100644 (file)
@@ -188,6 +188,7 @@ $wgAutoloadLocalClasses = [
        'BenchUtf8TitleCheck' => __DIR__ . '/maintenance/benchmarks/bench_utf8_title_check.php',
        'BenchWfIsWindows' => __DIR__ . '/maintenance/benchmarks/bench_wfIsWindows.php',
        'BenchWikimediaBaseConvert' => __DIR__ . '/maintenance/benchmarks/bench_Wikimedia_base_convert.php',
+       'BenchmarkCSSMin' => __DIR__ . '/maintenance/benchmarks/benchmarkCSSMin.php',
        'BenchmarkDeleteTruncate' => __DIR__ . '/maintenance/benchmarks/bench_delete_truncate.php',
        'BenchmarkHooks' => __DIR__ . '/maintenance/benchmarks/benchmarkHooks.php',
        'BenchmarkParse' => __DIR__ . '/maintenance/benchmarks/benchmarkParse.php',
diff --git a/maintenance/benchmarks/benchmarkCSSMin.php b/maintenance/benchmarks/benchmarkCSSMin.php
new file mode 100644 (file)
index 0000000..3eaa88d
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Benchmark
+ * @author Timo Tijhof
+ */
+
+require_once __DIR__ . '/Benchmarker.php';
+
+/**
+ * Maintenance script that benchmarks CSSMin.
+ *
+ * @ingroup Benchmark
+ */
+class BenchmarkCSSMin extends Benchmarker {
+       public function __construct() {
+               parent::__construct();
+               $this->addDescription( 'Benchmarks CSSMin.' );
+               $this->addOption( 'file', 'Path to CSS file (may be gzipped)', false, true );
+               $this->addOption( 'out', 'Echo output of one run to stdout for inspection', false, false );
+       }
+
+       public function execute() {
+               $file = $this->getOption( 'file', __DIR__ . '/cssmin/styles.css' );
+               $filename = basename( $file );
+               $css = $this->loadFile( $file );
+
+               if ( $this->hasOption( 'out' ) ) {
+                       echo "## minify\n\n",
+                               CSSMin::minify( $css ),
+                               "\n\n";
+                       echo "## remap\n\n",
+                               CSSMin::remap( $css, dirname( $file ), 'https://example.org/test/', true ),
+                               "\n";
+                       return;
+               }
+
+               $this->bench( [
+                       "minify ($filename)" => [
+                               'function' => [ 'CSSMin', 'minify' ],
+                               'args' => [ $css ]
+                       ],
+                       "remap ($filename)" => [
+                               'function' => [ 'CSSMin', 'remap' ],
+                               'args' => [ $css, dirname( $file ), 'https://example.org/test/', true ]
+                       ],
+               ] );
+       }
+
+       private function loadFile( $file ) {
+               $css = file_get_contents( $file );
+               // Detect GZIP compression header
+               if ( substr( $css, 0, 2 ) === "\037\213" ) {
+                       $css = gzdecode( $css );
+               }
+               return $css;
+       }
+}
+
+$maintClass = 'BenchmarkCSSMin';
+require_once RUN_MAINTENANCE_IF_MAIN;
diff --git a/maintenance/benchmarks/cssmin/circle.svg b/maintenance/benchmarks/cssmin/circle.svg
new file mode 100644 (file)
index 0000000..6b7d1af
--- /dev/null
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8">
+<circle cx="4" cy="4" r="2"/>
+</svg>
diff --git a/maintenance/benchmarks/cssmin/styles.css b/maintenance/benchmarks/cssmin/styles.css
new file mode 100644 (file)
index 0000000..3cc1520
--- /dev/null
@@ -0,0 +1,32 @@
+/**
+ * Header
+ */
+
+.foo {
+       background: url(wiki.png);
+}
+
+.foo {
+       background: url(unknown.png);
+}
+
+.foo {
+       background: url(https://example.org/foo.png);
+       background: url('https://example.org/foo.png');
+       background: url("https://example.org/foo.png");
+}
+
+.foo {
+       /* @embed */
+       background: url(wiki.png);
+}
+
+.foo {
+       /* @embed */
+       background: url(circle.svg);
+}
+
+.foo {
+       /* @embed */
+       background: url(wiki.png), url(wiki.png);
+}
diff --git a/maintenance/benchmarks/cssmin/wiki.png b/maintenance/benchmarks/cssmin/wiki.png
new file mode 100644 (file)
index 0000000..8c42118
Binary files /dev/null and b/maintenance/benchmarks/cssmin/wiki.png differ