1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 01:59:06 -05:00

fix(std/testing/bench): clock assertions without --allow-hrtime (#6069)

This commit is contained in:
Szalay Kristóf 2020-06-03 19:45:37 +02:00 committed by GitHub
parent 4ef38bad43
commit cab273476a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 10 deletions

View file

@ -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

View file

@ -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