Skip to content

Commit 3121640

Browse files
committed
Move bench to separate internal library
1 parent 455e896 commit 3121640

12 files changed

+236
-242
lines changed

bench/bench.ml

-232
This file was deleted.
File renamed without changes.
File renamed without changes.

bench/bench/bench.ml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Times = Times
2+
module Stats = Stats
3+
module Suite = Suite
4+
module Cmd = Cmd
5+
module Util = Util

bench/bench.mli bench/bench/bench.mli

+6
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ end
3333
module Cmd : sig
3434
val run : benchmarks:(string * Suite.t) list -> unit -> unit
3535
end
36+
37+
module Util : sig
38+
val iter_factor : int
39+
val alloc : ?batch:int -> int Atomic.t -> int
40+
val cross : 'a list -> 'b list -> ('a * 'b) list
41+
end

bench/bench/cmd.ml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
let run ~benchmarks () =
2+
let budgetf = ref 0.025 in
3+
let filters = ref [] in
4+
5+
let rec specs =
6+
[
7+
("-budget", Arg.Set_float budgetf, "seconds\t Budget for a benchmark");
8+
("-help", Unit help, "\t Show this help message");
9+
("--help", Unit help, "\t Show this help message");
10+
]
11+
and help () =
12+
Arg.usage (Arg.align specs)
13+
(Printf.sprintf
14+
"\n\
15+
Usage: %s <option>* filter*\n\n\
16+
The filters are regular expressions for selecting benchmarks to run.\n\n\
17+
Benchmarks:\n\n\
18+
%s\n\n\
19+
Options:\n"
20+
(Filename.basename Sys.argv.(0))
21+
(benchmarks
22+
|> List.map (fun (name, _) -> " " ^ name)
23+
|> String.concat "\n"));
24+
exit 1
25+
in
26+
Arg.parse specs (fun filter -> filters := filter :: !filters) "";
27+
28+
let budgetf = !budgetf in
29+
30+
let run (name, fn) =
31+
let metrics = fn ~budgetf in
32+
`Assoc [ ("name", `String name); ("metrics", `List metrics) ]
33+
in
34+
35+
let filter =
36+
match !filters with
37+
| [] -> Fun.const true
38+
| filters -> (
39+
let regexps = filters |> List.map Str.regexp in
40+
fun (name, _) ->
41+
regexps
42+
|> List.exists @@ fun regexp ->
43+
match Str.search_forward regexp name 0 with
44+
| _ -> true
45+
| exception Not_found -> false)
46+
in
47+
48+
`Assoc
49+
[ ("results", `List (benchmarks |> List.filter filter |> List.map run)) ]
50+
|> Yojson.Safe.pretty_print ~std:true Format.std_formatter

bench/bench/dune

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(library
2+
(name bench)
3+
(package kcas_data)
4+
(libraries
5+
multicore-magic
6+
domain-local-await
7+
domain_shims
8+
mtime
9+
mtime.clock.os
10+
yojson
11+
str))

bench/bench/stats.ml

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
type t = {
2+
mean : float;
3+
median : float;
4+
sd : float;
5+
inverted : bool;
6+
best : float;
7+
runs : int;
8+
}
9+
10+
let scale factor { mean; median; sd; inverted; best; runs } =
11+
{
12+
mean = mean *. factor;
13+
median = median *. factor;
14+
sd = sd *. factor;
15+
inverted;
16+
best = best *. factor;
17+
runs;
18+
}
19+
20+
let mean_of times =
21+
Array.fold_left ( +. ) 0.0 times /. Float.of_int (Array.length times)
22+
23+
let sd_of times mean =
24+
Float.sqrt (mean_of (Array.map (fun v -> Float.abs (v -. mean) ** 2.) times))
25+
26+
let median_of times =
27+
Array.sort Float.compare times;
28+
let n = Array.length times in
29+
if n land 1 = 0 then (times.((n asr 1) - 1) +. times.(n asr 1)) /. 2.0
30+
else times.(n asr 1)
31+
32+
let of_times Times.{ inverted; times; runs } =
33+
let mean = mean_of times in
34+
let sd = sd_of times mean in
35+
let median = median_of times in
36+
let best =
37+
if inverted then Array.fold_left Float.max Float.min_float times
38+
else Array.fold_left Float.min Float.max_float times
39+
in
40+
{ mean; sd; median; inverted; best; runs }
41+
42+
let to_nonbreaking s =
43+
s |> String.split_on_char ' ' |> String.concat " " (* a non-breaking space *)
44+
45+
let to_json ~name ~description ~units t =
46+
let trend =
47+
if t.inverted then `String "higher-is-better" else `String "lower-is-better"
48+
in
49+
[
50+
`Assoc
51+
[
52+
("name", `String (to_nonbreaking name));
53+
("value", `Float t.median);
54+
("units", `String units);
55+
("trend", trend);
56+
("description", `String description);
57+
("#best", `Float t.best);
58+
("#mean", `Float t.mean);
59+
("#median", `Float t.median);
60+
("#sd", `Float t.sd);
61+
("#runs", `Int t.runs);
62+
];
63+
]

bench/bench/suite.ml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type t = budgetf:float -> Yojson.Safe.t list

0 commit comments

Comments
 (0)