Permalink
Browse files

Introduced a concurrency factor to the test - we now get much better …

…results!
  • Loading branch information...
1 parent bb25758 commit 3d775d66ef90ce9c106cfb1364f97ef80464d664 @hiddentao committed Feb 26, 2014
Showing with 74 additions and 27 deletions.
  1. +2 −1 package.json
  2. +19 −10 perf-co-vs-bluebird.js
  3. +23 −16 perf-generator-delegation.js
  4. +30 −0 tools.js
View
@@ -9,7 +9,8 @@
"benchmark": "~1.0.0",
"microtime": "~0.5.1",
"co": "~3.0.2",
- "bluebird": "~1.0.4"
+ "bluebird": "~1.0.4",
+ "commander": "~2.1.0"
},
"devDependencies": {},
"repository": {
View
@@ -2,9 +2,10 @@ var Benchmark = require('benchmark'),
co = require('co'),
Promise = require('bluebird');
+var tools = require('./tools');
-// thunk
-var setTimeoutPromise = function(ms) {
+
+setTimeoutPromise = function(ms) {
return new Promise(function(resolve, reject){
setTimeout(resolve, ms);
});
@@ -14,48 +15,55 @@ var setTimeoutPromise = function(ms) {
var gen = function*() {
yield setTimeoutPromise(1);
yield setTimeoutPromise(1);
- yield setTimeoutPromise(1);
- yield setTimeoutPromise(1);
};
var bluebirdCoroutine = Promise.coroutine(gen);
var coCoroutine = co(gen);
-
var suite = new Benchmark.Suite;
+
// add tests
suite.add('Bluebird-Promise.spawn', {
defer: true,
fn: function(deferred) {
- Promise.spawn(gen).then(function() {
+ return tools.run(function(cb) {
+ Promise.spawn(gen).then(cb);
+ }, function() {
deferred.resolve();
});
}
})
.add('co', {
defer: true,
fn: function(deferred) {
- co(gen)(function() {
+ return tools.run(function(cb) {
+ co(gen)(cb);
+ }, function() {
deferred.resolve();
});
}
})
.add('Bluebird-Promise.coroutine (prepared)', {
defer: true,
fn: function(deferred) {
- bluebirdCoroutine().then(function() {
+ return tools.run(function(cb) {
+ bluebirdCoroutine().then(cb);
+ }, function() {
deferred.resolve();
});
}
})
.add('co (prepared)', {
defer: true,
fn: function(deferred) {
- coCoroutine(function() {
+ return tools.run(function(cb) {
+ coCoroutine(cb);
+ }, function() {
deferred.resolve();
});
+
}
})
.on('cycle', function(event) {
@@ -64,4 +72,5 @@ suite.add('Bluebird-Promise.spawn', {
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
-.run({ 'async': true });
+.run({ 'async': true });
+
@@ -2,9 +2,10 @@ var Benchmark = require('benchmark'),
co = require('co'),
Promise = require('bluebird');
+var tools = require('./tools');
-// thunk
-var setTimeoutThunk = function(ms) {
+
+setTimeoutThunk = function(ms) {
return function(cb){
setTimeout(cb, ms);
};
@@ -14,8 +15,6 @@ var setTimeoutThunk = function(ms) {
var delegated = function*() {
yield setTimeoutThunk(1);
yield setTimeoutThunk(1);
- yield setTimeoutThunk(1);
- yield setTimeoutThunk(1);
};
var delegator = function*() {
@@ -30,27 +29,35 @@ var suite = new Benchmark.Suite;
suite.add('Without delegation', {
defer: true,
fn: function(deferred) {
- var gen = delegated(),
- done = false;
+ tools.run(function(cb) {
+ var gen = delegated(),
+ done = false;
- while (!done) {
- done = gen.next().done;
- }
+ while (!done) {
+ done = gen.next().done;
+ }
- deferred.resolve();
+ cb();
+ }, function() {
+ deferred.resolve();
+ });
}
})
.add('With delegation', {
defer: true,
fn: function(deferred) {
- var gen = delegator(),
- done = false;
+ tools.run(function(cb) {
+ var gen = delegator(),
+ done = false;
- while (!done) {
- done = gen.next().done;
- }
+ while (!done) {
+ done = gen.next().done;
+ }
- deferred.resolve();
+ cb();
+ }, function() {
+ deferred.resolve();
+ });
}
})
.on('cycle', function(event) {
View
@@ -0,0 +1,30 @@
+var program = require('commander');
+program
+ .option('-c, --concurrency <num>', 'Concurrency factor.')
+ .parse(process.argv);
+
+console.log('Concurrency factor: ' + program.concurrency);
+
+
+/**
+ * Run given asynchronous function X no. of times concurrently.
+ * @param {Function} fn function to execute (takes single callback parameter)
+ * @param {Function} cb callback once done
+ */
+exports.run = function(fn, cb) {
+ var yetToReturn = numCallsToMake = program.concurrency;
+
+ var fnCb = function() {
+ if (0 === --yetToReturn) cb();
+ };
+
+ while (0 < numCallsToMake--) {
+ fn(fnCb);
+ }
+};
+
+
+
+
+
+

0 comments on commit 3d775d6

Please sign in to comment.