diff options
Diffstat (limited to 'benchmark/async_hooks/async-local-storage-propagate-asyncresource.js')
-rw-r--r-- | benchmark/async_hooks/async-local-storage-propagate-asyncresource.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/benchmark/async_hooks/async-local-storage-propagate-asyncresource.js b/benchmark/async_hooks/async-local-storage-propagate-asyncresource.js new file mode 100644 index 0000000000..e5523452af --- /dev/null +++ b/benchmark/async_hooks/async-local-storage-propagate-asyncresource.js @@ -0,0 +1,46 @@ +'use strict'; +const common = require('../common.js'); +const { AsyncLocalStorage, AsyncResource } = require('async_hooks'); + +/** + * This benchmark verifies the performance degradation of + * async resource propagation on the increasing number of + * active `AsyncLocalStorage`s. + * + * - AsyncLocalStorage.run() * storageCount + * - new AsyncResource() + * - new AsyncResource() + * ... + * - N new Asyncresource() + */ +const bench = common.createBenchmark(main, { + storageCount: [0, 1, 10, 100], + n: [1e3], +}); + +function runStores(stores, value, cb, idx = 0) { + if (idx === stores.length) { + cb(); + } else { + stores[idx].run(value, () => { + runStores(stores, value, cb, idx + 1); + }); + } +} + +function runBenchmark(n) { + for (let i = 0; i < n; i++) { + new AsyncResource('noop'); + } +} + +function main({ n, storageCount }) { + const stores = new Array(storageCount).fill(0).map(() => new AsyncLocalStorage()); + const contextValue = {}; + + runStores(stores, contextValue, () => { + bench.start(); + runBenchmark(n); + bench.end(n); + }); +} |