• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3const { createHook, AsyncResource } = require('async_hooks');
4
5const bench = common.createBenchmark(main, {
6  n: [1e6],
7  method: [
8    'trackingEnabled',
9    'trackingEnabledWithDestroyHook',
10    'trackingDisabled',
11  ]
12}, {
13  flags: ['--expose-gc']
14});
15
16function endAfterGC(n) {
17  setImmediate(() => {
18    global.gc();
19    setImmediate(() => {
20      bench.end(n);
21    });
22  });
23}
24
25function main({ n, method }) {
26  switch (method) {
27    case 'trackingEnabled':
28      bench.start();
29      for (let i = 0; i < n; i++) {
30        new AsyncResource('foobar');
31      }
32      endAfterGC(n);
33      break;
34    case 'trackingEnabledWithDestroyHook':
35      createHook({ destroy: () => {} }).enable();
36      bench.start();
37      for (let i = 0; i < n; i++) {
38        new AsyncResource('foobar');
39      }
40      endAfterGC(n);
41      break;
42    case 'trackingDisabled':
43      bench.start();
44      for (let i = 0; i < n; i++) {
45        new AsyncResource('foobar', { requireManualDestroy: true });
46      }
47      endAfterGC(n);
48      break;
49    default:
50      throw new Error(`Unsupported method "${method}"`);
51  }
52}
53