Skip to content

Commit 4f9d57d

Browse files
legendecascodebytere
authored andcommittedMar 14, 2020
benchmark: benchmarking impacts of async hooks on promises
PR-URL: #31188 Refs: nodejs/diagnostics#124 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent a987972 commit 4f9d57d

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed
 

‎benchmark/async_hooks/http-server.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ function main({ asyncHooks, connections }) {
2626
}
2727
}
2828
const server = require('../fixtures/simple-http-server.js')
29-
.listen(common.PORT)
30-
.on('listening', () => {
31-
const path = '/buffer/4/4/normal/1';
29+
.listen(common.PORT)
30+
.on('listening', () => {
31+
const path = '/buffer/4/4/normal/1';
3232

33-
bench.http({
34-
connections,
35-
path,
36-
}, () => {
37-
server.close();
33+
bench.http({
34+
connections,
35+
path,
36+
}, () => {
37+
server.close();
38+
});
3839
});
39-
});
4040
}

‎benchmark/async_hooks/promises.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const { createHook } = require('async_hooks');
4+
5+
const bench = common.createBenchmark(main, {
6+
n: [1e6],
7+
asyncHooks: [
8+
'enabled',
9+
'disabled',
10+
]
11+
});
12+
13+
async function run(n) {
14+
for (let i = 0; i < n; i++) {
15+
await new Promise((resolve) => resolve())
16+
.then(() => { throw new Error('foobar'); })
17+
.catch((e) => e);
18+
}
19+
}
20+
21+
function main({ n, asyncHooks }) {
22+
const hook = createHook({ promiseResolve() {} });
23+
if (asyncHooks !== 'disabled') {
24+
hook.enable();
25+
}
26+
bench.start();
27+
run(n).then(() => {
28+
bench.end(n);
29+
});
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.