blob: 3e10959a8882f076b52039cbcde7281e43146418 [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
bungeman@google.com85669f92011-06-17 13:58:14 +000021 print '-b <bench> the bench to show.'
22 print '-c <config> the config to show (GPU, 8888, 565, etc).'
epoger@google.com5b2e01c2012-06-25 20:29:04 +000023 print '-d <dir> a directory containing bench_r<revision>_<scalar> files.'
bungeman@google.com85669f92011-06-17 13:58:14 +000024 print '-f <revision>[:<revision>] the revisions to use for fitting.'
epoger@google.com37260002011-08-08 20:27:04 +000025 print ' Negative <revision> is taken as offset from most recent revision.'
epoger@google.com5b2e01c2012-06-25 20:29:04 +000026 print '-l <title> title to use for the output graph'
27 print '-o <path> path to which to write output; writes to stdout if not specified'
28 print '-r <revision>[:<revision>] the revisions to show.'
29 print ' Negative <revision> is taken as offset from most recent revision.'
30 print '-s <setting>[=<value>] a setting to show (alpha, scalar, etc).'
31 print '-t <time> the time to show (w, c, g, etc).'
bungeman@google.com85669f92011-06-17 13:58:14 +000032 print '-x <int> the desired width of the svg.'
33 print '-y <int> the desired height of the svg.'
34 print '--default-setting <setting>[=<value>] setting for those without.'
35
36
37class Label:
38 """The information in a label.
39
40 (str, str, str, str, {str:str})"""
41 def __init__(self, bench, config, time_type, settings):
42 self.bench = bench
43 self.config = config
44 self.time_type = time_type
45 self.settings = settings
46
47 def __repr__(self):
48 return "Label(%s, %s, %s, %s)" % (
49 str(self.bench),
50 str(self.config),
51 str(self.time_type),
52 str(self.settings),
53 )
54
55 def __str__(self):
56 return "%s_%s_%s_%s" % (
57 str(self.bench),
58 str(self.config),
59 str(self.time_type),
60 str(self.settings),
61 )
62
63 def __eq__(self, other):
64 return (self.bench == other.bench and
65 self.config == other.config and
66 self.time_type == other.time_type and
67 self.settings == other.settings)
68
69 def __hash__(self):
70 return (hash(self.bench) ^
71 hash(self.config) ^
72 hash(self.time_type) ^
73 hash(frozenset(self.settings.iteritems())))
74
epoger@google.com37260002011-08-08 20:27:04 +000075def get_latest_revision(directory):
76 """Returns the latest revision number found within this directory.
77 """
78 latest_revision_found = -1
79 for bench_file in os.listdir(directory):
80 file_name_match = re.match('bench_r(\d+)_(\S+)', bench_file)
81 if (file_name_match is None):
82 continue
83 revision = int(file_name_match.group(1))
84 if revision > latest_revision_found:
85 latest_revision_found = revision
86 if latest_revision_found < 0:
87 return None
88 else:
89 return latest_revision_found
90
bungeman@google.com85669f92011-06-17 13:58:14 +000091def parse_dir(directory, default_settings, oldest_revision, newest_revision):
92 """Parses bench data from files like bench_r<revision>_<scalar>.
93
94 (str, {str, str}, Number, Number) -> {int:[BenchDataPoints]}"""
95 revision_data_points = {} # {revision : [BenchDataPoints]}
96 for bench_file in os.listdir(directory):
97 file_name_match = re.match('bench_r(\d+)_(\S+)', bench_file)
98 if (file_name_match is None):
99 continue
epoger@google.com3d8cd172012-05-11 18:26:16 +0000100
bungeman@google.com85669f92011-06-17 13:58:14 +0000101 revision = int(file_name_match.group(1))
102 scalar_type = file_name_match.group(2)
epoger@google.com3d8cd172012-05-11 18:26:16 +0000103
bungeman@google.com85669f92011-06-17 13:58:14 +0000104 if (revision < oldest_revision or revision > newest_revision):
105 continue
epoger@google.com3d8cd172012-05-11 18:26:16 +0000106
bungeman@google.com85669f92011-06-17 13:58:14 +0000107 file_handle = open(directory + '/' + bench_file, 'r')
epoger@google.com3d8cd172012-05-11 18:26:16 +0000108
bungeman@google.com85669f92011-06-17 13:58:14 +0000109 if (revision not in revision_data_points):
110 revision_data_points[revision] = []
111 default_settings['scalar'] = scalar_type
112 revision_data_points[revision].extend(
113 bench_util.parse(default_settings, file_handle))
114 file_handle.close()
115 return revision_data_points
116
epoger@google.com3d8cd172012-05-11 18:26:16 +0000117def add_to_revision_data_points(new_point, revision, revision_data_points):
118 """Add new_point to set of revision_data_points we are building up.
119 """
120 if (revision not in revision_data_points):
121 revision_data_points[revision] = []
122 revision_data_points[revision].append(new_point)
123
124def filter_data_points(unfiltered_revision_data_points):
125 """Filter out any data points that are utterly bogus.
126
127 Returns (allowed_revision_data_points, ignored_revision_data_points):
128 allowed_revision_data_points: points that survived the filter
129 ignored_revision_data_points: points that did NOT survive the filter
130 """
131 allowed_revision_data_points = {} # {revision : [BenchDataPoints]}
132 ignored_revision_data_points = {} # {revision : [BenchDataPoints]}
133 revisions = unfiltered_revision_data_points.keys()
134 revisions.sort()
135 for revision in revisions:
136 for point in unfiltered_revision_data_points[revision]:
137 if point.time < MIN_REASONABLE_TIME or point.time > MAX_REASONABLE_TIME:
138 add_to_revision_data_points(point, revision, ignored_revision_data_points)
139 else:
140 add_to_revision_data_points(point, revision, allowed_revision_data_points)
141 return (allowed_revision_data_points, ignored_revision_data_points)
142
epoger@google.com5b2e01c2012-06-25 20:29:04 +0000143def redirect_stdout(output_path):
144 """Redirect all following stdout to a file.
145
146 You may be asking yourself, why redirect stdout within Python rather than
147 redirecting the script's output in the calling shell?
148 The answer lies in https://code.google.com/p/skia/issues/detail?id=674
149 ('buildbot: windows GenerateBenchGraphs step fails due to filename length'):
150 On Windows, we need to generate the absolute path within Python to avoid
151 the operating system's 260-character pathname limit, including chdirs."""
152 abs_path = os.path.abspath(output_path)
153 sys.stdout = open(abs_path, 'w')
154
bungeman@google.com85669f92011-06-17 13:58:14 +0000155def create_lines(revision_data_points, settings
156 , bench_of_interest, config_of_interest, time_of_interest):
157 """Convert revision data into sorted line data.
158
159 ({int:[BenchDataPoints]}, {str:str}, str?, str?, str?)
160 -> {Label:[(x,y)] | [n].x <= [n+1].x}"""
161 revisions = revision_data_points.keys()
162 revisions.sort()
163 lines = {} # {Label:[(x,y)] | x[n] <= x[n+1]}
164 for revision in revisions:
165 for point in revision_data_points[revision]:
166 if (bench_of_interest is not None and
167 not bench_of_interest == point.bench):
168 continue
169
170 if (config_of_interest is not None and
171 not config_of_interest == point.config):
172 continue
173
174 if (time_of_interest is not None and
175 not time_of_interest == point.time_type):
176 continue
177
178 skip = False
179 for key, value in settings.items():
180 if key in point.settings and point.settings[key] != value:
181 skip = True
182 break
183 if skip:
184 continue
185
186 line_name = Label(point.bench
187 , point.config
188 , point.time_type
189 , point.settings)
190
191 if line_name not in lines:
192 lines[line_name] = []
193
194 lines[line_name].append((revision, point.time))
195
196 return lines
197
198def bounds(lines):
199 """Finds the bounding rectangle for the lines.
200
201 {Label:[(x,y)]} -> ((min_x, min_y),(max_x,max_y))"""
202 min_x = bench_util.Max
203 min_y = bench_util.Max
204 max_x = bench_util.Min
205 max_y = bench_util.Min
206
207 for line in lines.itervalues():
208 for x, y in line:
209 min_x = min(min_x, x)
210 min_y = min(min_y, y)
211 max_x = max(max_x, x)
212 max_y = max(max_y, y)
213
214 return ((min_x, min_y), (max_x, max_y))
215
216def create_regressions(lines, start_x, end_x):
217 """Creates regression data from line segments.
218
219 ({Label:[(x,y)] | [n].x <= [n+1].x}, Number, Number)
220 -> {Label:LinearRegression}"""
221 regressions = {} # {Label : LinearRegression}
222
223 for label, line in lines.iteritems():
224 regression_line = [p for p in line if start_x <= p[0] <= end_x]
225
226 if (len(regression_line) < 2):
227 continue
228 regression = bench_util.LinearRegression(regression_line)
229 regressions[label] = regression
230
231 return regressions
232
233def bounds_slope(regressions):
234 """Finds the extreme up and down slopes of a set of linear regressions.
235
236 ({Label:LinearRegression}) -> (max_up_slope, min_down_slope)"""
237 max_up_slope = 0
238 min_down_slope = 0
239 for regression in regressions.itervalues():
240 min_slope = regression.find_min_slope()
241 max_up_slope = max(max_up_slope, min_slope)
242 min_down_slope = min(min_down_slope, min_slope)
243
244 return (max_up_slope, min_down_slope)
245
246def main():
247 """Parses command line and writes output."""
248
249 try:
250 opts, _ = getopt.getopt(sys.argv[1:]
epoger@google.com5b2e01c2012-06-25 20:29:04 +0000251 , "b:c:d:f:l:o:r:s:t:x:y:"
bungeman@google.com85669f92011-06-17 13:58:14 +0000252 , "default-setting=")
253 except getopt.GetoptError, err:
254 print str(err)
255 usage()
256 sys.exit(2)
257
258 directory = None
259 config_of_interest = None
260 bench_of_interest = None
261 time_of_interest = None
epoger@google.com37260002011-08-08 20:27:04 +0000262 revision_range = '0:'
263 regression_range = '0:'
264 latest_revision = None
bungeman@google.com85669f92011-06-17 13:58:14 +0000265 requested_height = None
266 requested_width = None
epoger@google.com37260002011-08-08 20:27:04 +0000267 title = 'Bench graph'
bungeman@google.com85669f92011-06-17 13:58:14 +0000268 settings = {}
269 default_settings = {}
epoger@google.com37260002011-08-08 20:27:04 +0000270
271 def parse_range(range):
272 """Takes '<old>[:<new>]' as a string and returns (old, new).
273 Any revision numbers that are dependent on the latest revision number
274 will be filled in based on latest_revision.
275 """
276 old, _, new = range.partition(":")
277 old = int(old)
278 if old < 0:
279 old += latest_revision;
bungeman@google.com85669f92011-06-17 13:58:14 +0000280 if not new:
epoger@google.com37260002011-08-08 20:27:04 +0000281 new = latest_revision;
282 new = int(new)
283 if new < 0:
284 new += latest_revision;
285 return (old, new)
286
bungeman@google.com85669f92011-06-17 13:58:14 +0000287 def add_setting(settings, setting):
288 """Takes <key>[=<value>] adds {key:value} or {key:True} to settings."""
289 name, _, value = setting.partition('=')
290 if not value:
291 settings[name] = True
292 else:
293 settings[name] = value
294
295 try:
296 for option, value in opts:
epoger@google.com5b2e01c2012-06-25 20:29:04 +0000297 if option == "-b":
bungeman@google.com85669f92011-06-17 13:58:14 +0000298 bench_of_interest = value
299 elif option == "-c":
300 config_of_interest = value
epoger@google.com5b2e01c2012-06-25 20:29:04 +0000301 elif option == "-d":
302 directory = value
bungeman@google.com85669f92011-06-17 13:58:14 +0000303 elif option == "-f":
epoger@google.com37260002011-08-08 20:27:04 +0000304 regression_range = value
epoger@google.com5b2e01c2012-06-25 20:29:04 +0000305 elif option == "-l":
306 title = value
307 elif option == "-o":
308 redirect_stdout(value)
309 elif option == "-r":
310 revision_range = value
311 elif option == "-s":
312 add_setting(settings, value)
313 elif option == "-t":
314 time_of_interest = value
bungeman@google.com85669f92011-06-17 13:58:14 +0000315 elif option == "-x":
316 requested_width = int(value)
317 elif option == "-y":
318 requested_height = int(value)
319 elif option == "--default-setting":
320 add_setting(default_settings, value)
321 else:
322 usage()
323 assert False, "unhandled option"
324 except ValueError:
325 usage()
326 sys.exit(2)
epoger@google.comc71174d2011-08-08 17:19:23 +0000327
bungeman@google.com85669f92011-06-17 13:58:14 +0000328 if directory is None:
329 usage()
330 sys.exit(2)
epoger@google.comc71174d2011-08-08 17:19:23 +0000331
epoger@google.com37260002011-08-08 20:27:04 +0000332 latest_revision = get_latest_revision(directory)
333 oldest_revision, newest_revision = parse_range(revision_range)
334 oldest_regression, newest_regression = parse_range(regression_range)
335
epoger@google.com3d8cd172012-05-11 18:26:16 +0000336 unfiltered_revision_data_points = parse_dir(directory
bungeman@google.com85669f92011-06-17 13:58:14 +0000337 , default_settings
338 , oldest_revision
339 , newest_revision)
epoger@google.comc71174d2011-08-08 17:19:23 +0000340
epoger@google.com3d8cd172012-05-11 18:26:16 +0000341 # Filter out any data points that are utterly bogus... make sure to report
342 # that we did so later!
343 (allowed_revision_data_points, ignored_revision_data_points) = filter_data_points(
344 unfiltered_revision_data_points)
345
epoger@google.comc71174d2011-08-08 17:19:23 +0000346 # Update oldest_revision and newest_revision based on the data we could find
epoger@google.com3d8cd172012-05-11 18:26:16 +0000347 all_revision_numbers = allowed_revision_data_points.keys()
epoger@google.comc71174d2011-08-08 17:19:23 +0000348 oldest_revision = min(all_revision_numbers)
349 newest_revision = max(all_revision_numbers)
350
epoger@google.com3d8cd172012-05-11 18:26:16 +0000351 lines = create_lines(allowed_revision_data_points
bungeman@google.com85669f92011-06-17 13:58:14 +0000352 , settings
353 , bench_of_interest
354 , config_of_interest
355 , time_of_interest)
epoger@google.comc71174d2011-08-08 17:19:23 +0000356
bungeman@google.com85669f92011-06-17 13:58:14 +0000357 regressions = create_regressions(lines
358 , oldest_regression
359 , newest_regression)
epoger@google.comc71174d2011-08-08 17:19:23 +0000360
epoger@google.com3d8cd172012-05-11 18:26:16 +0000361 output_xhtml(lines, oldest_revision, newest_revision, ignored_revision_data_points,
epoger@google.com37260002011-08-08 20:27:04 +0000362 regressions, requested_width, requested_height, title)
bungeman@google.com85669f92011-06-17 13:58:14 +0000363
364def qa(out):
365 """Stringify input and quote as an xml attribute."""
366 return xml.sax.saxutils.quoteattr(str(out))
367def qe(out):
368 """Stringify input and escape as xml data."""
369 return xml.sax.saxutils.escape(str(out))
370
371def create_select(qualifier, lines, select_id=None):
372 """Output select with options showing lines which qualifier maps to it.
373
374 ((Label) -> str, {Label:_}, str?) -> _"""
375 options = {} #{ option : [Label]}
376 for label in lines.keys():
377 option = qualifier(label)
378 if (option not in options):
379 options[option] = []
380 options[option].append(label)
381 option_list = list(options.keys())
382 option_list.sort()
383 print '<select class="lines"',
384 if select_id is not None:
385 print 'id=%s' % qa(select_id)
386 print 'multiple="true" size="10" onchange="updateSvg();">'
387 for option in option_list:
388 print '<option value=' + qa('[' +
389 reduce(lambda x,y:x+json.dumps(str(y))+',',options[option],"")[0:-1]
390 + ']') + '>'+qe(option)+'</option>'
391 print '</select>'
392
epoger@google.com3d8cd172012-05-11 18:26:16 +0000393def output_ignored_data_points_warning(ignored_revision_data_points):
394 """Write description of ignored_revision_data_points to stdout as xhtml.
395 """
396 num_ignored_points = 0
397 description = ''
398 revisions = ignored_revision_data_points.keys()
399 if revisions:
400 revisions.sort()
401 revisions.reverse()
402 for revision in revisions:
403 num_ignored_points += len(ignored_revision_data_points[revision])
404 points_at_this_revision = []
405 for point in ignored_revision_data_points[revision]:
406 points_at_this_revision.append(point.bench)
407 points_at_this_revision.sort()
408 description += 'r%d: %s\n' % (revision, points_at_this_revision)
409 if num_ignored_points == 0:
410 print 'Did not discard any data points; all were within the range [%d-%d]' % (
411 MIN_REASONABLE_TIME, MAX_REASONABLE_TIME)
412 else:
413 print '<table width="100%" bgcolor="ff0000"><tr><td align="center">'
414 print 'Discarded %d data points outside of range [%d-%d]' % (
415 num_ignored_points, MIN_REASONABLE_TIME, MAX_REASONABLE_TIME)
416 print '</td></tr><tr><td width="100%" align="center">'
417 print ('<textarea rows="4" style="width:97%" readonly="true" wrap="off">'
418 + qe(description) + '</textarea>')
419 print '</td></tr></table>'
420
421def output_xhtml(lines, oldest_revision, newest_revision, ignored_revision_data_points,
epoger@google.com37260002011-08-08 20:27:04 +0000422 regressions, requested_width, requested_height, title):
bungeman@google.com85669f92011-06-17 13:58:14 +0000423 """Outputs an svg/xhtml view of the data."""
424 print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"',
425 print '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
426 print '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">'
427 print '<head>'
epoger@google.com3d8cd172012-05-11 18:26:16 +0000428 print '<title>%s</title>' % qe(title)
bungeman@google.com85669f92011-06-17 13:58:14 +0000429 print '</head>'
430 print '<body>'
431
432 output_svg(lines, regressions, requested_width, requested_height)
epoger@google.com3d8cd172012-05-11 18:26:16 +0000433
bungeman@google.com85669f92011-06-17 13:58:14 +0000434 #output the manipulation controls
435 print """
436<script type="text/javascript">//<![CDATA[
437 function getElementsByClass(node, searchClass, tag) {
438 var classElements = new Array();
439 var elements = node.getElementsByTagName(tag);
440 var pattern = new RegExp("^|\\s"+searchClass+"\\s|$");
441 for (var i = 0, elementsFound = 0; i < elements.length; ++i) {
442 if (pattern.test(elements[i].className)) {
443 classElements[elementsFound] = elements[i];
444 ++elementsFound;
445 }
446 }
447 return classElements;
448 }
449 function getAllLines() {
450 var selectElem = document.getElementById('benchSelect');
451 var linesObj = {};
452 for (var i = 0; i < selectElem.options.length; ++i) {
453 var lines = JSON.parse(selectElem.options[i].value);
454 for (var j = 0; j < lines.length; ++j) {
455 linesObj[lines[j]] = true;
456 }
457 }
458 return linesObj;
459 }
460 function getOptions(selectElem) {
461 var linesSelectedObj = {};
462 for (var i = 0; i < selectElem.options.length; ++i) {
463 if (!selectElem.options[i].selected) continue;
464
465 var linesSelected = JSON.parse(selectElem.options[i].value);
466 for (var j = 0; j < linesSelected.length; ++j) {
467 linesSelectedObj[linesSelected[j]] = true;
468 }
469 }
470 return linesSelectedObj;
471 }
472 function objectEmpty(obj) {
473 for (var p in obj) {
474 return false;
475 }
476 return true;
477 }
478 function markSelectedLines(selectElem, allLines) {
479 var linesSelected = getOptions(selectElem);
480 if (!objectEmpty(linesSelected)) {
481 for (var line in allLines) {
482 allLines[line] &= (linesSelected[line] == true);
483 }
484 }
485 }
486 function updateSvg() {
487 var allLines = getAllLines();
488
489 var selects = getElementsByClass(document, 'lines', 'select');
490 for (var i = 0; i < selects.length; ++i) {
491 markSelectedLines(selects[i], allLines);
492 }
493
494 for (var line in allLines) {
495 var svgLine = document.getElementById(line);
496 var display = (allLines[line] ? 'inline' : 'none');
497 svgLine.setAttributeNS(null,'display', display);
498 }
499 }
500
501 function mark(markerId) {
502 for (var line in getAllLines()) {
503 var svgLineGroup = document.getElementById(line);
504 var display = svgLineGroup.getAttributeNS(null,'display');
505 if (display == null || display == "" || display != "none") {
506 var svgLine = document.getElementById(line+'_line');
507 if (markerId == null) {
508 svgLine.removeAttributeNS(null,'marker-mid');
509 } else {
510 svgLine.setAttributeNS(null,'marker-mid', markerId);
511 }
512 }
513 }
514 }
515//]]></script>"""
epoger@google.comc71174d2011-08-08 17:19:23 +0000516
517 print '<table border="0" width="%s">' % requested_width
518 print """
epoger@google.com3d8cd172012-05-11 18:26:16 +0000519<tr valign="top"><td width="50%">
520<table border="0" width="100%">
521<tr><td align="center"><table border="0">
epoger@google.comc71174d2011-08-08 17:19:23 +0000522<form>
523<tr valign="bottom" align="center">
524<td width="1">Bench&nbsp;Type</td>
525<td width="1">Bitmap Config</td>
526<td width="1">Timer&nbsp;Type (Cpu/Gpu/wall)</td>
527<td width="1"><!--buttons--></td>
epoger@google.com3d8cd172012-05-11 18:26:16 +0000528</tr><tr valign="top" align="center">
529"""
epoger@google.comc71174d2011-08-08 17:19:23 +0000530 print '<td width="1">'
bungeman@google.com85669f92011-06-17 13:58:14 +0000531 create_select(lambda l: l.bench, lines, 'benchSelect')
epoger@google.comc71174d2011-08-08 17:19:23 +0000532 print '</td><td width="1">'
bungeman@google.com85669f92011-06-17 13:58:14 +0000533 create_select(lambda l: l.config, lines)
epoger@google.comc71174d2011-08-08 17:19:23 +0000534 print '</td><td width="1">'
bungeman@google.com85669f92011-06-17 13:58:14 +0000535 create_select(lambda l: l.time_type, lines)
epoger@google.comc71174d2011-08-08 17:19:23 +0000536
bungeman@google.com85669f92011-06-17 13:58:14 +0000537 all_settings = {}
538 variant_settings = set()
539 for label in lines.keys():
540 for key, value in label.settings.items():
541 if key not in all_settings:
542 all_settings[key] = value
543 elif all_settings[key] != value:
544 variant_settings.add(key)
epoger@google.comc71174d2011-08-08 17:19:23 +0000545
bungeman@google.com85669f92011-06-17 13:58:14 +0000546 for k in variant_settings:
547 create_select(lambda l: l.settings[k], lines)
epoger@google.comc71174d2011-08-08 17:19:23 +0000548
549 print '</td><td width="1"><button type="button"',
bungeman@google.com85669f92011-06-17 13:58:14 +0000550 print 'onclick=%s' % qa("mark('url(#circleMark)'); return false;"),
epoger@google.comc71174d2011-08-08 17:19:23 +0000551 print '>Mark Points</button>'
552 print '<button type="button" onclick="mark(null);">Clear Points</button>'
epoger@google.com3d8cd172012-05-11 18:26:16 +0000553 print '</td>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000554 print """
epoger@google.com3d8cd172012-05-11 18:26:16 +0000555</tr>
556</form>
557</table></td></tr>
558<tr><td align="center">
559<hr />
560"""
561
562 output_ignored_data_points_warning(ignored_revision_data_points)
563 print '</td></tr></table>'
564 print '</td><td width="2%"><!--gutter--></td>'
565
566 print '<td><table border="0">'
567 print '<tr><td align="center">%s<br></br>revisions r%s - r%s</td></tr>' % (
568 qe(title),
569 bench_util.CreateRevisionLink(oldest_revision),
570 bench_util.CreateRevisionLink(newest_revision))
571 print """
572<tr><td align="left">
epoger@google.comc71174d2011-08-08 17:19:23 +0000573<p>Brighter red indicates tests that have gotten worse; brighter green
574indicates tests that have gotten better.</p>
575<p>To highlight individual tests, hold down CONTROL and mouse over
576graph lines.</p>
577<p>To highlight revision numbers, hold down SHIFT and mouse over
578the graph area.</p>
579<p>To only show certain tests on the graph, select any combination of
580tests in the selectors at left. (To show all, select all.)</p>
581<p>Use buttons at left to mark/clear points on the lines for selected
582benchmarks.</p>
epoger@google.com3d8cd172012-05-11 18:26:16 +0000583</td></tr>
584</table>
585
epoger@google.comc71174d2011-08-08 17:19:23 +0000586</td>
587</tr>
epoger@google.comc71174d2011-08-08 17:19:23 +0000588</table>
589</body>
590</html>"""
bungeman@google.com85669f92011-06-17 13:58:14 +0000591
592def compute_size(requested_width, requested_height, rev_width, time_height):
593 """Converts potentially empty requested size into a concrete size.
594
595 (Number?, Number?) -> (Number, Number)"""
596 pic_width = 0
597 pic_height = 0
598 if (requested_width is not None and requested_height is not None):
599 pic_height = requested_height
600 pic_width = requested_width
601
602 elif (requested_width is not None):
603 pic_width = requested_width
604 pic_height = pic_width * (float(time_height) / rev_width)
605
606 elif (requested_height is not None):
607 pic_height = requested_height
608 pic_width = pic_height * (float(rev_width) / time_height)
609
610 else:
611 pic_height = 800
612 pic_width = max(rev_width*3
613 , pic_height * (float(rev_width) / time_height))
614
615 return (pic_width, pic_height)
616
617def output_svg(lines, regressions, requested_width, requested_height):
618 """Outputs an svg view of the data."""
619
620 (global_min_x, _), (global_max_x, global_max_y) = bounds(lines)
621 max_up_slope, min_down_slope = bounds_slope(regressions)
622
623 #output
624 global_min_y = 0
625 x = global_min_x
626 y = global_min_y
627 w = global_max_x - global_min_x
628 h = global_max_y - global_min_y
629 font_size = 16
630 line_width = 2
631
632 pic_width, pic_height = compute_size(requested_width, requested_height
633 , w, h)
634
635 def cw(w1):
636 """Converts a revision difference to display width."""
637 return (pic_width / float(w)) * w1
638 def cx(x):
639 """Converts a revision to a horizontal display position."""
640 return cw(x - global_min_x)
641
642 def ch(h1):
643 """Converts a time difference to a display height."""
644 return -(pic_height / float(h)) * h1
645 def cy(y):
646 """Converts a time to a vertical display position."""
647 return pic_height + ch(y - global_min_y)
648
649 print '<svg',
650 print 'width=%s' % qa(str(pic_width)+'px')
651 print 'height=%s' % qa(str(pic_height)+'px')
652 print 'viewBox="0 0 %s %s"' % (str(pic_width), str(pic_height))
653 print 'onclick=%s' % qa(
654 "var event = arguments[0] || window.event;"
655 " if (event.shiftKey) { highlightRevision(null); }"
656 " if (event.ctrlKey) { highlight(null); }"
657 " return false;")
658 print 'xmlns="http://www.w3.org/2000/svg"'
659 print 'xmlns:xlink="http://www.w3.org/1999/xlink">'
660
661 print """
662<defs>
663 <marker id="circleMark"
664 viewBox="0 0 2 2" refX="1" refY="1"
665 markerUnits="strokeWidth"
666 markerWidth="2" markerHeight="2"
667 orient="0">
668 <circle cx="1" cy="1" r="1"/>
669 </marker>
670</defs>"""
671
672 #output the revisions
673 print """
674<script type="text/javascript">//<![CDATA[
675 var previousRevision;
676 var previousRevisionFill;
677 var previousRevisionStroke
678 function highlightRevision(id) {
679 if (previousRevision == id) return;
epoger@google.comc71174d2011-08-08 17:19:23 +0000680
681 document.getElementById('revision').firstChild.nodeValue = 'r' + id;
682 document.getElementById('rev_link').setAttribute('xlink:href',
683 'http://code.google.com/p/skia/source/detail?r=' + id);
bungeman@google.com85669f92011-06-17 13:58:14 +0000684
685 var preRevision = document.getElementById(previousRevision);
686 if (preRevision) {
687 preRevision.setAttributeNS(null,'fill', previousRevisionFill);
688 preRevision.setAttributeNS(null,'stroke', previousRevisionStroke);
689 }
690
691 var revision = document.getElementById(id);
692 previousRevision = id;
693 if (revision) {
694 previousRevisionFill = revision.getAttributeNS(null,'fill');
695 revision.setAttributeNS(null,'fill','rgb(100%, 95%, 95%)');
696
697 previousRevisionStroke = revision.getAttributeNS(null,'stroke');
698 revision.setAttributeNS(null,'stroke','rgb(100%, 90%, 90%)');
699 }
700 }
701//]]></script>"""
702
703 def print_rect(x, y, w, h, revision):
704 """Outputs a revision rectangle in display space,
705 taking arguments in revision space."""
706 disp_y = cy(y)
707 disp_h = ch(h)
708 if disp_h < 0:
709 disp_y += disp_h
710 disp_h = -disp_h
711
712 print '<rect id=%s x=%s y=%s' % (qa(revision), qa(cx(x)), qa(disp_y),),
713 print 'width=%s height=%s' % (qa(cw(w)), qa(disp_h),),
714 print 'fill="white"',
epoger@google.comc71174d2011-08-08 17:19:23 +0000715 print 'stroke="rgb(98%%,98%%,88%%)" stroke-width=%s' % qa(line_width),
bungeman@google.com85669f92011-06-17 13:58:14 +0000716 print 'onmouseover=%s' % qa(
717 "var event = arguments[0] || window.event;"
718 " if (event.shiftKey) {"
719 " highlightRevision('"+str(revision)+"');"
720 " return false;"
721 " }"),
722 print ' />'
723
724 xes = set()
725 for line in lines.itervalues():
726 for point in line:
727 xes.add(point[0])
728 revisions = list(xes)
729 revisions.sort()
730
731 left = x
732 current_revision = revisions[0]
733 for next_revision in revisions[1:]:
734 width = (((next_revision - current_revision) / 2.0)
735 + (current_revision - left))
736 print_rect(left, y, width, h, current_revision)
737 left += width
738 current_revision = next_revision
739 print_rect(left, y, x+w - left, h, current_revision)
epoger@google.comc71174d2011-08-08 17:19:23 +0000740
bungeman@google.com85669f92011-06-17 13:58:14 +0000741 #output the lines
742 print """
743<script type="text/javascript">//<![CDATA[
744 var previous;
745 var previousColor;
746 var previousOpacity;
747 function highlight(id) {
748 if (previous == id) return;
epoger@google.comc71174d2011-08-08 17:19:23 +0000749
bungeman@google.com85669f92011-06-17 13:58:14 +0000750 document.getElementById('label').firstChild.nodeValue = id;
epoger@google.comc71174d2011-08-08 17:19:23 +0000751
bungeman@google.com85669f92011-06-17 13:58:14 +0000752 var preGroup = document.getElementById(previous);
753 if (preGroup) {
754 var preLine = document.getElementById(previous+'_line');
755 preLine.setAttributeNS(null,'stroke', previousColor);
756 preLine.setAttributeNS(null,'opacity', previousOpacity);
epoger@google.comc71174d2011-08-08 17:19:23 +0000757
bungeman@google.com85669f92011-06-17 13:58:14 +0000758 var preSlope = document.getElementById(previous+'_linear');
759 if (preSlope) {
760 preSlope.setAttributeNS(null,'visibility', 'hidden');
761 }
762 }
epoger@google.comc71174d2011-08-08 17:19:23 +0000763
bungeman@google.com85669f92011-06-17 13:58:14 +0000764 var group = document.getElementById(id);
765 previous = id;
766 if (group) {
767 group.parentNode.appendChild(group);
768
769 var line = document.getElementById(id+'_line');
770 previousColor = line.getAttributeNS(null,'stroke');
771 previousOpacity = line.getAttributeNS(null,'opacity');
772 line.setAttributeNS(null,'stroke', 'blue');
773 line.setAttributeNS(null,'opacity', '1');
774
775 var slope = document.getElementById(id+'_linear');
776 if (slope) {
777 slope.setAttributeNS(null,'visibility', 'visible');
778 }
779 }
780 }
781//]]></script>"""
782 for label, line in lines.items():
783 print '<g id=%s>' % qa(label)
784 r = 128
785 g = 128
786 b = 128
787 a = .10
788 if label in regressions:
789 regression = regressions[label]
790 min_slope = regression.find_min_slope()
791 if min_slope < 0:
792 d = max(0, (min_slope / min_down_slope))
793 g += int(d*128)
794 a += d*0.9
795 elif min_slope > 0:
796 d = max(0, (min_slope / max_up_slope))
797 r += int(d*128)
798 a += d*0.9
799
800 slope = regression.slope
801 intercept = regression.intercept
802 min_x = regression.min_x
803 max_x = regression.max_x
804 print '<polyline id=%s' % qa(str(label)+'_linear'),
805 print 'fill="none" stroke="yellow"',
806 print 'stroke-width=%s' % qa(abs(ch(regression.serror*2))),
807 print 'opacity="0.5" pointer-events="none" visibility="hidden"',
808 print 'points="',
809 print '%s,%s' % (str(cx(min_x)), str(cy(slope*min_x + intercept))),
810 print '%s,%s' % (str(cx(max_x)), str(cy(slope*max_x + intercept))),
811 print '"/>'
812
813 print '<polyline id=%s' % qa(str(label)+'_line'),
814 print 'onmouseover=%s' % qa(
815 "var event = arguments[0] || window.event;"
816 " if (event.ctrlKey) {"
817 " highlight('"+str(label).replace("'", "\\'")+"');"
818 " return false;"
819 " }"),
820 print 'fill="none" stroke="rgb(%s,%s,%s)"' % (str(r), str(g), str(b)),
821 print 'stroke-width=%s' % qa(line_width),
822 print 'opacity=%s' % qa(a),
823 print 'points="',
824 for point in line:
825 print '%s,%s' % (str(cx(point[0])), str(cy(point[1]))),
826 print '"/>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000827
bungeman@google.com85669f92011-06-17 13:58:14 +0000828 print '</g>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000829
bungeman@google.com85669f92011-06-17 13:58:14 +0000830 #output the labels
831 print '<text id="label" x="0" y=%s' % qa(font_size),
832 print 'font-size=%s> </text>' % qa(font_size)
epoger@google.comc71174d2011-08-08 17:19:23 +0000833
834 print '<a id="rev_link" xlink:href="" target="_top">'
835 print '<text id="revision" x="0" y=%s style="' % qa(font_size*2)
836 print 'font-size: %s; ' % qe(font_size)
837 print 'stroke: #0000dd; text-decoration: underline; '
838 print '"> </text></a>'
839
bungeman@google.com85669f92011-06-17 13:58:14 +0000840 print '</svg>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000841
bungeman@google.com85669f92011-06-17 13:58:14 +0000842if __name__ == "__main__":
843 main()