mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
d1229eeca4
PR-URL: https://github.com/nodejs/node/pull/48528 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
49 lines
1.1 KiB
JavaScript
49 lines
1.1 KiB
JavaScript
'use strict';
|
|
const common = require('../common.js');
|
|
const { AsyncLocalStorage, AsyncResource } = require('async_hooks');
|
|
|
|
/**
|
|
* This benchmark verifies the performance of
|
|
* `AsyncLocalStorage.getStore()` on propagation through async
|
|
* resource scopes.
|
|
*
|
|
* - AsyncLocalStorage.run()
|
|
* - AsyncResource.runInAsyncScope
|
|
* - AsyncResource.runInAsyncScope
|
|
* ...
|
|
* - AsyncResource.runInAsyncScope
|
|
* - AsyncLocalStorage.getStore()
|
|
*/
|
|
const bench = common.createBenchmark(main, {
|
|
resourceCount: [10, 100, 1000],
|
|
n: [5e5],
|
|
});
|
|
|
|
function runBenchmark(store, n) {
|
|
for (let i = 0; i < n; i++) {
|
|
store.getStore();
|
|
}
|
|
}
|
|
|
|
function runInAsyncScopes(resourceCount, cb, i = 0) {
|
|
if (i === resourceCount) {
|
|
cb();
|
|
} else {
|
|
const resource = new AsyncResource('noop');
|
|
resource.runInAsyncScope(() => {
|
|
runInAsyncScopes(resourceCount, cb, i + 1);
|
|
});
|
|
}
|
|
}
|
|
|
|
function main({ n, resourceCount }) {
|
|
const store = new AsyncLocalStorage();
|
|
store.run({}, () => {
|
|
runInAsyncScopes(resourceCount, () => {
|
|
bench.start();
|
|
runBenchmark(store, n);
|
|
bench.end(n);
|
|
});
|
|
});
|
|
}
|