4 # Copyright (C) 2012, 2014 Michal Sojka <sojka@os.inf.tu-dresden.de>
5 # Licensed under the GNU Library General Public License, version 2.
6 # See the included file named LICENSE for license information.
8 # This script converts a sequence of wvtest protocol outputs with
9 # results of performance measurements to interactive graphs (HTML +
10 # JavaScript). An example can be seen at
11 # http://os.inf.tu-dresden.de/~sojka/nul/performance.html.
13 from __future__ import print_function
24 re_prefix = "\([0-9]+\) (?:# )?"
25 re_date = re.compile('^Date: (.*)')
26 re_testing = re.compile('^('+re_prefix+')?\s*Testing "(.*)" in (.*):\s*$')
27 re_commit = re.compile('.*(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*, commit: (.*)')
28 re_commithash = re.compile('([0-9a-f]{7})(-dirty)? \(')
29 re_assertion = re.compile('^('+re_prefix+')?!\s*(.*?)\s+(\S+)\s*$')
30 re_perf = re.compile('^('+re_prefix+')?!\s*(.*?)\s+PERF:\s*(.*?)\s+(\S+)\s*$')
31 re_perfaxis = re.compile('axis="([^"]+)"')
34 d = time.gmtime(time.mktime(date))
35 return int(time.mktime(d))*1000
39 def __init__(self, name=None, units=None):
44 if self.units and self.name:
45 return "%s [%s]" % (self.name, self.units)
50 def __repr__(self): return "Axis(name=%s units=%s, id=%s)" % (self.name, self.units, hex(id(self)))
53 def __init__(self, name, units, axis):
57 def __repr__(self): return "Column(name=%s units=%s axis=%s)" % (self.name, self.units, repr(self.axis))
60 def __init__(self, graph, date):
63 def __getitem__(self, column):
65 return dict.__getitem__(self, column)
69 return dateConv(self.date)
72 def __init__(self, id, title):
74 self.columns_ordered = []
80 self.axes_ordered = []
81 self.dataname = title.translate(string.maketrans(" /", "--"), "\"':,+()").lower()+".json"
83 def __getitem__(self, date):
85 rownum = self.date2row[date]
87 rownum = len(self.rows)
88 self.date2row[date] = rownum
90 return self.rows[rownum]
92 self.rows[rownum:rownum] = [Row(self, date)]
93 return self.rows[rownum]
95 def addValue(self, date, col, val, units):
98 if col not in self.columns:
99 axis=self.getAxis(units) or self.addAxis(units, Axis(units=units))
100 column = Column(col, units, axis)
101 self.columns[col] = column
102 self.columns_ordered.append(column)
104 column = self.columns[col]
105 self.columns_ordered.remove(column)
106 self.columns_ordered.append(column)
107 self.columns[col].units=units
108 self.columns[col].axis.units=units
110 def addAxis(self, key, axis):
111 self.axes[key] = axis
114 def setAxis(self, col, key):
115 self.columns[col].axis = self.getAxis(key) or self.addAxis(key, Axis(name=key))
117 def getAxis(self, key):
118 if key not in self.axes: return None
119 return self.axes[key]
121 def findRanges(self):
122 for axis in list(self.axes.values()):
123 cols = [col for col in list(self.columns.values()) if col.axis == axis]
128 values = np.array([row[col.name] for row in self.rows if row[col.name] != None], np.float64)
129 if low == None and high == None:
130 lastmonth = values[-30:]
131 median = np.median(lastmonth)
135 if (values > high).any() or (values < low).any():
138 axis.yrange_max = high
139 axis.yrange_min = low
141 axis.yrange_max = None
142 axis.yrange_min = None
144 def fixupAxisNumbers(self):
145 # Sort axes according to the columns and number them
147 for column in self.columns_ordered:
149 if axis not in self.axes_ordered:
150 self.axes_ordered.insert(0, axis)
154 for axis in self.axes_ordered:
158 def options_json(self):
169 'verticalAlign': "top",
174 'formatter': "FUN(tooltip_formatter)END",
176 'whiteSpace': 'normal',
183 'click': "FUN(series_onclick)END",
192 'labels': { 'align': 'right',
194 'title': { 'text': axis.getLabel() },
195 'min': axis.yrange_min,
196 'max': axis.yrange_max,
197 } for axis in self.axes_ordered],
198 'series': [{ 'name': '%s [%s]' % (col.name, col.units),
199 'yAxis': col.axis.num }
200 for col in self.columns_ordered]
202 return json.dumps(options, indent=True).replace('"FUN(', '').replace(')END"', '')
205 data = [[[row.getDate(), row[col.name]] for row in self.rows] for col in self.columns_ordered]
206 return json.dumps(data).replace('], [', '],\n[')
216 date = time.localtime(time.time())
218 for line in sys.stdin:
221 match = re_date.match(line)
223 date = time.strptime(match.group(1), "%a, %d %b %Y %H:%M:%S +0200")
226 match = re_testing.match(line)
228 what = match.group(2)
229 where = match.group(3)
231 match = re_commit.match(what)
233 date = time.strptime(match.group(1), "%Y-%m-%d %H:%M:%S")
234 commit = match.group(2)
235 match = re_commithash.search(commit);
237 commithash = match.group(1)
240 date2commit[dateConv(date)] = commithash
241 commit2msg[commithash] = commit
243 (basename, ext) = os.path.splitext(os.path.basename(where))
245 if what != "all": title = what
246 else: title = basename
248 graph = graphs[basename]
250 graph = Graph(basename, title)
251 graphs[basename] = graph
254 match = re_perf.match(line)
256 perfstr = match.group(3)
257 perf = perfstr.split()
265 if '=' in units: units = None
268 if match.group(4) != "ok":
271 graph.addValue(date, col, val, units)
273 match = re_perfaxis.search(perfstr)
275 graph.setAxis(col, match.group(1));
277 graphs = [g for g in list(graphs.values()) if len(g.columns)]
278 graphs = sorted(graphs, key=lambda g: g.title.lower())
288 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
289 <title>NUL Performance Plots</title>
291 <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
292 <script type="text/javascript">
293 function tooltip_formatter() {
294 var s = '<b>'+ Highcharts.dateFormat('%a, %d %b %Y %H:%M:%S', this.x) +'</b><br/>';
295 s += commit2msg[date2commit[this.x]];
296 $.each(this.points, function(i, point) {
297 s += '<br/><span style="color:'+ point.series.color+';">'+ point.series.name +'</span>: '+point.y;
301 function series_onclick(event) {
302 var prevpoint = null;
303 for (var i in this.data) {
304 if (event.point == this.data[i]) {
305 if (i > 0) prevpoint = this.data[i-1];
309 if (prevpoint && date2commit[prevpoint.x] != date2commit[event.point.x])
310 window.location = "https://github.com/TUD-OS/NUL/compare/"+date2commit[prevpoint.x]+'...'+date2commit[event.point.x];
312 window.location = "https://github.com/TUD-OS/NUL/commit/"+date2commit[event.point.x];
315 def make_int_keys(json):
316 r = re.compile('"(\d+)"(.*)')
318 for match in r.finditer(json):
319 s += match.expand('\\1\\2')
322 print("var date2commit = {%s};" % ",\n".join(["%d: '%s'" % (k, date2commit[k]) for k in sorted(date2commit.keys())]))
323 print("var commit2msg = %s;" % json.dumps(commit2msg, indent=True))
324 # for d in sorted(date2commit.keys()):
326 # print('\t%d: { msg: "%s", hash: "%s" },' % (1000*time.mktime(d), v[0].replace('"', '\\"'), str(v[1]).replace('"', '\\"')))
329 <script type="text/javascript" src="js/highstock.js"></script>
333 <h1>NUL Performance Plots</h1>
334 <p>The graphs below show performance numbers from various
335 benchmarks that run nightly on NUL repository.</p>
336 <p>Table of content:</p>
340 print(" <li><a href='#%s'>%s</a></li>" % (graph.title, graph.title))
344 <h2><a name='%(title)s'>%(title)s</a></h2>
345 <div id="%(id)s" style="height: 400px"></div>
347 $.getJSON('%(dataname)s', function(data) {
348 var options = %(options)s
349 for (var i=0; i < data.length; i++)
350 options['series'][i]['data'] = data[i];
351 $("#%(id)s").highcharts("StockChart", options);
354 """ % {'id': graph.id,
355 'title': graph.title,
356 'dataname': graph.dataname,
357 'options': graph.options_json()})
358 print(graph.getData(), file=open(graph.dataname, 'w'))
365 # compile-command: "make perf"