mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
85ab4a5f12
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>
79 lines
1.6 KiB
JavaScript
79 lines
1.6 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 bench = common.createBenchmark(main, {
|
|
dur: [5],
|
|
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 = Buffer.alloc(size, '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');
|
|
}
|
|
}
|