Skip to content

Commit 6e75254

Browse files
authored
fix(std/testing/bench): Make progress callback async (denoland#6175)
1 parent 6c21ba0 commit 6e75254

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

std/testing/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ runBenchmarks({ silent: true }, (p: BenchmarkRunProgress) => {
226226

227227
Registers a benchmark that will be run once `runBenchmarks` is called.
228228

229-
##### `runBenchmarks(opts?: BenchmarkRunOptions, progressCb?: (p: BenchmarkRunProgress) => void): Promise<BenchmarkRunResult>`
229+
##### `runBenchmarks(opts?: BenchmarkRunOptions, progressCb?: (p: BenchmarkRunProgress) => void | Promise<void>): Promise<BenchmarkRunResult>`
230230

231231
Runs all registered benchmarks serially. Filtering can be applied by setting
232232
`BenchmarkRunOptions.only` and/or `BenchmarkRunOptions.skip` to regular

std/testing/bench.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export function clearBenchmarks({
187187
*/
188188
export async function runBenchmarks(
189189
{ only = /[^\s]/, skip = /^\s*$/, silent }: BenchmarkRunOptions = {},
190-
progressCb?: (progress: BenchmarkRunProgress) => void
190+
progressCb?: (progress: BenchmarkRunProgress) => void | Promise<void>
191191
): Promise<BenchmarkRunResult> {
192192
// Filtering candidates by the "only" and "skip" constraint
193193
const benchmarks: BenchmarkDefinition[] = candidates.filter(
@@ -213,7 +213,7 @@ export async function runBenchmarks(
213213
};
214214

215215
// Publish initial progress data
216-
publishProgress(progress, ProgressState.BenchmarkingStart, progressCb);
216+
await publishProgress(progress, ProgressState.BenchmarkingStart, progressCb);
217217

218218
if (!silent) {
219219
console.log(
@@ -243,7 +243,7 @@ export async function runBenchmarks(
243243
// Init the progress of the running benchmark
244244
progress.running = { name, runsCount: runs, measuredRunsMs: [] };
245245
// Publish starting of a benchmark
246-
publishProgress(progress, ProgressState.BenchStart, progressCb);
246+
await publishProgress(progress, ProgressState.BenchStart, progressCb);
247247

248248
// Trying benchmark.func
249249
let result = "";
@@ -267,7 +267,11 @@ export async function runBenchmarks(
267267
// Adding partial result
268268
progress.running.measuredRunsMs.push(measuredMs);
269269
// Publish partial benchmark results
270-
publishProgress(progress, ProgressState.BenchPartialResult, progressCb);
270+
await publishProgress(
271+
progress,
272+
ProgressState.BenchPartialResult,
273+
progressCb
274+
);
271275

272276
// Resetting the benchmark clock
273277
clock.start = clock.stop = NaN;
@@ -288,7 +292,11 @@ export async function runBenchmarks(
288292
// Clear currently running
289293
delete progress.running;
290294
// Publish results of the benchmark
291-
publishProgress(progress, ProgressState.BenchResult, progressCb);
295+
await publishProgress(
296+
progress,
297+
ProgressState.BenchResult,
298+
progressCb
299+
);
292300
break;
293301
}
294302
}
@@ -317,7 +325,7 @@ export async function runBenchmarks(
317325
// Indicate finished running
318326
delete progress.queued;
319327
// Publish final result in Cb too
320-
publishProgress(progress, ProgressState.BenchmarkingEnd, progressCb);
328+
await publishProgress(progress, ProgressState.BenchmarkingEnd, progressCb);
321329

322330
if (!silent) {
323331
// Closing results
@@ -340,12 +348,12 @@ export async function runBenchmarks(
340348
return benchmarkRunResult;
341349
}
342350

343-
function publishProgress(
351+
async function publishProgress(
344352
progress: BenchmarkRunProgress,
345353
state: ProgressState,
346-
progressCb?: (progress: BenchmarkRunProgress) => void
347-
): void {
348-
progressCb && progressCb(cloneProgressWithState(progress, state));
354+
progressCb?: (progress: BenchmarkRunProgress) => void | Promise<void>
355+
): Promise<void> {
356+
progressCb && (await progressCb(cloneProgressWithState(progress, state)));
349357
}
350358

351359
function cloneProgressWithState(

std/testing/bench_test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,27 @@ test({
338338
},
339339
});
340340

341+
test({
342+
name: "async progressCallback",
343+
fn: async function (): Promise<void> {
344+
clearBenchmarks();
345+
dummyBench("single");
346+
347+
const asyncCallbacks = [];
348+
349+
await runBenchmarks({ silent: true }, (progress) => {
350+
return new Promise((resolve) => {
351+
queueMicrotask(() => {
352+
asyncCallbacks.push(progress);
353+
resolve();
354+
});
355+
});
356+
});
357+
358+
assertEquals(asyncCallbacks.length, 5);
359+
},
360+
});
361+
341362
function dummyBench(name: string, runs = 1): void {
342363
bench({
343364
name,

0 commit comments

Comments
 (0)