]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - gw-tests/genhtml/genhtml.py
8906e552ee67d44c211cc36add5994851c3aad2f
[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'>Script source</a><br />" % (cdup+self.name+".sh.html")
176         print >>html, "<a href='%s'>Back to top</a><br />" % cdup
177         
178         html.close()
179
180 class Space(list):
181     """List of Dimensions()s (order matters)"""
182     def __init__(self, *dimensions):
183         self.extend(list(dimensions))
184     def iterValues(self):
185         idx = [0 for i in xrange(len(self))]
186         done=False
187         while not done:
188             values=DimValues()
189             for i in xrange(len(self)):
190                 values.append(self[i].values()[idx[i]])
191             yield values
192             done=True
193             for i in xrange(len(self)):
194                 idx[i] += 1
195                 if idx[i] < len(self[i]):
196                     done=False
197                     break
198                 idx[i] = 0
199     def reorder(self, dimValues):
200         reordered = DimValues()
201         for d in self:
202             for v in dimValues:
203                 if v.dim == d:
204                     reordered.append(v)
205         return reordered
206     def iterDimensionPairs(self):
207         for i in xrange(len(self)):
208             for j in xrange(i+1, len(self)):
209                 yield (self[i], self[j])
210                 yield (self[j], self[i])
211     def iterRemainingDimensions(self, dimensionPair):
212         for d in self:
213             if d not in dimensionPair:
214                 yield d
215
216
217 class Tests(dict):
218     """Represents all tests organized along several dimensions"""
219     def __init__(self, rootpath, space):
220         dict.__init__(self)
221         self.space = space
222         if (rootpath):
223             self.populate(rootpath)
224     def getTest(self, key):
225         if len(key) != len(self.space):
226             raise KeyError("The coordinates in key do not match the dimension of the space")
227         realkey = self.space.reorder(key)
228         return self[realkey.key()]
229
230     def addTest(self, test):
231         self[test.values.key()] = test
232         
233     def populate(self, rootpath):
234         for root, dirs, files in os.walk(rootpath):
235             if (root.find(rootpath) == 0):
236                 coordinates = root[len(rootpath):].split("/")
237             else:
238                 coordinates = rootpath.split("/")
239             if Test.isOnPath(root):
240                 if len(coordinates) != len(self.space):
241                     raise KeyError("The number coordinates do not match the number of dimensions: " + str(coordinates))
242                 dv = DimValues([DimValue(self.space[i], coordinates[i]) for i in xrange(len(coordinates))])
243                 self.addTest(Test(root, dv, self))
244     def generateHtml(self):
245         for pair in self.space.iterDimensionPairs():
246             remDims = Space(*tuple([d for d in self.space.iterRemainingDimensions(pair)]))
247             for vals in remDims.iterValues():
248                 page = Page(pair, vals, self)
249                 print page.getName()
250                 page.generate()
251         try:
252             os.remove("index.html")
253         except OSError: pass
254         os.symlink(page.getName(), "index.html")
255         css = open("style.css", "w")
256         print >>css, """img { border: 0; }
257 table { border-collapse: collapse; }
258 th, td { border: 1px solid lightgray; padding: 4px;}
259 h4 { margin: 0; }
260 .box { border: 1px solid black; padding: 1ex; margin: 1ex 0}
261 """
262         css.close()
263         for test in self.values():
264             print test.path
265             test.generateHtml()
266
267         os.system("source-highlight -d --output-dir=. ../*.sh > /dev/null")
268
269 class Page:
270     def __init__(self, dimPair, valsOther, tests):
271         self.dimy, self.dimx = dimPair
272         self.dimOther = [v.dim for v in valsOther]
273         self.valsOther = tests.space.reorder(valsOther)
274         self.tests = tests
275     def getName(self):
276         return "%s-vs-%s-for-%s.html"%(self.dimy.type, self.dimx.type,
277                                        "-".join([v.value for v in self.valsOther]))
278     def generate(self):
279         html = open(self.getName(), "w")
280         title = "CAN gateway timing analysis" 
281         print >> html, """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
282 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
283 <head>
284 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
285 <title>%s</title>
286 <link rel="stylesheet" href="style.css" type="text/css" /> 
287 </head>
288 <body>
289 <h1>%s</h1>"""  % (title, title)
290         params = ["%s %s" % (v.dim, v) for v in self.valsOther]
291         print >>html, "<h3>Results for ", ", ".join(params), "</h3>"
292         print >>html, "<div class='box'><h4>Other views</h4>"
293         for d in self.dimOther:
294             print >>html, "%s: " % d
295             print >>html, "<a href='%s'>X axis</a>, " % \
296                 Page((self.dimy, d), self.valsOther - d + self.dimx.getValue(0), self.tests).getName()
297             print >>html, "<a href='%s'>Y axis</a>;&nbsp;&nbsp;" % \
298                 Page((d, self.dimx), self.valsOther - d + self.dimy.getValue(0), self.tests).getName()
299             links = []
300             for v in d.values():
301                 if v in self.valsOther:
302                     links.append(str(v))
303                 else:
304                     vv = DimValues(self.valsOther)
305                     vv.replace(v)
306                     links.append("<a href='%s'>%s</a>"%(urllib.quote(Page((self.dimy, self.dimx), vv, self.tests).getName()), str(v)))
307             print >>html, " | ".join(links)
308             print >>html, "<br>"
309             try:
310                 print >>html, d.htmlPreamble()
311             except Exception:
312                 pass
313         print >>html, "</div>"
314
315         print >>html, "<table><thead><tr><td>%s &rarr; <br />%s &darr;</td>" % (self.dimx.name, self.dimy.name)
316         for x in self.dimx:
317             print >>html, "<th>%s</th>" % x.htmlTableHeading()
318         print >>html, "</tr></thead>"
319         for y in self.dimy:
320             print >>html, "<tr><th>%s</th>" % y.htmlTableHeading()
321
322             for x in self.dimx:
323                 print >>html, "<td>"
324                 idx = [x,y]
325                 idx.extend(self.valsOther)
326                 try:
327                     test = tests.getTest(idx)
328                     test.printThumbLink(html)
329                 except KeyError:
330                     print >>html, "N/A"
331                 print >>html, "</td>"
332             print >>html, "</tr>"
333         print >> html, """
334 </table>
335 <div style="font-size: small; color: gray; margin-top: 1em;">Authors: Michal Sojka, Pavel Píša, Copyright © 2010 Czech Technical University in Prague</div>
336 </body>
337 """
338
339
340 if __name__ == "__main__":
341     os.chdir(sys.argv[1])
342     os.system("rm *.html")
343     tests = Tests("./", Space(DimensionHostKern(), DimensionKern(), DimensionTraffic(), DimensionTest()))
344     tests.generateHtml()
345     sys.exit(0)