On this page

C

BenchContext

History

An instance of BenchContext is passed to every benchmark invocation. A new instance is created for every warmup and measured sample.

P

context.index

History
Attributes

The zero-based invocation index within the current context.phase. Warmup and measured samples have separate index sequences.

P

context.name

History
Attributes

The benchmark name.

P

context.params

History
Attributes

The benchmark's canonicalized parameter metadata.

P

context.phase

History
Attributes

The current sample phase. It is 'warmup' for an unreported warmup invocation and 'measurement' for a measured invocation.

P

context.signal

History
Attributes

An abort signal that is triggered when the benchmark is aborted, times out, or finishes.

M

context.start

History
context.start(): void

Starts the measured region using process.hrtime.bigint(). Calling start() more than once is an error.

M

context.end

History
context.end(operations, options?): Object
Attributes
operations:number
The number of completed operations. Must be a positive safe integer.
options:Object
detail:any
Additional structured-cloneable sample data. With CLI process isolation, it must also be supported by advanced child process serialization.
Returns:Object
The sample's operations, duration_ns, computed rate, and optional cloned detail.

Ends the measured region. The end timestamp is captured before operations is validated. Calling end() before start(), calling it more than once, or recording a zero-duration sample is an error. When provided, detail is cloned after the end timestamp is captured, so cloning time is outside the measured region.

M

context.record

History
context.record(sample): Object
Attributes
sample:Object
operations:number
The number of completed operations. Must be a positive safe integer.
duration_ns:bigint
An externally measured positive duration in nanoseconds no greater than Number.MAX_SAFE_INTEGER.
detail:any
Additional structured-cloneable sample data. With CLI process isolation, it must also be supported by advanced child process serialization.
Returns:Object
The normalized sample, including its computed rate and optional cloned detail.

Records a measurement made by another clock or execution environment. This is useful when a higher-level tool measures work in a worker and needs to exclude message transport from the duration. record() is mutually exclusive with start() and end() within one callback and must be called exactly once.

M

context.diagnostic

History
context.diagnostic(message, options?): undefined
Attributes
message:any
A structured-cloneable diagnostic value. With CLI process isolation, it must also be supported by advanced child process serialization.
options:Object
level?:string
Either 'info' or 'warning'. Default: 'info'.
detail:any
Additional structured-cloneable diagnostic data. With CLI process isolation, it must also be supported by advanced child process serialization.
Returns:undefined

Queues a diagnostic associated with the current benchmark, phase, and sample index. Multiple diagnostics preserve call order. They are emitted after the sample callback settles and before that sample's 'bench:sample' event. Warmup diagnostics are emitted even though warmup samples are not. Diagnostics queued before a callback failure are emitted before the failed 'bench:complete' event and do not themselves cause the benchmark to fail. If a timeout or abort wins before the callback settles, queued diagnostics might not be emitted.

The message and detail are cloned synchronously. Options are also validated synchronously. Calling diagnostic() between context.start() and context.end() therefore includes that work in the measured duration. Invalid arguments or an uncloneable message or detail violate the sample contract.

M

context.done

History
context.done(): void

Requests successful benchmark completion after the current measured sample. The callback must still call either start() and end(), or record(). Calling done() during a warmup invocation is an error. The configured samples value remains the maximum number of measured invocations if done() is not called.