Skip to content

Commit b520749

Browse files
Julowgpetiot
authored andcommitted
driver.mld: Factorize metrics computing code
Co-authored-by: Guillaume Petiot <[email protected]>
1 parent 8401336 commit b520749

File tree

1 file changed

+69
-66
lines changed

1 file changed

+69
-66
lines changed

doc/driver.mld

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -892,12 +892,16 @@ Some code to analyze the list of executed commands:
892892
{[
893893
(** Return the list of executed commands where the first argument was [cmd]. *)
894894
let filter_commands cmd =
895-
List.filter
896-
(fun c ->
897-
match Bos.Cmd.to_list c.cmd with
898-
| _ :: cmd' :: _ -> cmd = cmd'
899-
| _ -> false)
900-
!commands
895+
match
896+
List.filter
897+
(fun c ->
898+
match Bos.Cmd.to_list c.cmd with
899+
| _ :: cmd' :: _ -> cmd = cmd'
900+
| _ -> false)
901+
!commands
902+
with
903+
| [] -> failwith ("No commands run for " ^ cmd)
904+
| (_ :: _) as cmds -> cmds
901905

902906
(** Returns the [k] commands that took the most time for a given subcommand. *)
903907
let k_longest_commands cmd k =
@@ -932,80 +936,79 @@ This last block analyze the running times so that they can be submitted to
932936
(* *)
933937
#require "yojson" ;;
934938

939+
let rec compute_min_max_avg min_ max_ total count = function
940+
| [] -> (min_, max_, total /. float count, count)
941+
| hd :: tl ->
942+
compute_min_max_avg (min min_ hd) (max max_ hd) (total +. hd) (count + 1) tl
943+
944+
let compute_min_max_avg = function
945+
| [] -> assert false
946+
| hd :: tl -> compute_min_max_avg hd hd hd 1 tl
947+
935948
(** Analyze the running time of a command. *)
936949
let compute_metric_cmd cmd =
937-
let rec compute min_ max_ total count = function
938-
| [] -> (min_, max_, total /. float count, count)
939-
| hd :: tl ->
940-
compute (min min_ hd.time) (max max_ hd.time) (total +. hd.time) (count + 1) tl
941-
in
942-
match filter_commands cmd with
943-
| [] -> failwith ("No commands run for " ^ cmd)
944-
| hd :: tl ->
945-
let min, max, avg, count = compute hd.time hd.time hd.time 1 tl in
950+
let cmds = filter_commands cmd in
951+
let times = List.map (fun c -> c.time) cmds in
952+
let min, max, avg, count = compute_min_max_avg times in
953+
[
954+
`Assoc
946955
[
947-
`Assoc
948-
[
949-
("name", `String ("total-" ^ cmd));
950-
("value", `Int count);
951-
( "description",
952-
`String ("Number of time 'odoc " ^ cmd ^ "' has run.") );
953-
];
954-
`Assoc
955-
[
956-
("name", `String ("time-" ^ cmd));
957-
( "value",
958-
`Assoc
959-
[
960-
("min", `Float min); ("max", `Float max); ("avg", `Float avg);
961-
] );
962-
("units", `String "s");
963-
("description", `String ("Time taken by 'odoc " ^ cmd ^ "'"));
964-
("trend", `String "lower-is-better");
965-
];
966-
]
956+
("name", `String ("total-" ^ cmd));
957+
("value", `Int count);
958+
( "description",
959+
`String ("Number of time 'odoc " ^ cmd ^ "' has run.") );
960+
];
961+
`Assoc
962+
[
963+
("name", `String ("time-" ^ cmd));
964+
( "value",
965+
`Assoc
966+
[
967+
("min", `Float min); ("max", `Float max); ("avg", `Float avg);
968+
] );
969+
("units", `String "s");
970+
("description", `String ("Time taken by 'odoc " ^ cmd ^ "'"));
971+
("trend", `String "lower-is-better");
972+
];
973+
]
967974

968975
(** Analyze the size of files produced by a command. *)
969976
let compute_produced_cmd cmd =
970977
let output_file_size c =
971978
match c.output_file with
972979
| Some f -> (
973980
match Bos.OS.Path.stat f with
974-
| Ok st -> Some st.Unix.st_size
981+
| Ok st -> Some (float st.Unix.st_size)
975982
| Error _ -> None)
976983
| None -> None
977984
in
978-
let rec compute min_ max_ total count = function
979-
| [] -> (min_, max_, int_of_float (total /. float count), count)
980-
| size :: tl ->
981-
compute (min min_ size) (max max_ size) (total +. float size) (count + 1) tl
982-
in
983-
match List.filter_map output_file_size (filter_commands cmd) with
984-
| [] -> failwith ("No commands run for " ^ cmd)
985-
| size0 :: tl ->
986-
let min, max, avg, count = compute size0 size0 (float size0) 1 tl in
985+
let sizes = List.filter_map output_file_size (filter_commands cmd) in
986+
let min, max, avg, count = compute_min_max_avg sizes in
987+
let min = int_of_float min in
988+
let max = int_of_float max in
989+
let avg = int_of_float avg in
990+
[
991+
`Assoc
987992
[
988-
`Assoc
989-
[
990-
("name", `String ("produced-total-" ^ cmd));
991-
("value", `Int count);
992-
( "description",
993-
`String ("Number of file produced by 'odoc " ^ cmd ^ "'") );
994-
];
995-
`Assoc
996-
[
997-
("name", `String ("produced-size-" ^ cmd));
998-
( "value",
999-
`Assoc
1000-
[
1001-
("min", `Int min); ("max", `Int max); ("avg", `Int avg);
1002-
] );
1003-
("units", `String "b");
1004-
( "description",
1005-
`String ("Size of file produced by 'odoc " ^ cmd ^ "'") );
1006-
("trend", `String "lower-is-better");
1007-
];
1008-
]
993+
("name", `String ("produced-total-" ^ cmd));
994+
("value", `Int count);
995+
( "description",
996+
`String ("Number of file produced by 'odoc " ^ cmd ^ "'") );
997+
];
998+
`Assoc
999+
[
1000+
("name", `String ("produced-size-" ^ cmd));
1001+
( "value",
1002+
`Assoc
1003+
[
1004+
("min", `Int min); ("max", `Int max); ("avg", `Int avg);
1005+
] );
1006+
("units", `String "b");
1007+
( "description",
1008+
`String ("Size of file produced by 'odoc " ^ cmd ^ "'") );
1009+
("trend", `String "lower-is-better");
1010+
];
1011+
]
10091012

10101013
(** Analyze the size of files produced by a command. *)
10111014
let compute_longest_cmd cmd =

0 commit comments

Comments
 (0)