11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst Writable = require('stream').Writable;
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
71cb0ef41Sopenharmony_ci  n: [2e6],
81cb0ef41Sopenharmony_ci  sync: ['yes', 'no'],
91cb0ef41Sopenharmony_ci  writev: ['yes', 'no'],
101cb0ef41Sopenharmony_ci  callback: ['yes', 'no'],
111cb0ef41Sopenharmony_ci  len: [1024, 32 * 1024],
121cb0ef41Sopenharmony_ci});
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction main({ n, sync, writev, callback, len }) {
151cb0ef41Sopenharmony_ci  const b = Buffer.allocUnsafe(len);
161cb0ef41Sopenharmony_ci  const s = new Writable();
171cb0ef41Sopenharmony_ci  sync = sync === 'yes';
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  const writecb = (cb) => {
201cb0ef41Sopenharmony_ci    if (sync)
211cb0ef41Sopenharmony_ci      cb();
221cb0ef41Sopenharmony_ci    else
231cb0ef41Sopenharmony_ci      process.nextTick(cb);
241cb0ef41Sopenharmony_ci  };
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  if (writev === 'yes') {
271cb0ef41Sopenharmony_ci    s._writev = (chunks, cb) => writecb(cb);
281cb0ef41Sopenharmony_ci  } else {
291cb0ef41Sopenharmony_ci    s._write = (chunk, encoding, cb) => writecb(cb);
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  const cb = callback === 'yes' ? () => {} : null;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  bench.start();
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  let k = 0;
371cb0ef41Sopenharmony_ci  function run() {
381cb0ef41Sopenharmony_ci    while (k++ < n && s.write(b, cb));
391cb0ef41Sopenharmony_ci    if (k >= n) {
401cb0ef41Sopenharmony_ci      bench.end(n);
411cb0ef41Sopenharmony_ci      s.removeListener('drain', run);
421cb0ef41Sopenharmony_ci    }
431cb0ef41Sopenharmony_ci  }
441cb0ef41Sopenharmony_ci  s.on('drain', run);
451cb0ef41Sopenharmony_ci  run();
461cb0ef41Sopenharmony_ci}
47