Debian Codegraph @

dev/bench/render_results.ml

raw symbols SCIP
1
2 (* ASSUMPTIONS:
3 - the 1-st command line argument (working directory):
4 - designates an existing readable directory
5 - which contains *.time and *.perf files produced by bench.sh script
6 - the 2-nd command line argument (number of iterations):
7 - is a positive integer
8 - the 3-rd command line argument (minimal user time):
9 - is a positive floating point number
10 - the 4-th command line argument determines the name of the column according to which the resulting table will be sorted.
11 Valid values are:
12 - package_name
13 - user_time_pdiff
14 - the rest of the command line-arguments
15 - are names of benchamarked Coq OPAM packages for which bench.sh script generated *.time and *.perf files
16 *)
17
18 open Printf
19 ;;
20
21 let _ = Printexc.record_backtrace true
22 ;;
23
24 type ('a,'b) pkg_timings = {
25 user_time : 'a;
26 num_instr : 'b;
27 num_mem : 'b;
28 }
29 ;;
30
31 let reduce_pkg_timings (m_f : 'a list -> 'c) (m_a : 'b list -> 'd) (t : ('a,'b) pkg_timings list) : ('c,'d) pkg_timings =
32 { user_time = m_f @@ CList.map (fun x -> x.user_time) t
33 ; num_instr = m_a @@ CList.map (fun x -> x.num_instr) t
34 ; num_mem = m_a @@ CList.map (fun x -> x.num_mem) t
35 }
36 ;;
37
38 (******************************************************************************)
39 (* BEGIN Copied from batteries, to remove *)
40 (******************************************************************************)
41 let run_and_read cmd =
42 (* This code is before the open of BatInnerIO
43 to avoid using batteries' wrapped IOs *)
44 let string_of_file fn =
45 let buff_size = 1024 in
46 let buff = Buffer.create buff_size in
47 let ic = open_in fn in
48 let line_buff = Bytes.create buff_size in
49 begin
50 let was_read = ref (input ic line_buff 0 buff_size) in
51 while !was_read <> 0 do
52 Buffer.add_subbytes buff line_buff 0 !was_read;
53 was_read := input ic line_buff 0 buff_size;
54 done;
55 close_in ic;
56 end;
57 Buffer.contents buff
58 in
59 let tmp_fn = Filename.temp_file "" "" in
60 let cmd_to_run = cmd ^ " > " ^ tmp_fn in
61 let status = Unix.system cmd_to_run in
62 let output = string_of_file tmp_fn in
63 Unix.unlink tmp_fn;
64 (status, output)
65 ;;
66
67 let ( %> ) f g x = g (f x)
68
69 let run = run_and_read %> snd
70
71 module Float = struct
72 let nan = nan
73 end
74
75 module CList = struct
76 include CList
77
78 let rec init_tailrec_aux acc i n f =
79 if i >= n then acc
80 else init_tailrec_aux (f i :: acc) (i+1) n f
81
82 let rec init_aux i n f =
83 if i >= n then []
84 else
85 let r = f i in
86 r :: init_aux (i+1) n f
87
88 let rev_init_threshold =
89 match Sys.backend_type with
90 | Sys.Native | Sys.Bytecode -> 10_000
91 (* We don't known the size of the stack, better be safe and assume it's small. *)
92 | Sys.Other _ -> 50
93
94 let init len f =
95 if len < 0 then invalid_arg "CList.init" else
96 if len > rev_init_threshold then rev (init_tailrec_aux [] 0 len f)
97 else init_aux 0 len f
98
99 let rec drop n = function
100 | _ :: l when n > 0 -> drop (n-1) l
101 | l -> l
102
103 let reduce f = function
104 | [] ->
105 invalid_arg "CList.reduce: Empty CList"
106 | h :: t ->
107 fold_left f h t
108
109 let min l = reduce Stdlib.min l
110
111 end
112 ;;
113
114 module String = struct
115
116 include String
117
118 let rchop ?(n = 1) s =
119 if n < 0 then
120 invalid_arg "String.rchop: number of characters to chop is negative"
121 else
122 let slen = length s in
123 if slen <= n then "" else sub s 0 (slen - n)
124
125 end
126 ;;
127
128 (******************************************************************************)
129 (* END Copied from batteries, to remove *)
130 (******************************************************************************)
131
132 let add_timings a b =
133 { user_time = a.user_time +. b.user_time;
134 num_instr = a.num_instr + b.num_instr;
135 num_mem = a.num_mem + b.num_mem;
136 }
137
138 let mk_pkg_timings work_dir pkg_name suffix iteration =
139 let command_prefix = "cat " ^ work_dir ^ "/" ^ pkg_name ^ suffix ^ string_of_int iteration in
140 let ncoms = command_prefix ^ ".ncoms" |> run |> String.rchop ~n:1 |> int_of_string in
141 let timings = CList.init ncoms (fun ncom ->
142 let command_prefix = command_prefix ^ "." ^ string_of_int (ncom+1) in
143 let time_command_output = command_prefix ^ ".time" |> run |> String.rchop ~n:1 |> String.split_on_char ' ' in
144
145 let nth x i = CList.nth i x in
146
147 { user_time = time_command_output |> nth 0 |> float_of_string
148 (* Perf can indeed be not supported in some systems, so we must fail gracefully *)
149 ; num_instr =
150 (try command_prefix ^ ".perf | grep instructions:u | awk '{print $1}' | sed 's/,//g'" |>
151 run |> String.rchop ~n:1 |> int_of_string
152 with Failure _ -> 0)
153 ; num_mem = time_command_output |> nth 1 |> int_of_string
154 })
155 in
156 match timings with
157 | [] -> assert false
158 | timing :: rest -> CList.fold_left add_timings timing rest
159 ;;
160
161 (* process command line paramters *)
162 assert (Array.length Sys.argv > 5);
163 let work_dir = Sys.argv.(1) in
164 let num_of_iterations = int_of_string Sys.argv.(2) in
165 let minimal_user_time = float_of_string Sys.argv.(3) in
166 let sorting_column = Sys.argv.(4) in
167 let coq_opam_packages = Sys.argv |> Array.to_list |> CList.drop 5 in
168
169 (* ASSUMPTIONS:
170
171 "working_dir" contains all the files produced by the following command:
172
173 two_points_on_the_same_branch.sh $working_directory $coq_repository $coq_branch[:$new:$old] $num_of_iterations coq_opam_package_1 coq_opam_package_2 ... coq_opam_package_N
174 -sf
175 *)
176
177 (* Run a given bash command;
178 wait until it termines;
179 check if its exit status is 0;
180 return its whole stdout as a string. *)
181
182 let proportional_difference_of_integers new_value old_value =
183 if old_value = 0
184 then Float.nan
185 else float_of_int (new_value - old_value) /. float_of_int old_value *. 100.0
186 in
187
188 (* parse the *.time and *.perf files *)
189 coq_opam_packages
190 |> CList.map
191 (fun package_name ->
192 package_name,(* compilation_results_for_NEW : (float * int * int * int) list *)
193 CList.init num_of_iterations succ |> CList.map (mk_pkg_timings work_dir package_name ".NEW."),
194 CList.init num_of_iterations succ |> CList.map (mk_pkg_timings work_dir package_name ".OLD."))
195
196 (* from the list of measured values, select just the minimal ones *)
197
198 |> CList.map
199 (fun ((package_name : string),
200 (new_measurements : (float, int) pkg_timings list),
201 (old_measurements : (float, int) pkg_timings list)) ->
202 let f_min : float list -> float = CList.min in
203 let i_min : int list -> int = CList.min in
204 package_name,
205 reduce_pkg_timings f_min i_min new_measurements,
206 reduce_pkg_timings f_min i_min old_measurements
207 )
208
209 (* compute the "proportional differences in % of the NEW measurement and the OLD measurement" of all measured values *)
210 |> CList.map
211 (fun (package_name, new_t, old_t) ->
212 package_name, new_t, old_t,
213 { user_time = (new_t.user_time -. old_t.user_time) /. old_t.user_time *. 100.0
214 ; num_instr = proportional_difference_of_integers new_t.num_instr old_t.num_instr
215 ; num_mem = proportional_difference_of_integers new_t.num_mem old_t.num_mem
216 })
217
218 (* sort the table with results *)
219 |> CList.sort
220 (match sorting_column with
221 | "user_time_pdiff" ->
222 fun (_,_,_,perf1) (_,_,_,perf2) ->
223 compare perf1.user_time perf2.user_time
224 | "package_name" ->
225 fun (n1,_,_,_) (n2,_,_,_) -> compare n1 n2
226 | _ ->
227 assert false
228 )
229
230 (* Keep only measurements that took at least "minimal_user_time" (in seconds). *)
231
232 |> CList.filter
233 (fun (_, new_t, old_t, _) ->
234 minimal_user_time <= new_t.user_time || minimal_user_time <= old_t.user_time)
235
236 (* Below we take the measurements and format them to stdout. *)
237
238 |> CList.map begin fun (package_name, new_t, old_t, perc) ->
239
240 let precision = 2 in
241 let prf f = Printf.sprintf "%.*f" precision f in
242 let pri n = Printf.sprintf "%d" n in
243
244 [
245 [ package_name ];
246 [ prf new_t.user_time; prf old_t.user_time; prf perc.user_time ];
247 [ pri new_t.num_instr; pri old_t.num_instr; prf perc.num_instr ];
248 [ pri new_t.num_mem; pri old_t.num_mem; prf perc.num_mem ];
249 ]
250
251 end
252
253 |> fun measurements ->
254
255 let headers = [
256 "";
257 "user time [s]";
258 "CPU instructions";
259 "max resident mem [KB]";
260 ] in
261
262 let descr = ["NEW"; "OLD"; "PDIFF"] in
263 let top = [ [ "package_name" ]; descr; descr; descr ] in
264
265 printf "%s%!" (Table.raw_print headers top measurements ())