1'use strict'; 2const common = require('../common.js'); 3const { AsyncLocalStorage } = require('async_hooks'); 4 5/** 6 * This benchmark verifies the performance of 7 * `AsyncLocalStorage.getStore()` on multiple `AsyncLocalStorage` instances 8 * nested `AsyncLocalStorage.run()`s. 9 * 10 * - AsyncLocalStorage1.run() 11 * - AsyncLocalStorage2.run() 12 * ... 13 * - AsyncLocalStorageN.run() 14 * - AsyncLocalStorage1.getStore() 15 */ 16const bench = common.createBenchmark(main, { 17 sotrageCount: [1, 10, 100], 18 n: [1e4], 19}); 20 21function runBenchmark(store, n) { 22 for (let idx = 0; idx < n; idx++) { 23 store.getStore(); 24 } 25} 26 27function runStores(stores, value, cb, idx = 0) { 28 if (idx === stores.length) { 29 cb(); 30 } else { 31 stores[idx].run(value, () => { 32 runStores(stores, value, cb, idx + 1); 33 }); 34 } 35} 36 37function main({ n, sotrageCount }) { 38 const stores = new Array(sotrageCount).fill(0).map(() => new AsyncLocalStorage()); 39 const contextValue = {}; 40 41 runStores(stores, contextValue, () => { 42 bench.start(); 43 runBenchmark(stores[0], n); 44 bench.end(n); 45 }); 46} 47