0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/benchmark/streams/writable-manywrites.js
Brian White b862a0c6d6
benchmark: check for and fix multiple end()
PR-URL: https://github.com/nodejs/node/pull/31624
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2020-02-08 21:36:31 -05:00

46 lines
870 B
JavaScript

'use strict';
const common = require('../common');
const Writable = require('stream').Writable;
const bench = common.createBenchmark(main, {
n: [2e6],
sync: ['yes', 'no'],
writev: ['yes', 'no'],
callback: ['yes', 'no']
});
function main({ n, sync, writev, callback }) {
const b = Buffer.allocUnsafe(1024);
const s = new Writable();
sync = sync === 'yes';
const writecb = (cb) => {
if (sync)
cb();
else
process.nextTick(cb);
};
if (writev === 'yes') {
s._writev = (chunks, cb) => writecb(cb);
} else {
s._write = (chunk, encoding, cb) => writecb(cb);
}
const cb = callback === 'yes' ? () => {} : null;
bench.start();
let k = 0;
function run() {
while (k++ < n && s.write(b, cb));
if (k >= n) {
bench.end(n);
s.removeListener('drain', run);
}
}
s.on('drain', run);
run();
}