1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Kind = struct
type t =
| Hashing
| Parsing
| Exec
end
let stats = Hashtbl.create 1000
let find kind = Hashtbl.find_opt stats kind |> Option.default 0.0
let bump kind time =
let acc = find kind in
Hashtbl.replace stats kind (acc +. time)
let time f x =
let before = Unix.gettimeofday () in
let res = f x in
let after = Unix.gettimeofday () in
(res, after -. before)
let record ~kind ~f x =
let res, time = time f x in
bump kind time;
(res, time)
let get ~kind = find kind
let dump () =
Format.asprintf "hashing: %f | parsing: %f | exec: %f" (find Kind.Hashing)
(find Kind.Parsing) (find Kind.Exec)
let reset () =
Hashtbl.remove stats Kind.Hashing;
Hashtbl.remove stats Kind.Parsing;
Hashtbl.remove stats Kind.Exec;
()