Debian Codegraph @

dev/bench/htmloutput.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open BenchUtil
12
13 let die fmt = Printf.kfprintf (fun _ -> exit 1) stderr (fmt^^"\n%!")
14
15 let colors = [|"#F08080"; "#EEE8AA"; "#98FB98"|]
16
17 let max_data_count = Array.length colors
18
19 let htmlescape =
20 let r = Str.regexp "[&<>\"]" in
21 let subst s = match Str.matched_string s with
22 | "&" -> "&amp;"
23 | "<" -> "&lt;"
24 | ">" -> "&gt;"
25 | "\"" -> "&quot;"
26 | _ -> assert false
27 in
28 fun s -> Str.global_substitute r subst s
29
30 let percentage ~max:m v =
31 Q.to_float Q.(v * of_int 100 / m)
32
33 let pp_words ~need_comma which w =
34 if w = "0 w" then need_comma, ""
35 else
36 true, (if need_comma then ", " else "")^(String.sub w 0 (String.length w - 1))^which^" w"
37
38 let pp_collect ~need_comma which c =
39 if c = 0 then need_comma, ""
40 else
41 true, Printf.sprintf "%s%d %s %s"
42 (if need_comma then ", " else "") c which
43 (if c = 1 then "collection" else "collections")
44
45 let pp_memory ch = function
46 | None -> ()
47 | Some {major_words; minor_words; major_collect; minor_collect} ->
48 (* need_comma <-> prefix is nontrivial *)
49 let need_comma, minor_words = pp_words ~need_comma:false "minor" minor_words in
50 let need_comma, major_words = pp_words ~need_comma "major" major_words in
51 let need_comma, minor_collect = pp_collect ~need_comma "minor" minor_collect in
52 let need_comma, major_collect = pp_collect ~need_comma "major" major_collect in
53 if need_comma then
54 Printf.fprintf ch " (%s%s%s%s)" minor_words major_words minor_collect major_collect
55
56 let pp_instr ch = function
57 | None -> ()
58 | Some i -> Printf.fprintf ch ", %d instr" i
59
60 let output ch ~vname ~data_files all_data =
61
62 let out fmt = Printf.fprintf ch fmt in
63 let ndata = Array.length data_files in
64
65 let totals = Array.fold_left (fun acc (_,data) ->
66 Array.map2 (fun acc d -> Q.add acc d.time.q) acc data)
67 (Array.make ndata Q.zero)
68 all_data
69 in
70
71 let maxq =
72 Array.fold_left (fun max (_,data) ->
73 Array.fold_left (fun max d ->
74 let dq = d.time.q in
75 if Q.lt max dq then dq
76 else max)
77 max
78 data)
79 Q.zero all_data
80 in
81
82 let () =
83 out
84 {|<html>
85 <head>
86 <title>%s</title>
87 <style>
88 |} vname
89 in
90
91 let () = data_files |> Array.iteri (fun i _ ->
92 let color = colors.(i) in
93 out
94 {|.time%d {
95 background-color: %s;
96 height: %d%%;
97 top: %d%%;
98 z-index: -1;
99 position: absolute;
100 opacity: 50%%;
101 }
102 |} (i+1) color (100 / ndata) (100 / ndata * i))
103 in
104
105 let () =
106 out
107 {|.code {
108 z-index: 0;
109 position: relative;
110 border-style: solid;
111 border-color: transparent;
112 border-width: 1px;
113 }
114 .code:hover {
115 border-color: black;
116 }
117 code::before {
118 content: attr(data-line);
119 right: 0.5em;
120 position: absolute;
121 text-align: right;
122 }
123 </style>
124 </head>
125 <body>
126 |}
127 in
128
129 let () = out "<h1>Timings for %s</h1>\n" vname in
130
131 let () = out "<ol>\n" in
132
133 let () = data_files |> Array.iteri (fun i data_file ->
134 out "<li style=\"background-color: %s\">%s (total time: %.3Gs)</li>\n"
135 colors.(i)
136 data_file
137 (Q.to_float totals.(i)))
138 in
139
140 let () = out "</ol>\n" in
141
142 let () = out "<pre>" in
143
144 let last_seen_line = ref 0 in
145
146 let line_id fmt l =
147 if l > !last_seen_line then begin
148 last_seen_line := l;
149 Printf.fprintf fmt "id=\"L%d\" " l
150 end
151 in
152
153 let () = all_data |> Array.iteri (fun j (loc,data) ->
154 let () = out {|<div class="code" title="File: %s
155 Line: %d
156
157 |} vname loc.line
158 in
159 let () = data |> Array.iteri (fun k d ->
160 out "Time%d: %ss%a%a\n" (k+1) d.time.str pp_instr d.instructions pp_memory d.memory)
161 in
162 let () = out {|">|} in
163
164 let () = data |> Array.iteri (fun k d ->
165 out {|<div class="time%d" style="width: %f%%"></div>|}
166 (k+1)
167 (percentage d.time.q ~max:maxq))
168 in
169
170 let text = loc.text in
171 let text = if text <> "" && text.[0] = '\n'
172 then String.sub text 1 (String.length text - 1)
173 else text
174 in
175 let sublines = String.split_on_char '\n' text in
176 let () = sublines |> List.iteri (fun i line ->
177 let lnum = loc.line + i in
178 out "<code %adata-line=\"%d\">%s</code>\n" line_id lnum lnum (htmlescape line))
179 in
180
181 let () = out "</div>" in
182 ())
183 in
184
185 let () =
186 out
187 {|
188 </pre>
189
190 </body>
191 </html>
192 |}
193 in
194
195 ()
196
197 let raw_output ch ~min_diff all_data =
198 all_data |> Array.iteri @@ fun j (loc,data) ->
199 let d1, d2 = match data with
200 | [|d1; d2|] -> d1, d2
201 | _ -> die "-raw-o only supports 2 data files, got %d" (Array.length data)
202 in
203 let diff = Q.(d2.time.q - d1.time.q) in
204 let ignore = Q.lt (Q.abs diff) min_diff in
205 if not ignore then begin
206 let pdiff = if Q.(equal zero d1.time.q) then Float.infinity
207 else Q.(to_float @@ ((of_int 100 * diff) / d1.time.q))
208 in
209 (* XXX %.4f makes sense for min_diff=1e-4 but should be smarter for other min_diff *)
210 Printf.fprintf ch "%s %s %.4f %3.2f%% %d\n"
211 d1.time.str d2.time.str (Q.to_float diff) pdiff loc.line
212 end