improve code reuse
authoralexlamsl <alexlamsl@gmail.com>
Mon, 1 Feb 2016 19:39:38 +0000 (03:39 +0800)
committeralexlamsl <alexlamsl@gmail.com>
Mon, 1 Feb 2016 19:39:38 +0000 (03:39 +0800)
benchmark.js

index a18ae53..4fe89ab 100644 (file)
@@ -82,53 +82,58 @@ run(fileNames.map(function (fileName) {
     console.log('Processing...', fileName);
 
     var filePath = path.join('benchmarks/', fileName + '.html');
-    var minifiedFilePath = path.join('benchmarks/generated/', fileName + '.min.html');
-    var minimizedFilePath = path.join('benchmarks/generated/', fileName + '.mz.html');
-    var willPeavyFilePath = path.join('benchmarks/generated/', fileName + '.wp.html');
-    var compressFilePath = path.join('benchmarks/generated/', fileName + '.hc.html');
-    var gzFilePath = path.join('benchmarks/generated/', fileName + '.html.gz');
-    var gzMinifiedFilePath = path.join('benchmarks/generated/', fileName + '.min.html.gz');
-    var gzMinimizedFilePath = path.join('benchmarks/generated/', fileName + '.mz.html.gz');
-    var gzWillPeavyFilePath = path.join('benchmarks/generated/', fileName + '.wp.html.gz');
-    var gzCompressFilePath = path.join('benchmarks/generated/', fileName + '.hc.html.gz');
-    var originalSize, gzOriginalSize,
-        minifiedSize, gzMinifiedSize,
-        minimizedSize, gzMinimizedSize,
-        willPeavySize, gzWillPeavySize,
-        compressSize, gzCompressSize,
-        minifiedTime, gzMinifiedTime;
+    var original = {
+      filePath: filePath,
+      gzFilePath: path.join('benchmarks/generated/', fileName + '.html.gz')
+    };
+    var infos = {};
+    ['minifier', 'minimize', 'willpeavy', 'compressor'].forEach(function (name) {
+      infos[name] = {
+        filePath: path.join('benchmarks/generated/', fileName + '.' + name + '.html'),
+        gzFilePath: path.join('benchmarks/generated/', fileName + '.' + name + '.html.gz')
+      };
+    });
+    var minifiedTime, gzMinifiedTime;
 
-    function testHTMLMinifier(done) {
-      // Begin timing after gzipped fixtures have been created
-      var startTime = Date.now();
-      fork('./cli', [filePath, '-c', 'benchmark.conf', '-o', minifiedFilePath]).on('exit', function () {
-        minifiedTime = Date.now() - startTime;
-        run([
-          // Gzip the minified output
-          function (done) {
-            gzip(minifiedFilePath, gzMinifiedFilePath, function () {
-              gzMinifiedTime = Date.now() - startTime;
-              // Open and read the size of the minified+gzipped output
-              fs.stat(gzMinifiedFilePath, function (err, stats) {
-                if (err) {
-                  throw new Error('There was an error reading ' + gzMinifiedFilePath);
-                }
-                gzMinifiedSize = stats.size;
-                done();
-              });
-            });
-          },
-          // Open and read the size of the minified output
-          function (done) {
-            fs.stat(minifiedFilePath, function (err, stats) {
+    function readSizes(info, done) {
+      run([
+        // Gzip the minified output
+        function (done) {
+          gzip(info.filePath, info.gzFilePath, function () {
+            // Open and read the size of the minified+gzipped output
+            fs.stat(info.gzFilePath, function (err, stats) {
               if (err) {
-                throw new Error('There was an error reading ' + minifiedFilePath);
+                throw new Error('There was an error reading ' + info.gzFilePath);
               }
-              minifiedSize = stats.size;
+              info.gzSize = stats.size;
               done();
             });
-          }
-        ], done);
+          });
+        },
+        // Open and read the size of the minified output
+        function (done) {
+          fs.stat(info.filePath, function (err, stats) {
+            if (err) {
+              throw new Error('There was an error reading ' + info.filePath);
+            }
+            info.size = stats.size;
+            done();
+          });
+        }
+      ], done);
+    }
+
+    function testHTMLMinifier(done) {
+      // Begin timing after gzipped fixtures have been created
+      var startTime = Date.now();
+      var info = infos.minifier;
+      var args = [filePath, '-c', 'benchmark.conf', '-o', info.filePath];
+      fork('./cli', args).on('exit', function () {
+        minifiedTime = Date.now() - startTime;
+        readSizes(info, function () {
+          gzMinifiedTime = Date.now() - startTime;
+          done();
+        });
       });
     }
 
@@ -138,22 +143,12 @@ run(fileNames.map(function (fileName) {
           throw new Error('There was an error reading ' + filePath);
         }
         minimize.parse(data, function (error, data) {
-          minimizedSize = data.length;
-          fs.writeFile(minimizedFilePath, data, function (err) {
+          var info = infos.minimize;
+          fs.writeFile(info.filePath, data, function (err) {
             if (err) {
-              throw new Error('There was an error writing ' + minimizedFilePath);
+              throw new Error('There was an error writing ' + info.filePath);
             }
-            // Gzip the minified output
-            gzip(minimizedFilePath, gzMinimizedFilePath, function () {
-              // Open and read the size of the minified+gzipped output
-              fs.stat(gzMinimizedFilePath, function (err, stats) {
-                if (err) {
-                  throw new Error('There was an error reading ' + gzMinimizedFilePath);
-                }
-                gzMinimizedSize = stats.size;
-                done();
-              });
-            });
+            readSizes(info, done);
           });
         });
       });
@@ -180,34 +175,14 @@ run(fileNames.map(function (fileName) {
             // Extract result from <textarea/>
             var start = response.indexOf('>', response.indexOf('<textarea'));
             var end = response.lastIndexOf('</textarea>');
-            fs.writeFile(willPeavyFilePath, response.substring(start + 1, end), {
+            var info = infos.willpeavy;
+            fs.writeFile(info.filePath, response.substring(start + 1, end), {
               encoding: 'utf8'
-            }, function () {
-              run([
-                // Gzip the minified output
-                function (done) {
-                  gzip(willPeavyFilePath, gzWillPeavyFilePath, function () {
-                    // Open and read the size of the minified+gzipped output
-                    fs.stat(gzWillPeavyFilePath, function (err, stats) {
-                      if (err) {
-                        throw new Error('There was an error reading ' + gzWillPeavyFilePath);
-                      }
-                      gzWillPeavySize = stats.size;
-                      done();
-                    });
-                  });
-                },
-                // Open and read the size of the minified output
-                function (done) {
-                  fs.stat(willPeavyFilePath, function (err, stats) {
-                    if (err) {
-                      throw new Error('There was an error reading ' + willPeavyFilePath);
-                    }
-                    willPeavySize = stats.size;
-                    done();
-                  });
-                }
-              ], done);
+            }, function (err) {
+              if (err) {
+                throw new Error('There was an error writing ' + info.filePath);
+              }
+              readSizes(info, done);
             });
           });
         }).end(querystring.stringify({
@@ -240,40 +215,21 @@ run(fileNames.map(function (fileName) {
           }).on('end', function () {
             /* global JSON: true */
             response = JSON.parse(response);
+            var info = infos.compressor;
             if (response.success) {
-              fs.writeFile(compressFilePath, response.result, {
+              fs.writeFile(info.filePath, response.result, {
                 encoding: 'utf8'
-              }, function () {
-                run([
-                  // Gzip the minified output
-                  function (done) {
-                    gzip(compressFilePath, gzCompressFilePath, function () {
-                      // Open and read the size of the minified+gzipped output
-                      fs.stat(gzCompressFilePath, function (err, stats) {
-                        if (err) {
-                          throw new Error('There was an error reading ' + gzCompressFilePath);
-                        }
-                        gzCompressSize = stats.size;
-                        done();
-                      });
-                    });
-                  },
-                  // Open and read the size of the minified output
-                  function (done) {
-                    fs.stat(compressFilePath, function (err, stats) {
-                      if (err) {
-                        throw new Error('There was an error reading ' + compressFilePath);
-                      }
-                      compressSize = stats.size;
-                      done();
-                    });
-                  }
-                ], done);
+              }, function (err) {
+                if (err) {
+                  throw new Error('There was an error writing ' + info.filePath);
+                }
+                readSizes(info, done);
               });
             }
             // Site refused to process content
             else {
-              compressSize = gzCompressSize = 0;
+              info.size = 0;
+              info.gzSize = 0;
               done();
             }
           });
@@ -294,43 +250,27 @@ run(fileNames.map(function (fileName) {
     }
 
     run([
-      // Open and read the size of the original input
-      function (done) {
-        fs.stat(filePath, function (err, stats) {
-          if (err) {
-            throw new Error('There was an error reading ' + filePath);
-          }
-          originalSize = stats.size;
-          done();
-        });
-      },
-      // Open and read the size of the gzipped original
       function (done) {
-        gzip(filePath, gzFilePath, function () {
-          fs.stat(gzFilePath, function (err, stats) {
-            if (err) {
-              throw new Error('There was an error reading ' + gzFilePath);
-            }
-            gzOriginalSize = stats.size;
-            done();
-          });
-        });
+        readSizes(original, done);
       },
       testHTMLMinifier,
       testMinimize,
       testWillPeavy,
       testHTMLCompressor
     ], function () {
-      rows[fileName] = [
+      var row = [
         [fileName, '+ gzipped'].join('\n'),
-        [redSize(originalSize), redSize(gzOriginalSize)].join('\n'),
-        [greenSize(minifiedSize), greenSize(gzMinifiedSize)].join('\n'),
-        [greenSize(minimizedSize), greenSize(gzMinimizedSize)].join('\n'),
-        [greenSize(willPeavySize), greenSize(gzWillPeavySize)].join('\n'),
-        [greenSize(compressSize), greenSize(gzCompressSize)].join('\n'),
-        [blueSavings(originalSize, minifiedSize), blueSavings(gzOriginalSize, gzMinifiedSize)].join('\n'),
-        [blueTime(minifiedTime), blueTime(gzMinifiedTime)].join('\n')
+        [redSize(original.size), redSize(original.gzSize)].join('\n')
       ];
+      for (var name in infos) {
+        var info = infos[name];
+        row.push([greenSize(info.size), greenSize(info.gzSize)].join('\n'));
+      }
+      row.push(
+        [blueSavings(original.size, infos.minifier.size), blueSavings(original.gzSize, infos.minifier.gzSize)].join('\n'),
+        [blueTime(minifiedTime), blueTime(gzMinifiedTime)].join('\n')
+      );
+      rows[fileName] = row;
       done();
     });
   };