mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
3000d77017
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>
49 lines
1.0 KiB
JavaScript
49 lines
1.0 KiB
JavaScript
'use strict';
|
|
const common = require('../common.js');
|
|
const { AsyncLocalStorage } = require('async_hooks');
|
|
|
|
/**
|
|
* This benchmark verifies the performance degradation of
|
|
* async resource propagation on the increasing number of
|
|
* active `AsyncLocalStorage`s.
|
|
*
|
|
* - AsyncLocalStorage.run()
|
|
* - Promise
|
|
* - Promise
|
|
* ...
|
|
* - Promise
|
|
*/
|
|
const bench = common.createBenchmark(main, {
|
|
storageCount: [0, 1, 10, 100],
|
|
n: [1e5],
|
|
});
|
|
|
|
function runStores(stores, value, cb, idx = 0) {
|
|
if (idx === stores.length) {
|
|
cb();
|
|
} else {
|
|
stores[idx].run(value, () => {
|
|
runStores(stores, value, cb, idx + 1);
|
|
});
|
|
}
|
|
}
|
|
|
|
async function runBenchmark(n) {
|
|
for (let i = 0; i < n; i++) {
|
|
// Avoid creating additional ticks.
|
|
await undefined;
|
|
}
|
|
}
|
|
|
|
function main({ n, storageCount }) {
|
|
const stores = new Array(storageCount).fill(0).map(() => new AsyncLocalStorage());
|
|
const contextValue = {};
|
|
|
|
runStores(stores, contextValue, () => {
|
|
bench.start();
|
|
runBenchmark(n).then(() => {
|
|
bench.end(n);
|
|
});
|
|
});
|
|
}
|