summaryrefslogtreecommitdiff
path: root/benchmark/async_hooks/async-local-storage-getstore-nested-run.js
blob: 5f8948f62eb13e601f322e0195046c4e31b4b4a0 (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 } = require('async_hooks');

/**
 * This benchmark verifies the performance of
 * `AsyncLocalStorage.getStore()` on multiple `AsyncLocalStorage` instances
 * nested `AsyncLocalStorage.run()`s.
 *
 * - AsyncLocalStorage1.run()
 *   - AsyncLocalStorage2.run()
 *    ...
 *      - AsyncLocalStorageN.run()
 *        - AsyncLocalStorage1.getStore()
 */
const bench = common.createBenchmark(main, {
  sotrageCount: [1, 10, 100],
  n: [1e4],
});

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

function runStores(stores, value, cb, idx = 0) {
  if (idx === stores.length) {
    cb();
  } else {
    stores[idx].run(value, () => {
      runStores(stores, value, cb, idx + 1);
    });
  }
}

function main({ n, sotrageCount }) {
  const stores = new Array(sotrageCount).fill(0).map(() => new AsyncLocalStorage());
  const contextValue = {};

  runStores(stores, contextValue, () => {
    bench.start();
    runBenchmark(stores[0], n);
    bench.end(n);
  });
}