diff options
Diffstat (limited to 'std/testing/bench_test.ts')
-rw-r--r-- | std/testing/bench_test.ts | 93 |
1 files changed, 89 insertions, 4 deletions
diff --git a/std/testing/bench_test.ts b/std/testing/bench_test.ts index 6dfc18b10..ef004807f 100644 --- a/std/testing/bench_test.ts +++ b/std/testing/bench_test.ts @@ -1,7 +1,11 @@ const { test } = Deno; -import { bench, runBenchmarks } from "./bench.ts"; - -import "./bench_example.ts"; +import { bench, runBenchmarks, BenchmarkRunError } from "./bench.ts"; +import { + assertEquals, + assert, + assertThrows, + assertThrowsAsync, +} from "./asserts.ts"; test({ name: "benching", @@ -57,6 +61,87 @@ test({ // Throws bc the timer's stop method is never called }); - await runBenchmarks({ skip: /throw/ }); + const benchResult = await runBenchmarks({ skip: /throw/ }); + + assertEquals(benchResult.measured, 5); + assertEquals(benchResult.filtered, 1); + assertEquals(benchResult.results.length, 5); + + const resultWithMultipleRunsFiltered = benchResult.results.filter( + (r) => r.name === "runs100ForIncrementX1e6" + ); + assertEquals(resultWithMultipleRunsFiltered.length, 1); + + const resultWithMultipleRuns = resultWithMultipleRunsFiltered[0]; + assert(!!resultWithMultipleRuns.runsCount); + assert(!!resultWithMultipleRuns.runsAvgMs); + assert(!!resultWithMultipleRuns.runsMs); + assertEquals(resultWithMultipleRuns.runsCount, 100); + assertEquals(resultWithMultipleRuns.runsMs!.length, 100); + }, +}); + +test({ + name: "benchWithoutName", + fn() { + assertThrows( + (): void => { + bench(() => {}); + }, + Error, + "The benchmark function must not be anonymous" + ); + }, +}); + +test({ + name: "benchWithoutStop", + fn: async function (): Promise<void> { + await assertThrowsAsync( + async (): Promise<void> => { + bench(function benchWithoutStop(b): void { + b.start(); + // Throws bc the timer's stop method is never called + }); + await runBenchmarks({ only: /benchWithoutStop/, silent: true }); + }, + BenchmarkRunError, + "The benchmark timer's stop method must be called" + ); + }, +}); + +test({ + name: "benchWithoutStart", + fn: async function (): Promise<void> { + await assertThrowsAsync( + async (): Promise<void> => { + bench(function benchWithoutStart(b): void { + b.stop(); + // Throws bc the timer's start method is never called + }); + await runBenchmarks({ only: /benchWithoutStart/, silent: true }); + }, + BenchmarkRunError, + "The benchmark timer's start method must be called" + ); + }, +}); + +test({ + name: "benchStopBeforeStart", + fn: async function (): Promise<void> { + await assertThrowsAsync( + async (): Promise<void> => { + bench(function benchStopBeforeStart(b): void { + b.stop(); + b.start(); + // Throws bc the timer's stop is called before start + }); + await runBenchmarks({ only: /benchStopBeforeStart/, silent: true }); + }, + BenchmarkRunError, + "The benchmark timer's start method must be called before its stop method" + ); }, }); |