summaryrefslogtreecommitdiff
path: root/perf
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2015-05-19 22:13:00 -0700
committerAlexander Early <alexander.early@gmail.com>2015-05-19 22:13:00 -0700
commit1f98d6a54dc8f74c582fa28f23893d2292cd7f81 (patch)
tree6ef2291ebfbc6aef48e1a2c9d9ac207718aa9524 /perf
parenta73eef321256b36f529dce57a1e6ad7efd34851a (diff)
downloadasync-1f98d6a54dc8f74c582fa28f23893d2292cd7f81.tar.gz
adding benchmark script
Diffstat (limited to 'perf')
-rwxr-xr-xperf/benchmark.js108
-rw-r--r--perf/suites.js40
2 files changed, 148 insertions, 0 deletions
diff --git a/perf/benchmark.js b/perf/benchmark.js
new file mode 100755
index 0000000..44dbc61
--- /dev/null
+++ b/perf/benchmark.js
@@ -0,0 +1,108 @@
+#!/usr/bin/env node
+
+var Benchmark = require("benchmark");
+var benchOptions = {defer: true, minSamples: 7};
+var exec = require("child_process").exec;
+var fs = require("fs");
+var mkdirp = require("mkdirp");
+var async = require("../");
+var suiteConfigs = require("./suites");
+
+var version1 = process.argv[2] || require("../package.json").version;
+var version2 = process.argv[3] || "current";
+var versionNames = [version1, version2];
+var versions;
+var wins = [0, 0];
+
+console.log("Comparing " + version1 + " with " + version2);
+console.log("--------------------------------------");
+
+
+async.eachSeries(versionNames, cloneVersion, function (err) {
+ versions = versionNames.map(requireVersion);
+
+ var suites = suiteConfigs.map(createSuite);
+
+ async.eachSeries(suites, runSuite, function () {
+ var wins0 = wins[0].length;
+ var wins1 = wins[1].length;
+
+ if (wins0 > wins1) {
+ console.log(versionNames[0] + " faster overall " +
+ "(" + wins0 + " wins vs. " + wins1 +" wins)");
+ } else if (wins1 > wins0) {
+ console.log(versionNames[1] + " faster overall " +
+ "(" + wins1 + " wins vs. " + wins0 +" wins)");
+ } else {
+ console.log("Both versions are equal");
+ }
+ });
+});
+
+function runSuite(suite, callback) {
+ suite.on("complete", function () {
+ callback();
+ }).run({async: true});
+}
+
+function createSuite(suiteConfig) {
+ var suite = new Benchmark.Suite();
+
+ function addBench(version, versionName) {
+ var title = suiteConfig.name + " " + versionName;
+ suite.add(title, function (deferred) {
+ suiteConfig.fn(versions[0], deferred);
+ }, benchOptions);
+ }
+
+ addBench(versions[0], versionNames[0]);
+ addBench(versions[1], versionNames[1]);
+
+ return suite.on('cycle', function(event) {
+ console.log(event.target + "");
+ })
+ .on('complete', function() {
+ var fastest = this.filter('fastest');
+ if (fastest.length === 2) {
+ console.log("Tie");
+ } else {
+ console.log(fastest[0].name + ' is faster');
+ var index = this.indexOf("fastest");
+ wins[index]++;
+ }
+ console.log("--------------------------------------");
+ });
+
+}
+
+function requireVersion(tag) {
+ if (tag === "current") {
+ return async;
+ }
+
+ return require("./versions/" + tag + "/");
+}
+
+function cloneVersion(tag, callback) {
+ if (tag === "current") return callback();
+
+ var versionDir = __dirname + "/versions/" + tag;
+ mkdirp.sync(versionDir);
+ fs.open(versionDir + "/package.json", "r", function (err, handle) {
+ if (!err) {
+ // version has already been cloned
+ fs.close(handle);
+ return callback();
+ }
+
+ var cmd = "git clone --branch " + tag + " ../ " + versionDir;
+
+ exec(cmd, function (err, stdout, stderr) {
+ if (err) {
+ throw err;
+ }
+ callback();
+ });
+
+ });
+}
diff --git a/perf/suites.js b/perf/suites.js
new file mode 100644
index 0000000..d9a7b19
--- /dev/null
+++ b/perf/suites.js
@@ -0,0 +1,40 @@
+module.exports = [
+ {
+ name: "each(10)",
+ fn: function (async, deferred) {
+ async.each(Array(10), function (num, cb) {
+ async.setImmediate(cb);
+ }, done(deferred));
+ }
+ },
+ {
+ name: "each(10000)",
+ fn: function (async, deferred) {
+ async.each(Array(10000), function (num, cb) {
+ async.setImmediate(cb);
+ }, done(deferred));
+ }
+ },
+ {
+ name: "eachSeries(10)",
+ fn: function (async, deferred) {
+ async.eachSeries(Array(10), function (num, cb) {
+ async.setImmediate(cb);
+ }, done(deferred));
+ }
+ },
+ {
+ name: "eachSeries(10000)",
+ fn: function (async, deferred) {
+ async.eachSeries(Array(10000), function (num, cb) {
+ async.setImmediate(cb);
+ }, done(deferred));
+ }
+ }
+];
+
+function done(deferred) {
+ return function () {
+ deferred.resolve();
+ };
+}