summaryrefslogtreecommitdiff
path: root/benchmark/async_hooks/async-local-storage-getstore-nested-resources.js
blob: 05b43af627a79e64d320dadf6e0ae7d8fee3fd70 (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
'use strict';
const common = require('../common.js');
const { AsyncLocalStorage, AsyncResource } = require('async_hooks');

/**
 * This benchmark verifies the performance of
 * `AsyncLocalStorage.getStore()` on propagation through async
 * resource scopes.
 *
 * - AsyncLocalStorage.run()
 *  - AsyncResource.runInAsyncScope
 *    - AsyncResource.runInAsyncScope
 *      ...
 *        - AsyncResource.runInAsyncScope
 *          - AsyncLocalStorage.getStore()
 */
const bench = common.createBenchmark(main, {
  resourceCount: [10, 100, 1000],
  n: [1e4],
});

function runBenchmark(store, n) {
  for (let i = 0; i < n; i++) {
    store.getStore();
  }
}

function runInAsyncScopes(resourceCount, cb, i = 0) {
  if (i === resourceCount) {
    cb();
  } else {
    const resource = new AsyncResource('noop');
    resource.runInAsyncScope(() => {
      runInAsyncScopes(resourceCount, cb, i + 1);
    });
  }
}

function main({ n, resourceCount }) {
  const store = new AsyncLocalStorage();
  runInAsyncScopes(resourceCount, () => {
    bench.start();
    runBenchmark(store, n);
    bench.end(n);
  });
}