Skip to content

Instantly share code, notes, and snippets.

@oconnore
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oconnore/2d696978044b057e9b48 to your computer and use it in GitHub Desktop.
Save oconnore/2d696978044b057e9b48 to your computer and use it in GitHub Desktop.
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