diff --git a/testing/bench.ts b/testing/bench.ts index 02e18bbda..87be641ab 100644 --- a/testing/bench.ts +++ b/testing/bench.ts @@ -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 */ @@ -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; + // Summing up + totalMs += measuredMs; + // Adding partial result + progress.running.measuredRunsMs.push(measuredMs); + // Publish partial benchmark results + publishProgress(progress, ProgressState.BenchPartialResult, progressCb); - // 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; - } + // 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) { diff --git a/testing/bench_test.ts b/testing/bench_test.ts index 566aada6d..a56d4fd55 100644 --- a/testing/bench_test.ts +++ b/testing/bench_test.ts @@ -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" ); @@ -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(); }, @@ -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); @@ -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++];