0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/benchmark/fs/readfile.js
isaacs 4b80f217cd bench: Simplify duration arguments to benchmarks
For throughput benchmarks, run with just 5s durations rather than 1s and 3s.

For startup benchmark, run with just a single 1s duration, since it's very
consistent anyway.
2013-02-19 17:16:55 -08:00

49 lines
1.1 KiB
JavaScript

// Call fs.readFile over and over again really fast.
// Then see how many times it got called.
// Yes, this is a silly benchmark. Most benchmarks are silly.
var path = require('path');
var common = require('../common.js');
var filename = path.resolve(__dirname, '.removeme-benchmark-garbage');
var fs = require('fs');
var bench = common.createBenchmark(main, {
dur: [5],
len: [1024, 16 * 1024 * 1024],
concurrent: [1, 10]
});
function main(conf) {
var len = +conf.len;
try { fs.unlinkSync(filename); } catch (e) {}
var data = new Buffer(len);
data.fill('x');
fs.writeFileSync(filename, data);
data = null;
var reads = 0;
bench.start();
setTimeout(function() {
bench.end(reads);
try { fs.unlinkSync(filename); } catch (e) {}
}, +conf.dur * 1000);
function read() {
fs.readFile(filename, afterRead);
}
function afterRead(er, data) {
if (er)
throw er;
if (data.length !== len)
throw new Error('wrong number of bytes returned');
reads++;
read();
}
var cur = +conf.concurrent;
while (cur--) read();
}