Add --html switch to benchcompare and make it generate text with ANSI codes by default
authorMikko Rasa <mikko.rasa@movial.fi>
Fri, 27 Mar 2009 12:58:24 +0000 (14:58 +0200)
committerMikko Rasa <mikko.rasa@movial.fi>
Fri, 27 Mar 2009 12:58:24 +0000 (14:58 +0200)
Improve the table layout in benchcompare

benchcompare.py

index fcc7604..9f020c6 100755 (executable)
@@ -2,45 +2,55 @@
 # -*- encoding: utf-8 -*-
 
 import sys
+import optparse
 
-order=[]
-marks={}
-
+parser=optparse.OptionParser()
+parser.add_option("-t", "--html", dest="html_output", action="store_true", default=False)
+(options, args)=parser.parse_args()
+html_output=options.html_output
 data=[]
-for fn in sys.argv[1:]:
+for fn in args:
        data.append([l[:-1].split(',') for l in file(fn)])
 
+order=[]
+marks={}
 for i in range(len(data)):
        for b in data[i]:
                if len(b)==5:
                        score=int(b[0])
                        margin=int(b[1])
                        label=b[4]
-               elif len(b)==1:
-                       score=int(b[0])
-                       margin=0
+               elif len(b)==3:
+                       score=float(b[0])
+                       margin=None
                        label="Overall"
                if label not in marks:
                        marks[label]=[(0, 0)]*len(data)
                        order.append(label)
                marks[label][i]=(score, margin)
 
-print """<?xml version="1.0" encoding="utf-8" ?>
+if html_output:
+       print """<?xml version="1.0" encoding="utf-8" ?>
 <!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">
 <head>
 <style type="text/css">
+body { background: #FFFFFF; }
 table { border-collapse: collapse; }
 tr { border-bottom: 1px dotted gray; }
-td { padding-left: 1em; }
-.best { color: green; }
-.worst { color: maroon; }
-.increase { color: lime; }
-.decrease { color: red; }
+td { padding-left: 1em; padding-right: 1em; vertical-align: top; }
+td.best { background: #E0FFE0; }
+td.worst { background: #FFE0E0; }
+.increase { color: #00E000; }
+.decrease { color: #FF0000; }
+.detail { font-size: 65%; }
 </style>
 </head>
 <body>
 <table>"""
+
+if not html_output:
+       table=[]
 for m in order:
        v=marks[m]
        scores=[i[0] for i in v if i[0]>0]
@@ -49,26 +59,55 @@ for m in order:
        high=max(scores)
        low=min(scores)
        baseline=scores[0]
-       print "<tr><td>%s</td>"%m
-       for i in range(len(data)):
-               diff=v[i][0]*100.0/baseline-100
-               if diff:
-                       if diff<0:
-                               k="decrease"
-                       elif diff>0:
-                               k="increase"
-                       diff=" (<span class=\"color:%s\">%+.1f%%</span>)"%(k, diff)
-               else:
-                       diff=""
 
-               k=""
-               if v[i][0]==high:
-                       k="best"
-               elif v[i][0]==low:
-                       k="worst"
+       if html_output:
+               print "<tr><td>%s</td>"%m
+               for i in range(len(data)):
+                       klass=""
+                       if v[i][0]==high:
+                               klass="best"
+                       elif v[i][0]==low:
+                               klass="worst"
+
+                       cell="<td class=\"%s\">"%klass
+                       cell+="%s"%(v[i][0])
+                       if v[i][1]!=None and v[i][0]:
+                               cell+=" <span class=\"detail\">±%.1f%%</span>"%(v[i][1]*100.0/v[i][0])
+
+                       if v[i][0]!=baseline:
+                               diff=v[i][0]*100.0/baseline-100
+                               if diff<0:
+                                       klass="decrease"
+                               elif diff>0:
+                                       klass="increase"
+                               cell+="<br /><span class=\"detail %s\">%+.1f%%</span>"%(klass, diff)
+
+                       print cell
+               print "</tr>"
+       else:
+               row=[(m, None)]
+               for i in range(len(data)):
+                       attr=None
+                       if v[i][0]==high:
+                               attr="32"
+                       elif v[i][0]==low:
+                               attr="31"
+                       row.append(("%s"%(v[i][0]), attr))
+               table.append(row)
 
-               print "<td><span class=\"%s\">%s</span> (±%.1f%%)%s</td>"%(k, v[i][0], v[i][1]*100.0/v[i][0], diff)
-       print "</tr>"
-print """</table>
+if html_output:
+       print """</table>
 </body>
 </html>"""
+else:
+       colw=[max(len(r[c][0]) for r in table) for c in range(len(data)+1)]
+       for row in table:
+               for col in range(len(row)):
+                       if col==0:
+                               fmt="%-*s"
+                       else:
+                               fmt="%*s"
+                       if row[col][1]:
+                               fmt="\033[%sm%s\033[0m"%(row[col][1], fmt)
+                       print fmt%(colw[col], row[col][0]),
+               print