diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index c0d1346..65e2e87 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -55,6 +55,12 @@ jobs: env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + - name: Run microbenchmarks + run: | + opam install testu01 core_bench core_unix --yes + opam exec -- dune build bin + opam exec -- dune exec -- bench + - name: Build Docs run: opam exec -- dune build @doc diff --git a/README.md b/README.md index 06e0988..da04b18 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ $ dune exec -- crush pcg64 All tests were passed ``` +## Benchmarks +A utility to compare the performance of each bitgenerator's `next_uint64` function is provided. +To compile the benchmark executor run `dune build bin`, and then run it using `dune exec -- bench`. +Once the benchmark run has completed, a summary table will be displayed in stdout. [1]: https://codecov.io/gh/zoj613/bitgenerators/graph/badge.svg?token=KOOG2Y1SH5 diff --git a/bin/bench.ml b/bin/bench.ml new file mode 100644 index 0000000..642171d --- /dev/null +++ b/bin/bench.ml @@ -0,0 +1,36 @@ +open Bitgen + +module type S = sig + type t + val next_uint64 : t -> Stdint.uint64 * t + val initialize : SeedSequence.t -> t +end + + +let make_bits (module M : S) = + let ss = SeedSequence.initialize [Stdint.Uint128.of_int 123456789] in + let t = ref (M.initialize ss) in + let bits () = match M.next_uint64 !t with + | (u, t') -> t := t'; u + in + bits + + +let pairs = [ + "PCG64", (module PCG64: S); + "SFC64", (module SFC64: S); + "Xoshiro256", (module Xoshiro256: S); + "Philox64", (module Philox64: S); +] + + +let make_fn (name, m) = + Core_bench.Bench.Test.create ~name:(name ^ ".next_uint64") (make_bits m) + + +let () = + Stdlib.Random.init 123456789; + [Core_bench.Bench.Test.create ~name:"Stdlib.Random.bits64" (fun () -> Stdlib.Random.int64 Int64.max_int)] @ + List.map make_fn pairs + |> Core_bench.Bench.make_command + |> Command_unix.run diff --git a/bin/dune b/bin/dune index 20db808..6f21825 100644 --- a/bin/dune +++ b/bin/dune @@ -1,5 +1,12 @@ (executable (public_name crush) - (name crush) + (modules crush) (ocamlopt_flags (:standard -O3)) (libraries bitgenerators testu01)) + + +(executable + (public_name bench) + (modules bench) + (ocamlopt_flags (:standard -O3)) + (libraries bitgenerators core_bench core_unix.command_unix))