node/benchmark/async_hooks/promises.js
Antoine du Hamel 997c47fba7
benchmark: add trailing commas in benchmark/async_hooks
PR-URL: https://github.com/nodejs/node/pull/46424
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2023-02-01 21:46:09 +00:00

57 lines
1023 B
JavaScript

'use strict';
const common = require('../common.js');
const { createHook } = require('async_hooks');
let hook;
const tests = {
disabled() {
hook = createHook({
promiseResolve() {},
});
},
enabled() {
hook = createHook({
promiseResolve() {},
}).enable();
},
enabledWithDestroy() {
hook = createHook({
promiseResolve() {},
destroy() {},
}).enable();
},
enabledWithInitOnly() {
hook = createHook({
init() {},
}).enable();
},
};
const bench = common.createBenchmark(main, {
n: [1e6],
asyncHooks: [
'enabled',
'enabledWithDestroy',
'enabledWithInitOnly',
'disabled',
],
});
const err = new Error('foobar');
async function run(n) {
for (let i = 0; i < n; i++) {
await new Promise((resolve) => resolve())
.then(() => { throw err; })
.catch((e) => e);
}
}
function main({ n, asyncHooks }) {
if (hook) hook.disable();
tests[asyncHooks]();
bench.start();
run(n).then(() => {
bench.end(n);
});
}