summaryrefslogtreecommitdiff
path: root/perf/benchmark.js
blob: d69243a6e19a6bf12da82e1e57d68f5e44a83d4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env node

/**
 * Compare the performance of any two tagged versions of async.  Can also
 * compare any tag with what is in the current working directory.
 *
 * Usage:
 *
 *     perf/benchmark.js 0.9.2 0.9.0
 *
 * Compare version 0.9.0 with version 0.9.2
 *
 *     perf/benchmark.js 0.6.0
 *
 * Compare version 0.6.0 with the current working version
 *
 *     perf/benchmark.js
 *
 * Compare the current working version with the latest tagged version.
 */

var _ = require("lodash");
var Benchmark = require("benchmark");
var benchOptions = {defer: true, minSamples: 1, maxTime: 2};
var exec = require("child_process").exec;
var fs = require("fs");
var path = require("path");
var mkdirp = require("mkdirp");
var async = require("../");
var suiteConfigs = require("./suites");

var version0 = process.argv[2] || require("../package.json").version;
var version1 = process.argv[3] || "current";
var versionNames = [version0, version1];
var versions;
var wins = {};
var totalTime = {};
totalTime[version0] = wins[version0] = 0;
totalTime[version1] = wins[version1] = 0;

console.log("Comparing " + version0 + " with " + version1);
console.log("--------------------------------------");


async.eachSeries(versionNames, cloneVersion, function (err) {
  versions = versionNames.map(requireVersion);

  var suites = suiteConfigs
    .map(setDefaultOptions)
    .reduce(handleMultipleArgs, [])
    .map(setName)
    .map(createSuite);

  async.eachSeries(suites, runSuite, function () {
    var totalTime0 = Math.round(totalTime[version0]);
    var totalTime1 = Math.round(totalTime[version1]);

    var wins0 = Math.round(wins[version0]);
    var wins1 = Math.round(wins[version1]);

    if ( Math.abs((totalTime0 / totalTime1) - 1) < 0.01) {
      // if < 1% difference, we're likely within the margins of error
      console.log("Both versions are about equal " +
        "(" + totalTime0 + "ms total vs. " + totalTime1  + "ms total)");
    } else if (totalTime0 < totalTime1) {
      console.log(version0 + " faster overall " +
        "(" + totalTime0 + "ms total vs. " + totalTime1  + "ms total)");
    } else if (totalTime1 < totalTime0) {
      console.log(version1 + " faster overall " +
        "(" + totalTime1 + "ms total vs. " + totalTime0  + "ms total)");
    }

    if (wins0 > wins1) {
      console.log(version0 + " won more benchmarks " +
        "(" + wins0 + " vs. " + wins1  + ")");
    } else if (wins1 > wins0) {
      console.log(version1 + " won more benchmarks " +
        "(" + wins1 + " vs. " + wins0  + ")");
    } else {
      console.log("Both versions won the same number of benchmarks " +
        "(" + wins0 + " vs. " + wins1  + ")");
    }
  });
});

function runSuite(suite, callback) {
  suite.on("complete", function () {
    callback();
  }).run({async: true});
}

function setDefaultOptions(suiteConfig) {
  suiteConfig.args = suiteConfig.args || [[]];
  suiteConfig.setup = suiteConfig.setup || function () {};
  return suiteConfig;
}

function handleMultipleArgs(list, suiteConfig) {
  return list.concat(suiteConfig.args.map(function (args) {
    return _.defaults({args: args}, suiteConfig);
  }));
}

function setName(suiteConfig) {
  suiteConfig.name = suiteConfig.name + "(" + suiteConfig.args.join(",") + ")";
  return suiteConfig;
}

function createSuite(suiteConfig) {
  var suite = new Benchmark.Suite();
  var args = suiteConfig.args;

  function addBench(version, versionName) {
    var name = suiteConfig.name + " " + versionName;
    suite.add(name, function (deferred) {
      suiteConfig.fn(version, function () {
        deferred.resolve();
      });
    }, _.extend({
      versionName: versionName,
      setup: _.partial.apply(null, [suiteConfig.setup].concat(args))
    }, benchOptions));
  }

  addBench(versions[0], versionNames[0]);
  addBench(versions[1], versionNames[1]);


  return suite.on('cycle', function(event) {
    var mean = event.target.stats.mean * 1000;
    console.log(event.target + ", " + mean.toFixed(1) + "ms per sample");
    var version = event.target.options.versionName;
    totalTime[version] += mean;
  })
  .on('complete', function() {
    var fastest = this.filter('fastest');
    if (fastest.length === 2) {
      console.log("Tie");
    } else {
      var winner = fastest[0].options.versionName;
      console.log(winner + ' is faster');
      wins[winner]++;
    }
    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 repoPath = path.join(__dirname, "..");

    var cmd = "git clone --branch " + tag + " " + repoPath + " " + versionDir;

    exec(cmd, function (err, stdout, stderr) {
      if (err) {
        throw err;
      }
      callback();
    });

  });
}