summaryrefslogtreecommitdiff
path: root/std/testing
diff options
context:
space:
mode:
Diffstat (limited to 'std/testing')
-rw-r--r--std/testing/bench.ts29
-rw-r--r--std/testing/bench_test.ts2
2 files changed, 21 insertions, 10 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
diff --git a/std/testing/bench_test.ts b/std/testing/bench_test.ts
index e36451fb0..566aada6d 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