Skip to content

Commit

Permalink
fix(testing/bench): clock assertions without --allow-hrtime (denoland…
Browse files Browse the repository at this point in the history
  • Loading branch information
littletof authored and caspervonb committed Jan 24, 2021
1 parent 6e76fe9 commit 260ad48
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
29 changes: 20 additions & 9 deletions testing/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { noColor } = Deno;
interface BenchmarkClock {
start: number;
stop: number;
for?: string;
}

/** Provides methods for starting and stopping a benchmark clock. */
Expand Down Expand Up @@ -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
);
}
}
Expand All @@ -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();
},
};
Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -319,6 +329,7 @@ export async function runBenchmarks(

// Resetting the benchmark clock
clock.start = clock.stop = NaN;
delete clock.for;
}

// Indicate finished running
Expand Down
2 changes: 1 addition & 1 deletion testing/bench_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 260ad48

Please sign in to comment.