11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst Readable = require('stream').Readable;
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
71cb0ef41Sopenharmony_ci  n: [1e5],
81cb0ef41Sopenharmony_ci  sync: ['yes', 'no'],
91cb0ef41Sopenharmony_ci});
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciasync function main({ n, sync }) {
121cb0ef41Sopenharmony_ci  sync = sync === 'yes';
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  const s = new Readable({
151cb0ef41Sopenharmony_ci    objectMode: true,
161cb0ef41Sopenharmony_ci    read() {
171cb0ef41Sopenharmony_ci      if (sync) {
181cb0ef41Sopenharmony_ci        this.push(1);
191cb0ef41Sopenharmony_ci      } else {
201cb0ef41Sopenharmony_ci        process.nextTick(() => {
211cb0ef41Sopenharmony_ci          this.push(1);
221cb0ef41Sopenharmony_ci        });
231cb0ef41Sopenharmony_ci      }
241cb0ef41Sopenharmony_ci    },
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  bench.start();
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  let x = 0;
301cb0ef41Sopenharmony_ci  for await (const chunk of s) {
311cb0ef41Sopenharmony_ci    x += chunk;
321cb0ef41Sopenharmony_ci    if (x > n) {
331cb0ef41Sopenharmony_ci      break;
341cb0ef41Sopenharmony_ci    }
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  // Use x to ensure V8 does not optimize away the loop as a noop.
381cb0ef41Sopenharmony_ci  console.assert(x);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  bench.end(n);
411cb0ef41Sopenharmony_ci}
42