tests: Hack res2html.sh
[gfxprim.git] / tests / framework / log2html.py
blob2263373da68fec2c29f621c82023da8ffbe44394
1 #!/usr/bin/env python
4 # Script to convert testsuite JSON log into html page
6 from sys import argv
7 import json
10 # Test result to html color dict
12 html_colors = {
13 'Success': '#008000',
14 'Skipped': '#888888',
15 'Untested': '#0000bb',
16 'Internal Error': '#800000',
17 'Segmentation Fault': '#e00000',
18 'Timeout': '#800080',
19 'Aborted': '#e00000',
20 'FP Exception': '#e00000',
21 'Memory Leak': '#a0a000',
22 'Failed': '#e00000'
26 # Convert bytes to human-readable string
28 def bytes_conv(size):
29 if (size < 512):
30 return "%iB" % (size)
32 if (size < 1024 * 512):
33 return "%.2fkB" % (float(size) / 1024)
35 if (size < 1024 * 1024 * 512):
36 return "%.2fMB" % (float(size) / 1024 / 1024)
38 return "%.2fGB" % (float(size) / 1024 / 1024 / 1024)
41 # Malloc statistics Class created from JSON dict
43 class MallocStats:
44 def __init__(self, malloc_stats):
45 self.total_size = malloc_stats["Total Size"]
46 self.total_chunks = malloc_stats["Total Chunks"]
47 self.max_size = malloc_stats["Max Size"]
48 self.max_chunks = malloc_stats["Max Chunks"]
49 self.lost_size = malloc_stats["Lost Size"]
50 self.lost_chunks = malloc_stats["Lost Chunks"]
52 def html(self):
53 print(' <tr>')
54 print(' <td bgcolor="#ffffcc" colspan="3">')
55 print(' <center>')
56 print(' <table>')
58 # Table header
59 print(' <tr>')
61 print(' <td bgcolor="#ffffaa">')
62 print(' <small>Total size</small>')
63 print(' </td>')
65 print(' <td bgcolor="#ffffaa">')
66 print(' <small>Total chunks</small>')
67 print(' </td>')
69 print(' <td bgcolor="#ffffaa">')
70 print(' <small>Max size</small>')
71 print(' </td>')
73 print(' <td bgcolor="#ffffaa">')
74 print(' <small>Max chunks</small>')
75 print(' </td>')
77 print(' <td bgcolor="#ffffaa">')
78 print(' <small>Lost size</small>')
79 print(' </td>')
81 print(' <td bgcolor="#ffffaa">')
82 print(' <small>Lost chunks</small>')
83 print(' </td>')
85 print(' </tr>')
87 # Malloc data
88 print(' <tr>')
90 print(' <td bgcolor="#ffffaa">')
91 print(' <center><small>%s</small></center>' %
92 bytes_conv(self.total_size))
93 print(' </td>')
95 print(' <td bgcolor="#ffffaa">')
96 print(' <center><small>%s</small></center>' %
97 bytes_conv(self.total_chunks))
98 print(' </td>')
100 print(' <td bgcolor="#ffffaa">')
101 print(' <center><small>%s</small></center>' %
102 bytes_conv(self.max_size))
103 print(' </td>')
105 print(' <td bgcolor="#ffffaa">')
106 print(' <center><small>%s</small></center>' %
107 bytes_conv(self.max_chunks))
108 print(' </td>')
110 print(' <td bgcolor="#ffffaa">')
111 print(' <center><small>%s</small></center>' %
112 bytes_conv(self.lost_size))
113 print(' </td>')
115 print(' <td bgcolor="#ffffaa">')
116 print(' <center><small>%s</small></center>' %
117 bytes_conv(self.lost_chunks))
118 print(' </td>')
120 print(' </tr>')
122 print(' </table>')
123 print(' </center>')
124 print(' </td>')
125 print(' </tr>')
128 # Benchmark statistics Class created from JSON dict
130 class BenchmarkData:
131 def __init__(self, bench_data):
132 self.time_mean = bench_data["Time Mean"]
133 self.time_variance = bench_data["Time Variance"]
134 self.iterations = bench_data["Iterations"]
136 def html(self):
137 print(' <tr>')
138 print(' <td bgcolor="#fd8" colspan="3">')
139 print(' <center>')
140 print(' <table>')
142 # Table header
143 print(' <tr>')
145 print(' <td colspan="2" bgcolor="#fb2">')
146 print(' <center><small>Benchmark data</small></center>')
147 print(' </td>')
149 print(' </tr>')
151 print(' <tr>')
153 print(' <td bgcolor="#fc4">')
154 print(' <center><small>Iterations</small></center>')
155 print(' </td>')
157 print(' <td bgcolor="#fc4">')
158 print(' <center><small>Mean &#x2213; Variance</small></center>')
159 print(' </td>')
161 print(' </tr>')
163 # Benchmark data
164 print(' <tr>')
166 print(' <td bgcolor=\"#fc4\">')
167 print(' <small>%i</small>' % (self.iterations))
168 print(' </td>')
170 print(' <td bgcolor=\"#fc4\">')
171 print(' <small>%.6fs &#x2213; %.6fs</small>' %
172 (self.time_mean, self.time_variance))
173 print(' </td>')
175 print(' </tr>')
177 print(' </table>')
178 print(' </center>')
179 print(' </td>')
180 print(' </tr>')
183 # Test Result Class created from JSON dict
185 class TestResult:
186 def __init__(self, test_result):
187 self.name = test_result["Test Name"]
188 self.result = test_result["Test Result"]
189 self.reports = test_result["Test Reports"]
190 self.cpu_time = test_result["CPU Time"]
191 self.run_time = test_result["Run Time"]
192 self.test_reports = test_result["Test Reports"]
194 if ("Malloc Stats" in test_result):
195 self.malloc_stats = MallocStats(test_result["Malloc Stats"])
197 if ("Benchmark" in test_result):
198 self.bench_data = BenchmarkData(test_result["Benchmark"])
200 def html(self, bg_color):
201 # Print test result
202 print(' <tr>')
204 print(' <td bgcolor="%s">%s&nbsp;</td>' % (bg_color, self.name))
206 print(' <td bgcolor="%s">' % (bg_color))
207 print(' <center><small><font color=\"#222\">')
208 print(' %.3fs %.3fs' % (self.run_time, self.cpu_time))
209 print(' </font></small></center>')
210 print(' </td>')
212 print(' <td bgcolor="%s">' % (html_colors[self.result]))
213 print(' <center><font color="white">&nbsp;%s&nbsp;</center>' %
214 (self.result))
215 print(' </td>')
217 print(' </tr>')
219 # Add malloc statistics, if present
220 if (hasattr(self, 'malloc_stats')):
221 self.malloc_stats.html()
223 # And benchmark data
224 if (hasattr(self, 'bench_data')):
225 self.bench_data.html()
227 # And test messages
228 if (self.test_reports):
229 print(' <tr>')
230 print(' <td colspan=\"3\" bgcolor=\"#eeeeee\">')
232 for msg in self.test_reports:
233 print(' &nbsp;&nbsp;<small>%s</small><br>' % (msg))
235 if (self.test_reports):
236 print(' </td>')
237 print(' </tr>')
239 class TestSuite:
240 def __init__(self, testsuite_result):
241 self.suite_name = testsuite_result["Suite Name"]
242 self.test_results = []
244 for test_result in testsuite_result["Test Results"]:
245 self.test_results.append(TestResult(test_result))
247 def html(self):
248 print('<html>')
249 print(' <head>')
250 print(' </head>')
251 print(' <body>')
253 print(' <table bgcolor="#99a">')
254 print(' <tr>')
255 print(' <td colspan="3" bgcolor="#bbbbff">');
256 print(' <center><b>%s</b></center>' % (self.suite_name))
257 print(' </td>')
258 print(' </tr>')
260 flag = False;
262 for tst in self.test_results:
263 if (flag):
264 bg_color = '#ccccee'
265 else:
266 bg_color = '#ddddee'
268 flag = not flag
270 tst.html(bg_color)
272 print(' </table>')
274 print(' </body>')
275 print('</html>')
277 def results(self):
278 res_dict = {}
280 for i in html_colors:
281 res_dict[i] = 0
283 for tst in self.test_results:
284 res_dict[tst.result] = res_dict[tst.result] + 1;
286 return res_dict
288 # Creates table row with a link to results page
289 def html_summary(self, link):
290 print(' <tr>')
292 res_dict = self.results()
294 test_ok = res_dict['Success']
295 test_ok += res_dict['Skipped']
296 test_ok += res_dict['Untested']
298 test_all = len(self.test_results)
300 if (test_ok < test_all):
301 bg_color = html_colors['Failed']
302 else:
303 bg_color = '#ccccee'
307 print(' <td bgcolor="#ccccee">%s</td>' % (self.suite_name))
308 print(' <td bgcolor="%s">%i</td>' % (bg_color, test_all - test_ok))
310 test_skipped = res_dict['Skipped']
312 if (test_skipped > 0):
313 bg_color = html_colors['Skipped']
314 else:
315 bg_color = '#ccccee'
317 print(' <td bgcolor="%s">%i</td>' % (bg_color, test_skipped))
318 print(' <td bgcolor="#ccccee">%i</td>' % (test_all))
319 print(' <td bgcolor="#ccccee"><small><a href="%s">Details</a></small></td>' % (link))
320 print(' </tr>')
322 def main():
323 filename = 'log.json'
324 summary = False
325 pars = 1
326 link = ''
328 if (len(argv) > 1 and argv[1] == '-s'):
329 link = argv[2]
330 pars = 3
331 summary = True
333 if (len(argv) > pars):
334 filename = argv[pars]
336 # parse JSON
337 f = open(filename)
338 data = json.load(f)
339 f.close()
341 # convert to python objects
342 test_suite = TestSuite(data)
344 if (summary):
345 test_suite.html_summary(link)
346 else:
347 test_suite.html()
349 if __name__ == '__main__':
350 main()