0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 23:43:09 +01:00
nodejs/benchmark/fs/read-stream-throughput.js
James M Snell 85ab4a5f12 buffer: add .from(), .alloc() and .allocUnsafe()
Several changes:

* Soft-Deprecate Buffer() constructors
* Add `Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`
* Add `--zero-fill-buffers` command line option
* Add byteOffset and length to `new Buffer(arrayBuffer)` constructor
* buffer.fill('') previously had no effect, now zero-fills
* Update the docs

PR-URL: https://github.com/nodejs/node/pull/4682
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2016-03-16 08:34:02 -07:00

89 lines
1.8 KiB
JavaScript

// test the throughput of the fs.WriteStream class.
'use strict';
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, {
highWaterMark: 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 = Buffer.allocUnsafe(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();
}
}