11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common.js');
31cb0ef41Sopenharmony_ciconst { createHook, AsyncResource } = require('async_hooks');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
61cb0ef41Sopenharmony_ci  n: [1e6],
71cb0ef41Sopenharmony_ci  method: [
81cb0ef41Sopenharmony_ci    'trackingEnabled',
91cb0ef41Sopenharmony_ci    'trackingEnabledWithDestroyHook',
101cb0ef41Sopenharmony_ci    'trackingDisabled',
111cb0ef41Sopenharmony_ci  ],
121cb0ef41Sopenharmony_ci}, {
131cb0ef41Sopenharmony_ci  flags: ['--expose-gc'],
141cb0ef41Sopenharmony_ci});
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cifunction endAfterGC(n) {
171cb0ef41Sopenharmony_ci  setImmediate(() => {
181cb0ef41Sopenharmony_ci    global.gc();
191cb0ef41Sopenharmony_ci    setImmediate(() => {
201cb0ef41Sopenharmony_ci      bench.end(n);
211cb0ef41Sopenharmony_ci    });
221cb0ef41Sopenharmony_ci  });
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cifunction main({ n, method }) {
261cb0ef41Sopenharmony_ci  switch (method) {
271cb0ef41Sopenharmony_ci    case 'trackingEnabled':
281cb0ef41Sopenharmony_ci      bench.start();
291cb0ef41Sopenharmony_ci      for (let i = 0; i < n; i++) {
301cb0ef41Sopenharmony_ci        new AsyncResource('foobar');
311cb0ef41Sopenharmony_ci      }
321cb0ef41Sopenharmony_ci      endAfterGC(n);
331cb0ef41Sopenharmony_ci      break;
341cb0ef41Sopenharmony_ci    case 'trackingEnabledWithDestroyHook':
351cb0ef41Sopenharmony_ci      createHook({ destroy: () => {} }).enable();
361cb0ef41Sopenharmony_ci      bench.start();
371cb0ef41Sopenharmony_ci      for (let i = 0; i < n; i++) {
381cb0ef41Sopenharmony_ci        new AsyncResource('foobar');
391cb0ef41Sopenharmony_ci      }
401cb0ef41Sopenharmony_ci      endAfterGC(n);
411cb0ef41Sopenharmony_ci      break;
421cb0ef41Sopenharmony_ci    case 'trackingDisabled':
431cb0ef41Sopenharmony_ci      bench.start();
441cb0ef41Sopenharmony_ci      for (let i = 0; i < n; i++) {
451cb0ef41Sopenharmony_ci        new AsyncResource('foobar', { requireManualDestroy: true });
461cb0ef41Sopenharmony_ci      }
471cb0ef41Sopenharmony_ci      endAfterGC(n);
481cb0ef41Sopenharmony_ci      break;
491cb0ef41Sopenharmony_ci    default:
501cb0ef41Sopenharmony_ci      throw new Error(`Unsupported method "${method}"`);
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci}
53