From 317d2dba45d3ce9b8404ca26f13edcd1e74f00f1 Mon Sep 17 00:00:00 2001 From: legendecas Date: Thu, 2 Jan 2020 23:09:20 +0800 Subject: [PATCH] benchmark: benchmarking impacts of async hooks on promises PR-URL: https://github.com/nodejs/node/pull/31188 Refs: https://github.com/nodejs/diagnostics/issues/124 Reviewed-By: Anna Henningsen Reviewed-By: Denys Otrishko Reviewed-By: Rich Trott Reviewed-By: Ruben Bridgewater --- benchmark/async_hooks/http-server.js | 18 ++++++++--------- benchmark/async_hooks/promises.js | 30 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 benchmark/async_hooks/promises.js diff --git a/benchmark/async_hooks/http-server.js b/benchmark/async_hooks/http-server.js index 493500fd1f2d66..9e1c1214240eaa 100644 --- a/benchmark/async_hooks/http-server.js +++ b/benchmark/async_hooks/http-server.js @@ -26,15 +26,15 @@ function main({ asyncHooks, connections }) { } } const server = require('../fixtures/simple-http-server.js') - .listen(common.PORT) - .on('listening', () => { - const path = '/buffer/4/4/normal/1'; + .listen(common.PORT) + .on('listening', () => { + const path = '/buffer/4/4/normal/1'; - bench.http({ - connections, - path, - }, () => { - server.close(); + bench.http({ + connections, + path, + }, () => { + server.close(); + }); }); - }); } diff --git a/benchmark/async_hooks/promises.js b/benchmark/async_hooks/promises.js new file mode 100644 index 00000000000000..eb90ca0368e079 --- /dev/null +++ b/benchmark/async_hooks/promises.js @@ -0,0 +1,30 @@ +'use strict'; +const common = require('../common.js'); +const { createHook } = require('async_hooks'); + +const bench = common.createBenchmark(main, { + n: [1e6], + asyncHooks: [ + 'enabled', + 'disabled', + ] +}); + +async function run(n) { + for (let i = 0; i < n; i++) { + await new Promise((resolve) => resolve()) + .then(() => { throw new Error('foobar'); }) + .catch((e) => e); + } +} + +function main({ n, asyncHooks }) { + const hook = createHook({ promiseResolve() {} }); + if (asyncHooks !== 'disabled') { + hook.enable(); + } + bench.start(); + run(n).then(() => { + bench.end(n); + }); +}