diff options
author | Szalay Kristóf <32012862+littletof@users.noreply.github.com> | 2020-06-03 19:45:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-03 13:45:37 -0400 |
commit | cab273476a4ff725d53a8e41bf3f8a90060236d4 (patch) | |
tree | 28b7f7c180dbdde32e65c77058147e77fe09eab7 /std/testing/bench.ts | |
parent | 4ef38bad4340b5c8100ec2522264c57f0522aa2d (diff) |
fix(std/testing/bench): clock assertions without --allow-hrtime (#6069)
Diffstat (limited to 'std/testing/bench.ts')
-rw-r--r-- | std/testing/bench.ts | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/std/testing/bench.ts b/std/testing/bench.ts index 1d27040f5..02e18bbda 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 |