• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common.js');
4const { Worker, parentPort } = require('worker_threads');
5
6if (process.argv[2] === 'idle cats') {
7  return parentPort.once('message', () => {});
8}
9
10const bench = common.createBenchmark(main, {
11  n: [1e6],
12  method: [
13    'ELU_simple',
14    'ELU_passed',
15  ],
16});
17
18function main({ method, n }) {
19  switch (method) {
20    case 'ELU_simple':
21      benchELUSimple(n);
22      break;
23    case 'ELU_passed':
24      benchELUPassed(n);
25      break;
26    default:
27      throw new Error(`Unsupported method ${method}`);
28  }
29}
30
31function benchELUSimple(n) {
32  const worker = new Worker(__filename, { argv: ['idle cats'] });
33
34  spinUntilIdle(worker, () => {
35    bench.start();
36    for (let i = 0; i < n; i++)
37      worker.performance.eventLoopUtilization();
38    bench.end(n);
39    worker.postMessage('bye');
40  });
41}
42
43function benchELUPassed(n) {
44  const worker = new Worker(__filename, { argv: ['idle cats'] });
45
46  spinUntilIdle(worker, () => {
47    let elu = worker.performance.eventLoopUtilization();
48    bench.start();
49    for (let i = 0; i < n; i++)
50      elu = worker.performance.eventLoopUtilization(elu);
51    bench.end(n);
52    worker.postMessage('bye');
53  });
54}
55
56function spinUntilIdle(w, cb) {
57  const t = w.performance.eventLoopUtilization();
58  if (t.idle + t.active > 0)
59    return process.nextTick(cb);
60  setTimeout(() => spinUntilIdle(w, cb), 1);
61}
62