From cab273476a4ff725d53a8e41bf3f8a90060236d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szalay=20Krist=C3=B3f?= <32012862+littletof@users.noreply.github.com> Date: Wed, 3 Jun 2020 19:45:37 +0200 Subject: [PATCH] fix(std/testing/bench): clock assertions without --allow-hrtime (#6069) --- std/testing/bench.ts | 29 ++++++++++++++++++++--------- std/testing/bench_test.ts | 2 +- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/std/testing/bench.ts b/std/testing/bench.ts index 1d27040f54..02e18bbda8 100644 --- a/std/testing/bench.ts +++ b/std/testing/bench.ts @@ -6,6 +6,7 @@ const { noColor } = Deno; interface BenchmarkClock { start: number; stop: number; + for?: string; } /** Provides methods for starting and stopping a benchmark clock. */ @@ -108,22 +109,22 @@ function verifyOr1Run(runs?: number): number { return runs && runs >= 1 && runs !== Infinity ? Math.floor(runs) : 1; } -function assertTiming(clock: BenchmarkClock, benchmarkName: string): void { +function assertTiming(clock: BenchmarkClock): void { // NaN indicates that a benchmark has not been timed properly if (!clock.stop) { throw new BenchmarkRunError( - `Running benchmarks FAILED during benchmark named [${benchmarkName}]. The benchmark timer's stop method must be called`, - benchmarkName + `Running benchmarks FAILED during benchmark named [${clock.for}]. The benchmark timer's stop method must be called`, + clock.for ); } else if (!clock.start) { throw new BenchmarkRunError( - `Running benchmarks FAILED during benchmark named [${benchmarkName}]. The benchmark timer's start method must be called`, - benchmarkName + `Running benchmarks FAILED during benchmark named [${clock.for}]. The benchmark timer's start method must be called`, + clock.for ); } else if (clock.start > clock.stop) { throw new BenchmarkRunError( - `Running benchmarks FAILED during benchmark named [${benchmarkName}]. The benchmark timer's start method must be called before its stop method`, - benchmarkName + `Running benchmarks FAILED during benchmark named [${clock.for}]. The benchmark timer's start method must be called before its stop method`, + clock.for ); } } @@ -134,6 +135,12 @@ function createBenchmarkTimer(clock: BenchmarkClock): BenchmarkTimer { clock.start = performance.now(); }, stop(): void { + if (isNaN(clock.start)) { + throw new BenchmarkRunError( + `Running benchmarks FAILED during benchmark named [${clock.for}]. The benchmark timer's start method must be called before its stop method`, + clock.for + ); + } clock.stop = performance.now(); }, }; @@ -223,6 +230,9 @@ export async function runBenchmarks( console.groupCollapsed(`benchmark ${name} ... `); } + // Provide the benchmark name for clock assertions + clock.for = name; + // Remove benchmark from queued const queueIndex = progress.queued.findIndex( (queued) => queued.name === name && queued.runsCount === runs @@ -242,7 +252,7 @@ export async function runBenchmarks( // b is a benchmark timer interfacing an unset (NaN) benchmark clock await func(b); // Making sure the benchmark was started/stopped properly - assertTiming(clock, name); + assertTiming(clock); // Calculate length of run const measuredMs = clock.stop - clock.start; @@ -263,7 +273,7 @@ export async function runBenchmarks( // b is a benchmark timer interfacing an unset (NaN) benchmark clock await func(b); // Making sure the benchmark was started/stopped properly - assertTiming(clock, name); + assertTiming(clock); // Calculate length of run const measuredMs = clock.stop - clock.start; @@ -319,6 +329,7 @@ export async function runBenchmarks( // Resetting the benchmark clock clock.start = clock.stop = NaN; + delete clock.for; } // Indicate finished running diff --git a/std/testing/bench_test.ts b/std/testing/bench_test.ts index e36451fb0d..566aada6d1 100644 --- a/std/testing/bench_test.ts +++ b/std/testing/bench_test.ts @@ -308,7 +308,7 @@ test({ ); assertEquals(resultOfMultiple.length, 1); assert(!!resultOfMultiple[0].measuredRunsMs); - assert(!!resultOfMultiple[0].measuredRunsAvgMs); + assert(!isNaN(resultOfMultiple[0].measuredRunsAvgMs!)); assertEquals(resultOfMultiple[0].measuredRunsMs!.length, 2); // The last progress should equal the final result from promise except the state property