• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3const { createHook } = require('async_hooks');
4
5let hook;
6const tests = {
7  disabled() {
8    hook = createHook({
9      promiseResolve() {},
10    });
11  },
12  enabled() {
13    hook = createHook({
14      promiseResolve() {},
15    }).enable();
16  },
17  enabledWithDestroy() {
18    hook = createHook({
19      promiseResolve() {},
20      destroy() {},
21    }).enable();
22  },
23  enabledWithInitOnly() {
24    hook = createHook({
25      init() {},
26    }).enable();
27  },
28};
29
30const bench = common.createBenchmark(main, {
31  n: [1e6],
32  asyncHooks: [
33    'enabled',
34    'enabledWithDestroy',
35    'enabledWithInitOnly',
36    'disabled',
37  ],
38});
39
40const err = new Error('foobar');
41async function run(n) {
42  for (let i = 0; i < n; i++) {
43    await new Promise((resolve) => resolve())
44      .then(() => { throw err; })
45      .catch((e) => e);
46  }
47}
48
49function main({ n, asyncHooks }) {
50  if (hook) hook.disable();
51  tests[asyncHooks]();
52  bench.start();
53  run(n).then(() => {
54    bench.end(n);
55  });
56}
57