Skip to content

Instantly share code, notes, and snippets.

@oconnore oconnore/bench.js
Last active Aug 29, 2015

Embed
What would you like to do?
Benchmarking fs calls
var fs = require('fs');
function chmodFile(cb) {
fs.chmod('./hello', 0644, function(err) {
cb(err);
});
}
function statFile(cb) {
fs.stat('./hello', function(err, st) {
if ((st.mode & 07777) !== 0644) {
console.log('chmodding after stat', st.mode);
fs.chmod('./hello', 0644, function(err) {
cb(err);
});
} else {
cb(null);
}
});
}
function bench(func, times, cb) {
var i = 0;
var start = Date.now();
function next(err) {
if (!err) {
if (i < times) {
i++;
func(next);
} else {
var tm = Date.now();
cb(null, {
start: start,
end: tm,
secs: (tm - start) / 1000,
times: times
});
}
} else {
console.error('error is', err, err.stack);
cb(err);
}
}
func(next);
}
bench(chmodFile, 10000, function(err, times) {
if (err) {
console.error('err was', err, err.stack);
return;
}
console.log('chmod: time was:', times.secs, 'per call:', times.secs / times.times);
bench2();
});
function bench2() {
bench(statFile, 10000, function(err, times) {
if (err) {
console.error('err was', err, err.stack);
return;
}
console.log('stat: time was:', times.secs, 'per call:', times.secs / times.times);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.