-
Notifications
You must be signed in to change notification settings - Fork 535
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
feat(benchmark infra) Add beforeEachBatchAsync callback #23391
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC there's a pattern somewhere in the benchmark package on how to use the sync VS async functions. I'm not sure I agree with just replacing beforeEachBatch
with beforeEachBatchAsync
withing runBenchmarkAsync
is the right way to go. If the beforeEachBatch function is not async but the benchmarkFn is, I think it's still valid to use sync beforeEachBatch inside runBenchmarkAsync, unless we rewrite the contracts and the docs a bit, so BenchmarkRunningOptionsAsync
can only take the async version of everything.
Depending on the final way we go, we might need to document this as a breaking change for the package (bump version to 0.52 and add an entry to CHANGELOG.md). Related: I think somewhere I might have a branch with the updated CHANGELOG that includes the changes in 0.51, I'll see if I can find it.
You're so right! I thought about this briefly then promptly forgot. Should I run both beforeEachBatch's in the async case...? |
May have the same problem where existing calls with sync beforeEachBatch + benchmarkFnAsync won't work. Or maybe it does, but types don't block the converse.
🔗 No broken links found! ✅ Your attention to detail is admirable. linkcheck output
|
batches++; | ||
}, | ||
benchmarkFnAsync: async (): Promise<void> => { | ||
assert(batches > 0, "beforeEachBatchAsync should be called before test body"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert(batches > 0, "beforeEachBatchAsync should be called before test body"); | |
assert(batches > 0, "beforeEachBatch should be called before test body"); |
iterations++; | ||
}, | ||
after: () => { | ||
// Restore to actual batch count for 'after' hook logic |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Restore to actual batch count for 'after' hook logic | |
// Restore to actual batch count for 'afterEach' hook logic which cares about the number of batches that ran |
batches++; | ||
}, | ||
benchmarkFn: (): void => { | ||
assert(batches > 0, "beforeEachBatchAsync should be called before test body"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert(batches > 0, "beforeEachBatchAsync should be called before test body"); | |
assert(batches > 0, "beforeEachBatch should be called before test body"); |
* | ||
* @public | ||
*/ | ||
export interface OnBatchAsync { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OnBatch
and OnBatchAsync
leave me a bit confused. Is it strictly necessary to have beforeEachBatchAsync?: never;
in the former? And should beforeEachBatch?: () => void;
really be deprecated on the latter? Seems like we allow both to be defined (since we call both in the "synthetic" beforeAfterBatch
we craft inside validateBenchmarkArguments
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key feature is to block synchronous benchmark with asynchronous beforeEachBatch. I tried a number of strategies, this was the best I came up with.
If you can see a simpler way to keep the tests passing, I'm all for it. Or maybe that requirement I held to isn't worth it? Seems like it to me but open to discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I deprecated the one in the Async type because it's unnecessary, as you pointed out. Later someone could migrate usages to the new Async function and remove that one.
export type BenchmarkRunningOptionsSync = BenchmarkSyncArguments; | ||
|
||
export type BenchmarkRunningOptionsAsync = BenchmarkAsyncArguments & | ||
BenchmarkTimingOptions & | ||
OnBatch; | ||
export type BenchmarkRunningOptionsAsync = BenchmarkAsyncArguments; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: these two feel redundant now, maybe we should remove them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine with me. @CraigMacomber ?
While I'm not generally opposed to this, I'd like to point out a couple things:
|
beforeEachBatchAsync?: never; | ||
} | ||
|
||
// @public | ||
export interface OnBatchAsync { | ||
// @deprecated | ||
beforeEachBatch?: () => void; | ||
beforeEachBatchAsync?: () => Promise<void>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems unnecessary complex. While we have to make the actual function being timed clearly sync or async to avoid the over head of checking which each iteration, I don't see an issue with making beforeEachBatch?: () => void;
just typed as beforeEachBatch?: () => void | promise<unknown>;
then just awaiting what ever it returns unconditionally.
That should keep the API identical, except that async beforeEachBatch will work. This is similar to how mocha deals with hooks: same API for sync and async.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's how I started. I was bothered by the lack of compiler error when passing args with synchronous benchmarkFn by async beforeEachBatch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the implementation, I guess that would be an issue with how a few things are currently factored, like runBenchmarkSync
, so some of this refactoring might be needed to support async beforeEachBatch.
Description
Fixes AB#19814
This adds an async version of
beforeEachBatch
, to allow awaiting async code before running a batch of benchmark iterations.