Skip to content

Commit

Permalink
refactor(testing/bench): Remove differentiating on runs count (denola…
Browse files Browse the repository at this point in the history
  • Loading branch information
littletof authored and caspervonb committed Jan 31, 2021
1 parent 7f23c5e commit bfcb872
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 62 deletions.
100 changes: 41 additions & 59 deletions testing/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ export interface BenchmarkResult {
name: string;
/** The total time it took to run a given bechmark */
totalMs: number;
/** Times the benchmark was run in succession. Only defined if `runs` for this bench is greater than 1. */
runsCount?: number;
/** The average time of running the benchmark in milliseconds. Only defined if `runs` for this bench is greater than 1. */
measuredRunsAvgMs?: number;
/** The individual measurements in millisecond it took to run the benchmark. Only defined if `runs` for this bench is greater than 1. */
measuredRunsMs?: number[];
/** Times the benchmark was run in succession. */
runsCount: number;
/** The average time of running the benchmark in milliseconds. */
measuredRunsAvgMs: number;
/** The individual measurements in milliseconds it took to run the benchmark.*/
measuredRunsMs: number[];
}

/** Defines the result of a `runBenchmarks` call */
Expand Down Expand Up @@ -248,66 +248,48 @@ export async function runBenchmarks(
// Trying benchmark.func
let result = "";
try {
if (runs === 1) {
// Averaging runs
let pendingRuns = runs;
let totalMs = 0;

// Would be better 2 not run these serially
while (true) {
// b is a benchmark timer interfacing an unset (NaN) benchmark clock
await func(b);
// Making sure the benchmark was started/stopped properly
assertTiming(clock);

// Calculate length of run
const measuredMs = clock.stop - clock.start;

result = `${measuredMs}ms`;
// Adding one-time run to results
progress.results.push({ name, totalMs: measuredMs });
// Clear currently running
delete progress.running;
// Publish one-time run benchmark finish
publishProgress(progress, ProgressState.BenchResult, progressCb);
} else if (runs > 1) {
// Averaging runs
let pendingRuns = runs;
let totalMs = 0;

// Would be better 2 not run these serially
while (true) {
// b is a benchmark timer interfacing an unset (NaN) benchmark clock
await func(b);
// Making sure the benchmark was started/stopped properly
assertTiming(clock);

// Calculate length of run
const measuredMs = clock.stop - clock.start;

// Summing up
totalMs += measuredMs;
// Adding partial result
progress.running.measuredRunsMs.push(measuredMs);
// Publish partial benchmark results
publishProgress(
progress,
ProgressState.BenchPartialResult,
progressCb
);

// Resetting the benchmark clock
clock.start = clock.stop = NaN;
// Once all ran
if (!--pendingRuns) {
result = `${runs} runs avg: ${totalMs / runs}ms`;
// Adding result of multiple runs
progress.results.push({
name,
totalMs,
runsCount: runs,
measuredRunsAvgMs: totalMs / runs,
measuredRunsMs: progress.running.measuredRunsMs,
});
// Clear currently running
delete progress.running;
// Publish results of a multiple run benchmark
publishProgress(progress, ProgressState.BenchResult, progressCb);
break;
}
// Summing up
totalMs += measuredMs;
// Adding partial result
progress.running.measuredRunsMs.push(measuredMs);
// Publish partial benchmark results
publishProgress(progress, ProgressState.BenchPartialResult, progressCb);

// Resetting the benchmark clock
clock.start = clock.stop = NaN;
// Once all ran
if (!--pendingRuns) {
result =
runs == 1
? `${totalMs}ms`
: `${runs} runs avg: ${totalMs / runs}ms`;
// Adding results
progress.results.push({
name,
totalMs,
runsCount: runs,
measuredRunsAvgMs: totalMs / runs,
measuredRunsMs: progress.running.measuredRunsMs,
});
// Clear currently running
delete progress.running;
// Publish results of the benchmark
publishProgress(progress, ProgressState.BenchResult, progressCb);
break;
}
}
} catch (err) {
Expand Down
25 changes: 22 additions & 3 deletions testing/bench_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ test({
assertEquals(benchResult.filtered, 1);
assertEquals(benchResult.results.length, 5);

const resultWithSingleRunsFiltered = benchResult.results.filter(
({ name }) => name === "forDecrementX1e9"
);
assertEquals(resultWithSingleRunsFiltered.length, 1);

const resultWithSingleRuns = resultWithSingleRunsFiltered[0];
assert(!!resultWithSingleRuns.runsCount);
assert(!!resultWithSingleRuns.measuredRunsAvgMs);
assert(!!resultWithSingleRuns.measuredRunsMs);
assertEquals(resultWithSingleRuns.runsCount, 1);
assertEquals(resultWithSingleRuns.measuredRunsMs.length, 1);

const resultWithMultipleRunsFiltered = benchResult.results.filter(
({ name }) => name === "runs100ForIncrementX1e6"
);
Expand All @@ -83,7 +95,7 @@ test({
assert(!!resultWithMultipleRuns.measuredRunsAvgMs);
assert(!!resultWithMultipleRuns.measuredRunsMs);
assertEquals(resultWithMultipleRuns.runsCount, 100);
assertEquals(resultWithMultipleRuns.measuredRunsMs!.length, 100);
assertEquals(resultWithMultipleRuns.measuredRunsMs.length, 100);

clearBenchmarks();
},
Expand Down Expand Up @@ -263,6 +275,13 @@ test({
});
assertEquals(progress.results, []);

// Assert running result of bench "single"
progress = progressCallbacks[pc++];
assertEquals(progress.state, ProgressState.BenchPartialResult);
assertEquals(progress.queued.length, 1);
assertEquals(progress.running!.measuredRunsMs.length, 1);
assertEquals(progress.results.length, 0);

// Assert result of bench "single"
progress = progressCallbacks[pc++];
assertEquals(progress.state, ProgressState.BenchResult);
Expand Down Expand Up @@ -308,8 +327,8 @@ test({
);
assertEquals(resultOfMultiple.length, 1);
assert(!!resultOfMultiple[0].measuredRunsMs);
assert(!isNaN(resultOfMultiple[0].measuredRunsAvgMs!));
assertEquals(resultOfMultiple[0].measuredRunsMs!.length, 2);
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
progress = progressCallbacks[pc++];
Expand Down

0 comments on commit bfcb872

Please sign in to comment.