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