Navigation Menu

Skip to content

Commit

Permalink
Update to work with nodejs 0.1.30
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 27, 2010
1 parent 8000071 commit 11fab3c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 53 deletions.
12 changes: 2 additions & 10 deletions README.md
Expand Up @@ -62,17 +62,9 @@ Export the following fields from your benchmark script.

## Asynchronous Benchmarking

Your test functions should return either a promise or `undefined`. If they return `undefined`, then they'll be run 1000 times before checking the timer. (This is to average out the vagaries of the garbage collector and other machine entropy.)
Just write your functions so that they take a single argument. That argument is your callback. Have fun with it.

If your test function returns a promise, then the next iteration won't occur until it calls `emitSuccess`.

Note that creating and returning a promise involves extra overhead. So, if you're trying to compare something where the overall time per iteration is around the same as the time to create a promise, then this will lead to useless or misleading results. To get around this problem, run your function many times before emitting success on the promise. As an example, check out `examples/nexttick-vs-settimeout.js`.

## Asynchronous Benchmarking without Promises

If you prefer callbacks to promises, that's fine. Just write your functions so that they take a single argument. That argument is your callback. Have fun with it.

Your callback will be fired using `process.nextTick`. This has a wee bit of overhead, so if you're testing something really fast, you should probably construct it to run many times before calling the callback. Check the `examples/callback-style.js` test for an example.
Your callback will be fired using `process.nextTick`. This has a wee bit of overhead, so if you're testing something really fast, you should probably construct it to run many times before calling the callback. Check the `examples/nexttick-vs-settimeout.js` test for an example.

## Using node-bench programmatically

Expand Down
21 changes: 0 additions & 21 deletions examples/callback-style.js

This file was deleted.

16 changes: 7 additions & 9 deletions examples/nexttick-vs-settimeout.js
@@ -1,23 +1,21 @@

// run 500 times so that we can abstract out the overhead of promise creation.
// run many times so that we can abstract out the overhead of promise creation.
var count = 500;

exports.countPerLap = count;
exports.compare = {
nextTick : function () {
var p = new process.Promise(), i = 0;
nextTick : function (done) {
var i = 0;
process.nextTick(function () {
if (i ++ > count) p.emitSuccess();
if (i ++ > count) done();
else process.nextTick(arguments.callee);
});
return p;
},
setTimeout : function () {
var p = new process.Promise(), i = 0;
setTimeout : function (done) {
var i = 0;
setTimeout(function () {
if (i ++ > count) p.emitSuccess();
if (i ++ > count) done();
else setTimeout(arguments.callee);
});
return p;
}
}
20 changes: 20 additions & 0 deletions examples/push-vs-length.js
@@ -0,0 +1,20 @@
#!/usr/bin/env node-bench

var p = [], l = [], pi = 0, li = 0, c = 100000;

exports.compare = {
push : function () {
if (pi === c) {
p = [];
pi = 0;
}
p.push(pi ++);
},
length : function () {
if (li === c) {
l = [];
li = 0;
}
l[l.length] = li ++;
}
}
17 changes: 6 additions & 11 deletions lib/bench.js
Expand Up @@ -9,15 +9,14 @@ exports.show = show;

var events = require("events");

function run (fn, time, countPerLap) {
function run (fn, time, countPerLap, cb) {
time = time || exports.TIME;
countPerLap = countPerLap || exports.COUNT_PER_LAP;
var runCount = 0,
go = true,
start = Date.now(),
stopTime = start + time,
now = start,
p = new events.Promise(),
stepsPerLap = exports.STEPS_PER_LAP,
useCallback = (fn.length === 1);

Expand All @@ -32,13 +31,11 @@ function run (fn, time, countPerLap) {
};
function done () {
var did = runCount / (Date.now() - start);
p.emitSuccess(did);
cb(null, did);
};
function noop () { runCount ++ };
function runner () {
var ret = fn(lap);
if (useCallback) return;
if (ret) return ret.addCallback(lap);
if (useCallback) return fn(lap);
// blocking case.
for (var i = 0; i < stepsPerLap; i ++) fn();
runCount += countPerLap * (stepsPerLap - 1);
Expand All @@ -49,7 +46,7 @@ function run (fn, time, countPerLap) {
return p;
};

function compare (set, compareCount, time, countPerLap) {
function compare (set, compareCount, time, countPerLap, cb) {
compareCount = compareCount || exports.COMPARE_COUNT;
time = time || exports.TIME;
countPerLap = countPerLap || exports.COUNT_PER_LAP;
Expand All @@ -62,14 +59,13 @@ function compare (set, compareCount, time, countPerLap) {
tests.push({name:i, fn:set[i]});
results.push([]);
}
var p = new events.Promise;
(function comparer (currentCompare) {
if (currentCompare <= (compareCount / testCount)) {
var testOrder = randomArray(testCount);
(function comparerInner (i) {
if (i < testCount) {
var current = testOrder[i];
run(tests[current].fn, time, countPerLap).addCallback(function (data) {
run(tests[current].fn, time, countPerLap, function (er, data) {
results[current].push(data);
comparerInner(i+1);
})
Expand All @@ -82,10 +78,9 @@ function compare (set, compareCount, time, countPerLap) {
for (var i = 0; i < testCount; i ++) {
ret[ tests[i].name ] = results[i];
}
p.emitSuccess(ret);
cb(null, ret);
}
})(0);
return p;
};

// http://rosettacode.org/wiki/Knuth_shuffle#JavaScript
Expand Down
4 changes: 2 additions & 2 deletions lib/cli-wrapper.js
Expand Up @@ -26,7 +26,7 @@ bench.compare(
test.compare,
test.compareCount || bench.COMPARE_COUNT,
test.time || bench.TIME,
test.countPerLap || bench.COUNT_PER_LAP
).addCallback(function (data) { (test.done || bench.show)(data) });
test.countPerLap || bench.COUNT_PER_LAP,
function (er, data) { (test.done || bench.show)(data) });


0 comments on commit 11fab3c

Please sign in to comment.