refactor functions
authoralexlamsl <alexlamsl@gmail.com>
Mon, 1 Feb 2016 18:59:49 +0000 (02:59 +0800)
committeralexlamsl <alexlamsl@gmail.com>
Mon, 1 Feb 2016 18:59:49 +0000 (02:59 +0800)
benchmark.js

index ebc4229..a18ae53 100644 (file)
@@ -98,224 +98,228 @@ run(fileNames.map(function (fileName) {
         compressSize, gzCompressSize,
         minifiedTime, gzMinifiedTime;
 
-    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);
+    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) {
+              if (err) {
+                throw new Error('There was an error reading ' + minifiedFilePath);
+              }
+              minifiedSize = stats.size;
+              done();
+            });
           }
-          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) {
+        ], done);
+      });
+    }
+
+    function testMinimize(done) {
+      fs.readFile(filePath, function (err, data) {
+        if (err) {
+          throw new Error('There was an error reading ' + filePath);
+        }
+        minimize.parse(data, function (error, data) {
+          minimizedSize = data.length;
+          fs.writeFile(minimizedFilePath, data, function (err) {
             if (err) {
-              throw new Error('There was an error reading ' + gzFilePath);
+              throw new Error('There was an error writing ' + minimizedFilePath);
             }
-            gzOriginalSize = stats.size;
-            done();
-          });
-        });
-      },
-      // HTMLMinifier test
-      function (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) {
+            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 ' + minifiedFilePath);
+                  throw new Error('There was an error reading ' + gzMinimizedFilePath);
                 }
-                minifiedSize = stats.size;
+                gzMinimizedSize = stats.size;
                 done();
               });
-            }
-          ], done);
-        });
-      },
-      // Minimize test
-      function (done) {
-        fs.readFile(filePath, function (err, data) {
-          if (err) {
-            throw new Error('There was an error reading ' + filePath);
-          }
-          minimize.parse(data, function (error, data) {
-            minimizedSize = data.length;
-            fs.writeFile(minimizedFilePath, data, function (err) {
-              if (err) {
-                throw new Error('There was an error writing ' + minimizedFilePath);
-              }
-              // 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();
-                });
-              });
             });
           });
         });
-      },
-      // Will Peavy test
-      function (done) {
-        fs.readFile(filePath, {
-          encoding: 'utf8'
-        }, function (err, data) {
-          if (err) {
-            throw new Error('There was an error reading ' + filePath);
+      });
+    }
+
+    function testWillPeavy(done) {
+      fs.readFile(filePath, {
+        encoding: 'utf8'
+      }, function (err, data) {
+        if (err) {
+          throw new Error('There was an error reading ' + filePath);
+        }
+        var options = url.parse('http://www.willpeavy.com/minifier/');
+        options.method = 'POST';
+        options.headers = {
+          'Content-Type': 'application/x-www-form-urlencoded'
+        };
+        http.request(options, function (res) {
+          res.setEncoding('utf8');
+          var response = '';
+          res.on('data', function (chunk) {
+            response += chunk;
+          }).on('end', function () {
+            // Extract result from <textarea/>
+            var start = response.indexOf('>', response.indexOf('<textarea'));
+            var end = response.lastIndexOf('</textarea>');
+            fs.writeFile(willPeavyFilePath, 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);
+            });
+          });
+        }).end(querystring.stringify({
+          html: data
+        }));
+      });
+    }
+
+    function testHTMLCompressor(done) {
+      fs.readFile(filePath, {
+        encoding: 'utf8'
+      }, function (err, data) {
+        if (err) {
+          throw new Error('There was an error reading ' + filePath);
+        }
+        var options = url.parse('http://htmlcompressor.com/compress_ajax_v2.php');
+        options.method = 'POST';
+        options.headers = {
+          'Accept-Encoding': 'gzip',
+          'Content-Type': 'application/x-www-form-urlencoded'
+        };
+        http.request(options, function (res) {
+          if (res.headers['content-encoding'] === 'gzip') {
+            res = res.pipe(zlib.createGunzip());
           }
-          var options = url.parse('http://www.willpeavy.com/minifier/');
-          options.method = 'POST';
-          options.headers = {
-            'Content-Type': 'application/x-www-form-urlencoded'
-          };
-          http.request(options, function (res) {
-            res.setEncoding('utf8');
-            var response = '';
-            res.on('data', function (chunk) {
-              response += chunk;
-            }).on('end', function () {
-              // Extract result from <textarea/>
-              var start = response.indexOf('>', response.indexOf('<textarea'));
-              var end = response.lastIndexOf('</textarea>');
-              fs.writeFile(willPeavyFilePath, response.substring(start + 1, end), {
+          res.setEncoding('utf8');
+          var response = '';
+          res.on('data', function (chunk) {
+            response += chunk;
+          }).on('end', function () {
+            /* global JSON: true */
+            response = JSON.parse(response);
+            if (response.success) {
+              fs.writeFile(compressFilePath, response.result, {
                 encoding: 'utf8'
               }, function () {
                 run([
                   // Gzip the minified output
                   function (done) {
-                    gzip(willPeavyFilePath, gzWillPeavyFilePath, function () {
+                    gzip(compressFilePath, gzCompressFilePath, function () {
                       // Open and read the size of the minified+gzipped output
-                      fs.stat(gzWillPeavyFilePath, function (err, stats) {
+                      fs.stat(gzCompressFilePath, function (err, stats) {
                         if (err) {
-                          throw new Error('There was an error reading ' + gzWillPeavyFilePath);
+                          throw new Error('There was an error reading ' + gzCompressFilePath);
                         }
-                        gzWillPeavySize = stats.size;
+                        gzCompressSize = stats.size;
                         done();
                       });
                     });
                   },
                   // Open and read the size of the minified output
                   function (done) {
-                    fs.stat(willPeavyFilePath, function (err, stats) {
+                    fs.stat(compressFilePath, function (err, stats) {
                       if (err) {
-                        throw new Error('There was an error reading ' + willPeavyFilePath);
+                        throw new Error('There was an error reading ' + compressFilePath);
                       }
-                      willPeavySize = stats.size;
+                      compressSize = stats.size;
                       done();
                     });
                   }
                 ], done);
               });
-            });
-          }).end(querystring.stringify({
-            html: data
-          }));
-        });
-      },
-      // HTML Compressor test
+            }
+            // Site refused to process content
+            else {
+              compressSize = gzCompressSize = 0;
+              done();
+            }
+          });
+        }).end(querystring.stringify({
+          code_type:'html',
+          verbose: 1,
+          html_level: 1,
+          minimize_style: 1,
+          minimize_events: 1,
+          minimize_js_href: 1,
+          minimize_css: 1,
+          minimize_js: 1,
+          js_engine: 'yui',
+          js_fallback: 1,
+          code: data
+        }));
+      });
+    }
+
+    run([
+      // Open and read the size of the original input
       function (done) {
-        fs.readFile(filePath, {
-          encoding: 'utf8'
-        }, function (err, data) {
+        fs.stat(filePath, function (err, stats) {
           if (err) {
             throw new Error('There was an error reading ' + filePath);
           }
-          var options = url.parse('http://htmlcompressor.com/compress_ajax_v2.php');
-          options.method = 'POST';
-          options.headers = {
-            'Accept-Encoding': 'gzip',
-            'Content-Type': 'application/x-www-form-urlencoded'
-          };
-          http.request(options, function (res) {
-            if (res.headers['content-encoding'] === 'gzip') {
-              res = res.pipe(zlib.createGunzip());
+          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);
             }
-            res.setEncoding('utf8');
-            var response = '';
-            res.on('data', function (chunk) {
-              response += chunk;
-            }).on('end', function () {
-              /* global JSON: true */
-              response = JSON.parse(response);
-              if (response.success) {
-                fs.writeFile(compressFilePath, 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);
-                });
-              }
-              // Site refused to process content
-              else {
-                compressSize = gzCompressSize = 0;
-                done();
-              }
-            });
-          }).end(querystring.stringify({
-            code_type:'html',
-            verbose: 1,
-            html_level: 1,
-            minimize_style: 1,
-            minimize_events: 1,
-            minimize_js_href: 1,
-            minimize_css: 1,
-            minimize_js: 1,
-            js_engine: 'yui',
-            js_fallback: 1,
-            code: data
-          }));
+            gzOriginalSize = stats.size;
+            done();
+          });
         });
-      }
+      },
+      testHTMLMinifier,
+      testMinimize,
+      testWillPeavy,
+      testHTMLCompressor
     ], function () {
       rows[fileName] = [
         [fileName, '+ gzipped'].join('\n'),