summaryrefslogtreecommitdiff
path: root/benchmark/streams/writable-manywrites.js
blob: e6ab65162c366cf008bb178a12476d22fc8d332e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'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'],
  len: [1024, 32 * 1024],
});

function main({ n, sync, writev, callback, len }) {
  const b = Buffer.allocUnsafe(len);
  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();
}