1'use strict'; 2 3const common = require('../common'); 4const Writable = require('stream').Writable; 5 6const bench = common.createBenchmark(main, { 7 n: [2e6], 8 sync: ['yes', 'no'], 9 writev: ['yes', 'no'], 10 callback: ['yes', 'no'], 11 len: [1024, 32 * 1024] 12}); 13 14function main({ n, sync, writev, callback, len }) { 15 const b = Buffer.allocUnsafe(len); 16 const s = new Writable(); 17 sync = sync === 'yes'; 18 19 const writecb = (cb) => { 20 if (sync) 21 cb(); 22 else 23 process.nextTick(cb); 24 }; 25 26 if (writev === 'yes') { 27 s._writev = (chunks, cb) => writecb(cb); 28 } else { 29 s._write = (chunk, encoding, cb) => writecb(cb); 30 } 31 32 const cb = callback === 'yes' ? () => {} : null; 33 34 bench.start(); 35 36 let k = 0; 37 function run() { 38 while (k++ < n && s.write(b, cb)); 39 if (k >= n) { 40 bench.end(n); 41 s.removeListener('drain', run); 42 } 43 } 44 s.on('drain', run); 45 run(); 46} 47