blob: 78a0366f75e14faca0b9dbccb8191f97012b353e [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).'
bensong@google.com87348162012-08-15 17:31:46 +000031 print '-m <representative> use "avg", "min", or "med" for bench value.'
32 print ' They correspond to average, minimum and median of bench iters.'
epoger@google.com5b2e01c2012-06-25 20:29:04 +000033 print '-t <time> the time to show (w, c, g, etc).'
bungeman@google.com85669f92011-06-17 13:58:14 +000034 print '-x <int> the desired width of the svg.'
35 print '-y <int> the desired height of the svg.'
36 print '--default-setting <setting>[=<value>] setting for those without.'
37
38
39class Label:
40 """The information in a label.
41
42 (str, str, str, str, {str:str})"""
43 def __init__(self, bench, config, time_type, settings):
44 self.bench = bench
45 self.config = config
46 self.time_type = time_type
47 self.settings = settings
48
49 def __repr__(self):
50 return "Label(%s, %s, %s, %s)" % (
51 str(self.bench),
52 str(self.config),
53 str(self.time_type),
54 str(self.settings),
55 )
56
57 def __str__(self):
58 return "%s_%s_%s_%s" % (
59 str(self.bench),
60 str(self.config),
61 str(self.time_type),
62 str(self.settings),
63 )
64
65 def __eq__(self, other):
66 return (self.bench == other.bench and
67 self.config == other.config and
68 self.time_type == other.time_type and
69 self.settings == other.settings)
70
71 def __hash__(self):
72 return (hash(self.bench) ^
73 hash(self.config) ^
74 hash(self.time_type) ^
75 hash(frozenset(self.settings.iteritems())))
76
epoger@google.com37260002011-08-08 20:27:04 +000077def get_latest_revision(directory):
78 """Returns the latest revision number found within this directory.
79 """
80 latest_revision_found = -1
81 for bench_file in os.listdir(directory):
82 file_name_match = re.match('bench_r(\d+)_(\S+)', bench_file)
83 if (file_name_match is None):
84 continue
85 revision = int(file_name_match.group(1))
86 if revision > latest_revision_found:
87 latest_revision_found = revision
88 if latest_revision_found < 0:
89 return None
90 else:
91 return latest_revision_found
92
bensong@google.com87348162012-08-15 17:31:46 +000093def parse_dir(directory, default_settings, oldest_revision, newest_revision,
94 rep):
bungeman@google.com85669f92011-06-17 13:58:14 +000095 """Parses bench data from files like bench_r<revision>_<scalar>.
96
97 (str, {str, str}, Number, Number) -> {int:[BenchDataPoints]}"""
98 revision_data_points = {} # {revision : [BenchDataPoints]}
99 for bench_file in os.listdir(directory):
100 file_name_match = re.match('bench_r(\d+)_(\S+)', bench_file)
101 if (file_name_match is None):
102 continue
epoger@google.com3d8cd172012-05-11 18:26:16 +0000103
bungeman@google.com85669f92011-06-17 13:58:14 +0000104 revision = int(file_name_match.group(1))
105 scalar_type = file_name_match.group(2)
epoger@google.com3d8cd172012-05-11 18:26:16 +0000106
bungeman@google.com85669f92011-06-17 13:58:14 +0000107 if (revision < oldest_revision or revision > newest_revision):
108 continue
epoger@google.com3d8cd172012-05-11 18:26:16 +0000109
bungeman@google.com85669f92011-06-17 13:58:14 +0000110 file_handle = open(directory + '/' + bench_file, 'r')
epoger@google.com3d8cd172012-05-11 18:26:16 +0000111
bungeman@google.com85669f92011-06-17 13:58:14 +0000112 if (revision not in revision_data_points):
113 revision_data_points[revision] = []
114 default_settings['scalar'] = scalar_type
115 revision_data_points[revision].extend(
bensong@google.com87348162012-08-15 17:31:46 +0000116 bench_util.parse(default_settings, file_handle, rep))
bungeman@google.com85669f92011-06-17 13:58:14 +0000117 file_handle.close()
118 return revision_data_points
119
epoger@google.com3d8cd172012-05-11 18:26:16 +0000120def add_to_revision_data_points(new_point, revision, revision_data_points):
121 """Add new_point to set of revision_data_points we are building up.
122 """
123 if (revision not in revision_data_points):
124 revision_data_points[revision] = []
125 revision_data_points[revision].append(new_point)
126
127def filter_data_points(unfiltered_revision_data_points):
128 """Filter out any data points that are utterly bogus.
129
130 Returns (allowed_revision_data_points, ignored_revision_data_points):
131 allowed_revision_data_points: points that survived the filter
132 ignored_revision_data_points: points that did NOT survive the filter
133 """
134 allowed_revision_data_points = {} # {revision : [BenchDataPoints]}
135 ignored_revision_data_points = {} # {revision : [BenchDataPoints]}
136 revisions = unfiltered_revision_data_points.keys()
137 revisions.sort()
138 for revision in revisions:
139 for point in unfiltered_revision_data_points[revision]:
140 if point.time < MIN_REASONABLE_TIME or point.time > MAX_REASONABLE_TIME:
141 add_to_revision_data_points(point, revision, ignored_revision_data_points)
142 else:
143 add_to_revision_data_points(point, revision, allowed_revision_data_points)
144 return (allowed_revision_data_points, ignored_revision_data_points)
145
epoger@google.com1513f6e2012-06-27 13:38:37 +0000146def get_abs_path(relative_path):
147 """My own implementation of os.path.abspath() that better handles paths
148 which approach Window's 260-character limit.
149 See https://code.google.com/p/skia/issues/detail?id=674
150
151 This implementation adds path components one at a time, resolving the
152 absolute path each time, to take advantage of any chdirs into outer
153 directories that will shorten the total path length.
154
155 TODO: share a single implementation with upload_to_bucket.py, instead
156 of pasting this same code into both files."""
157 if os.path.isabs(relative_path):
158 return relative_path
159 path_parts = relative_path.split(os.sep)
160 abs_path = os.path.abspath('.')
161 for path_part in path_parts:
162 abs_path = os.path.abspath(os.path.join(abs_path, path_part))
163 return abs_path
164
epoger@google.com5b2e01c2012-06-25 20:29:04 +0000165def redirect_stdout(output_path):
166 """Redirect all following stdout to a file.
167
168 You may be asking yourself, why redirect stdout within Python rather than
169 redirecting the script's output in the calling shell?
170 The answer lies in https://code.google.com/p/skia/issues/detail?id=674
171 ('buildbot: windows GenerateBenchGraphs step fails due to filename length'):
172 On Windows, we need to generate the absolute path within Python to avoid
173 the operating system's 260-character pathname limit, including chdirs."""
epoger@google.com1513f6e2012-06-27 13:38:37 +0000174 abs_path = get_abs_path(output_path)
epoger@google.com5b2e01c2012-06-25 20:29:04 +0000175 sys.stdout = open(abs_path, 'w')
176
bungeman@google.com85669f92011-06-17 13:58:14 +0000177def create_lines(revision_data_points, settings
178 , bench_of_interest, config_of_interest, time_of_interest):
179 """Convert revision data into sorted line data.
180
181 ({int:[BenchDataPoints]}, {str:str}, str?, str?, str?)
182 -> {Label:[(x,y)] | [n].x <= [n+1].x}"""
183 revisions = revision_data_points.keys()
184 revisions.sort()
185 lines = {} # {Label:[(x,y)] | x[n] <= x[n+1]}
186 for revision in revisions:
187 for point in revision_data_points[revision]:
188 if (bench_of_interest is not None and
189 not bench_of_interest == point.bench):
190 continue
191
192 if (config_of_interest is not None and
193 not config_of_interest == point.config):
194 continue
195
196 if (time_of_interest is not None and
197 not time_of_interest == point.time_type):
198 continue
199
200 skip = False
201 for key, value in settings.items():
202 if key in point.settings and point.settings[key] != value:
203 skip = True
204 break
205 if skip:
206 continue
207
208 line_name = Label(point.bench
209 , point.config
210 , point.time_type
211 , point.settings)
212
213 if line_name not in lines:
214 lines[line_name] = []
215
216 lines[line_name].append((revision, point.time))
217
218 return lines
219
220def bounds(lines):
221 """Finds the bounding rectangle for the lines.
222
223 {Label:[(x,y)]} -> ((min_x, min_y),(max_x,max_y))"""
224 min_x = bench_util.Max
225 min_y = bench_util.Max
226 max_x = bench_util.Min
227 max_y = bench_util.Min
228
229 for line in lines.itervalues():
230 for x, y in line:
231 min_x = min(min_x, x)
232 min_y = min(min_y, y)
233 max_x = max(max_x, x)
234 max_y = max(max_y, y)
235
236 return ((min_x, min_y), (max_x, max_y))
237
238def create_regressions(lines, start_x, end_x):
239 """Creates regression data from line segments.
240
241 ({Label:[(x,y)] | [n].x <= [n+1].x}, Number, Number)
242 -> {Label:LinearRegression}"""
243 regressions = {} # {Label : LinearRegression}
244
245 for label, line in lines.iteritems():
246 regression_line = [p for p in line if start_x <= p[0] <= end_x]
247
248 if (len(regression_line) < 2):
249 continue
250 regression = bench_util.LinearRegression(regression_line)
251 regressions[label] = regression
252
253 return regressions
254
255def bounds_slope(regressions):
256 """Finds the extreme up and down slopes of a set of linear regressions.
257
258 ({Label:LinearRegression}) -> (max_up_slope, min_down_slope)"""
259 max_up_slope = 0
260 min_down_slope = 0
261 for regression in regressions.itervalues():
262 min_slope = regression.find_min_slope()
263 max_up_slope = max(max_up_slope, min_slope)
264 min_down_slope = min(min_down_slope, min_slope)
265
266 return (max_up_slope, min_down_slope)
267
268def main():
269 """Parses command line and writes output."""
270
271 try:
272 opts, _ = getopt.getopt(sys.argv[1:]
bensong@google.com87348162012-08-15 17:31:46 +0000273 , "b:c:d:f:l:m:o:r:s:t:x:y:"
bungeman@google.com85669f92011-06-17 13:58:14 +0000274 , "default-setting=")
275 except getopt.GetoptError, err:
276 print str(err)
277 usage()
278 sys.exit(2)
279
280 directory = None
281 config_of_interest = None
282 bench_of_interest = None
283 time_of_interest = None
bensong@google.com87348162012-08-15 17:31:46 +0000284 rep = "avg" # bench representative calculation algorithm
epoger@google.com37260002011-08-08 20:27:04 +0000285 revision_range = '0:'
286 regression_range = '0:'
287 latest_revision = None
bungeman@google.com85669f92011-06-17 13:58:14 +0000288 requested_height = None
289 requested_width = None
epoger@google.com37260002011-08-08 20:27:04 +0000290 title = 'Bench graph'
bungeman@google.com85669f92011-06-17 13:58:14 +0000291 settings = {}
292 default_settings = {}
epoger@google.com37260002011-08-08 20:27:04 +0000293
294 def parse_range(range):
295 """Takes '<old>[:<new>]' as a string and returns (old, new).
296 Any revision numbers that are dependent on the latest revision number
297 will be filled in based on latest_revision.
298 """
299 old, _, new = range.partition(":")
300 old = int(old)
301 if old < 0:
302 old += latest_revision;
bungeman@google.com85669f92011-06-17 13:58:14 +0000303 if not new:
epoger@google.com37260002011-08-08 20:27:04 +0000304 new = latest_revision;
305 new = int(new)
306 if new < 0:
307 new += latest_revision;
308 return (old, new)
309
bungeman@google.com85669f92011-06-17 13:58:14 +0000310 def add_setting(settings, setting):
311 """Takes <key>[=<value>] adds {key:value} or {key:True} to settings."""
312 name, _, value = setting.partition('=')
313 if not value:
314 settings[name] = True
315 else:
316 settings[name] = value
317
318 try:
319 for option, value in opts:
epoger@google.com5b2e01c2012-06-25 20:29:04 +0000320 if option == "-b":
bungeman@google.com85669f92011-06-17 13:58:14 +0000321 bench_of_interest = value
322 elif option == "-c":
323 config_of_interest = value
epoger@google.com5b2e01c2012-06-25 20:29:04 +0000324 elif option == "-d":
325 directory = value
bungeman@google.com85669f92011-06-17 13:58:14 +0000326 elif option == "-f":
epoger@google.com37260002011-08-08 20:27:04 +0000327 regression_range = value
epoger@google.com5b2e01c2012-06-25 20:29:04 +0000328 elif option == "-l":
329 title = value
bensong@google.com87348162012-08-15 17:31:46 +0000330 elif option == "-m":
331 rep = value
332 if rep not in ["avg", "min", "med"]:
333 raise Exception("Invalid -m representative: %s" % rep)
epoger@google.com5b2e01c2012-06-25 20:29:04 +0000334 elif option == "-o":
335 redirect_stdout(value)
336 elif option == "-r":
337 revision_range = value
338 elif option == "-s":
339 add_setting(settings, value)
340 elif option == "-t":
341 time_of_interest = value
bungeman@google.com85669f92011-06-17 13:58:14 +0000342 elif option == "-x":
343 requested_width = int(value)
344 elif option == "-y":
345 requested_height = int(value)
346 elif option == "--default-setting":
347 add_setting(default_settings, value)
348 else:
349 usage()
350 assert False, "unhandled option"
351 except ValueError:
352 usage()
353 sys.exit(2)
epoger@google.comc71174d2011-08-08 17:19:23 +0000354
bungeman@google.com85669f92011-06-17 13:58:14 +0000355 if directory is None:
356 usage()
357 sys.exit(2)
epoger@google.comc71174d2011-08-08 17:19:23 +0000358
bensong@google.com8c1de762012-08-15 18:27:38 +0000359 title += ' [representation: %s]' % rep
360
epoger@google.com37260002011-08-08 20:27:04 +0000361 latest_revision = get_latest_revision(directory)
362 oldest_revision, newest_revision = parse_range(revision_range)
363 oldest_regression, newest_regression = parse_range(regression_range)
364
epoger@google.com3d8cd172012-05-11 18:26:16 +0000365 unfiltered_revision_data_points = parse_dir(directory
bungeman@google.com85669f92011-06-17 13:58:14 +0000366 , default_settings
367 , oldest_revision
bensong@google.com87348162012-08-15 17:31:46 +0000368 , newest_revision
369 , rep)
epoger@google.comc71174d2011-08-08 17:19:23 +0000370
epoger@google.com3d8cd172012-05-11 18:26:16 +0000371 # Filter out any data points that are utterly bogus... make sure to report
372 # that we did so later!
373 (allowed_revision_data_points, ignored_revision_data_points) = filter_data_points(
374 unfiltered_revision_data_points)
375
epoger@google.comc71174d2011-08-08 17:19:23 +0000376 # Update oldest_revision and newest_revision based on the data we could find
epoger@google.com3d8cd172012-05-11 18:26:16 +0000377 all_revision_numbers = allowed_revision_data_points.keys()
epoger@google.comc71174d2011-08-08 17:19:23 +0000378 oldest_revision = min(all_revision_numbers)
379 newest_revision = max(all_revision_numbers)
380
epoger@google.com3d8cd172012-05-11 18:26:16 +0000381 lines = create_lines(allowed_revision_data_points
bungeman@google.com85669f92011-06-17 13:58:14 +0000382 , settings
383 , bench_of_interest
384 , config_of_interest
385 , time_of_interest)
epoger@google.comc71174d2011-08-08 17:19:23 +0000386
bungeman@google.com85669f92011-06-17 13:58:14 +0000387 regressions = create_regressions(lines
388 , oldest_regression
389 , newest_regression)
epoger@google.comc71174d2011-08-08 17:19:23 +0000390
epoger@google.com3d8cd172012-05-11 18:26:16 +0000391 output_xhtml(lines, oldest_revision, newest_revision, ignored_revision_data_points,
epoger@google.com37260002011-08-08 20:27:04 +0000392 regressions, requested_width, requested_height, title)
bungeman@google.com85669f92011-06-17 13:58:14 +0000393
394def qa(out):
395 """Stringify input and quote as an xml attribute."""
396 return xml.sax.saxutils.quoteattr(str(out))
397def qe(out):
398 """Stringify input and escape as xml data."""
399 return xml.sax.saxutils.escape(str(out))
400
401def create_select(qualifier, lines, select_id=None):
402 """Output select with options showing lines which qualifier maps to it.
403
404 ((Label) -> str, {Label:_}, str?) -> _"""
405 options = {} #{ option : [Label]}
406 for label in lines.keys():
407 option = qualifier(label)
408 if (option not in options):
409 options[option] = []
410 options[option].append(label)
411 option_list = list(options.keys())
412 option_list.sort()
413 print '<select class="lines"',
414 if select_id is not None:
415 print 'id=%s' % qa(select_id)
416 print 'multiple="true" size="10" onchange="updateSvg();">'
417 for option in option_list:
418 print '<option value=' + qa('[' +
419 reduce(lambda x,y:x+json.dumps(str(y))+',',options[option],"")[0:-1]
420 + ']') + '>'+qe(option)+'</option>'
421 print '</select>'
422
epoger@google.com3d8cd172012-05-11 18:26:16 +0000423def output_ignored_data_points_warning(ignored_revision_data_points):
424 """Write description of ignored_revision_data_points to stdout as xhtml.
425 """
426 num_ignored_points = 0
427 description = ''
428 revisions = ignored_revision_data_points.keys()
429 if revisions:
430 revisions.sort()
431 revisions.reverse()
432 for revision in revisions:
433 num_ignored_points += len(ignored_revision_data_points[revision])
434 points_at_this_revision = []
435 for point in ignored_revision_data_points[revision]:
436 points_at_this_revision.append(point.bench)
437 points_at_this_revision.sort()
438 description += 'r%d: %s\n' % (revision, points_at_this_revision)
439 if num_ignored_points == 0:
440 print 'Did not discard any data points; all were within the range [%d-%d]' % (
441 MIN_REASONABLE_TIME, MAX_REASONABLE_TIME)
442 else:
443 print '<table width="100%" bgcolor="ff0000"><tr><td align="center">'
444 print 'Discarded %d data points outside of range [%d-%d]' % (
445 num_ignored_points, MIN_REASONABLE_TIME, MAX_REASONABLE_TIME)
446 print '</td></tr><tr><td width="100%" align="center">'
447 print ('<textarea rows="4" style="width:97%" readonly="true" wrap="off">'
448 + qe(description) + '</textarea>')
449 print '</td></tr></table>'
450
451def output_xhtml(lines, oldest_revision, newest_revision, ignored_revision_data_points,
epoger@google.com37260002011-08-08 20:27:04 +0000452 regressions, requested_width, requested_height, title):
bungeman@google.com85669f92011-06-17 13:58:14 +0000453 """Outputs an svg/xhtml view of the data."""
454 print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"',
455 print '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
456 print '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">'
457 print '<head>'
epoger@google.com3d8cd172012-05-11 18:26:16 +0000458 print '<title>%s</title>' % qe(title)
bungeman@google.com85669f92011-06-17 13:58:14 +0000459 print '</head>'
460 print '<body>'
461
462 output_svg(lines, regressions, requested_width, requested_height)
epoger@google.com3d8cd172012-05-11 18:26:16 +0000463
bungeman@google.com85669f92011-06-17 13:58:14 +0000464 #output the manipulation controls
465 print """
466<script type="text/javascript">//<![CDATA[
467 function getElementsByClass(node, searchClass, tag) {
468 var classElements = new Array();
469 var elements = node.getElementsByTagName(tag);
470 var pattern = new RegExp("^|\\s"+searchClass+"\\s|$");
471 for (var i = 0, elementsFound = 0; i < elements.length; ++i) {
472 if (pattern.test(elements[i].className)) {
473 classElements[elementsFound] = elements[i];
474 ++elementsFound;
475 }
476 }
477 return classElements;
478 }
479 function getAllLines() {
480 var selectElem = document.getElementById('benchSelect');
481 var linesObj = {};
482 for (var i = 0; i < selectElem.options.length; ++i) {
483 var lines = JSON.parse(selectElem.options[i].value);
484 for (var j = 0; j < lines.length; ++j) {
485 linesObj[lines[j]] = true;
486 }
487 }
488 return linesObj;
489 }
490 function getOptions(selectElem) {
491 var linesSelectedObj = {};
492 for (var i = 0; i < selectElem.options.length; ++i) {
493 if (!selectElem.options[i].selected) continue;
494
495 var linesSelected = JSON.parse(selectElem.options[i].value);
496 for (var j = 0; j < linesSelected.length; ++j) {
497 linesSelectedObj[linesSelected[j]] = true;
498 }
499 }
500 return linesSelectedObj;
501 }
502 function objectEmpty(obj) {
503 for (var p in obj) {
504 return false;
505 }
506 return true;
507 }
508 function markSelectedLines(selectElem, allLines) {
509 var linesSelected = getOptions(selectElem);
510 if (!objectEmpty(linesSelected)) {
511 for (var line in allLines) {
512 allLines[line] &= (linesSelected[line] == true);
513 }
514 }
515 }
516 function updateSvg() {
517 var allLines = getAllLines();
518
519 var selects = getElementsByClass(document, 'lines', 'select');
520 for (var i = 0; i < selects.length; ++i) {
521 markSelectedLines(selects[i], allLines);
522 }
523
524 for (var line in allLines) {
525 var svgLine = document.getElementById(line);
526 var display = (allLines[line] ? 'inline' : 'none');
527 svgLine.setAttributeNS(null,'display', display);
528 }
529 }
530
531 function mark(markerId) {
532 for (var line in getAllLines()) {
533 var svgLineGroup = document.getElementById(line);
534 var display = svgLineGroup.getAttributeNS(null,'display');
535 if (display == null || display == "" || display != "none") {
536 var svgLine = document.getElementById(line+'_line');
537 if (markerId == null) {
538 svgLine.removeAttributeNS(null,'marker-mid');
539 } else {
540 svgLine.setAttributeNS(null,'marker-mid', markerId);
541 }
542 }
543 }
544 }
545//]]></script>"""
epoger@google.comc71174d2011-08-08 17:19:23 +0000546
547 print '<table border="0" width="%s">' % requested_width
548 print """
epoger@google.com3d8cd172012-05-11 18:26:16 +0000549<tr valign="top"><td width="50%">
550<table border="0" width="100%">
551<tr><td align="center"><table border="0">
epoger@google.comc71174d2011-08-08 17:19:23 +0000552<form>
553<tr valign="bottom" align="center">
554<td width="1">Bench&nbsp;Type</td>
555<td width="1">Bitmap Config</td>
556<td width="1">Timer&nbsp;Type (Cpu/Gpu/wall)</td>
557<td width="1"><!--buttons--></td>
epoger@google.com3d8cd172012-05-11 18:26:16 +0000558</tr><tr valign="top" align="center">
559"""
epoger@google.comc71174d2011-08-08 17:19:23 +0000560 print '<td width="1">'
bungeman@google.com85669f92011-06-17 13:58:14 +0000561 create_select(lambda l: l.bench, lines, 'benchSelect')
epoger@google.comc71174d2011-08-08 17:19:23 +0000562 print '</td><td width="1">'
bungeman@google.com85669f92011-06-17 13:58:14 +0000563 create_select(lambda l: l.config, lines)
epoger@google.comc71174d2011-08-08 17:19:23 +0000564 print '</td><td width="1">'
bungeman@google.com85669f92011-06-17 13:58:14 +0000565 create_select(lambda l: l.time_type, lines)
epoger@google.comc71174d2011-08-08 17:19:23 +0000566
bungeman@google.com85669f92011-06-17 13:58:14 +0000567 all_settings = {}
568 variant_settings = set()
569 for label in lines.keys():
570 for key, value in label.settings.items():
571 if key not in all_settings:
572 all_settings[key] = value
573 elif all_settings[key] != value:
574 variant_settings.add(key)
epoger@google.comc71174d2011-08-08 17:19:23 +0000575
bungeman@google.com85669f92011-06-17 13:58:14 +0000576 for k in variant_settings:
bungeman@google.comb6981552012-07-10 15:31:52 +0000577 create_select(lambda l: l.settings.get(k, "<missing>"), lines)
epoger@google.comc71174d2011-08-08 17:19:23 +0000578
579 print '</td><td width="1"><button type="button"',
bungeman@google.com85669f92011-06-17 13:58:14 +0000580 print 'onclick=%s' % qa("mark('url(#circleMark)'); return false;"),
epoger@google.comc71174d2011-08-08 17:19:23 +0000581 print '>Mark Points</button>'
582 print '<button type="button" onclick="mark(null);">Clear Points</button>'
epoger@google.com3d8cd172012-05-11 18:26:16 +0000583 print '</td>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000584 print """
epoger@google.com3d8cd172012-05-11 18:26:16 +0000585</tr>
586</form>
587</table></td></tr>
588<tr><td align="center">
589<hr />
590"""
591
592 output_ignored_data_points_warning(ignored_revision_data_points)
593 print '</td></tr></table>'
594 print '</td><td width="2%"><!--gutter--></td>'
595
596 print '<td><table border="0">'
597 print '<tr><td align="center">%s<br></br>revisions r%s - r%s</td></tr>' % (
598 qe(title),
599 bench_util.CreateRevisionLink(oldest_revision),
600 bench_util.CreateRevisionLink(newest_revision))
601 print """
602<tr><td align="left">
epoger@google.comc71174d2011-08-08 17:19:23 +0000603<p>Brighter red indicates tests that have gotten worse; brighter green
604indicates tests that have gotten better.</p>
605<p>To highlight individual tests, hold down CONTROL and mouse over
606graph lines.</p>
607<p>To highlight revision numbers, hold down SHIFT and mouse over
608the graph area.</p>
609<p>To only show certain tests on the graph, select any combination of
610tests in the selectors at left. (To show all, select all.)</p>
611<p>Use buttons at left to mark/clear points on the lines for selected
612benchmarks.</p>
epoger@google.com3d8cd172012-05-11 18:26:16 +0000613</td></tr>
614</table>
615
epoger@google.comc71174d2011-08-08 17:19:23 +0000616</td>
617</tr>
epoger@google.comc71174d2011-08-08 17:19:23 +0000618</table>
619</body>
620</html>"""
bungeman@google.com85669f92011-06-17 13:58:14 +0000621
622def compute_size(requested_width, requested_height, rev_width, time_height):
623 """Converts potentially empty requested size into a concrete size.
624
625 (Number?, Number?) -> (Number, Number)"""
626 pic_width = 0
627 pic_height = 0
628 if (requested_width is not None and requested_height is not None):
629 pic_height = requested_height
630 pic_width = requested_width
631
632 elif (requested_width is not None):
633 pic_width = requested_width
634 pic_height = pic_width * (float(time_height) / rev_width)
635
636 elif (requested_height is not None):
637 pic_height = requested_height
638 pic_width = pic_height * (float(rev_width) / time_height)
639
640 else:
641 pic_height = 800
642 pic_width = max(rev_width*3
643 , pic_height * (float(rev_width) / time_height))
644
645 return (pic_width, pic_height)
646
647def output_svg(lines, regressions, requested_width, requested_height):
648 """Outputs an svg view of the data."""
649
650 (global_min_x, _), (global_max_x, global_max_y) = bounds(lines)
651 max_up_slope, min_down_slope = bounds_slope(regressions)
652
653 #output
654 global_min_y = 0
655 x = global_min_x
656 y = global_min_y
657 w = global_max_x - global_min_x
658 h = global_max_y - global_min_y
659 font_size = 16
660 line_width = 2
661
662 pic_width, pic_height = compute_size(requested_width, requested_height
663 , w, h)
664
665 def cw(w1):
666 """Converts a revision difference to display width."""
667 return (pic_width / float(w)) * w1
668 def cx(x):
669 """Converts a revision to a horizontal display position."""
670 return cw(x - global_min_x)
671
672 def ch(h1):
673 """Converts a time difference to a display height."""
674 return -(pic_height / float(h)) * h1
675 def cy(y):
676 """Converts a time to a vertical display position."""
677 return pic_height + ch(y - global_min_y)
678
679 print '<svg',
680 print 'width=%s' % qa(str(pic_width)+'px')
681 print 'height=%s' % qa(str(pic_height)+'px')
682 print 'viewBox="0 0 %s %s"' % (str(pic_width), str(pic_height))
683 print 'onclick=%s' % qa(
684 "var event = arguments[0] || window.event;"
685 " if (event.shiftKey) { highlightRevision(null); }"
686 " if (event.ctrlKey) { highlight(null); }"
687 " return false;")
688 print 'xmlns="http://www.w3.org/2000/svg"'
689 print 'xmlns:xlink="http://www.w3.org/1999/xlink">'
690
691 print """
692<defs>
693 <marker id="circleMark"
694 viewBox="0 0 2 2" refX="1" refY="1"
695 markerUnits="strokeWidth"
696 markerWidth="2" markerHeight="2"
697 orient="0">
698 <circle cx="1" cy="1" r="1"/>
699 </marker>
700</defs>"""
701
702 #output the revisions
703 print """
704<script type="text/javascript">//<![CDATA[
705 var previousRevision;
706 var previousRevisionFill;
707 var previousRevisionStroke
708 function highlightRevision(id) {
709 if (previousRevision == id) return;
epoger@google.comc71174d2011-08-08 17:19:23 +0000710
711 document.getElementById('revision').firstChild.nodeValue = 'r' + id;
712 document.getElementById('rev_link').setAttribute('xlink:href',
713 'http://code.google.com/p/skia/source/detail?r=' + id);
bungeman@google.com85669f92011-06-17 13:58:14 +0000714
715 var preRevision = document.getElementById(previousRevision);
716 if (preRevision) {
717 preRevision.setAttributeNS(null,'fill', previousRevisionFill);
718 preRevision.setAttributeNS(null,'stroke', previousRevisionStroke);
719 }
720
721 var revision = document.getElementById(id);
722 previousRevision = id;
723 if (revision) {
724 previousRevisionFill = revision.getAttributeNS(null,'fill');
725 revision.setAttributeNS(null,'fill','rgb(100%, 95%, 95%)');
726
727 previousRevisionStroke = revision.getAttributeNS(null,'stroke');
728 revision.setAttributeNS(null,'stroke','rgb(100%, 90%, 90%)');
729 }
730 }
731//]]></script>"""
732
733 def print_rect(x, y, w, h, revision):
734 """Outputs a revision rectangle in display space,
735 taking arguments in revision space."""
736 disp_y = cy(y)
737 disp_h = ch(h)
738 if disp_h < 0:
739 disp_y += disp_h
740 disp_h = -disp_h
741
742 print '<rect id=%s x=%s y=%s' % (qa(revision), qa(cx(x)), qa(disp_y),),
743 print 'width=%s height=%s' % (qa(cw(w)), qa(disp_h),),
744 print 'fill="white"',
epoger@google.comc71174d2011-08-08 17:19:23 +0000745 print 'stroke="rgb(98%%,98%%,88%%)" stroke-width=%s' % qa(line_width),
bungeman@google.com85669f92011-06-17 13:58:14 +0000746 print 'onmouseover=%s' % qa(
747 "var event = arguments[0] || window.event;"
748 " if (event.shiftKey) {"
749 " highlightRevision('"+str(revision)+"');"
750 " return false;"
751 " }"),
752 print ' />'
753
754 xes = set()
755 for line in lines.itervalues():
756 for point in line:
757 xes.add(point[0])
758 revisions = list(xes)
759 revisions.sort()
760
761 left = x
762 current_revision = revisions[0]
763 for next_revision in revisions[1:]:
764 width = (((next_revision - current_revision) / 2.0)
765 + (current_revision - left))
766 print_rect(left, y, width, h, current_revision)
767 left += width
768 current_revision = next_revision
769 print_rect(left, y, x+w - left, h, current_revision)
epoger@google.comc71174d2011-08-08 17:19:23 +0000770
bungeman@google.com85669f92011-06-17 13:58:14 +0000771 #output the lines
772 print """
773<script type="text/javascript">//<![CDATA[
774 var previous;
775 var previousColor;
776 var previousOpacity;
777 function highlight(id) {
778 if (previous == id) return;
epoger@google.comc71174d2011-08-08 17:19:23 +0000779
bungeman@google.com85669f92011-06-17 13:58:14 +0000780 document.getElementById('label').firstChild.nodeValue = id;
epoger@google.comc71174d2011-08-08 17:19:23 +0000781
bungeman@google.com85669f92011-06-17 13:58:14 +0000782 var preGroup = document.getElementById(previous);
783 if (preGroup) {
784 var preLine = document.getElementById(previous+'_line');
785 preLine.setAttributeNS(null,'stroke', previousColor);
786 preLine.setAttributeNS(null,'opacity', previousOpacity);
epoger@google.comc71174d2011-08-08 17:19:23 +0000787
bungeman@google.com85669f92011-06-17 13:58:14 +0000788 var preSlope = document.getElementById(previous+'_linear');
789 if (preSlope) {
790 preSlope.setAttributeNS(null,'visibility', 'hidden');
791 }
792 }
epoger@google.comc71174d2011-08-08 17:19:23 +0000793
bungeman@google.com85669f92011-06-17 13:58:14 +0000794 var group = document.getElementById(id);
795 previous = id;
796 if (group) {
797 group.parentNode.appendChild(group);
798
799 var line = document.getElementById(id+'_line');
800 previousColor = line.getAttributeNS(null,'stroke');
801 previousOpacity = line.getAttributeNS(null,'opacity');
802 line.setAttributeNS(null,'stroke', 'blue');
803 line.setAttributeNS(null,'opacity', '1');
804
805 var slope = document.getElementById(id+'_linear');
806 if (slope) {
807 slope.setAttributeNS(null,'visibility', 'visible');
808 }
809 }
810 }
811//]]></script>"""
812 for label, line in lines.items():
813 print '<g id=%s>' % qa(label)
814 r = 128
815 g = 128
816 b = 128
817 a = .10
818 if label in regressions:
819 regression = regressions[label]
820 min_slope = regression.find_min_slope()
821 if min_slope < 0:
822 d = max(0, (min_slope / min_down_slope))
823 g += int(d*128)
824 a += d*0.9
825 elif min_slope > 0:
826 d = max(0, (min_slope / max_up_slope))
827 r += int(d*128)
828 a += d*0.9
829
830 slope = regression.slope
831 intercept = regression.intercept
832 min_x = regression.min_x
833 max_x = regression.max_x
834 print '<polyline id=%s' % qa(str(label)+'_linear'),
835 print 'fill="none" stroke="yellow"',
836 print 'stroke-width=%s' % qa(abs(ch(regression.serror*2))),
837 print 'opacity="0.5" pointer-events="none" visibility="hidden"',
838 print 'points="',
839 print '%s,%s' % (str(cx(min_x)), str(cy(slope*min_x + intercept))),
840 print '%s,%s' % (str(cx(max_x)), str(cy(slope*max_x + intercept))),
841 print '"/>'
842
843 print '<polyline id=%s' % qa(str(label)+'_line'),
844 print 'onmouseover=%s' % qa(
845 "var event = arguments[0] || window.event;"
846 " if (event.ctrlKey) {"
847 " highlight('"+str(label).replace("'", "\\'")+"');"
848 " return false;"
849 " }"),
850 print 'fill="none" stroke="rgb(%s,%s,%s)"' % (str(r), str(g), str(b)),
851 print 'stroke-width=%s' % qa(line_width),
852 print 'opacity=%s' % qa(a),
853 print 'points="',
854 for point in line:
855 print '%s,%s' % (str(cx(point[0])), str(cy(point[1]))),
856 print '"/>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000857
bungeman@google.com85669f92011-06-17 13:58:14 +0000858 print '</g>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000859
bungeman@google.com85669f92011-06-17 13:58:14 +0000860 #output the labels
861 print '<text id="label" x="0" y=%s' % qa(font_size),
862 print 'font-size=%s> </text>' % qa(font_size)
epoger@google.comc71174d2011-08-08 17:19:23 +0000863
864 print '<a id="rev_link" xlink:href="" target="_top">'
865 print '<text id="revision" x="0" y=%s style="' % qa(font_size*2)
866 print 'font-size: %s; ' % qe(font_size)
867 print 'stroke: #0000dd; text-decoration: underline; '
868 print '"> </text></a>'
869
bungeman@google.com85669f92011-06-17 13:58:14 +0000870 print '</svg>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000871
bungeman@google.com85669f92011-06-17 13:58:14 +0000872if __name__ == "__main__":
873 main()