summaryrefslogtreecommitdiff
path: root/std/testing/bench_test.ts
diff options
context:
space:
mode:
authorSzalay Kristóf <32012862+littletof@users.noreply.github.com>2020-05-29 08:29:58 +0200
committerGitHub <noreply@github.com>2020-05-29 02:29:58 -0400
commit6de59f1908430b5eac48e9f3a74caf6b262221a9 (patch)
tree6f7dba1777b7735e0221aaf1320de412e815c988 /std/testing/bench_test.ts
parentfe7d6824c91b0c2b45c1248fc58847250f6c9f42 (diff)
Return results in benchmark promise (#5842)
Diffstat (limited to 'std/testing/bench_test.ts')
-rw-r--r--std/testing/bench_test.ts93
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"
+ );
},
});