Skip to content

Commit

Permalink
[dist] More benchmarking.
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Dec 12, 2014
1 parent ed2340a commit 4c17662
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 14 deletions.
99 changes: 99 additions & 0 deletions benchmark/advanced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
var Understudy = require('..');
var benchmark = require('benchmark');

var hooks = {
before: function (opts, next) {
opts.y = 4 + 5 + 6;
next();
},
after: function (opts, next) {
opts.z = 7 + 8 + 9;
next();
}
};

var underHooks = {
before: function (opts, next) {
opts.y = 4 + 5 + 6;
next();
},
after: function (opts, next) {
opts.z = 7 + 8 + 9;
next();
}
};


var NoHooks = function () {}
NoHooks.prototype.method = function (callback) {
var opts = { x: 1 + 2 + 3 };
hooks.before(opts, function () {
hooks.after(opts, function () {
opts.zz = 'a' + 'b' + 'c';
callback();
});
});
};

var YesHooks = function () {
Understudy.call(this);

this.before('method', underHooks.before);
this.after('method', underHooks.after);
}

YesHooks.prototype.method = function (callback) {
var opts = { x: 1 + 2 + 3 };
this.perform('method', opts, function (next) {
opts.zz = 'a' + 'b' + 'c';
next();
}, callback);
}

var tests = {
noHooks: new NoHooks(),
yesHooks: new YesHooks()
};

function testThing(thing, n, next) {
var completed = 0;
function checkDone() {
completed++;
if (completed === n) {
next();
}
}

for (var i = 0; i < n; i++) {
thing.method(checkDone);
}
}

function testAtN(n) {
console.log('Comparing at %s', n);
var suite = new benchmark.Suite();

suite.add('NoHooks', function nohooks1() {
testThing(tests.noHooks, n, function () { })
}).add('YesHooks', function nohooks1() {
testThing(tests.yesHooks, n, function () { })
}).on('cycle', function cycle(e) {
var details = e.target;

console.log('Finished benchmarking: "%s"', details.name);
console.log('Count (%d), Cycles (%d), Elapsed (%d), Hz (%d)'
, details.count
, details.cycles
, details.times.elapsed
, details.hz
);
}).on('complete', function completed() {
console.log('Benchmark: "%s" is the fastest.'
, this.filter('fastest').pluck('name')
);
}).run();

}


testAtN(100000)
41 changes: 28 additions & 13 deletions benchmark/simple.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
var Understudy = require('../');
var Understudy = require('..');
var accounting = require('accounting');

var LEN = 1e7;

function ops_per_sec(s) {
return accounting.format(LEN / format(s));
function ops_per_sec(ops, s) {
return (ops / format(s));
}

function format(s) {
return ((s[0] * 1e3 + s[1] / 1e6) / 1e3).toFixed(2);
return ((s[0] * 1e3 + s[1] / 1e6) / 1e3);
}

var hooks = {
Expand Down Expand Up @@ -71,9 +69,17 @@ function testThing(thing, key, n, next) {
function checkDone() {
completed++;
if (completed === n) {
var end = process.hrtime(start);
console.log(' completed in %ss, op/s %s', format(end), ops_per_sec(end));
next();
var end = process.hrtime(start),
elapsed = format(end);
opsPerSec = ops_per_sec(n, end);

console.log(
' completed in %ss, op/s %s',
elapsed.toFixed(2),
accounting.format(opsPerSec)
);

next({ elapsed: elapsed, opsPerSec: opsPerSec });
}
}

Expand All @@ -88,14 +94,23 @@ function compareThing(n) {
yesHooks: new YesHooks()
};

testThing(tests.noHooks, 'noHooks', n, function () {
testThing(tests.yesHooks, 'yesHooks', n, function () {
testThing(tests.noHooks, 'noHooks', n, function (noRes) {
testThing(tests.yesHooks, 'yesHooks', n, function (yesRes) {
console.log('noHooks is %s times faster', (noRes.opsPerSec / yesRes.opsPerSec).toFixed(2));

console.log('Done!');
});
});
}

// console.log('Running benchmarks in silent mode to warmup jit');
// var log = console.log;
// console.log = function () {}
// compareThing(1000000);
// compareThing(100000);

// console.log = log;
// console.log('Not silent anymore...');

compareThing(1000000);
compareThing(100000);
compareThing(10000);
compareThing(1000);
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
},
"dependencies": {},
"devDependencies": {
"accounting": "~0.4.1"
"accounting": "~0.4.1",
"benchmark": "~1.0.0",
"microtime": "~1.0.1"
},
"optionalDependencies": {},
"scripts": {
Expand Down

0 comments on commit 4c17662

Please sign in to comment.