Skip to content

Commit

Permalink
Add Metric.make to support ad hoc metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
polytypic committed May 18, 2024
1 parent c1cc3c4 commit 949abfa
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
19 changes: 19 additions & 0 deletions lib/metric.ml
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
type t = Yojson.Safe.t

let to_nonbreaking s =
s |> String.split_on_char ' ' |> String.concat " " (* a non-breaking space *)

let name ~metric ~config = to_nonbreaking (metric ^ "/" ^ config)

let make ~metric ~config ~value ?units ?trend ?description () =
let[@inline] ( @: ) x_opt xs =
match x_opt with None -> xs | Some x -> x :: xs
in
`Assoc
(Some ("name", `String (name ~metric ~config))
@: Some ("value", `Float value)
@: Option.map (fun units -> ("units", `String units)) units
@: Option.map (fun trend -> ("trend", Trend.to_json trend)) trend
@: Option.map
(fun description -> ("description", `String description))
description
@: [])
1 change: 1 addition & 0 deletions lib/multicore_bench.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module Trend = Trend
module Metric = Metric
module Unit_of_rate = Unit_of_rate
module Unit_of_time = Unit_of_time
Expand Down
19 changes: 19 additions & 0 deletions lib/multicore_bench.mli
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,30 @@
which brings a number of submodules into scope. *)

module Trend : sig
(** Dealing with trends. *)

type t = [ `Lower_is_better | `Higher_is_better ]
(** Whether a lower or higher value is better. *)
end

module Metric : sig
(** Dealing with benchmark metrics. *)

type t
(** Represents a metric. *)

val make :
metric:string ->
config:string ->
value:float ->
?units:string ->
?trend:Trend.t ->
?description:string ->
unit ->
t
(** [make ~metric ~config ~value ... ()] constructs a metric with given
specification. *)
end

module Unit_of_rate : sig
Expand Down
14 changes: 6 additions & 8 deletions lib/times.ml
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,15 @@ module Stats = struct
in
{ mean; sd; median; inverted; best; runs }

let to_nonbreaking s =
s |> String.split_on_char ' '
|> String.concat " " (* a non-breaking space *)

let to_json ~name ~description ~units t =
let to_json ~metric ~config ~description ~units t =
let trend =
if t.inverted then `String "higher-is-better"
else `String "lower-is-better"
in
[
`Assoc
[
("name", `String (to_nonbreaking name));
("name", `String (Metric.name ~metric ~config));
("value", `Float t.median);
("units", `String units);
("trend", trend);
Expand All @@ -301,13 +297,15 @@ let to_thruput_metrics ~n ~singular ?(plural = singular ^ "s") ~config
times |> Stats.of_times
|> Stats.scale (Unit_of_time.to_multiplier unit_of_time /. Float.of_int n)
|> Stats.to_json
~name:(Printf.sprintf "time per %s/%s" singular config)
~metric:(Printf.sprintf "time per %s" singular)
~config
~description:(Printf.sprintf "Time to process one %s" singular)
~units:(Unit_of_time.to_mnemonic unit_of_time);
times |> average |> invert |> Stats.of_times
|> Stats.scale (Float.of_int n /. Unit_of_rate.to_divisor unit_of_rate)
|> Stats.to_json
~name:(Printf.sprintf "%s over time/%s" plural config)
~metric:(Printf.sprintf "%s over time" plural)
~config
~description:(Printf.sprintf "Total number of %s processed" plural)
~units:(Unit_of_rate.to_mnemonic unit_of_rate);
]
5 changes: 5 additions & 0 deletions lib/trend.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type t = [ `Lower_is_better | `Higher_is_better ]

let to_json = function
| `Lower_is_better -> `String "lower-is-better"
| `Higher_is_better -> `String "higher-is-better"

0 comments on commit 949abfa

Please sign in to comment.