Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: benchmarking impacts of async hooks on promises #31188

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions benchmark/async_hooks/promises.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
const common = require('../common.js');

const bench = common.createBenchmark(main, {
n: [1e6],
method: [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: These aren't methods. They're descriptive strings, right? I don't have a better idea than method though, so maybe it's fine and if someone comes up with something better in the future, they can PR that in.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

tracking: [
  'enabled',
  'disabled'
]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(If we do make this change, which I like, we'll need to add tracking to test-benchmark-async-hooks.js.)

'trackingEnabled',
'trackingDisabled',
]
});

async function run(n) {
for (let i = 0; i < n; i++) {
await new Promise((resolve) => resolve())
.then(() => { throw new Error('foobar'); })
.catch((e) => e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

    await Promise.resolve()
      .then(() => { throw new Error('foobar'); })
      .catch((e) => e);

}
}

function main({ n, method }) {
const hook = require('async_hooks').createHook({ promiseResolve() {} });
switch (method) {
case 'trackingEnabled':
hook.enable();
lundibundi marked this conversation as resolved.
Show resolved Hide resolved
bench.start();
run(n).then(() => {
bench.end(n);
});
break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could fall through here after the hook.enable() call, if you like.

case 'trackingDisabled':
bench.start();
run(n).then(() => {
bench.end(n);
});
break;
default:
throw new Error(`Unsupported method "${method}"`);
}
}