summaryrefslogtreecommitdiff
path: root/benchmark/async_hooks/promises.js
blob: d60ae70192c8cb101cfad92895839f30586e70c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
const common = require('../common.js');
const { createHook } = require('async_hooks');

let hook;
const tests = {
  disabled() {
    hook = createHook({
      promiseResolve() {}
    });
  },
  enabled() {
    hook = createHook({
      promiseResolve() {}
    }).enable();
  },
  enabledWithDestroy() {
    hook = createHook({
      promiseResolve() {},
      destroy() {}
    }).enable();
  },
  enabledWithInitOnly() {
    hook = createHook({
      init() {}
    }).enable();
  }
};

const bench = common.createBenchmark(main, {
  n: [1e6],
  asyncHooks: [
    'enabled',
    'enabledWithDestroy',
    'enabledWithInitOnly',
    'disabled',
  ]
});

const err = new Error('foobar');
async function run(n) {
  for (let i = 0; i < n; i++) {
    await new Promise((resolve) => resolve())
      .then(() => { throw err; })
      .catch((e) => e);
  }
}

function main({ n, asyncHooks }) {
  if (hook) hook.disable();
  tests[asyncHooks]();
  bench.start();
  run(n).then(() => {
    bench.end(n);
  });
}