11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common.js');
41cb0ef41Sopenharmony_ciconst { randomInt } = require('crypto');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
71cb0ef41Sopenharmony_ci  mode: ['sync', 'async-sequential', 'async-parallel'],
81cb0ef41Sopenharmony_ci  min: [-(2 ** 47) + 1, -10_000, -100],
91cb0ef41Sopenharmony_ci  max: [100, 10_000, 2 ** 47],
101cb0ef41Sopenharmony_ci  n: [1e3, 1e5],
111cb0ef41Sopenharmony_ci});
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifunction main({ mode, min, max, n }) {
141cb0ef41Sopenharmony_ci  if (mode === 'sync') {
151cb0ef41Sopenharmony_ci    bench.start();
161cb0ef41Sopenharmony_ci    for (let i = 0; i < n; i++)
171cb0ef41Sopenharmony_ci      randomInt(min, max);
181cb0ef41Sopenharmony_ci    bench.end(n);
191cb0ef41Sopenharmony_ci  } else if (mode === 'async-sequential') {
201cb0ef41Sopenharmony_ci    bench.start();
211cb0ef41Sopenharmony_ci    (function next(i) {
221cb0ef41Sopenharmony_ci      if (i === n)
231cb0ef41Sopenharmony_ci        return bench.end(n);
241cb0ef41Sopenharmony_ci      randomInt(min, max, () => {
251cb0ef41Sopenharmony_ci        next(i + 1);
261cb0ef41Sopenharmony_ci      });
271cb0ef41Sopenharmony_ci    })(0);
281cb0ef41Sopenharmony_ci  } else {
291cb0ef41Sopenharmony_ci    bench.start();
301cb0ef41Sopenharmony_ci    let done = 0;
311cb0ef41Sopenharmony_ci    for (let i = 0; i < n; i++) {
321cb0ef41Sopenharmony_ci      randomInt(min, max, () => {
331cb0ef41Sopenharmony_ci        if (++done === n)
341cb0ef41Sopenharmony_ci          bench.end(n);
351cb0ef41Sopenharmony_ci      });
361cb0ef41Sopenharmony_ci    }
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci}
39