1'use strict'; 2const common = require('../common.js'); 3const { AsyncLocalStorage } = require('async_hooks'); 4 5/** 6 * This benchmark verifies the performance degradation of 7 * async resource propagation on the increasing number of 8 * active `AsyncLocalStorage`s. 9 * 10 * - AsyncLocalStorage.run() 11 * - Promise 12 * - Promise 13 * ... 14 * - Promise 15 */ 16const bench = common.createBenchmark(main, { 17 storageCount: [0, 1, 10, 100], 18 n: [1e5], 19}); 20 21function runStores(stores, value, cb, idx = 0) { 22 if (idx === stores.length) { 23 cb(); 24 } else { 25 stores[idx].run(value, () => { 26 runStores(stores, value, cb, idx + 1); 27 }); 28 } 29} 30 31async function runBenchmark(n) { 32 for (let i = 0; i < n; i++) { 33 // Avoid creating additional ticks. 34 await undefined; 35 } 36} 37 38function main({ n, storageCount }) { 39 const stores = new Array(storageCount).fill(0).map(() => new AsyncLocalStorage()); 40 const contextValue = {}; 41 42 runStores(stores, contextValue, () => { 43 bench.start(); 44 runBenchmark(n).then(() => { 45 bench.end(n); 46 }); 47 }); 48} 49