Skip to content

Commit f767131

Browse files
committed
Randomize the order of benchmark suites by default
The intention behind this is to try to account for things that might put the runtime into a state where it has major effects on specific benchmarks. After randomizing, we should see variation from run to run rather than e.g. having the potential effects only shown in specific suites.
1 parent 3ea8caf commit f767131

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/cmd.ml

+23-2
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,26 @@ let build_filter = function
7070
| exception Not_found -> false
7171
end
7272

73+
let shuffle xs =
74+
let xs = Array.of_list xs in
75+
let state = Random.State.make_self_init () in
76+
let n = Array.length xs in
77+
for i = 0 to n - 2 do
78+
let j = Random.State.int state (n - i) + i in
79+
let t = xs.(i) in
80+
xs.(i) <- xs.(j);
81+
xs.(j) <- t
82+
done;
83+
Array.to_list xs
84+
7385
let run ~benchmarks ?(budgetf = 0.025) ?(filters = []) ?(debug = false)
74-
?(output = `JSON) ?(argv = Sys.argv) ?(flush = true) () =
86+
?(output = `JSON) ?(argv = Sys.argv) ?(flush = true) ?(randomize = true) ()
87+
=
7588
let budgetf = ref budgetf in
7689
let filters = ref filters in
7790
let debug = ref debug in
7891
let output = ref output in
92+
let randomize = ref randomize in
7993

8094
let rec specs =
8195
[
@@ -119,7 +133,14 @@ let run ~benchmarks ?(budgetf = 0.025) ?(filters = []) ?(debug = false)
119133
`List
120134
(benchmarks
121135
|> List.filter (build_filter !filters)
122-
|> List.map (run_benchmark ~debug:!debug ~budgetf:!budgetf)) );
136+
|> (if !randomize then shuffle else Fun.id)
137+
|> List.map (run_benchmark ~debug:!debug ~budgetf:!budgetf)
138+
|> List.sort (fun l r ->
139+
let name_of = function
140+
| `Assoc (("name", `String name) :: _) -> name
141+
| _ -> failwith "bug"
142+
in
143+
String.compare (name_of l) (name_of r))) );
123144
]
124145
in
125146

lib/multicore_bench.mli

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ module Cmd : sig
180180
?output:output ->
181181
?argv:string array ->
182182
?flush:bool ->
183+
?randomize:bool ->
183184
unit ->
184185
unit
185186
(** [run ~benchmarks ()] interprets command line arguments and runs the
@@ -205,6 +206,9 @@ module Cmd : sig
205206
- [~flush]: Whether to flush the standard output after writing it.
206207
Defaults to [true].
207208
209+
- [~randomize]: Whether to randomize the order of suites or not. Defaults
210+
to [true].
211+
208212
Command line arguments take precedence over the optional arguments. In
209213
other words, you can specify the optional arguments to give defaults for
210214
the benchmark executable. *)

0 commit comments

Comments
 (0)