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