nodejs/benchmark/async_hooks/async-local-storage-getstore-nested-run.js
Chengzhong Wu 3000d77017
async_hooks: add async local storage propagation benchmarks
Add micro-benchmarks to verify the performance degradation related to
the number of active `AsyncLocalStorage`s.

With these benchmarks, trying to improve the async context propagation
to be an O(1) operation, which is an operation more frequent compared
to `asyncLocalStorage.run` and `asyncLocalStorage.getStore`.

PR-URL: https://github.com/nodejs/node/pull/46414
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
2023-02-03 21:12:17 +00:00

47 lines
1.1 KiB
JavaScript

'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);
});
}