blob: 13446ce5afc4ffbec313a0d2ff5b36fc91b1e3aa [file] [log] [blame]
bungeman@google.com85669f92011-06-17 13:58:14 +00001'''
2Created on May 16, 2011
3
4@author: bungeman
5'''
6import sys
7import getopt
8import re
9import os
10import bench_util
11import json
12import xml.sax.saxutils
13
epoger@google.com3d8cd172012-05-11 18:26:16 +000014# We throw out any measurement outside this range, and log a warning.
15MIN_REASONABLE_TIME = 0
16MAX_REASONABLE_TIME = 99999
17
bungeman@google.com85669f92011-06-17 13:58:14 +000018def usage():
19 """Prints simple usage information."""
20
21 print '-d <dir> a directory containing bench_r<revision>_<scalar> files.'
22 print '-b <bench> the bench to show.'
23 print '-c <config> the config to show (GPU, 8888, 565, etc).'
24 print '-t <time> the time to show (w, c, g, etc).'
25 print '-s <setting>[=<value>] a setting to show (alpha, scalar, etc).'
26 print '-r <revision>[:<revision>] the revisions to show.'
epoger@google.com37260002011-08-08 20:27:04 +000027 print ' Negative <revision> is taken as offset from most recent revision.'
bungeman@google.com85669f92011-06-17 13:58:14 +000028 print '-f <revision>[:<revision>] the revisions to use for fitting.'
epoger@google.com37260002011-08-08 20:27:04 +000029 print ' Negative <revision> is taken as offset from most recent revision.'
bungeman@google.com85669f92011-06-17 13:58:14 +000030 print '-x <int> the desired width of the svg.'
31 print '-y <int> the desired height of the svg.'
epoger@google.com37260002011-08-08 20:27:04 +000032 print '-l <title> title to use for the output graph'
bungeman@google.com85669f92011-06-17 13:58:14 +000033 print '--default-setting <setting>[=<value>] setting for those without.'
34
35
36class Label:
37 """The information in a label.
38
39 (str, str, str, str, {str:str})"""
40 def __init__(self, bench, config, time_type, settings):
41 self.bench = bench
42 self.config = config
43 self.time_type = time_type
44 self.settings = settings
45
46 def __repr__(self):
47 return "Label(%s, %s, %s, %s)" % (
48 str(self.bench),
49 str(self.config),
50 str(self.time_type),
51 str(self.settings),
52 )
53
54 def __str__(self):
55 return "%s_%s_%s_%s" % (
56 str(self.bench),
57 str(self.config),
58 str(self.time_type),
59 str(self.settings),
60 )
61
62 def __eq__(self, other):
63 return (self.bench == other.bench and
64 self.config == other.config and
65 self.time_type == other.time_type and
66 self.settings == other.settings)
67
68 def __hash__(self):
69 return (hash(self.bench) ^
70 hash(self.config) ^
71 hash(self.time_type) ^
72 hash(frozenset(self.settings.iteritems())))
73
epoger@google.com37260002011-08-08 20:27:04 +000074def get_latest_revision(directory):
75 """Returns the latest revision number found within this directory.
76 """
77 latest_revision_found = -1
78 for bench_file in os.listdir(directory):
79 file_name_match = re.match('bench_r(\d+)_(\S+)', bench_file)
80 if (file_name_match is None):
81 continue
82 revision = int(file_name_match.group(1))
83 if revision > latest_revision_found:
84 latest_revision_found = revision
85 if latest_revision_found < 0:
86 return None
87 else:
88 return latest_revision_found
89
bungeman@google.com85669f92011-06-17 13:58:14 +000090def parse_dir(directory, default_settings, oldest_revision, newest_revision):
91 """Parses bench data from files like bench_r<revision>_<scalar>.
92
93 (str, {str, str}, Number, Number) -> {int:[BenchDataPoints]}"""
94 revision_data_points = {} # {revision : [BenchDataPoints]}
95 for bench_file in os.listdir(directory):
96 file_name_match = re.match('bench_r(\d+)_(\S+)', bench_file)
97 if (file_name_match is None):
98 continue
epoger@google.com3d8cd172012-05-11 18:26:16 +000099
bungeman@google.com85669f92011-06-17 13:58:14 +0000100 revision = int(file_name_match.group(1))
101 scalar_type = file_name_match.group(2)
epoger@google.com3d8cd172012-05-11 18:26:16 +0000102
bungeman@google.com85669f92011-06-17 13:58:14 +0000103 if (revision < oldest_revision or revision > newest_revision):
104 continue
epoger@google.com3d8cd172012-05-11 18:26:16 +0000105
bungeman@google.com85669f92011-06-17 13:58:14 +0000106 file_handle = open(directory + '/' + bench_file, 'r')
epoger@google.com3d8cd172012-05-11 18:26:16 +0000107
bungeman@google.com85669f92011-06-17 13:58:14 +0000108 if (revision not in revision_data_points):
109 revision_data_points[revision] = []
110 default_settings['scalar'] = scalar_type
111 revision_data_points[revision].extend(
112 bench_util.parse(default_settings, file_handle))
113 file_handle.close()
114 return revision_data_points
115
epoger@google.com3d8cd172012-05-11 18:26:16 +0000116def add_to_revision_data_points(new_point, revision, revision_data_points):
117 """Add new_point to set of revision_data_points we are building up.
118 """
119 if (revision not in revision_data_points):
120 revision_data_points[revision] = []
121 revision_data_points[revision].append(new_point)
122
123def filter_data_points(unfiltered_revision_data_points):
124 """Filter out any data points that are utterly bogus.
125
126 Returns (allowed_revision_data_points, ignored_revision_data_points):
127 allowed_revision_data_points: points that survived the filter
128 ignored_revision_data_points: points that did NOT survive the filter
129 """
130 allowed_revision_data_points = {} # {revision : [BenchDataPoints]}
131 ignored_revision_data_points = {} # {revision : [BenchDataPoints]}
132 revisions = unfiltered_revision_data_points.keys()
133 revisions.sort()
134 for revision in revisions:
135 for point in unfiltered_revision_data_points[revision]:
136 if point.time < MIN_REASONABLE_TIME or point.time > MAX_REASONABLE_TIME:
137 add_to_revision_data_points(point, revision, ignored_revision_data_points)
138 else:
139 add_to_revision_data_points(point, revision, allowed_revision_data_points)
140 return (allowed_revision_data_points, ignored_revision_data_points)
141
bungeman@google.com85669f92011-06-17 13:58:14 +0000142def create_lines(revision_data_points, settings
143 , bench_of_interest, config_of_interest, time_of_interest):
144 """Convert revision data into sorted line data.
145
146 ({int:[BenchDataPoints]}, {str:str}, str?, str?, str?)
147 -> {Label:[(x,y)] | [n].x <= [n+1].x}"""
148 revisions = revision_data_points.keys()
149 revisions.sort()
150 lines = {} # {Label:[(x,y)] | x[n] <= x[n+1]}
151 for revision in revisions:
152 for point in revision_data_points[revision]:
153 if (bench_of_interest is not None and
154 not bench_of_interest == point.bench):
155 continue
156
157 if (config_of_interest is not None and
158 not config_of_interest == point.config):
159 continue
160
161 if (time_of_interest is not None and
162 not time_of_interest == point.time_type):
163 continue
164
165 skip = False
166 for key, value in settings.items():
167 if key in point.settings and point.settings[key] != value:
168 skip = True
169 break
170 if skip:
171 continue
172
173 line_name = Label(point.bench
174 , point.config
175 , point.time_type
176 , point.settings)
177
178 if line_name not in lines:
179 lines[line_name] = []
180
181 lines[line_name].append((revision, point.time))
182
183 return lines
184
185def bounds(lines):
186 """Finds the bounding rectangle for the lines.
187
188 {Label:[(x,y)]} -> ((min_x, min_y),(max_x,max_y))"""
189 min_x = bench_util.Max
190 min_y = bench_util.Max
191 max_x = bench_util.Min
192 max_y = bench_util.Min
193
194 for line in lines.itervalues():
195 for x, y in line:
196 min_x = min(min_x, x)
197 min_y = min(min_y, y)
198 max_x = max(max_x, x)
199 max_y = max(max_y, y)
200
201 return ((min_x, min_y), (max_x, max_y))
202
203def create_regressions(lines, start_x, end_x):
204 """Creates regression data from line segments.
205
206 ({Label:[(x,y)] | [n].x <= [n+1].x}, Number, Number)
207 -> {Label:LinearRegression}"""
208 regressions = {} # {Label : LinearRegression}
209
210 for label, line in lines.iteritems():
211 regression_line = [p for p in line if start_x <= p[0] <= end_x]
212
213 if (len(regression_line) < 2):
214 continue
215 regression = bench_util.LinearRegression(regression_line)
216 regressions[label] = regression
217
218 return regressions
219
220def bounds_slope(regressions):
221 """Finds the extreme up and down slopes of a set of linear regressions.
222
223 ({Label:LinearRegression}) -> (max_up_slope, min_down_slope)"""
224 max_up_slope = 0
225 min_down_slope = 0
226 for regression in regressions.itervalues():
227 min_slope = regression.find_min_slope()
228 max_up_slope = max(max_up_slope, min_slope)
229 min_down_slope = min(min_down_slope, min_slope)
230
231 return (max_up_slope, min_down_slope)
232
233def main():
234 """Parses command line and writes output."""
235
236 try:
237 opts, _ = getopt.getopt(sys.argv[1:]
epoger@google.com37260002011-08-08 20:27:04 +0000238 , "d:b:c:l:t:s:r:f:x:y:"
bungeman@google.com85669f92011-06-17 13:58:14 +0000239 , "default-setting=")
240 except getopt.GetoptError, err:
241 print str(err)
242 usage()
243 sys.exit(2)
244
245 directory = None
246 config_of_interest = None
247 bench_of_interest = None
248 time_of_interest = None
epoger@google.com37260002011-08-08 20:27:04 +0000249 revision_range = '0:'
250 regression_range = '0:'
251 latest_revision = None
bungeman@google.com85669f92011-06-17 13:58:14 +0000252 requested_height = None
253 requested_width = None
epoger@google.com37260002011-08-08 20:27:04 +0000254 title = 'Bench graph'
bungeman@google.com85669f92011-06-17 13:58:14 +0000255 settings = {}
256 default_settings = {}
epoger@google.com37260002011-08-08 20:27:04 +0000257
258 def parse_range(range):
259 """Takes '<old>[:<new>]' as a string and returns (old, new).
260 Any revision numbers that are dependent on the latest revision number
261 will be filled in based on latest_revision.
262 """
263 old, _, new = range.partition(":")
264 old = int(old)
265 if old < 0:
266 old += latest_revision;
bungeman@google.com85669f92011-06-17 13:58:14 +0000267 if not new:
epoger@google.com37260002011-08-08 20:27:04 +0000268 new = latest_revision;
269 new = int(new)
270 if new < 0:
271 new += latest_revision;
272 return (old, new)
273
bungeman@google.com85669f92011-06-17 13:58:14 +0000274 def add_setting(settings, setting):
275 """Takes <key>[=<value>] adds {key:value} or {key:True} to settings."""
276 name, _, value = setting.partition('=')
277 if not value:
278 settings[name] = True
279 else:
280 settings[name] = value
281
282 try:
283 for option, value in opts:
284 if option == "-d":
285 directory = value
286 elif option == "-b":
287 bench_of_interest = value
288 elif option == "-c":
289 config_of_interest = value
290 elif option == "-t":
291 time_of_interest = value
292 elif option == "-s":
293 add_setting(settings, value)
294 elif option == "-r":
epoger@google.com37260002011-08-08 20:27:04 +0000295 revision_range = value
bungeman@google.com85669f92011-06-17 13:58:14 +0000296 elif option == "-f":
epoger@google.com37260002011-08-08 20:27:04 +0000297 regression_range = value
bungeman@google.com85669f92011-06-17 13:58:14 +0000298 elif option == "-x":
299 requested_width = int(value)
300 elif option == "-y":
301 requested_height = int(value)
epoger@google.com37260002011-08-08 20:27:04 +0000302 elif option == "-l":
303 title = value
bungeman@google.com85669f92011-06-17 13:58:14 +0000304 elif option == "--default-setting":
305 add_setting(default_settings, value)
306 else:
307 usage()
308 assert False, "unhandled option"
309 except ValueError:
310 usage()
311 sys.exit(2)
epoger@google.comc71174d2011-08-08 17:19:23 +0000312
bungeman@google.com85669f92011-06-17 13:58:14 +0000313 if directory is None:
314 usage()
315 sys.exit(2)
epoger@google.comc71174d2011-08-08 17:19:23 +0000316
epoger@google.com37260002011-08-08 20:27:04 +0000317 latest_revision = get_latest_revision(directory)
318 oldest_revision, newest_revision = parse_range(revision_range)
319 oldest_regression, newest_regression = parse_range(regression_range)
320
epoger@google.com3d8cd172012-05-11 18:26:16 +0000321 unfiltered_revision_data_points = parse_dir(directory
bungeman@google.com85669f92011-06-17 13:58:14 +0000322 , default_settings
323 , oldest_revision
324 , newest_revision)
epoger@google.comc71174d2011-08-08 17:19:23 +0000325
epoger@google.com3d8cd172012-05-11 18:26:16 +0000326 # Filter out any data points that are utterly bogus... make sure to report
327 # that we did so later!
328 (allowed_revision_data_points, ignored_revision_data_points) = filter_data_points(
329 unfiltered_revision_data_points)
330
epoger@google.comc71174d2011-08-08 17:19:23 +0000331 # Update oldest_revision and newest_revision based on the data we could find
epoger@google.com3d8cd172012-05-11 18:26:16 +0000332 all_revision_numbers = allowed_revision_data_points.keys()
epoger@google.comc71174d2011-08-08 17:19:23 +0000333 oldest_revision = min(all_revision_numbers)
334 newest_revision = max(all_revision_numbers)
335
epoger@google.com3d8cd172012-05-11 18:26:16 +0000336 lines = create_lines(allowed_revision_data_points
bungeman@google.com85669f92011-06-17 13:58:14 +0000337 , settings
338 , bench_of_interest
339 , config_of_interest
340 , time_of_interest)
epoger@google.comc71174d2011-08-08 17:19:23 +0000341
bungeman@google.com85669f92011-06-17 13:58:14 +0000342 regressions = create_regressions(lines
343 , oldest_regression
344 , newest_regression)
epoger@google.comc71174d2011-08-08 17:19:23 +0000345
epoger@google.com3d8cd172012-05-11 18:26:16 +0000346 output_xhtml(lines, oldest_revision, newest_revision, ignored_revision_data_points,
epoger@google.com37260002011-08-08 20:27:04 +0000347 regressions, requested_width, requested_height, title)
bungeman@google.com85669f92011-06-17 13:58:14 +0000348
349def qa(out):
350 """Stringify input and quote as an xml attribute."""
351 return xml.sax.saxutils.quoteattr(str(out))
352def qe(out):
353 """Stringify input and escape as xml data."""
354 return xml.sax.saxutils.escape(str(out))
355
356def create_select(qualifier, lines, select_id=None):
357 """Output select with options showing lines which qualifier maps to it.
358
359 ((Label) -> str, {Label:_}, str?) -> _"""
360 options = {} #{ option : [Label]}
361 for label in lines.keys():
362 option = qualifier(label)
363 if (option not in options):
364 options[option] = []
365 options[option].append(label)
366 option_list = list(options.keys())
367 option_list.sort()
368 print '<select class="lines"',
369 if select_id is not None:
370 print 'id=%s' % qa(select_id)
371 print 'multiple="true" size="10" onchange="updateSvg();">'
372 for option in option_list:
373 print '<option value=' + qa('[' +
374 reduce(lambda x,y:x+json.dumps(str(y))+',',options[option],"")[0:-1]
375 + ']') + '>'+qe(option)+'</option>'
376 print '</select>'
377
epoger@google.com3d8cd172012-05-11 18:26:16 +0000378def output_ignored_data_points_warning(ignored_revision_data_points):
379 """Write description of ignored_revision_data_points to stdout as xhtml.
380 """
381 num_ignored_points = 0
382 description = ''
383 revisions = ignored_revision_data_points.keys()
384 if revisions:
385 revisions.sort()
386 revisions.reverse()
387 for revision in revisions:
388 num_ignored_points += len(ignored_revision_data_points[revision])
389 points_at_this_revision = []
390 for point in ignored_revision_data_points[revision]:
391 points_at_this_revision.append(point.bench)
392 points_at_this_revision.sort()
393 description += 'r%d: %s\n' % (revision, points_at_this_revision)
394 if num_ignored_points == 0:
395 print 'Did not discard any data points; all were within the range [%d-%d]' % (
396 MIN_REASONABLE_TIME, MAX_REASONABLE_TIME)
397 else:
398 print '<table width="100%" bgcolor="ff0000"><tr><td align="center">'
399 print 'Discarded %d data points outside of range [%d-%d]' % (
400 num_ignored_points, MIN_REASONABLE_TIME, MAX_REASONABLE_TIME)
401 print '</td></tr><tr><td width="100%" align="center">'
402 print ('<textarea rows="4" style="width:97%" readonly="true" wrap="off">'
403 + qe(description) + '</textarea>')
404 print '</td></tr></table>'
405
406def output_xhtml(lines, oldest_revision, newest_revision, ignored_revision_data_points,
epoger@google.com37260002011-08-08 20:27:04 +0000407 regressions, requested_width, requested_height, title):
bungeman@google.com85669f92011-06-17 13:58:14 +0000408 """Outputs an svg/xhtml view of the data."""
409 print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"',
410 print '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
411 print '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">'
412 print '<head>'
epoger@google.com3d8cd172012-05-11 18:26:16 +0000413 print '<title>%s</title>' % qe(title)
bungeman@google.com85669f92011-06-17 13:58:14 +0000414 print '</head>'
415 print '<body>'
416
417 output_svg(lines, regressions, requested_width, requested_height)
epoger@google.com3d8cd172012-05-11 18:26:16 +0000418
bungeman@google.com85669f92011-06-17 13:58:14 +0000419 #output the manipulation controls
420 print """
421<script type="text/javascript">//<![CDATA[
422 function getElementsByClass(node, searchClass, tag) {
423 var classElements = new Array();
424 var elements = node.getElementsByTagName(tag);
425 var pattern = new RegExp("^|\\s"+searchClass+"\\s|$");
426 for (var i = 0, elementsFound = 0; i < elements.length; ++i) {
427 if (pattern.test(elements[i].className)) {
428 classElements[elementsFound] = elements[i];
429 ++elementsFound;
430 }
431 }
432 return classElements;
433 }
434 function getAllLines() {
435 var selectElem = document.getElementById('benchSelect');
436 var linesObj = {};
437 for (var i = 0; i < selectElem.options.length; ++i) {
438 var lines = JSON.parse(selectElem.options[i].value);
439 for (var j = 0; j < lines.length; ++j) {
440 linesObj[lines[j]] = true;
441 }
442 }
443 return linesObj;
444 }
445 function getOptions(selectElem) {
446 var linesSelectedObj = {};
447 for (var i = 0; i < selectElem.options.length; ++i) {
448 if (!selectElem.options[i].selected) continue;
449
450 var linesSelected = JSON.parse(selectElem.options[i].value);
451 for (var j = 0; j < linesSelected.length; ++j) {
452 linesSelectedObj[linesSelected[j]] = true;
453 }
454 }
455 return linesSelectedObj;
456 }
457 function objectEmpty(obj) {
458 for (var p in obj) {
459 return false;
460 }
461 return true;
462 }
463 function markSelectedLines(selectElem, allLines) {
464 var linesSelected = getOptions(selectElem);
465 if (!objectEmpty(linesSelected)) {
466 for (var line in allLines) {
467 allLines[line] &= (linesSelected[line] == true);
468 }
469 }
470 }
471 function updateSvg() {
472 var allLines = getAllLines();
473
474 var selects = getElementsByClass(document, 'lines', 'select');
475 for (var i = 0; i < selects.length; ++i) {
476 markSelectedLines(selects[i], allLines);
477 }
478
479 for (var line in allLines) {
480 var svgLine = document.getElementById(line);
481 var display = (allLines[line] ? 'inline' : 'none');
482 svgLine.setAttributeNS(null,'display', display);
483 }
484 }
485
486 function mark(markerId) {
487 for (var line in getAllLines()) {
488 var svgLineGroup = document.getElementById(line);
489 var display = svgLineGroup.getAttributeNS(null,'display');
490 if (display == null || display == "" || display != "none") {
491 var svgLine = document.getElementById(line+'_line');
492 if (markerId == null) {
493 svgLine.removeAttributeNS(null,'marker-mid');
494 } else {
495 svgLine.setAttributeNS(null,'marker-mid', markerId);
496 }
497 }
498 }
499 }
500//]]></script>"""
epoger@google.comc71174d2011-08-08 17:19:23 +0000501
502 print '<table border="0" width="%s">' % requested_width
503 print """
epoger@google.com3d8cd172012-05-11 18:26:16 +0000504<tr valign="top"><td width="50%">
505<table border="0" width="100%">
506<tr><td align="center"><table border="0">
epoger@google.comc71174d2011-08-08 17:19:23 +0000507<form>
508<tr valign="bottom" align="center">
509<td width="1">Bench&nbsp;Type</td>
510<td width="1">Bitmap Config</td>
511<td width="1">Timer&nbsp;Type (Cpu/Gpu/wall)</td>
512<td width="1"><!--buttons--></td>
epoger@google.com3d8cd172012-05-11 18:26:16 +0000513</tr><tr valign="top" align="center">
514"""
epoger@google.comc71174d2011-08-08 17:19:23 +0000515 print '<td width="1">'
bungeman@google.com85669f92011-06-17 13:58:14 +0000516 create_select(lambda l: l.bench, lines, 'benchSelect')
epoger@google.comc71174d2011-08-08 17:19:23 +0000517 print '</td><td width="1">'
bungeman@google.com85669f92011-06-17 13:58:14 +0000518 create_select(lambda l: l.config, lines)
epoger@google.comc71174d2011-08-08 17:19:23 +0000519 print '</td><td width="1">'
bungeman@google.com85669f92011-06-17 13:58:14 +0000520 create_select(lambda l: l.time_type, lines)
epoger@google.comc71174d2011-08-08 17:19:23 +0000521
bungeman@google.com85669f92011-06-17 13:58:14 +0000522 all_settings = {}
523 variant_settings = set()
524 for label in lines.keys():
525 for key, value in label.settings.items():
526 if key not in all_settings:
527 all_settings[key] = value
528 elif all_settings[key] != value:
529 variant_settings.add(key)
epoger@google.comc71174d2011-08-08 17:19:23 +0000530
bungeman@google.com85669f92011-06-17 13:58:14 +0000531 for k in variant_settings:
532 create_select(lambda l: l.settings[k], lines)
epoger@google.comc71174d2011-08-08 17:19:23 +0000533
534 print '</td><td width="1"><button type="button"',
bungeman@google.com85669f92011-06-17 13:58:14 +0000535 print 'onclick=%s' % qa("mark('url(#circleMark)'); return false;"),
epoger@google.comc71174d2011-08-08 17:19:23 +0000536 print '>Mark Points</button>'
537 print '<button type="button" onclick="mark(null);">Clear Points</button>'
epoger@google.com3d8cd172012-05-11 18:26:16 +0000538 print '</td>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000539 print """
epoger@google.com3d8cd172012-05-11 18:26:16 +0000540</tr>
541</form>
542</table></td></tr>
543<tr><td align="center">
544<hr />
545"""
546
547 output_ignored_data_points_warning(ignored_revision_data_points)
548 print '</td></tr></table>'
549 print '</td><td width="2%"><!--gutter--></td>'
550
551 print '<td><table border="0">'
552 print '<tr><td align="center">%s<br></br>revisions r%s - r%s</td></tr>' % (
553 qe(title),
554 bench_util.CreateRevisionLink(oldest_revision),
555 bench_util.CreateRevisionLink(newest_revision))
556 print """
557<tr><td align="left">
epoger@google.comc71174d2011-08-08 17:19:23 +0000558<p>Brighter red indicates tests that have gotten worse; brighter green
559indicates tests that have gotten better.</p>
560<p>To highlight individual tests, hold down CONTROL and mouse over
561graph lines.</p>
562<p>To highlight revision numbers, hold down SHIFT and mouse over
563the graph area.</p>
564<p>To only show certain tests on the graph, select any combination of
565tests in the selectors at left. (To show all, select all.)</p>
566<p>Use buttons at left to mark/clear points on the lines for selected
567benchmarks.</p>
epoger@google.com3d8cd172012-05-11 18:26:16 +0000568</td></tr>
569</table>
570
epoger@google.comc71174d2011-08-08 17:19:23 +0000571</td>
572</tr>
epoger@google.comc71174d2011-08-08 17:19:23 +0000573</table>
574</body>
575</html>"""
bungeman@google.com85669f92011-06-17 13:58:14 +0000576
577def compute_size(requested_width, requested_height, rev_width, time_height):
578 """Converts potentially empty requested size into a concrete size.
579
580 (Number?, Number?) -> (Number, Number)"""
581 pic_width = 0
582 pic_height = 0
583 if (requested_width is not None and requested_height is not None):
584 pic_height = requested_height
585 pic_width = requested_width
586
587 elif (requested_width is not None):
588 pic_width = requested_width
589 pic_height = pic_width * (float(time_height) / rev_width)
590
591 elif (requested_height is not None):
592 pic_height = requested_height
593 pic_width = pic_height * (float(rev_width) / time_height)
594
595 else:
596 pic_height = 800
597 pic_width = max(rev_width*3
598 , pic_height * (float(rev_width) / time_height))
599
600 return (pic_width, pic_height)
601
602def output_svg(lines, regressions, requested_width, requested_height):
603 """Outputs an svg view of the data."""
604
605 (global_min_x, _), (global_max_x, global_max_y) = bounds(lines)
606 max_up_slope, min_down_slope = bounds_slope(regressions)
607
608 #output
609 global_min_y = 0
610 x = global_min_x
611 y = global_min_y
612 w = global_max_x - global_min_x
613 h = global_max_y - global_min_y
614 font_size = 16
615 line_width = 2
616
617 pic_width, pic_height = compute_size(requested_width, requested_height
618 , w, h)
619
620 def cw(w1):
621 """Converts a revision difference to display width."""
622 return (pic_width / float(w)) * w1
623 def cx(x):
624 """Converts a revision to a horizontal display position."""
625 return cw(x - global_min_x)
626
627 def ch(h1):
628 """Converts a time difference to a display height."""
629 return -(pic_height / float(h)) * h1
630 def cy(y):
631 """Converts a time to a vertical display position."""
632 return pic_height + ch(y - global_min_y)
633
634 print '<svg',
635 print 'width=%s' % qa(str(pic_width)+'px')
636 print 'height=%s' % qa(str(pic_height)+'px')
637 print 'viewBox="0 0 %s %s"' % (str(pic_width), str(pic_height))
638 print 'onclick=%s' % qa(
639 "var event = arguments[0] || window.event;"
640 " if (event.shiftKey) { highlightRevision(null); }"
641 " if (event.ctrlKey) { highlight(null); }"
642 " return false;")
643 print 'xmlns="http://www.w3.org/2000/svg"'
644 print 'xmlns:xlink="http://www.w3.org/1999/xlink">'
645
646 print """
647<defs>
648 <marker id="circleMark"
649 viewBox="0 0 2 2" refX="1" refY="1"
650 markerUnits="strokeWidth"
651 markerWidth="2" markerHeight="2"
652 orient="0">
653 <circle cx="1" cy="1" r="1"/>
654 </marker>
655</defs>"""
656
657 #output the revisions
658 print """
659<script type="text/javascript">//<![CDATA[
660 var previousRevision;
661 var previousRevisionFill;
662 var previousRevisionStroke
663 function highlightRevision(id) {
664 if (previousRevision == id) return;
epoger@google.comc71174d2011-08-08 17:19:23 +0000665
666 document.getElementById('revision').firstChild.nodeValue = 'r' + id;
667 document.getElementById('rev_link').setAttribute('xlink:href',
668 'http://code.google.com/p/skia/source/detail?r=' + id);
bungeman@google.com85669f92011-06-17 13:58:14 +0000669
670 var preRevision = document.getElementById(previousRevision);
671 if (preRevision) {
672 preRevision.setAttributeNS(null,'fill', previousRevisionFill);
673 preRevision.setAttributeNS(null,'stroke', previousRevisionStroke);
674 }
675
676 var revision = document.getElementById(id);
677 previousRevision = id;
678 if (revision) {
679 previousRevisionFill = revision.getAttributeNS(null,'fill');
680 revision.setAttributeNS(null,'fill','rgb(100%, 95%, 95%)');
681
682 previousRevisionStroke = revision.getAttributeNS(null,'stroke');
683 revision.setAttributeNS(null,'stroke','rgb(100%, 90%, 90%)');
684 }
685 }
686//]]></script>"""
687
688 def print_rect(x, y, w, h, revision):
689 """Outputs a revision rectangle in display space,
690 taking arguments in revision space."""
691 disp_y = cy(y)
692 disp_h = ch(h)
693 if disp_h < 0:
694 disp_y += disp_h
695 disp_h = -disp_h
696
697 print '<rect id=%s x=%s y=%s' % (qa(revision), qa(cx(x)), qa(disp_y),),
698 print 'width=%s height=%s' % (qa(cw(w)), qa(disp_h),),
699 print 'fill="white"',
epoger@google.comc71174d2011-08-08 17:19:23 +0000700 print 'stroke="rgb(98%%,98%%,88%%)" stroke-width=%s' % qa(line_width),
bungeman@google.com85669f92011-06-17 13:58:14 +0000701 print 'onmouseover=%s' % qa(
702 "var event = arguments[0] || window.event;"
703 " if (event.shiftKey) {"
704 " highlightRevision('"+str(revision)+"');"
705 " return false;"
706 " }"),
707 print ' />'
708
709 xes = set()
710 for line in lines.itervalues():
711 for point in line:
712 xes.add(point[0])
713 revisions = list(xes)
714 revisions.sort()
715
716 left = x
717 current_revision = revisions[0]
718 for next_revision in revisions[1:]:
719 width = (((next_revision - current_revision) / 2.0)
720 + (current_revision - left))
721 print_rect(left, y, width, h, current_revision)
722 left += width
723 current_revision = next_revision
724 print_rect(left, y, x+w - left, h, current_revision)
epoger@google.comc71174d2011-08-08 17:19:23 +0000725
bungeman@google.com85669f92011-06-17 13:58:14 +0000726 #output the lines
727 print """
728<script type="text/javascript">//<![CDATA[
729 var previous;
730 var previousColor;
731 var previousOpacity;
732 function highlight(id) {
733 if (previous == id) return;
epoger@google.comc71174d2011-08-08 17:19:23 +0000734
bungeman@google.com85669f92011-06-17 13:58:14 +0000735 document.getElementById('label').firstChild.nodeValue = id;
epoger@google.comc71174d2011-08-08 17:19:23 +0000736
bungeman@google.com85669f92011-06-17 13:58:14 +0000737 var preGroup = document.getElementById(previous);
738 if (preGroup) {
739 var preLine = document.getElementById(previous+'_line');
740 preLine.setAttributeNS(null,'stroke', previousColor);
741 preLine.setAttributeNS(null,'opacity', previousOpacity);
epoger@google.comc71174d2011-08-08 17:19:23 +0000742
bungeman@google.com85669f92011-06-17 13:58:14 +0000743 var preSlope = document.getElementById(previous+'_linear');
744 if (preSlope) {
745 preSlope.setAttributeNS(null,'visibility', 'hidden');
746 }
747 }
epoger@google.comc71174d2011-08-08 17:19:23 +0000748
bungeman@google.com85669f92011-06-17 13:58:14 +0000749 var group = document.getElementById(id);
750 previous = id;
751 if (group) {
752 group.parentNode.appendChild(group);
753
754 var line = document.getElementById(id+'_line');
755 previousColor = line.getAttributeNS(null,'stroke');
756 previousOpacity = line.getAttributeNS(null,'opacity');
757 line.setAttributeNS(null,'stroke', 'blue');
758 line.setAttributeNS(null,'opacity', '1');
759
760 var slope = document.getElementById(id+'_linear');
761 if (slope) {
762 slope.setAttributeNS(null,'visibility', 'visible');
763 }
764 }
765 }
766//]]></script>"""
767 for label, line in lines.items():
768 print '<g id=%s>' % qa(label)
769 r = 128
770 g = 128
771 b = 128
772 a = .10
773 if label in regressions:
774 regression = regressions[label]
775 min_slope = regression.find_min_slope()
776 if min_slope < 0:
777 d = max(0, (min_slope / min_down_slope))
778 g += int(d*128)
779 a += d*0.9
780 elif min_slope > 0:
781 d = max(0, (min_slope / max_up_slope))
782 r += int(d*128)
783 a += d*0.9
784
785 slope = regression.slope
786 intercept = regression.intercept
787 min_x = regression.min_x
788 max_x = regression.max_x
789 print '<polyline id=%s' % qa(str(label)+'_linear'),
790 print 'fill="none" stroke="yellow"',
791 print 'stroke-width=%s' % qa(abs(ch(regression.serror*2))),
792 print 'opacity="0.5" pointer-events="none" visibility="hidden"',
793 print 'points="',
794 print '%s,%s' % (str(cx(min_x)), str(cy(slope*min_x + intercept))),
795 print '%s,%s' % (str(cx(max_x)), str(cy(slope*max_x + intercept))),
796 print '"/>'
797
798 print '<polyline id=%s' % qa(str(label)+'_line'),
799 print 'onmouseover=%s' % qa(
800 "var event = arguments[0] || window.event;"
801 " if (event.ctrlKey) {"
802 " highlight('"+str(label).replace("'", "\\'")+"');"
803 " return false;"
804 " }"),
805 print 'fill="none" stroke="rgb(%s,%s,%s)"' % (str(r), str(g), str(b)),
806 print 'stroke-width=%s' % qa(line_width),
807 print 'opacity=%s' % qa(a),
808 print 'points="',
809 for point in line:
810 print '%s,%s' % (str(cx(point[0])), str(cy(point[1]))),
811 print '"/>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000812
bungeman@google.com85669f92011-06-17 13:58:14 +0000813 print '</g>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000814
bungeman@google.com85669f92011-06-17 13:58:14 +0000815 #output the labels
816 print '<text id="label" x="0" y=%s' % qa(font_size),
817 print 'font-size=%s> </text>' % qa(font_size)
epoger@google.comc71174d2011-08-08 17:19:23 +0000818
819 print '<a id="rev_link" xlink:href="" target="_top">'
820 print '<text id="revision" x="0" y=%s style="' % qa(font_size*2)
821 print 'font-size: %s; ' % qe(font_size)
822 print 'stroke: #0000dd; text-decoration: underline; '
823 print '"> </text></a>'
824
bungeman@google.com85669f92011-06-17 13:58:14 +0000825 print '</svg>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000826
bungeman@google.com85669f92011-06-17 13:58:14 +0000827if __name__ == "__main__":
828 main()