diff options
author | isaacs <i@izs.me> | 2013-02-11 21:05:43 -0800 |
---|---|---|
committer | isaacs <i@izs.me> | 2013-02-19 14:14:36 -0800 |
commit | 1fc6f993402f9b0f5b291c104e6edf379d75ba21 (patch) | |
tree | 8da180aa2d3396c1bd799dcbce841f53d7c93eb8 /benchmark | |
parent | 6d116be7cf26abf3a4940e9e75ec329248de96d8 (diff) | |
download | node-new-1fc6f993402f9b0f5b291c104e6edf379d75ba21.tar.gz |
bench: Add read-stream throughput
Diffstat (limited to 'benchmark')
-rw-r--r-- | benchmark/fs/read-stream-throughput.js | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/benchmark/fs/read-stream-throughput.js b/benchmark/fs/read-stream-throughput.js new file mode 100644 index 0000000000..af9a235b91 --- /dev/null +++ b/benchmark/fs/read-stream-throughput.js @@ -0,0 +1,87 @@ +// 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 filesize = 1000 * 1024 * 1024; +var assert = require('assert'); + +var type, encoding, size; + +var bench = common.createBenchmark(main, { + type: ['buf', 'asc', 'utf'], + size: [1024, 4096, 65535, 1024*1024] +}); + +function main(conf) { + type = conf.type; + size = +conf.size; + + switch (type) { + case 'buf': + encoding = null; + break; + case 'asc': + encoding = 'ascii'; + break; + case 'utf': + encoding = 'utf8'; + break; + default: + throw new Error('invalid type'); + } + + makeFile(runTest); +} + +function runTest() { + assert(fs.statSync(filename).size === filesize); + var rs = fs.createReadStream(filename, { + bufferSize: size, + encoding: encoding + }); + + rs.on('open', function() { + bench.start(); + }); + + var bytes = 0; + rs.on('data', function(chunk) { + bytes += chunk.length; + }); + + rs.on('end', function() { + try { fs.unlinkSync(filename); } catch (e) {} + // MB/sec + bench.end(bytes / (1024 * 1024)); + }); +} + +function makeFile() { + var buf = new Buffer(filesize / 1024); + if (encoding === 'utf8') { + // ΓΌ + for (var i = 0; i < buf.length; i++) { + buf[i] = i % 2 === 0 ? 0xC3 : 0xBC; + } + } else if (encoding === 'ascii') { + buf.fill('a'); + } else { + buf.fill('x'); + } + + try { fs.unlinkSync(filename); } catch (e) {} + var w = 1024; + var ws = fs.createWriteStream(filename); + ws.on('close', runTest); + ws.on('drain', write); + write(); + function write() { + do { + w--; + } while (false !== ws.write(buf) && w > 0); + if (w === 0) + ws.end(); + } +} |