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}); 12 13function main({ n, sync, writev, callback }) { 14 const b = Buffer.allocUnsafe(1024); 15 const s = new Writable(); 16 sync = sync === 'yes'; 17 18 const writecb = (cb) => { 19 if (sync) 20 cb(); 21 else 22 process.nextTick(cb); 23 }; 24 25 if (writev === 'yes') { 26 s._writev = (chunks, cb) => writecb(cb); 27 } else { 28 s._write = (chunk, encoding, cb) => writecb(cb); 29 } 30 31 const cb = callback === 'yes' ? () => {} : null; 32 33 bench.start(); 34 35 let k = 0; 36 function run() { 37 while (k++ < n && s.write(b, cb)); 38 if (k >= n) { 39 bench.end(n); 40 s.removeListener('drain', run); 41 } 42 } 43 s.on('drain', run); 44 run(); 45} 46