2017-01-03 00:37:26 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const Writable = require('stream').Writable;
|
|
|
|
|
|
|
|
const bench = common.createBenchmark(main, {
|
2019-11-29 02:04:46 +01:00
|
|
|
n: [2e6],
|
2019-12-23 11:43:36 +01:00
|
|
|
sync: ['yes', 'no'],
|
|
|
|
writev: ['yes', 'no'],
|
2020-04-16 21:49:41 +02:00
|
|
|
callback: ['yes', 'no'],
|
|
|
|
len: [1024, 32 * 1024]
|
2017-01-03 00:37:26 +01:00
|
|
|
});
|
|
|
|
|
2020-04-16 21:49:41 +02:00
|
|
|
function main({ n, sync, writev, callback, len }) {
|
|
|
|
const b = Buffer.allocUnsafe(len);
|
2017-01-03 00:37:26 +01:00
|
|
|
const s = new Writable();
|
2019-11-29 02:04:46 +01:00
|
|
|
sync = sync === 'yes';
|
2019-12-23 11:43:36 +01:00
|
|
|
|
|
|
|
const writecb = (cb) => {
|
2019-11-29 02:04:46 +01:00
|
|
|
if (sync)
|
|
|
|
cb();
|
|
|
|
else
|
|
|
|
process.nextTick(cb);
|
2017-01-03 00:37:26 +01:00
|
|
|
};
|
|
|
|
|
2019-12-23 11:43:36 +01:00
|
|
|
if (writev === 'yes') {
|
|
|
|
s._writev = (chunks, cb) => writecb(cb);
|
|
|
|
} else {
|
|
|
|
s._write = (chunk, encoding, cb) => writecb(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
const cb = callback === 'yes' ? () => {} : null;
|
|
|
|
|
2017-01-03 00:37:26 +01:00
|
|
|
bench.start();
|
2019-12-15 13:44:15 +01:00
|
|
|
|
|
|
|
let k = 0;
|
|
|
|
function run() {
|
2019-12-23 11:43:36 +01:00
|
|
|
while (k++ < n && s.write(b, cb));
|
2020-02-03 22:18:29 +01:00
|
|
|
if (k >= n) {
|
2019-12-15 13:44:15 +01:00
|
|
|
bench.end(n);
|
2020-02-03 22:18:29 +01:00
|
|
|
s.removeListener('drain', run);
|
|
|
|
}
|
2017-01-03 00:37:26 +01:00
|
|
|
}
|
2019-12-15 13:44:15 +01:00
|
|
|
s.on('drain', run);
|
|
|
|
run();
|
2017-01-03 00:37:26 +01:00
|
|
|
}
|