Skip to content

Commit

Permalink
BREAKING: split Benchmark module
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Aug 26, 2024
1 parent 379feab commit fd55f69
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 55 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
55 changes: 2 additions & 53 deletions src/Vitest.res
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,16 @@ type testOptions = {
fails?: bool,
}

type benchOptions = {
time: option<int>,
iterations: option<int>,
warmupTime: option<int>,
warmupIterations: option<int>,
}

type suiteDef = (string, suiteOptions, unit => unit) => unit
type testDef = (string, testOptions, unit => unit) => unit
type testAsyncDef = (string, testOptions, unit => promise<unit>) => unit
type benchDef = (string, unit => unit, benchOptions) => unit
type benchAsyncDef = (string, unit => promise<unit>, benchOptions) => unit

module type Runner = {
let describe: suiteDef
let test: testDef
let testAsync: testAsyncDef
let it: testDef
let itAsync: testAsyncDef
let bench: benchDef
let benchAsync: benchAsyncDef
}

module type ConcurrentRunner = {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -1215,3 +1162,5 @@ module InSource = {
@scope("import.meta.vitest") @val
external benchAsync: (string, @uncurry unit => promise<unit>) => unit = "it"
}

module Benchmark = Vitest_Benchmark
159 changes: 159 additions & 0 deletions src/Vitest_Benchmark.res
Original file line number Diff line number Diff line change
@@ -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<unit>, 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<unit> = "todo"
@inline
let benchAsync = name => todo_bench->benchAsync(name)
}
4 changes: 3 additions & 1 deletion tests/sort.bench.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
open Js
open Vitest
open Vitest.Benchmark

describe("sort", () => {
bench("normal", _ => {
Expand All @@ -24,4 +24,6 @@ describe("sort", () => {
)
->ignore
})

Todo.bench("todo")
})

0 comments on commit fd55f69

Please sign in to comment.