Permalink
Browse files

Style and bin fixes

  • Loading branch information...
1 parent a785b0f commit 9352bb81e529e6fe6ed20135203e692e22e84968 @isaacs committed Jul 17, 2011
Showing with 123 additions and 115 deletions.
  1. 0 examples/with-bench/with-vs-func.js
  2. +101 −93 lib/bench.js
  3. +22 −22 lib/cli-wrapper.js
View
No changes.
View
@@ -1,15 +1,17 @@
-exports.TIME = 1000; // how many ms to run tests for.
-exports.COMPARE_COUNT = 4; // default number of runs for comparisons.
-exports.COUNT_PER_LAP = 1;
-exports.STEPS_PER_LAP = 1000;
-exports.run = run;
-exports.compare = compare;
-exports.show = show;
+exports.TIME = 1000 // how many ms to run tests for.
+exports.COMPARE_COUNT = 4 // default number of runs for comparisons.
+exports.COUNT_PER_LAP = 1
+exports.STEPS_PER_LAP = 1000
+exports.run = run
+exports.compare = compare
+exports.show = show
exports.runMain = runMain
function runMain () {
var test = require.main.exports
+
+ if (!test) return
if (test === require("./cli-wrapper")) return
console.log("benchmarking "+require.main.filename+"\nPlease be patient.")
@@ -24,147 +26,151 @@ function runMain () {
)
}
-var events = require("events"),
- sys = require("sys");
+var events = require("events")
+ , sys = require("sys")
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,
- stepsPerLap = exports.STEPS_PER_LAP,
- useCallback = (fn.length === 1);
-
+ time = time || exports.TIME
+ countPerLap = countPerLap || exports.COUNT_PER_LAP
+ var runCount = 0
+ , go = true
+ , start = Date.now()
+ , stopTime = start + time
+ , now = start
+ , stepsPerLap = exports.STEPS_PER_LAP
+ , useCallback = (fn.length === 1)
+
if (fn.length > 1) throw new Error(
"Please read the instructions. Test functions should take 0 or 1 arguments."
- );
-
+ )
+
function lap () {
- runCount += countPerLap;
- if (Date.now() > stopTime) return done();
- process.nextTick(runner);
- };
+ runCount += countPerLap
+ if (Date.now() > stopTime) return done()
+ process.nextTick(runner)
+ }
function done () {
- var did = runCount / (Date.now() - start);
- cb(null, did);
- };
- function noop () { runCount ++ };
+ var did = runCount / (Date.now() - start)
+ cb(null, did)
+ }
+ function noop () { runCount ++ }
function runner () {
- if (useCallback) return fn(lap);
+ if (useCallback) return fn(lap)
// blocking case.
- for (var i = 0; i < stepsPerLap; i ++) fn();
- runCount += countPerLap * (stepsPerLap - 1);
- lap();
- };
- runner();
-};
+ for (var i = 0; i < stepsPerLap; i ++) fn()
+ runCount += countPerLap * (stepsPerLap - 1)
+ lap()
+ }
+ runner()
+}
function compare (set, compareCount, time, countPerLap, cb) {
- compareCount = compareCount || exports.COMPARE_COUNT;
- time = time || exports.TIME;
- countPerLap = countPerLap || exports.COUNT_PER_LAP;
-
- var tests = [],
- results = [],
- testCount = 0;
+ compareCount = compareCount || exports.COMPARE_COUNT
+ time = time || exports.TIME
+ countPerLap = countPerLap || exports.COUNT_PER_LAP
+
+ var tests = []
+ , results = []
+ , testCount = 0
+
for (var i in set) {
- testCount ++;
- tests.push({name:i, fn:set[i]});
- results.push([]);
+ testCount ++
+ tests.push({name:i, fn:set[i]})
+ results.push([])
}
- (function comparer (currentCompare) {
+
+ ;(function comparer (currentCompare) {
if (currentCompare <= compareCount) {
- var testOrder = randomArray(testCount);
- (function comparerInner (i) {
+ var testOrder = randomArray(testCount)
+ ;(function comparerInner (i) {
if (i < testCount) {
- var current = testOrder[i];
+ var current = testOrder[i]
run(tests[current].fn, time, countPerLap, function (er, data) {
- results[current].push(data);
- comparerInner(i+1);
+ results[current].push(data)
+ comparerInner(i+1)
})
} else {
- process.nextTick(function () { comparer(currentCompare+1) });
+ process.nextTick(function () { comparer(currentCompare+1) })
}
- })(0);
+ })(0)
} else {
- var ret = {};
+ var ret = {}
for (var i = 0; i < testCount; i ++) {
- ret[ tests[i].name ] = results[i];
+ ret[ tests[i].name ] = results[i]
}
- cb(null, ret);
+ cb(null, ret)
}
- })(0);
-};
+ })(0)
+}
// http://rosettacode.org/wiki/Knuth_shuffle#JavaScript
function knuth (a) {
- var n = a.length;
- var r, temp;
+ var n = a.length
+ var r, temp
while (n > 1) {
- r = Math.floor(n * Math.random());
- n--;
- temp = a[n];
- a[n] = a[r];
- a[r] = temp;
+ r = Math.floor(n * Math.random())
+ n--
+ temp = a[n]
+ a[n] = a[r]
+ a[r] = temp
}
- return a;
+ return a
}
function randomArray (l) {
- var a = [];
- while (l --> 0) a.push(l);
- return knuth(a);
+ var a = []
+ while (l --> 0) a.push(l)
+ return knuth(a)
}
-function print (m,cr) { sys.print(m+(cr===false?"":"\n")); return print };
+function print (m,cr) { sys.print(m+(cr===false?"":"\n")); return print }
function show (results) {
- var averages = [];
+ var averages = []
for (var i in results) {
- averages.push({name:i, avg:avg(results[i])});
+ averages.push({name:i, avg:avg(results[i])})
}
- averages.sort(sortScores);
-
- print("Scores: (bigger is better)\n");
+ averages.sort(sortScores)
+
+ print("Scores: (bigger is better)\n")
for (var i = 0, l = averages.length; i < l; i ++) {
- var res = results[ averages[i].name ];
+ var res = results[ averages[i].name ]
print
(averages[i].name)
("Raw:")
(" > "+res.join("\n > "))
("Average (mean) " + averages[i].avg)
- ("");
+ ("")
}
- var winner = averages.shift(), second = averages.shift(), loser = averages.pop();
-
+ var winner = averages.shift()
+ , second = averages.shift()
+ , loser = averages.pop()
+
print
("Winner: " + winner.name)
("Compared with next highest ("+second.name+"), it's:")
(pct(winner.avg, second.avg)+"% faster")
(times(winner.avg, second.avg)+" times as fast")
- (oom(winner.avg, second.avg)+" order(s) of magnitude faster");
+ (oom(winner.avg, second.avg)+" order(s) of magnitude faster")
if (loser) print
("\nCompared with the slowest ("+loser.name+"), it's:")
(pct(winner.avg, loser.avg)+"% faster")
(times(winner.avg, loser.avg)+" times as fast")
- (oom(winner.avg, loser.avg)+" order(s) of magnitude faster");
-
- print("");
- return;
-};
+ (oom(winner.avg, loser.avg)+" order(s) of magnitude faster")
+
+ print("")
+ return
+}
function sortScores (a, b) {
- return a.avg === b.avg ? 0 : a.avg > b.avg ? -1 : 1;
+ return a.avg === b.avg ? 0 : a.avg > b.avg ? -1 : 1
}
function pct (num1, num2) {
- return Math.round((1-(Math.min(num1, num2)/Math.max(num1, num2))) * 10000) / 100;
+ return Math.round((1-(Math.min(num1, num2)/Math.max(num1, num2))) * 10000) / 100
}
function times (num1, num2) {
- return Math.round((Math.max(num1, num2)/Math.min(num1, num2)) * 100) / 100;
+ return Math.round((Math.max(num1, num2)/Math.min(num1, num2)) * 100) / 100
}
function logX (n, x) {
return Math.log(n)/Math.log(x)
@@ -173,10 +179,12 @@ function oom (num1, num2) {
var l1 = logX(num1, 10)
, l2 = logX(num2, 10)
, diff = Math.abs( l1 - l2 )
+
return Math.round(diff * 100) / 100
}
function avg (nums) {
- var sum = 0;
- nums.forEach(function (n) { sum+=n });
- return sum/nums.length;
-};
+ var sum = 0
+
+ nums.forEach(function (n) { sum+=n })
+ return sum/nums.length
+}
View
@@ -1,35 +1,35 @@
+#!/usr/bin/env node
+
+module.exports = null
if (module !== require.main) return
-var path = require("path"),
- url = require("url"),
- sys = require("sys");
+var path = require("path")
+ , url = require("url")
+ , sys = require("sys")
-var args = process.argv.slice(0);
+var args = process.argv.slice(0)
-for (
- var arg = args.shift();
- arg !== __filename && path.basename(arg) !== "node-bench";
- arg = args.shift()
-);
+for ( var arg = args.shift()
+ ; arg !== __filename && path.basename(arg) !== "node-bench"
+ ; arg = args.shift() );
// strip the extension.
var test = url.resolve(process.cwd()+"/", path.join(
path.dirname(args[0]),
path.basename(args[0], path.extname(args[0]))
- )),
- bench = require("./bench");
-
-sys.puts("benchmarking "+test+"\nPlease be patient.");
-test = require(test);
-
-if (test.stepsPerLap) bench.STEPS_PER_LAP = test.stepsPerLap;
+ ))
+ , bench = require("./bench")
-bench.compare(
- test.compare,
- test.compareCount || bench.COMPARE_COUNT,
- test.time || bench.TIME,
- test.countPerLap || bench.COUNT_PER_LAP,
- function (er, data) { (test.done || bench.show)(data) });
+sys.puts("benchmarking "+test+"\nPlease be patient.")
+test = require(test)
+if (test.stepsPerLap) bench.STEPS_PER_LAP = test.stepsPerLap
+bench.compare
+ ( test.compare
+ , test.compareCount || bench.COMPARE_COUNT
+ , test.time || bench.TIME
+ , test.countPerLap || bench.COUNT_PER_LAP
+ , function (er, data) { (test.done || bench.show)(data) }
+ )

0 comments on commit 9352bb8

Please sign in to comment.