Plot graphs with cairo - it is faster and nicer
[can-benchmark.git] / gw-tests / genhtml / genhtml.py
index 5dacc9e..be1bb4e 100755 (executable)
@@ -4,15 +4,22 @@ import os;
 import dircache;
 import sys;
 import urllib
+import traceback
 
-class DimValue:
+class DimValue(object):
+    def __new__(cls, dim, value):
+        if value in dim:
+            return dim[value]
+        else:
+            return super(DimValue, cls).__new__(cls, dim, value)
     def __init__(self, dim, value):
        self.dim = dim
        self.value = value
+        self.dim.addValue(self)
     def __str__(self):
         return self.dim.val2str(self.value)
     def __repr__(self):
-       return "DimValue("+str(self.dim.__class__.__name__) + ", "+repr(self.value)+")"
+       return "DimValue(%s, %s)" % (repr(self.dim), repr(self.value))
     def htmlTableHeading(self):
        return self.dim.htmlTableHeading(self.value)
 
@@ -22,14 +29,17 @@ class DimValues(list):
             if self[i].dim == val.dim:
                 self[i] = val
     def __add__(self, val):
-        return DimValues(self).append(val)
+        ret = DimValues(self)
+        ret.append(val)
+        return ret
     def __sub__(self, dim):
         result = DimValues(self)
-        print repr(result)
         for v in self:
             if v.dim == dim:
                 result.remove(v)
         return result
+    def key(self):
+        return tuple([v.value for v in self])
 
 class Dimension(dict):
     def __init__(self, atype, name=None):
@@ -44,15 +54,17 @@ class Dimension(dict):
        for i in xrange(len(self)):
            yield self.getValue(i)
     def getValue(self, index):
-        print self.sortedKeys
         return self[self.sortedKeys[index]]
 
-    def addValue(self, *values):
-       for value in values:
-           if value not in self:
-               self[value] = DimValue(self, value)
-       self.sortedKeys = self.keys()
-       self.sortedKeys.sort()
+    def addValue(self, value):
+        if value not in self:
+            if isinstance(value, DimValue):
+                self[value.value] = value
+            else:
+                raise Exception("Unsupported usage of addValue")
+                #self[value] = DimValue(self, value)
+            self.sortedKeys = self.keys()
+            self.sortedKeys.sort()
     def val2str(self, v):
         return str(v)
     def htmlTableHeading(self, v):
@@ -60,7 +72,7 @@ class Dimension(dict):
     def __str__(self):
         return self.name
     def __repr__(self):
-        return self.type
+        return "Dimension(%s)"%self.type
 
 class DimensionKern(Dimension):
     def __init__(self):
@@ -103,6 +115,10 @@ class DimensionTest(Dimension):
     def htmlTableHeading(self, v):
        return v+"<br><a href='%s.sh.html'>source</a>"%(urllib.quote(v))
 
+class DimensionLoad(Dimension):
+    def __init__(self):
+       Dimension.__init__(self, 'load', 'Load')
+
 class DimensionTraffic(Dimension):
     def __init__(self):
        Dimension.__init__(self, 'traf', 'Traffic')
@@ -115,14 +131,16 @@ class DimensionTraffic(Dimension):
             return v
     def htmlTableHeading(self, v):
         return self.val2str(v)
-class Test:
+class Test(object):
     @classmethod
     def isOnPath(cls, path):
-       f = os.path.join(path, '.results')
+       f = os.path.join(path, 'plot.sh')
        return os.path.isfile(f)
-    def __init__(self, path):
+    def __init__(self, path, values, tests=None):
        self.path = path
         self.name = os.path.basename(path)
+        self.values = values
+        self.tests = tests
     def printThumbLink(self, file):
         thumb = self.path+'/thumb'
         try:
@@ -130,89 +148,157 @@ class Test:
         except OSError:
             imgs = [ self.name + ".png" ]
         for img in imgs:
-            print >>file, "<a href='%s/%s'><img src='%s/thumb/%s'></a>" % \
-                  (urllib.quote(self.path), img, urllib.quote(self.path), img)
+            print >>file, "<a href='%s/results.html'><img src='%s/thumb/%s'></a>" % \
+                  (urllib.quote(self.path), urllib.quote(self.path), img)
+    def generateHtml(self):
+        html = open(os.path.join(self.path, 'results.html'), "w")
+       title = "CAN gateway timing analysis"
+        cdup = "../"*len(self.values)
+       print >> html, """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
+<head>
+<meta http-equiv="content-type" content="text/html; charset=utf-8" />
+<title>%s</title>
+<link rel="stylesheet" href="%sstyle.css" type="text/css" /> 
+</head>
+<body>
+<h1>%s</h1>"""  % (title, cdup, title)
+        params = ["%s %s" % (v.dim, v) for v in self.values]
+        print >>html, "Results for:", ", ".join(params)
+        print >>html, "<div class='otherview'><h4>Other results</h4><table>"
+       for d in self.tests.space:
+            links = []
+            for v in d:
+                if v in self.values:
+                    links.append("<span class='value current'>%s</span>"%str(v))
+                else:
+                    vv = DimValues(self.values)
+                    vv.replace(v)
+                    try:
+                        href = cdup + urllib.quote(self.tests[vv.key()].path+"/results.html")
+                        links.append("<span class='value other'><a href='%s'>%s</a></span>"%(href, str(v)))
+                    except KeyError:
+                        links.append("<span class='value missing'>%s</span>"%str(v))
+            print >>html, "<tr><th>%s</th><td>" % d, " ".join(links), "</td></tr>"
+
+        print >>html, "</table></div>"
+        print >>html, "<div><img src='%s' /></div>" % (self.name+".png")
+        print >>html, "<a href='./'>Raw data</a><br />"
+        print >>html, "<a href='%s'>Script source</a><br />" % (cdup+self.name+".sh.html")
+        print >>html, "<a href='%s'>Back to top</a><br />" % cdup
+        
+        html.close()
+
+class Space(list):
+    """List of Dimensions()s (order matters)"""
+    def __init__(self, *dimensions):
+        self.extend(list(dimensions))
+    def path2dimValues(self, path):
+        coordinates = path.split("/")
+        if len(coordinates) != len(self):
+            raise KeyError("The number coordinates do not match the number of dimensions: " + str(coordinates))
+        
+        dv = DimValues([DimValue(self[i], coordinates[i]) \
+                            for i in xrange(len(coordinates))])
+        return dv
+
+    def iterValues(self):
+        idx = [0 for i in xrange(len(self))]
+        done=False
+        while not done:
+            values=DimValues()
+            for i in xrange(len(self)):
+                values.append(self[i].values()[idx[i]])
+            yield values
+            done=True
+            for i in xrange(len(self)):
+                idx[i] += 1
+                if idx[i] < len(self[i]):
+                    done=False
+                    break
+                idx[i] = 0
+    def reorder(self, dimValues):
+        reordered = DimValues()
+        for d in self:
+           for v in dimValues:
+               if v.dim == d:
+                   reordered.append(v)
+        return reordered
+    def iterDimensionPairs(self):
+       for i in xrange(len(self)):
+           for j in xrange(i+1, len(self)):
+               yield (self[i], self[j])
+               yield (self[j], self[i])
+    def iterRemainingDimensions(self, dimensionPair):
+       for d in self:
+           if d not in dimensionPair:
+               yield d
 
-def iterDimValues(dimensions):
-    idx = [0 for i in xrange(len(dimensions))]
-    done=False
-    while not done:
-       values=DimValues()
-       for i in xrange(len(dimensions)):
-           values.append(dimensions[i].values()[idx[i]])
-       yield values
-       done=True
-       for i in xrange(len(dimensions)):
-           idx[i] += 1
-           if idx[i] < len(dimensions[i]):
-               done=False
-               break
-           idx[i] = 0
 
 class Tests(dict):
     """Represents all tests organized along several dimensions"""
-    def __init__(self, rootpath, *dimensions):
+    def __init__(self, rootpath, space):
        dict.__init__(self)
-       self.dimensions = dimensions
+       self.space = space
        if (rootpath):
            self.populate(rootpath)
     def getTest(self, key):
-       realkey=[]
-       for d in self.dimensions:
-           for i in key:
-               if i.dim == d:
-                   realkey.append(i.value)
-       if len(realkey) != len(self.dimensions):
-           raise KeyError("The coordinates in key do not match dimensions")
-       return self[tuple(realkey)]
-
-    def addTest(self, test, coordinates):
-       if len(coordinates) != len(self.dimensions):
-           raise KeyError("The number coordinates do not match the number of dimensions: " + str(coordinates))
-       self[tuple(coordinates)] = test
-       for i in xrange(len(coordinates)):
-           self.dimensions[i].addValue(coordinates[i])
+       if len(key) != len(self.space):
+           raise KeyError("The coordinates in key do not match the dimension of the space")
+        realkey = self.space.reorder(key)
+       return self[realkey.key()]
 
+    def addTest(self, test):
+       self[test.values.key()] = test
+       
     def populate(self, rootpath):
        for root, dirs, files in os.walk(rootpath):
            if (root.find(rootpath) == 0):
-               coordinates = root[len(rootpath):]
+               path = root[len(rootpath):]
            else:
-               coordinates = rootpath
+               path = rootpath
            if Test.isOnPath(root):
-               self.addTest(Test(root), coordinates.split("/"))
-    def iterDimensionPairs(self):
-       for i in xrange(len(self.dimensions)):
-           for j in xrange(i+1, len(self.dimensions)):
-               yield (self.dimensions[i], self.dimensions[j])
-               yield (self.dimensions[j], self.dimensions[i])
-    def iterRemainingDimensions(self, dimensionPair):
-       for d in self.dimensions:
-           if d not in dimensionPair:
-               yield d
+                dv = self.space.path2dimValues(path)
+               self.addTest(Test(root, dv, self))
     def generateHtml(self):
-       for pair in self.iterDimensionPairs():
-           remDims = [d for d in self.iterRemainingDimensions(pair)]
-           for vals in iterDimValues(remDims):
-               page = Page(pair, remDims, vals, self)
+       for pair in self.space.iterDimensionPairs():
+           remDims = Space(*tuple([d for d in self.space.iterRemainingDimensions(pair)]))
+           for vals in remDims.iterValues():
+               page = Page(pair, vals, self)
+                print page.getName()
                page.generate()
        try:
            os.remove("index.html")
        except OSError: pass
        os.symlink(page.getName(), "index.html")
+        css = open("style.css", "w")
+        print >>css, """img { border: 0; }
+table { border-collapse: collapse; }
+th, td { border: 1px solid lightgray; padding: 4px;}
+h4 { margin: 0; }
+.otherview { margin: 1ex 0}
+.otherview .value { color: black; padding: 0ex 1ex; -moz-border-radius: 1ex; border-radius: 1ex;}
+.otherview .value a { color: inherit; text-decoration: none; }
+.otherview .other:hover { background: #eee; }
+.otherview .missing { color: gray; }
+.otherview .current { background: #ccc; }
+"""
+        css.close()
+        for test in self.values():
+            print test.path
+            test.generateHtml()
 
-       #os.system("source-highlight -d --output-dir=. ../*.sh")
+       os.system("source-highlight -d --output-dir=. ../*.sh > /dev/null")
 
-class Page:
-    def __init__(self, dimPair, dimOther, valsOther, tests):
+class Page(object):
+    def __init__(self, dimPair, valsOther, tests):
        self.dimy, self.dimx = dimPair
-       self.dimOther = dimOther
-       self.valsOther = valsOther
+       self.dimOther = [v.dim for v in valsOther]
+       self.valsOther = tests.space.reorder(valsOther)
        self.tests = tests
-    def getName(self, dimy=None, dimx=None, valsOther=None):
-       return "%s-vs-%s-for-%s.html"%(dimx or self.dimy.type,
-                                       dimy or self.dimx.type,
-                                       "-".join([v.value for v in valsOther or self.valsOther]))
+    def getName(self):
+       return "%s-vs-%s-for-%s.html"%(self.dimy.type, self.dimx.type,
+                                       "-".join([v.value for v in self.valsOther]))
     def generate(self):
        html = open(self.getName(), "w")
        title = "CAN gateway timing analysis" 
@@ -221,36 +307,32 @@ class Page:
 <head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 <title>%s</title>
-<style>
-img { border: 0; }
-table { border-collapse: collapse; }
-th, td { border: 1px solid lightgray; padding: 4px;}
-</style>
+<link rel="stylesheet" href="style.css" type="text/css" /> 
 </head>
 <body>
 <h1>%s</h1>"""  % (title, title)
         params = ["%s %s" % (v.dim, v) for v in self.valsOther]
-        print >>html, "Results for:", ", ".join(params), "<hr />"
+        print >>html, "<h3>Results for ", ", ".join(params), "</h3>"
+        print >>html, "<div class='otherview'><h4>Other views</h4>"
+        print >>html, "<table><tr>"
        for d in self.dimOther:
-            print >>html, "%s: " % d
-            print >>html, "<a href='%s'>X axis</a>, " % \
-                self.getName(dimx=d,
-                             valsOther=self.valsOther - d + self.dimx.getValue(0))
-            print >>html, "<a href='%s'>Y axis</a>;&nbsp;&nbsp;" % self.getName(dimy=d)
+            print >>html, "<th>%s</th>" % d
+            print >>html, "<td><a href='%s'>&rarr;</a> " % \
+                Page((self.dimy, d), self.valsOther - d + self.dimx.getValue(0), self.tests).getName()
+            print >>html, "<a href='%s'>&darr;</a></td>" % \
+                Page((d, self.dimx), self.valsOther - d + self.dimy.getValue(0), self.tests).getName()
             links = []
-            for v in d.values():
+            print >>html, "<td>"
+            for v in d:
                 if v in self.valsOther:
-                    links.append(str(v))
+                    links.append("<span class='value current'>%s</span>"%str(v))
                 else:
                     vv = DimValues(self.valsOther)
                     vv.replace(v)
-                    links.append("<a href='%s'>%s</a>"%(urllib.quote(self.getName(valsOther=vv)), str(v)))
-            print >>html, " | ".join(links)
-            print >>html, "<br>"
-            try:
-                print >>html, d.htmlPreamble()
-            except Exception:
-                pass
+                    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)))
+            print >>html, " ".join(links)
+            print >>html, "</td></tr>"
+        print >>html, "</table></div>"
 
        print >>html, "<table><thead><tr><td>%s &rarr; <br />%s &darr;</td>" % (self.dimx.name, self.dimy.name)
        for x in self.dimx:
@@ -280,6 +362,6 @@ th, td { border: 1px solid lightgray; padding: 4px;}
 if __name__ == "__main__":
     os.chdir(sys.argv[1])
     os.system("rm *.html")
-    tests = Tests("./", DimensionHostKern(), DimensionKern(), DimensionTraffic(), DimensionTest())
+    tests = Tests("./", Space(DimensionHostKern(), DimensionKern(), DimensionTraffic(), DimensionLoad(), DimensionTest()))
     tests.generateHtml()
     sys.exit(0)