diff --git a/package.json b/package.json index 7384258..9df6385 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "prepack": "yarn clean && yarn build", "clean": "rescript clean", "build": "rescript build", - "test": "vitest" + "test": "vitest", + "bench": "vitest bench" }, "files": [ "bsconfig.json", diff --git a/src/Vitest.res b/src/Vitest.res index a5621d9..9655eea 100644 --- a/src/Vitest.res +++ b/src/Vitest.res @@ -54,18 +54,9 @@ type testOptions = { fails?: bool, } -type benchOptions = { - time: option, - iterations: option, - warmupTime: option, - warmupIterations: option, -} - type suiteDef = (string, suiteOptions, unit => unit) => unit type testDef = (string, testOptions, unit => unit) => unit type testAsyncDef = (string, testOptions, unit => promise) => unit -type benchDef = (string, unit => unit, benchOptions) => unit -type benchAsyncDef = (string, unit => promise, benchOptions) => unit module type Runner = { let describe: suiteDef @@ -73,8 +64,6 @@ module type Runner = { let testAsync: testAsyncDef let it: testDef let itAsync: testAsyncDef - let bench: benchDef - let benchAsync: benchAsyncDef } module type ConcurrentRunner = { @@ -233,20 +222,6 @@ module MakeRunner = (Runner: Runner) => { }, () => callback(suite), ) - - @inline - let bench = (name, ~time=?, ~iterations=?, ~warmupTime=?, ~warmupIterations=?, callback) => - Runner.bench( - name, - () => { - callback(suite) - }, - {time, iterations, warmupTime, warmupIterations}, - ) - - @inline - let benchAsync = (name, ~time=?, ~iterations=?, ~warmupTime=?, ~warmupIterations=?, callback) => - Runner.benchAsync(name, () => callback(suite), {time, iterations, warmupTime, warmupIterations}) } module MakeConcurrentRunner = (Runner: ConcurrentRunner) => { @@ -344,12 +319,6 @@ include MakeRunner({ @module("vitest") @val external itAsync: testAsyncDef = "it" - - @module("vitest") @val - external bench: benchDef = "bench" - - @module("vitest") @val - external benchAsync: benchAsyncDef = "bench" }) module Concurrent = { @@ -400,9 +369,6 @@ module Only = { @module("vitest") @val external only_it: only_it = "it" - - @module("vitest") @val - external only_bench: only_bench = "bench" ) @get @@ -420,20 +386,12 @@ module Only = { @get external itAsync: only_it => testAsyncDef = "only" - @get - external bench: only_bench => benchDef = "only" - - @get - external benchAsync: only_bench => benchAsyncDef = "only" - include MakeRunner({ let describe = only_describe->describe let test = only_test->test let testAsync = only_test->testAsync let it = only_it->it let itAsync = only_it->itAsync - let bench = only_bench->bench - let benchAsync = only_bench->benchAsync }) module Concurrent = { @@ -484,9 +442,6 @@ module Skip = { @module("vitest") @val external skip_it: skip_it = "it" - - @module("vitest") @val - external skip_bench: skip_bench = "bench" ) @get @@ -504,20 +459,12 @@ module Skip = { @get external itAsync: skip_it => testAsyncDef = "skip" - @get - external bench: skip_bench => benchDef = "skip" - - @get - external benchAsync: skip_bench => benchAsyncDef = "skip" - include MakeRunner({ let describe = skip_describe->describe let test = skip_test->test let testAsync = skip_test->testAsync let it = skip_it->it let itAsync = skip_it->itAsync - let bench = skip_bench->bench - let benchAsync = skip_bench->benchAsync }) module Concurrent = { @@ -1215,3 +1162,5 @@ module InSource = { @scope("import.meta.vitest") @val external benchAsync: (string, @uncurry unit => promise) => unit = "it" } + +module Benchmark = Vitest_Benchmark \ No newline at end of file diff --git a/src/Vitest_Benchmark.res b/src/Vitest_Benchmark.res new file mode 100644 index 0000000..7fd5054 --- /dev/null +++ b/src/Vitest_Benchmark.res @@ -0,0 +1,159 @@ +// TODO: import rescript-tinybench + +type bench + +type suiteOptions = { + skip?: bool, + only?: bool, + todo?: bool, +} + +type benchOptions = { + time?: int, + iterations?: int, + throws?: bool, + warmupTime?: int, + warmupIterations?: int, +} + +type suiteDef = (string, suiteOptions, unit => unit) => unit +type benchDef = (string, bench => unit, benchOptions) => unit +type benchAsyncDef = (string, bench => promise, benchOptions) => unit + +module type Runner = { + let describe: suiteDef + let bench: benchDef + let benchAsync: benchAsyncDef +} + +module MakeRunner = (Runner: Runner) => { + @inline + let describe = (name, ~skip=?, ~only=?, ~todo=?, callback) => + Runner.describe( + name, + { + ?skip, + ?only, + ?todo, + }, + callback, + ) + + @inline + let bench = ( + name, + ~time=?, + ~iterations=?, + ~throws=?, + ~warmupTime=?, + ~warmupIterations=?, + callback, + ) => Runner.bench(name, callback, {?time, ?iterations, ?throws, ?warmupTime, ?warmupIterations}) + + @inline + let benchAsync = ( + name, + ~time=?, + ~iterations=?, + ~throws=?, + ~warmupTime=?, + ~warmupIterations=?, + callback, + ) => + Runner.benchAsync(name, callback, {?time, ?iterations, ?throws, ?warmupTime, ?warmupIterations}) +} + +include MakeRunner({ + @module("vitest") @val + external describe: suiteDef = "describe" + + @module("vitest") @val + external bench: benchDef = "bench" + + @module("vitest") @val + external benchAsync: benchAsyncDef = "bench" +}) + +module Only = { + type only_describe + type only_bench + + %%private( + @module("vitest") @val + external only_describe: only_describe = "describe" + + @module("vitest") @val + external only_bench: only_bench = "bench" + ) + + @get + external describe: only_describe => suiteDef = "only" + + @get + external bench: only_bench => benchDef = "only" + + @get + external benchAsync: only_bench => benchAsyncDef = "only" + + include MakeRunner({ + let describe = only_describe->describe + let bench = only_bench->bench + let benchAsync = only_bench->benchAsync + }) +} + +module Skip = { + type skip_describe + type skip_bench + + %%private( + @module("vitest") @val + external skip_describe: skip_describe = "describe" + + @module("vitest") @val + external skip_bench: skip_bench = "bench" + ) + + @get + external describe: skip_describe => suiteDef = "skip" + + @get + external bench: skip_bench => benchDef = "skip" + + @get + external benchAsync: skip_bench => benchAsyncDef = "skip" + + include MakeRunner({ + let describe = skip_describe->describe + let bench = skip_bench->bench + let benchAsync = skip_bench->benchAsync + }) +} + +module Todo = { + type todo_describe + type todo_bench + + %%private( + @module("vitest") @val + external todo_describe: todo_describe = "describe" + + @module("vitest") @val + external todo_bench: todo_bench = "bench" + ) + + @send + external describe: (todo_describe, string) => unit = "todo" + @inline + let describe = name => todo_describe->describe(name) + + @send + external bench: (todo_bench, string) => unit = "todo" + @inline + let bench = name => todo_bench->bench(name) + + @send + external benchAsync: (todo_bench, string) => promise = "todo" + @inline + let benchAsync = name => todo_bench->benchAsync(name) +} diff --git a/tests/sort.bench.res b/tests/sort.bench.res index 8a30b7d..e77e795 100644 --- a/tests/sort.bench.res +++ b/tests/sort.bench.res @@ -1,5 +1,5 @@ open Js -open Vitest +open Vitest.Benchmark describe("sort", () => { bench("normal", _ => { @@ -24,4 +24,6 @@ describe("sort", () => { ) ->ignore }) + + Todo.bench("todo") })