]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - gw-tests/genhtml/genhtml.py
Add horenmar as a co-author
[can-benchmark.git] / gw-tests / genhtml / genhtml.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 import os;
4 import dircache;
5 import sys;
6 import urllib
7 import traceback
8 import glob
9 import datetime
10
11 html_copyright = """<div style="font-size: small; color: gray; margin-top: 1em;">Authors: Michal Sojka, Pavel Píša, Martin Hořeňovský, Copyright © 2010-%d Czech Technical University in Prague</div>""" % datetime.date.today().year
12
13 class DimValue(object):
14     def __new__(cls, dim, value):
15         if value in dim:
16             return dim[value]
17         else:
18             return super(DimValue, cls).__new__(cls)
19     def __init__(self, dim, value):
20         self.dim = dim
21         self.value = value
22         self.dim.addValue(self)
23     def __str__(self):
24         return self.dim.val2str(self.value)
25     def __repr__(self):
26         return "DimValue(%s, %s)" % (repr(self.dim), repr(self.value))
27     def htmlTableHeading(self):
28         return self.dim.htmlTableHeading(self.value)
29     def index(self):
30         return self.dim.sortedKeys.index(self.value)
31
32 class DimValues(list):
33     def replace(self, val):
34         for i in xrange(len(self)):
35             if self[i].dim == val.dim:
36                 self[i] = val
37     def __add__(self, val):
38         ret = DimValues(self)
39         ret.append(val)
40         return ret
41     def __sub__(self, dim):
42         result = DimValues(self)
43         for v in self:
44             if v.dim == dim:
45                 result.remove(v)
46         return result
47     def key(self):
48         return tuple([v.value for v in self])
49
50 class Dimension(dict):
51     def __init__(self, atype, name=None):
52         self.type = atype
53         if (name):
54             self.name = name
55         else:
56             self.name = atype
57         self.sortedKeys = []
58
59     def __iter__(self):
60         for i in xrange(len(self)):
61             yield self.getValue(i)
62     def getValue(self, index):
63         return self[self.sortedKeys[index]]
64
65     def addValue(self, value):
66         if value not in self:
67             if isinstance(value, DimValue):
68                 self[value.value] = value
69             else:
70                 raise Exception("Unsupported usage of addValue")
71                 #self[value] = DimValue(self, value)
72             self.sortedKeys = self.keys()
73             self.sortedKeys.sort()
74     def val2str(self, v):
75         return str(v)
76     def htmlTableHeading(self, v):
77         return self.val2str(v)
78     def __str__(self):
79         return self.name
80     def __repr__(self):
81         return "Dimension(%s)"%self.type
82
83 class DimensionKern(Dimension):
84     def __init__(self):
85         Dimension.__init__(self, 'gwkern', 'GW kernel')
86     def htmlTableHeading(self, v):
87         i=v.find(":")
88         if i>0: kver=v[:i]
89         else: kver=v
90         return v+"<br><a href='config-%s'>config</a>"%(urllib.quote(kver))
91     def versions(self):
92         for v in self.values:
93             i=v.find(":")
94             if i>0: kver=v[:i]
95             else: kver=v
96             yield kver
97
98 class DimensionHostKern(Dimension):
99     def __init__(self):
100         Dimension.__init__(self, 'hostkern', 'Host kernel')
101     def val2str(self, v):
102         if v.find("host-") == 0:
103             v = v[5:]
104         return v
105     def htmlTableHeading(self, v):
106         v = self.val2str(v)
107         i = v.find(":")
108         if i>0: kver = v[:i]
109         else: kver = v
110         return v+"<br><a href='config-%s'>config</a>"%(urllib.quote(kver))
111     def versions(self):
112         for v in self.values:
113             i=v.find(":")
114             if i>0: kver=v[:i]
115             else: kver=v
116             yield kver
117
118 class DimensionTest(Dimension):
119     def __init__(self):
120         Dimension.__init__(self, 'test', 'Test')
121     def htmlTableHeading(self, v):
122         return v+"<br><a href='%s.sh.html'>source</a>"%(urllib.quote(v))
123
124 class DimensionLoad(Dimension):
125     def __init__(self):
126         Dimension.__init__(self, 'load', 'Load')
127
128 class DimensionTraffic(Dimension):
129     def __init__(self):
130         Dimension.__init__(self, 'traf', 'Traffic')
131     def val2str(self, v):
132         if v == "50":
133             return "50%"
134         elif v == "oneatatime":
135             return "one message at a time"
136         else:
137             return v
138     def htmlTableHeading(self, v):
139         return self.val2str(v)
140 class Test(object):
141     @classmethod
142     def isOnPath(cls, path):
143         f = os.path.join(path, 'plot.sh')
144         return os.path.isfile(f)
145     def __init__(self, path, values, tests=None):
146         self.path = path
147         self.name = os.path.basename(path)
148         self.values = values
149         self.tests = tests
150     def printThumbLink(self, file):
151 #         try:
152 #             imgs = [img for img in dircache.listdir(thumb)]
153 #         except OSError:
154 #             imgs = [ self.name + ".png" ]
155         imgs = [ 'tgraph.png' ]
156         for img in imgs:
157             print >>file, "<a href='%s/results.html'><img src='%s/%s'></a>" % \
158                   (urllib.quote(self.path), urllib.quote(self.path), img)
159     def fullImgLink(self, pngName):
160         ps = "<a href='%s'>Plot source</a>" % ("plot"+pngName[5:-4]+".gp")
161         if 'NO_PDF' in os.environ:
162             return "<div><img src='%s' />%s</div>" % (pngName, ps)
163         else:
164             return "<div><a href='%s'><img src='%s' /></a>%s</div>" % \
165                    (pngName[:-4]+".pdf", pngName, ps)
166         
167     def htmlPrintStats(self, html):
168         cwd = os.getcwd()
169         os.chdir(self.path)
170         stats = glob.glob("*-stat.txt")
171         print >>html, "<h3>Statistics</h3>"
172         print >>html, "<table><tr>"
173         stats.sort()
174         for i in stats:
175             lines = open(i).readlines()
176             def fixupLine(l):
177                 comment = l.find("#")
178                 if comment >= 0:
179                     l = l[:comment-1]
180                 if l.find("cmdline=") == 0:
181                     l = "<abbr title=%s>cmdline=...</abbr>" % str(l[8:])
182                 return l
183             lines = [fixupLine(l) for l in lines]
184             print >>html, "<td><h4>%s</h4>%s</td>" % (i, "<br />".join(lines))
185         print >>html, "</tr></table>"
186         os.chdir(cwd)
187         
188     def generateHtml(self):
189         html = open(os.path.join(self.path, 'results.html'), "w")
190         title = "CAN gateway timing analysis"
191         cdup = "../"*len(self.values)
192         print >> html, """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
193 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
194 <head>
195 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
196 <title>%s</title>
197 <link rel="stylesheet" href="%sstyle.css" type="text/css" /> 
198 </head>
199 <body>
200 <h1>%s</h1>"""  % (title, cdup, title)
201         params = ["%s %s" % (v.dim, v) for v in self.values]
202         print >>html, "Results for:", ", ".join(params)
203         print >>html, "<div class='otherview'><h4>Other results</h4><table>"
204         for d in self.tests.space:
205             links = []
206             for v in d:
207                 if v in self.values:
208                     links.append("<span class='value current'>%s</span>"%str(v))
209                 else:
210                     vv = DimValues(self.values)
211                     vv.replace(v)
212                     try:
213                         href = cdup + urllib.quote(self.tests[vv.key()].path+"/results.html")
214                         links.append("<span class='value other'><a href='%s'>%s</a></span>"%(href, str(v)))
215                     except KeyError:
216                         links.append("<span class='value missing'>%s</span>"%str(v))
217             print >>html, "<tr><th>%s</th><td>" % d, " ".join(links), "</td></tr>"
218
219         print >>html, "</table></div>"
220         print >>html, self.fullImgLink("graph.png")
221         self.htmlPrintStats(html)
222         cwd = os.getcwd()
223         os.chdir(self.path)
224         additionalImgs = glob.glob("graph?*.png")
225         if additionalImgs: print >>html, "<h3>Additional graphs</h3>"
226         for i in additionalImgs:
227             print >>html, "<h4>%s</h4>" % i[5:-4]
228             print >>html, self.fullImgLink(i)
229         os.chdir(cwd)
230         
231         print >>html, "<hr />"
232         print >>html, "<a href='./'>Raw data</a><br />"
233         print >>html, "<a href='%s'>Script source</a><br />" % (cdup+self.name+".sh.html")
234         print >>html, "<a href='%s'>Back to top</a><br />" % cdup
235         print >>html,"%s</body></html>" % html_copyright
236         html.close()
237
238 class Space(list):
239     """List of Dimensions()s (order matters)"""
240     def __init__(self, *dimensions):
241         self.extend(list(dimensions))
242     def path2dimValues(self, path):
243         coordinates = path.split("/")
244         if len(coordinates) != len(self):
245             raise KeyError("The number coordinates do not match the number of dimensions: " + str(coordinates))
246         
247         dv = DimValues([DimValue(self[i], coordinates[i]) \
248                             for i in xrange(len(coordinates))])
249         return dv
250
251     def iterValues(self):
252         idx = [0 for i in xrange(len(self))]
253         done=False
254         while not done:
255             values=DimValues()
256             for i in xrange(len(self)):
257                 values.append(self[i].values()[idx[i]])
258             yield values
259             done=True
260             for i in xrange(len(self)):
261                 idx[i] += 1
262                 if idx[i] < len(self[i]):
263                     done=False
264                     break
265                 idx[i] = 0
266     def reorder(self, dimValues):
267         reordered = DimValues()
268         for d in self:
269             for v in dimValues:
270                 if v.dim == d:
271                     reordered.append(v)
272         return reordered
273     def iterDimensionPairs(self):
274         for i in xrange(len(self)):
275             for j in xrange(i+1, len(self)):
276                 yield (self[i], self[j])
277                 yield (self[j], self[i])
278     def iterRemainingDimensions(self, dimensionPair):
279         for d in self:
280             if d not in dimensionPair:
281                 yield d
282
283
284 class Tests(dict):
285     """Represents all tests organized along several dimensions"""
286     def __init__(self, rootpath, space):
287         dict.__init__(self)
288         self.space = space
289         if (rootpath):
290             self.populate(rootpath)
291     def getTest(self, key):
292         if len(key) != len(self.space):
293             raise KeyError("The coordinates in key do not match the dimension of the space")
294         realkey = self.space.reorder(key)
295         return self[realkey.key()]
296
297     def addTest(self, test):
298         self[test.values.key()] = test
299         
300     def populate(self, rootpath):
301         for root, dirs, files in os.walk(rootpath):
302             if (root.find(rootpath) == 0):
303                 path = root[len(rootpath):]
304             else:
305                 path = rootpath
306             if Test.isOnPath(root):
307                 dv = self.space.path2dimValues(path)
308                 self.addTest(Test(root, dv, self))
309     def generateHtml(self):
310         for pair in self.space.iterDimensionPairs():
311             remDims = Space(*tuple([d for d in self.space.iterRemainingDimensions(pair)]))
312             for vals in remDims.iterValues():
313                 page = Page(pair, vals, self)
314                 print page.getName()
315                 page.generate()
316         try:
317             os.remove("index.html")
318         except OSError: pass
319         os.symlink(page.getName(), "index.html")
320         css = open("style.css", "w")
321         print >>css, """img { border: 0; }
322 table { border-collapse: collapse; }
323 th, td { border: 1px solid lightgray; padding: 4px;}
324 h4 { margin: 0; }
325 .otherview { margin: 1ex 0}
326 .otherview .value { color: black; padding: 0ex 1ex; -moz-border-radius: 1ex; border-radius: 1ex;}
327 .otherview .value a { color: inherit; text-decoration: none; }
328 .otherview .other:hover { background: #eee; }
329 .otherview .missing { color: gray; }
330 .otherview .current { background: #ccc; }
331 .na { width: 150px; height: 109px; display:  table-cell; text-align: center; vertical-align: middle; }
332 """
333         css.close()
334         for test in self.values():
335             print test.path
336             test.generateHtml()
337
338         os.system("source-highlight -d --output-dir=. %s/*.sh > /dev/null" % sh_dir)
339
340 class Page(object):
341     def __init__(self, dimPair, valsOther, tests):
342         self.dimy, self.dimx = dimPair
343         self.dimOther = [v.dim for v in valsOther]
344         self.valsOther = tests.space.reorder(valsOther)
345         self.tests = tests
346     def getName(self):
347         return "%s-vs-%s-for-%s.html"%(self.dimy.type, self.dimx.type,
348                                        "-".join(["%02d"%v.index() for v in self.valsOther]))
349     def generate(self):
350         html = open(self.getName(), "w")
351         title = "CAN gateway timing analysis" 
352         print >> html, """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
353 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
354 <head>
355 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
356 <title>%s</title>
357 <link rel="stylesheet" href="style.css" type="text/css" /> 
358 </head>
359 <body>
360 <h1>%s</h1>"""  % (title, title)
361         params = ["%s %s" % (v.dim, v) for v in self.valsOther]
362         print >>html, "<h3>Results for ", ", ".join(params), "</h3>"
363         print >>html, "<div class='otherview'><h4>Other views</h4>"
364         print >>html, "<table><tr>"
365         for d in self.dimOther:
366             print >>html, "<th>%s</th>" % d
367             print >>html, "<td><a href='%s'>&rarr;</a> " % \
368                 Page((self.dimy, d), self.valsOther - d + self.dimx.getValue(0), self.tests).getName()
369             print >>html, "<a href='%s'>&darr;</a></td>" % \
370                 Page((d, self.dimx), self.valsOther - d + self.dimy.getValue(0), self.tests).getName()
371             links = []
372             print >>html, "<td>"
373             for v in d:
374                 if v in self.valsOther:
375                     links.append("<span class='value current'>%s</span>"%str(v))
376                 else:
377                     vv = DimValues(self.valsOther)
378                     vv.replace(v)
379                     links.append("<span class='value other'><a href='%s'>%s</a></span>"%(urllib.quote(Page((self.dimy, self.dimx), vv, self.tests).getName()), str(v)))
380             print >>html, " ".join(links)
381             print >>html, "</td></tr>"
382         print >>html, "</table></div>"
383
384         print >>html, "<table><thead><tr><td>%s &rarr; <br />%s &darr;</td>" % (self.dimx.name, self.dimy.name)
385         for x in self.dimx:
386             print >>html, "<th>%s</th>" % x.htmlTableHeading()
387         print >>html, "</tr></thead>"
388         for y in self.dimy:
389             print >>html, "<tr><th>%s</th>" % y.htmlTableHeading()
390
391             for x in self.dimx:
392                 print >>html, "<td>"
393                 idx = [x,y]
394                 idx.extend(self.valsOther)
395                 try:
396                     test = tests.getTest(idx)
397                     test.printThumbLink(html)
398                 except KeyError:
399                     print >>html, "<span class='na'>N/A</span>"
400                 print >>html, "</td>"
401             print >>html, "</tr>"
402         print >> html, """
403 </table>
404 %s
405 </body>
406 """ % html_copyright
407
408
409 if __name__ == "__main__":
410     os.chdir(sys.argv[1])
411     sh_dir = sys.argv[2]
412     os.system("rm *.html")
413     tests = Tests("./", Space(DimensionHostKern(), DimensionKern(), DimensionTraffic(), DimensionLoad(), DimensionTest()))
414     tests.generateHtml()
415     sys.exit(0)