From 2a64edb025b5b62315748faecd76f8382755fc34 Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 13 Feb 2013 10:42:23 -0800 Subject: bench: Add fs write stream throughput --- benchmark/fs/write-stream-throughput.js | 78 +++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 benchmark/fs/write-stream-throughput.js (limited to 'benchmark') diff --git a/benchmark/fs/write-stream-throughput.js b/benchmark/fs/write-stream-throughput.js new file mode 100644 index 0000000000..d37299ab38 --- /dev/null +++ b/benchmark/fs/write-stream-throughput.js @@ -0,0 +1,78 @@ +// test the througput of the fs.WriteStream class. + +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: [1, 3], + type: ['buf', 'asc', 'utf'], + size: [2, 1024, 65535, 1024 * 1024] +}); + +function main(conf) { + var dur = +conf.dur; + var type = conf.type; + var size = +conf.size; + var encoding; + + var chunk; + switch (type) { + case 'buf': + chunk = new Buffer(size); + chunk.fill('b'); + break; + case 'asc': + chunk = new Array(size + 1).join('a'); + encoding = 'ascii'; + break; + case 'utf': + chunk = new Array(Math.ceil(size/2) + 1).join('ΓΌ'); + encoding = 'utf8'; + break; + default: + throw new Error('invalid type'); + } + + try { fs.unlinkSync(filename); } catch (e) {} + + var started = false; + var ending = false; + var ended = false; + setTimeout(function() { + ending = true; + f.end(); + }, dur * 1000); + + var f = fs.createWriteStream(filename); + f.on('drain', write); + f.on('open', write); + f.on('close', done); + f.on('finish', function() { + ended = true; + var written = fs.statSync(filename).size / 1024; + try { fs.unlinkSync(filename); } catch (e) {} + bench.end(written / 1024); + }); + + + function write() { + // don't try to write after we end, even if a 'drain' event comes. + // v0.8 streams are so sloppy! + if (ending) + return; + + if (!started) { + started = true; + bench.start(); + } + + while (false !== f.write(chunk, encoding)); + } + + function done() { + if (!ended) + f.emit('finish'); + } +} -- cgit v1.2.3