11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common.js');
41cb0ef41Sopenharmony_ciconst assert = require('assert').ok;
51cb0ef41Sopenharmony_ciconst { performance } = require('perf_hooks');
61cb0ef41Sopenharmony_ciconst { nodeTiming, eventLoopUtilization } = performance;
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
91cb0ef41Sopenharmony_ci  n: [1e6],
101cb0ef41Sopenharmony_ci  method: [
111cb0ef41Sopenharmony_ci    'idleTime',
121cb0ef41Sopenharmony_ci    'ELU_simple',
131cb0ef41Sopenharmony_ci    'ELU_passed',
141cb0ef41Sopenharmony_ci  ],
151cb0ef41Sopenharmony_ci});
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cifunction main({ method, n }) {
181cb0ef41Sopenharmony_ci  switch (method) {
191cb0ef41Sopenharmony_ci    case 'idleTime':
201cb0ef41Sopenharmony_ci      benchIdleTime(n);
211cb0ef41Sopenharmony_ci      break;
221cb0ef41Sopenharmony_ci    case 'ELU_simple':
231cb0ef41Sopenharmony_ci      benchELUSimple(n);
241cb0ef41Sopenharmony_ci      break;
251cb0ef41Sopenharmony_ci    case 'ELU_passed':
261cb0ef41Sopenharmony_ci      benchELUPassed(n);
271cb0ef41Sopenharmony_ci      break;
281cb0ef41Sopenharmony_ci    default:
291cb0ef41Sopenharmony_ci      throw new Error(`Unsupported method ${method}`);
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_cifunction benchIdleTime(n) {
341cb0ef41Sopenharmony_ci  bench.start();
351cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++)
361cb0ef41Sopenharmony_ci    nodeTiming.idleTime; // eslint-disable-line no-unused-expressions
371cb0ef41Sopenharmony_ci  bench.end(n);
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cifunction benchELUSimple(n) {
411cb0ef41Sopenharmony_ci  // Need to put this in setImmediate or will always return 0.
421cb0ef41Sopenharmony_ci  setImmediate(() => {
431cb0ef41Sopenharmony_ci    const elu = eventLoopUtilization();
441cb0ef41Sopenharmony_ci    assert(elu.active + elu.idle > 0);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    bench.start();
471cb0ef41Sopenharmony_ci    for (let i = 0; i < n; i++)
481cb0ef41Sopenharmony_ci      eventLoopUtilization();
491cb0ef41Sopenharmony_ci    bench.end(n);
501cb0ef41Sopenharmony_ci  });
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_cifunction benchELUPassed(n) {
541cb0ef41Sopenharmony_ci  // Need to put this in setImmediate or will always return 0.
551cb0ef41Sopenharmony_ci  setImmediate(() => {
561cb0ef41Sopenharmony_ci    let elu = eventLoopUtilization();
571cb0ef41Sopenharmony_ci    assert(elu.active + elu.idle > 0);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci    bench.start();
601cb0ef41Sopenharmony_ci    for (let i = 0; i < n; i++)
611cb0ef41Sopenharmony_ci      elu = eventLoopUtilization(elu);
621cb0ef41Sopenharmony_ci    bench.end(n);
631cb0ef41Sopenharmony_ci  });
641cb0ef41Sopenharmony_ci}
65