Last active
August 29, 2015 14:04
-
-
Save oconnore/2d696978044b057e9b48 to your computer and use it in GitHub Desktop.
Benchmarking fs calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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