Skip to content

Commit

Permalink
Add -brief output mode
Browse files Browse the repository at this point in the history
This is useful as the output mode when running a benchmark executable as a test
and the JSON output is just too much.
  • Loading branch information
polytypic committed Mar 10, 2024
1 parent f1ab51a commit d54512d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Next release

- Add `-brief` to show results in a concise human readable format (@polytypic)
- Add `-diff base.json` switch to diff against base results from file
(@polytypic)

Expand Down
2 changes: 2 additions & 0 deletions bench/dune
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ let () =

(test
(name main)
(action
(run %{test} -brief))
(libraries
multicore-bench
backoff
Expand Down
30 changes: 23 additions & 7 deletions lib/cmd.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
let print_brief json =
let open Data in
json |> Results.parse
|> Option.iter @@ fun (results : Results.t) ->
results
|> List.iter @@ fun (bench : Benchmark.t) ->
Printf.printf "%s:\n" bench.name;
bench.metrics
|> List.iter @@ fun (metric : Metric.t) ->
Printf.printf " %s:\n" metric.name;
Printf.printf " %.2f %s\n" metric.value metric.units

let print_diff base next =
let open Data in
Option.pair (Results.parse base) (Results.parse next)
Expand Down Expand Up @@ -49,12 +61,12 @@ let build_filter = function
| exception Not_found -> false
end

let run ~benchmarks ?(budgetf = 0.025) ?(filters = []) ?(debug = false) ?diff
?(argv = Sys.argv) ?(flush = true) () =
let run ~benchmarks ?(budgetf = 0.025) ?(filters = []) ?(debug = false)
?(output = `JSON) ?(argv = Sys.argv) ?(flush = true) () =
let budgetf = ref budgetf in
let filters = ref filters in
let debug = ref debug in
let diff = ref diff in
let output = ref output in

let rec specs =
[
Expand All @@ -63,8 +75,11 @@ let run ~benchmarks ?(budgetf = 0.025) ?(filters = []) ?(debug = false) ?diff
Arg.Set debug,
"\t Print progress information to help debugging" );
( "-diff",
Arg.String (fun path -> diff := Some path),
Arg.String (fun path -> output := `Diff path),
"path.json\t Show diff against specified base results" );
( "-brief",
Arg.Unit (fun () -> output := `Brief),
"\t Show brief human readable results." );
("-help", Unit help, "\t Show this help message");
("--help", Unit help, "\t Show this help message");
]
Expand Down Expand Up @@ -100,9 +115,10 @@ let run ~benchmarks ?(budgetf = 0.025) ?(filters = []) ?(debug = false) ?diff
in

begin
match !diff with
| None -> Yojson.Safe.pretty_print ~std:true Format.std_formatter results
| Some fname -> print_diff (Yojson.Safe.from_file fname) results
match !output with
| `JSON -> Yojson.Safe.pretty_print ~std:true Format.std_formatter results
| `Brief -> print_brief results
| `Diff fname -> print_diff (Yojson.Safe.from_file fname) results
end;

if flush then Format.print_flush ()
8 changes: 5 additions & 3 deletions lib/multicore_bench.mli
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ module Cmd : sig
?budgetf:float ->
?filters:string list ->
?debug:bool ->
?diff:string ->
?output:[ `JSON | `Brief | `Diff of string ] ->
?argv:string array ->
?flush:bool ->
unit ->
Expand All @@ -169,8 +169,10 @@ module Cmd : sig
- [~debug]: Print progress information to help debugging. Defaults to
[false].
- [~diff]: Name of JSON file of results to show diff against. Defaults to
[None].
- [~output]: Output mode. [`JSON] gives the JSON output for
current-bench. [`Brief] gives concise human readable output. [`Diff
"path.json"] gives concise human readable diff against results stored in
specified [path.json] file. Defaults to [`Json].
- [~argv]: Array of command line arguments. Defaults to [Sys.argv].
Expand Down

0 comments on commit d54512d

Please sign in to comment.