-
-
Notifications
You must be signed in to change notification settings - Fork 55
feat: add timer-overhead correction, saturation warning, and resolution diagnostic #571
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
base: main
Are you sure you want to change the base?
Changes from all commits
6a71cb2
5477644
49e619e
2b1e3fe
e9f9fe4
dc5d765
a24e811
869e64e
b93d693
556d884
411c1d7
eac7e03
2a5a701
b7e7e42
ce3c2c4
2f8232a
a3b9a37
f9a0a6a
1376205
c53307d
32e0ee5
718cff9
42fd0be
0a221a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ import { BenchEvent } from './event' | |
| import { Task } from './task' | ||
| import { | ||
| assert, | ||
| calibrateTimerOverhead, | ||
| defaultConvertTaskResultForConsoleTable, | ||
| getTimestampProvider, | ||
| runtime, | ||
|
|
@@ -95,6 +96,16 @@ export class Bench extends EventTarget implements BenchLike { | |
| */ | ||
| readonly signal?: AbortSignal | ||
|
|
||
| /** | ||
| * Whether to subtract an estimated timestamp provider call overhead from | ||
| * each raw latency sample. | ||
| * | ||
| * Incompatible with `concurrency: 'task'`; the constraint is enforced | ||
| * at construction and at the start of {@link Bench.run}. | ||
| * @default false | ||
| */ | ||
| readonly subtractTimerOverhead: boolean | ||
|
|
||
| /** | ||
| * A teardown function that runs after each task execution. | ||
| */ | ||
|
|
@@ -120,6 +131,15 @@ export class Bench extends EventTarget implements BenchLike { | |
| */ | ||
| readonly time: number | ||
|
|
||
| /** | ||
| * The estimated cost of one timestamp provider call in milliseconds. | ||
| * | ||
| * `undefined` when {@link subtractTimerOverhead} is `false`. | ||
| * Otherwise calibrated once at construction time via | ||
| * {@link calibrateTimerOverhead}. | ||
| */ | ||
| readonly timerOverhead: number | undefined | ||
|
|
||
| /** | ||
| * A timestamp provider and its related functions. | ||
| */ | ||
|
|
@@ -195,6 +215,14 @@ export class Bench extends EventTarget implements BenchLike { | |
| this.throws = restOptions.throws ?? false | ||
| this.signal = restOptions.signal | ||
| this.retainSamples = restOptions.retainSamples === true | ||
| this.subtractTimerOverhead = restOptions.subtractTimerOverhead === true | ||
| assert( | ||
| !(this.subtractTimerOverhead && this.concurrency === 'task'), | ||
| '`subtractTimerOverhead` cannot be used with `concurrency: "task"` — set `concurrency` to `null` or `"bench"`, or disable `subtractTimerOverhead`' | ||
| ) | ||
|
jerome-benoit marked this conversation as resolved.
|
||
| this.timerOverhead = this.subtractTimerOverhead | ||
| ? calibrateTimerOverhead(this.timestampProvider) | ||
| : undefined | ||
|
|
||
| if (this.signal) { | ||
| this.signal.addEventListener( | ||
|
|
@@ -264,6 +292,10 @@ export class Bench extends EventTarget implements BenchLike { | |
| * @returns the tasks array | ||
| */ | ||
| async run (): Promise<Task[]> { | ||
| assert( | ||
| !(this.subtractTimerOverhead && this.concurrency === 'task'), | ||
| '`subtractTimerOverhead` cannot be used with `concurrency: "task"` — set `concurrency` to `null` or `"bench"`, or disable `subtractTimerOverhead`' | ||
|
Comment on lines
292
to
+297
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This invariant is only checked in Useful? React with 👍 / 👎. |
||
| ) | ||
|
Comment on lines
+295
to
+298
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fresh evidence beyond the earlier direct Useful? React with 👍 / 👎. |
||
| if (this.warmup) { | ||
| await this.#warmupTasks() | ||
| } | ||
|
|
||
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.
When a user copies this new direct-calibration example,
import { hrtimeNowTimestampProvider } from 'tinybench'fails because the package entry point only exportshrtimeNowand nothrtimeNowTimestampProviderfromsrc/index.ts. Either export the provider constant or change the example to use a publicTimestampProviderso the documented API is actually importable.Useful? React with 👍 / 👎.