lib: add context.diagnostic api to node:bench · nodejs/node@0a4c50e · GitHub
Skip to content

Commit 0a4c50e

Browse files
jasnelladuh95
authored andcommitted
lib: add context.diagnostic api to node:bench
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode PR-URL: #65631 Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent a97def7 commit 0a4c50e

9 files changed

Lines changed: 446 additions & 32 deletions

File tree

‎doc/api/bench.md‎

Lines changed: 34 additions & 4 deletions

‎lib/internal/bench_runner/benchmark.js‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,20 @@ class BenchContext {
245245
#endCalled = false;
246246
#index;
247247
#invalid = false;
248+
#onDiagnostic;
248249
#phase;
249250
#recordCalled = false;
250251
#sample = null;
251252
#startCalled = false;
252253
#startTime;
253254

254-
constructor(bench, signal, phase, index) {
255+
constructor(bench, signal, phase, index, onDiagnostic) {
255256
this.name = bench.name;
256257
this.params = bench.params;
257258
this.signal = signal;
258259
this.#phase = phase;
259260
this.#index = index;
261+
this.#onDiagnostic = onDiagnostic;
260262
}
261263

262264
get index() {
@@ -267,6 +269,29 @@ class BenchContext {
267269
return this.#phase;
268270
}
269271

272+
diagnostic(message, options = kEmptyObject) {
273+
if (this.#closed) {
274+
this.#invalid = true;
275+
throw new ERR_INVALID_STATE('benchmark sample is no longer active');
276+
}
277+
try {
278+
validateString(message, 'message');
279+
validateObject(options, 'options');
280+
const { detail, level = 'info' } = options;
281+
validateString(level, 'options.level');
282+
if (level !== 'info' && level !== 'warning') {
283+
throw new ERR_INVALID_ARG_VALUE(
284+
'options.level', level, "must be 'info' or 'warning'");
285+
}
286+
const diagnostic = { __proto__: null, level, message };
287+
if (detail !== undefined) diagnostic.detail = structuredClone(detail);
288+
this.#onDiagnostic(diagnostic);
289+
} catch (error) {
290+
this.#invalid = true;
291+
throw error;
292+
}
293+
}
294+
270295
start() {
271296
if (this.#closed) {
272297
this.#invalid = true;
@@ -402,7 +427,7 @@ class BenchContext {
402427
this.#closed = true;
403428
if (this.#invalid) {
404429
throw new ERR_INVALID_STATE(
405-
'benchmark sample violated the start()/end() contract');
430+
'benchmark sample violated the context method contract');
406431
}
407432
if (!this.#recordCalled && !this.#startCalled) {
408433
throw new ERR_INVALID_STATE(

‎lib/internal/bench_runner/cli.js‎

Lines changed: 41 additions & 2 deletions

0 commit comments

Comments
 (0)