blob: 517e691a9340644d95b9be1db15d2955efb34d22 [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.comb6204b12012-08-16 20:49:28 +000031 print '-m <representation> representation of bench value.'
32 print ' See _ListAlgorithm class in bench_util.py.'
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.comb6204b12012-08-16 20:49:28 +0000284 rep = None # bench representation 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
epoger@google.com5b2e01c2012-06-25 20:29:04 +0000332 elif option == "-o":
333 redirect_stdout(value)
334 elif option == "-r":
335 revision_range = value
336 elif option == "-s":
337 add_setting(settings, value)
338 elif option == "-t":
339 time_of_interest = value
bungeman@google.com85669f92011-06-17 13:58:14 +0000340 elif option == "-x":
341 requested_width = int(value)
342 elif option == "-y":
343 requested_height = int(value)
344 elif option == "--default-setting":
345 add_setting(default_settings, value)
346 else:
347 usage()
348 assert False, "unhandled option"
349 except ValueError:
350 usage()
351 sys.exit(2)
epoger@google.comc71174d2011-08-08 17:19:23 +0000352
bungeman@google.com85669f92011-06-17 13:58:14 +0000353 if directory is None:
354 usage()
355 sys.exit(2)
epoger@google.comc71174d2011-08-08 17:19:23 +0000356
bensong@google.com8c1de762012-08-15 18:27:38 +0000357 title += ' [representation: %s]' % rep
358
epoger@google.com37260002011-08-08 20:27:04 +0000359 latest_revision = get_latest_revision(directory)
360 oldest_revision, newest_revision = parse_range(revision_range)
361 oldest_regression, newest_regression = parse_range(regression_range)
362
epoger@google.com3d8cd172012-05-11 18:26:16 +0000363 unfiltered_revision_data_points = parse_dir(directory
bungeman@google.com85669f92011-06-17 13:58:14 +0000364 , default_settings
365 , oldest_revision
bensong@google.com87348162012-08-15 17:31:46 +0000366 , newest_revision
367 , rep)
epoger@google.comc71174d2011-08-08 17:19:23 +0000368
epoger@google.com3d8cd172012-05-11 18:26:16 +0000369 # Filter out any data points that are utterly bogus... make sure to report
370 # that we did so later!
371 (allowed_revision_data_points, ignored_revision_data_points) = filter_data_points(
372 unfiltered_revision_data_points)
373
epoger@google.comc71174d2011-08-08 17:19:23 +0000374 # Update oldest_revision and newest_revision based on the data we could find
epoger@google.com3d8cd172012-05-11 18:26:16 +0000375 all_revision_numbers = allowed_revision_data_points.keys()
epoger@google.comc71174d2011-08-08 17:19:23 +0000376 oldest_revision = min(all_revision_numbers)
377 newest_revision = max(all_revision_numbers)
378
epoger@google.com3d8cd172012-05-11 18:26:16 +0000379 lines = create_lines(allowed_revision_data_points
bungeman@google.com85669f92011-06-17 13:58:14 +0000380 , settings
381 , bench_of_interest
382 , config_of_interest
383 , time_of_interest)
epoger@google.comc71174d2011-08-08 17:19:23 +0000384
bungeman@google.com85669f92011-06-17 13:58:14 +0000385 regressions = create_regressions(lines
386 , oldest_regression
387 , newest_regression)
epoger@google.comc71174d2011-08-08 17:19:23 +0000388
epoger@google.com3d8cd172012-05-11 18:26:16 +0000389 output_xhtml(lines, oldest_revision, newest_revision, ignored_revision_data_points,
epoger@google.com37260002011-08-08 20:27:04 +0000390 regressions, requested_width, requested_height, title)
bungeman@google.com85669f92011-06-17 13:58:14 +0000391
392def qa(out):
393 """Stringify input and quote as an xml attribute."""
394 return xml.sax.saxutils.quoteattr(str(out))
395def qe(out):
396 """Stringify input and escape as xml data."""
397 return xml.sax.saxutils.escape(str(out))
398
399def create_select(qualifier, lines, select_id=None):
400 """Output select with options showing lines which qualifier maps to it.
401
402 ((Label) -> str, {Label:_}, str?) -> _"""
403 options = {} #{ option : [Label]}
404 for label in lines.keys():
405 option = qualifier(label)
406 if (option not in options):
407 options[option] = []
408 options[option].append(label)
409 option_list = list(options.keys())
410 option_list.sort()
411 print '<select class="lines"',
412 if select_id is not None:
413 print 'id=%s' % qa(select_id)
414 print 'multiple="true" size="10" onchange="updateSvg();">'
415 for option in option_list:
416 print '<option value=' + qa('[' +
417 reduce(lambda x,y:x+json.dumps(str(y))+',',options[option],"")[0:-1]
418 + ']') + '>'+qe(option)+'</option>'
419 print '</select>'
420
epoger@google.com3d8cd172012-05-11 18:26:16 +0000421def output_ignored_data_points_warning(ignored_revision_data_points):
422 """Write description of ignored_revision_data_points to stdout as xhtml.
423 """
424 num_ignored_points = 0
425 description = ''
426 revisions = ignored_revision_data_points.keys()
427 if revisions:
428 revisions.sort()
429 revisions.reverse()
430 for revision in revisions:
431 num_ignored_points += len(ignored_revision_data_points[revision])
432 points_at_this_revision = []
433 for point in ignored_revision_data_points[revision]:
434 points_at_this_revision.append(point.bench)
435 points_at_this_revision.sort()
436 description += 'r%d: %s\n' % (revision, points_at_this_revision)
437 if num_ignored_points == 0:
438 print 'Did not discard any data points; all were within the range [%d-%d]' % (
439 MIN_REASONABLE_TIME, MAX_REASONABLE_TIME)
440 else:
441 print '<table width="100%" bgcolor="ff0000"><tr><td align="center">'
442 print 'Discarded %d data points outside of range [%d-%d]' % (
443 num_ignored_points, MIN_REASONABLE_TIME, MAX_REASONABLE_TIME)
444 print '</td></tr><tr><td width="100%" align="center">'
445 print ('<textarea rows="4" style="width:97%" readonly="true" wrap="off">'
446 + qe(description) + '</textarea>')
447 print '</td></tr></table>'
448
449def output_xhtml(lines, oldest_revision, newest_revision, ignored_revision_data_points,
epoger@google.com37260002011-08-08 20:27:04 +0000450 regressions, requested_width, requested_height, title):
bungeman@google.com85669f92011-06-17 13:58:14 +0000451 """Outputs an svg/xhtml view of the data."""
452 print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"',
453 print '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
454 print '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">'
455 print '<head>'
epoger@google.com3d8cd172012-05-11 18:26:16 +0000456 print '<title>%s</title>' % qe(title)
bungeman@google.com85669f92011-06-17 13:58:14 +0000457 print '</head>'
458 print '<body>'
459
460 output_svg(lines, regressions, requested_width, requested_height)
epoger@google.com3d8cd172012-05-11 18:26:16 +0000461
bungeman@google.com85669f92011-06-17 13:58:14 +0000462 #output the manipulation controls
463 print """
464<script type="text/javascript">//<![CDATA[
465 function getElementsByClass(node, searchClass, tag) {
466 var classElements = new Array();
467 var elements = node.getElementsByTagName(tag);
468 var pattern = new RegExp("^|\\s"+searchClass+"\\s|$");
469 for (var i = 0, elementsFound = 0; i < elements.length; ++i) {
470 if (pattern.test(elements[i].className)) {
471 classElements[elementsFound] = elements[i];
472 ++elementsFound;
473 }
474 }
475 return classElements;
476 }
477 function getAllLines() {
478 var selectElem = document.getElementById('benchSelect');
479 var linesObj = {};
480 for (var i = 0; i < selectElem.options.length; ++i) {
481 var lines = JSON.parse(selectElem.options[i].value);
482 for (var j = 0; j < lines.length; ++j) {
483 linesObj[lines[j]] = true;
484 }
485 }
486 return linesObj;
487 }
488 function getOptions(selectElem) {
489 var linesSelectedObj = {};
490 for (var i = 0; i < selectElem.options.length; ++i) {
491 if (!selectElem.options[i].selected) continue;
492
493 var linesSelected = JSON.parse(selectElem.options[i].value);
494 for (var j = 0; j < linesSelected.length; ++j) {
495 linesSelectedObj[linesSelected[j]] = true;
496 }
497 }
498 return linesSelectedObj;
499 }
500 function objectEmpty(obj) {
501 for (var p in obj) {
502 return false;
503 }
504 return true;
505 }
506 function markSelectedLines(selectElem, allLines) {
507 var linesSelected = getOptions(selectElem);
508 if (!objectEmpty(linesSelected)) {
509 for (var line in allLines) {
510 allLines[line] &= (linesSelected[line] == true);
511 }
512 }
513 }
514 function updateSvg() {
515 var allLines = getAllLines();
516
517 var selects = getElementsByClass(document, 'lines', 'select');
518 for (var i = 0; i < selects.length; ++i) {
519 markSelectedLines(selects[i], allLines);
520 }
521
522 for (var line in allLines) {
523 var svgLine = document.getElementById(line);
524 var display = (allLines[line] ? 'inline' : 'none');
525 svgLine.setAttributeNS(null,'display', display);
526 }
527 }
528
529 function mark(markerId) {
530 for (var line in getAllLines()) {
531 var svgLineGroup = document.getElementById(line);
532 var display = svgLineGroup.getAttributeNS(null,'display');
533 if (display == null || display == "" || display != "none") {
534 var svgLine = document.getElementById(line+'_line');
535 if (markerId == null) {
536 svgLine.removeAttributeNS(null,'marker-mid');
537 } else {
538 svgLine.setAttributeNS(null,'marker-mid', markerId);
539 }
540 }
541 }
542 }
543//]]></script>"""
epoger@google.comc71174d2011-08-08 17:19:23 +0000544
545 print '<table border="0" width="%s">' % requested_width
546 print """
epoger@google.com3d8cd172012-05-11 18:26:16 +0000547<tr valign="top"><td width="50%">
548<table border="0" width="100%">
549<tr><td align="center"><table border="0">
epoger@google.comc71174d2011-08-08 17:19:23 +0000550<form>
551<tr valign="bottom" align="center">
552<td width="1">Bench&nbsp;Type</td>
553<td width="1">Bitmap Config</td>
554<td width="1">Timer&nbsp;Type (Cpu/Gpu/wall)</td>
555<td width="1"><!--buttons--></td>
epoger@google.com3d8cd172012-05-11 18:26:16 +0000556</tr><tr valign="top" align="center">
557"""
epoger@google.comc71174d2011-08-08 17:19:23 +0000558 print '<td width="1">'
bungeman@google.com85669f92011-06-17 13:58:14 +0000559 create_select(lambda l: l.bench, lines, 'benchSelect')
epoger@google.comc71174d2011-08-08 17:19:23 +0000560 print '</td><td width="1">'
bungeman@google.com85669f92011-06-17 13:58:14 +0000561 create_select(lambda l: l.config, lines)
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.time_type, lines)
epoger@google.comc71174d2011-08-08 17:19:23 +0000564
bungeman@google.com85669f92011-06-17 13:58:14 +0000565 all_settings = {}
566 variant_settings = set()
567 for label in lines.keys():
568 for key, value in label.settings.items():
569 if key not in all_settings:
570 all_settings[key] = value
571 elif all_settings[key] != value:
572 variant_settings.add(key)
epoger@google.comc71174d2011-08-08 17:19:23 +0000573
bungeman@google.com85669f92011-06-17 13:58:14 +0000574 for k in variant_settings:
bungeman@google.comb6981552012-07-10 15:31:52 +0000575 create_select(lambda l: l.settings.get(k, "<missing>"), lines)
epoger@google.comc71174d2011-08-08 17:19:23 +0000576
577 print '</td><td width="1"><button type="button"',
bungeman@google.com85669f92011-06-17 13:58:14 +0000578 print 'onclick=%s' % qa("mark('url(#circleMark)'); return false;"),
epoger@google.comc71174d2011-08-08 17:19:23 +0000579 print '>Mark Points</button>'
580 print '<button type="button" onclick="mark(null);">Clear Points</button>'
epoger@google.com3d8cd172012-05-11 18:26:16 +0000581 print '</td>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000582 print """
epoger@google.com3d8cd172012-05-11 18:26:16 +0000583</tr>
584</form>
585</table></td></tr>
586<tr><td align="center">
587<hr />
588"""
589
590 output_ignored_data_points_warning(ignored_revision_data_points)
591 print '</td></tr></table>'
592 print '</td><td width="2%"><!--gutter--></td>'
593
594 print '<td><table border="0">'
595 print '<tr><td align="center">%s<br></br>revisions r%s - r%s</td></tr>' % (
596 qe(title),
597 bench_util.CreateRevisionLink(oldest_revision),
598 bench_util.CreateRevisionLink(newest_revision))
599 print """
600<tr><td align="left">
epoger@google.comc71174d2011-08-08 17:19:23 +0000601<p>Brighter red indicates tests that have gotten worse; brighter green
602indicates tests that have gotten better.</p>
603<p>To highlight individual tests, hold down CONTROL and mouse over
604graph lines.</p>
605<p>To highlight revision numbers, hold down SHIFT and mouse over
606the graph area.</p>
607<p>To only show certain tests on the graph, select any combination of
608tests in the selectors at left. (To show all, select all.)</p>
609<p>Use buttons at left to mark/clear points on the lines for selected
610benchmarks.</p>
epoger@google.com3d8cd172012-05-11 18:26:16 +0000611</td></tr>
612</table>
613
epoger@google.comc71174d2011-08-08 17:19:23 +0000614</td>
615</tr>
epoger@google.comc71174d2011-08-08 17:19:23 +0000616</table>
617</body>
618</html>"""
bungeman@google.com85669f92011-06-17 13:58:14 +0000619
620def compute_size(requested_width, requested_height, rev_width, time_height):
621 """Converts potentially empty requested size into a concrete size.
622
623 (Number?, Number?) -> (Number, Number)"""
624 pic_width = 0
625 pic_height = 0
626 if (requested_width is not None and requested_height is not None):
627 pic_height = requested_height
628 pic_width = requested_width
629
630 elif (requested_width is not None):
631 pic_width = requested_width
632 pic_height = pic_width * (float(time_height) / rev_width)
633
634 elif (requested_height is not None):
635 pic_height = requested_height
636 pic_width = pic_height * (float(rev_width) / time_height)
637
638 else:
639 pic_height = 800
640 pic_width = max(rev_width*3
641 , pic_height * (float(rev_width) / time_height))
642
643 return (pic_width, pic_height)
644
645def output_svg(lines, regressions, requested_width, requested_height):
646 """Outputs an svg view of the data."""
647
648 (global_min_x, _), (global_max_x, global_max_y) = bounds(lines)
649 max_up_slope, min_down_slope = bounds_slope(regressions)
650
651 #output
652 global_min_y = 0
653 x = global_min_x
654 y = global_min_y
655 w = global_max_x - global_min_x
656 h = global_max_y - global_min_y
657 font_size = 16
658 line_width = 2
659
660 pic_width, pic_height = compute_size(requested_width, requested_height
661 , w, h)
662
663 def cw(w1):
664 """Converts a revision difference to display width."""
665 return (pic_width / float(w)) * w1
666 def cx(x):
667 """Converts a revision to a horizontal display position."""
668 return cw(x - global_min_x)
669
670 def ch(h1):
671 """Converts a time difference to a display height."""
672 return -(pic_height / float(h)) * h1
673 def cy(y):
674 """Converts a time to a vertical display position."""
675 return pic_height + ch(y - global_min_y)
676
677 print '<svg',
678 print 'width=%s' % qa(str(pic_width)+'px')
679 print 'height=%s' % qa(str(pic_height)+'px')
680 print 'viewBox="0 0 %s %s"' % (str(pic_width), str(pic_height))
681 print 'onclick=%s' % qa(
682 "var event = arguments[0] || window.event;"
683 " if (event.shiftKey) { highlightRevision(null); }"
684 " if (event.ctrlKey) { highlight(null); }"
685 " return false;")
686 print 'xmlns="http://www.w3.org/2000/svg"'
687 print 'xmlns:xlink="http://www.w3.org/1999/xlink">'
688
689 print """
690<defs>
691 <marker id="circleMark"
692 viewBox="0 0 2 2" refX="1" refY="1"
693 markerUnits="strokeWidth"
694 markerWidth="2" markerHeight="2"
695 orient="0">
696 <circle cx="1" cy="1" r="1"/>
697 </marker>
698</defs>"""
699
700 #output the revisions
701 print """
702<script type="text/javascript">//<![CDATA[
703 var previousRevision;
704 var previousRevisionFill;
705 var previousRevisionStroke
706 function highlightRevision(id) {
707 if (previousRevision == id) return;
epoger@google.comc71174d2011-08-08 17:19:23 +0000708
709 document.getElementById('revision').firstChild.nodeValue = 'r' + id;
710 document.getElementById('rev_link').setAttribute('xlink:href',
711 'http://code.google.com/p/skia/source/detail?r=' + id);
bungeman@google.com85669f92011-06-17 13:58:14 +0000712
713 var preRevision = document.getElementById(previousRevision);
714 if (preRevision) {
715 preRevision.setAttributeNS(null,'fill', previousRevisionFill);
716 preRevision.setAttributeNS(null,'stroke', previousRevisionStroke);
717 }
718
719 var revision = document.getElementById(id);
720 previousRevision = id;
721 if (revision) {
722 previousRevisionFill = revision.getAttributeNS(null,'fill');
723 revision.setAttributeNS(null,'fill','rgb(100%, 95%, 95%)');
724
725 previousRevisionStroke = revision.getAttributeNS(null,'stroke');
726 revision.setAttributeNS(null,'stroke','rgb(100%, 90%, 90%)');
727 }
728 }
729//]]></script>"""
730
731 def print_rect(x, y, w, h, revision):
732 """Outputs a revision rectangle in display space,
733 taking arguments in revision space."""
734 disp_y = cy(y)
735 disp_h = ch(h)
736 if disp_h < 0:
737 disp_y += disp_h
738 disp_h = -disp_h
739
740 print '<rect id=%s x=%s y=%s' % (qa(revision), qa(cx(x)), qa(disp_y),),
741 print 'width=%s height=%s' % (qa(cw(w)), qa(disp_h),),
742 print 'fill="white"',
epoger@google.comc71174d2011-08-08 17:19:23 +0000743 print 'stroke="rgb(98%%,98%%,88%%)" stroke-width=%s' % qa(line_width),
bungeman@google.com85669f92011-06-17 13:58:14 +0000744 print 'onmouseover=%s' % qa(
745 "var event = arguments[0] || window.event;"
746 " if (event.shiftKey) {"
747 " highlightRevision('"+str(revision)+"');"
748 " return false;"
749 " }"),
750 print ' />'
751
752 xes = set()
753 for line in lines.itervalues():
754 for point in line:
755 xes.add(point[0])
756 revisions = list(xes)
757 revisions.sort()
758
759 left = x
760 current_revision = revisions[0]
761 for next_revision in revisions[1:]:
762 width = (((next_revision - current_revision) / 2.0)
763 + (current_revision - left))
764 print_rect(left, y, width, h, current_revision)
765 left += width
766 current_revision = next_revision
767 print_rect(left, y, x+w - left, h, current_revision)
epoger@google.comc71174d2011-08-08 17:19:23 +0000768
bungeman@google.com85669f92011-06-17 13:58:14 +0000769 #output the lines
770 print """
771<script type="text/javascript">//<![CDATA[
772 var previous;
773 var previousColor;
774 var previousOpacity;
775 function highlight(id) {
776 if (previous == id) return;
epoger@google.comc71174d2011-08-08 17:19:23 +0000777
bungeman@google.com85669f92011-06-17 13:58:14 +0000778 document.getElementById('label').firstChild.nodeValue = id;
epoger@google.comc71174d2011-08-08 17:19:23 +0000779
bungeman@google.com85669f92011-06-17 13:58:14 +0000780 var preGroup = document.getElementById(previous);
781 if (preGroup) {
782 var preLine = document.getElementById(previous+'_line');
783 preLine.setAttributeNS(null,'stroke', previousColor);
784 preLine.setAttributeNS(null,'opacity', previousOpacity);
epoger@google.comc71174d2011-08-08 17:19:23 +0000785
bungeman@google.com85669f92011-06-17 13:58:14 +0000786 var preSlope = document.getElementById(previous+'_linear');
787 if (preSlope) {
788 preSlope.setAttributeNS(null,'visibility', 'hidden');
789 }
790 }
epoger@google.comc71174d2011-08-08 17:19:23 +0000791
bungeman@google.com85669f92011-06-17 13:58:14 +0000792 var group = document.getElementById(id);
793 previous = id;
794 if (group) {
795 group.parentNode.appendChild(group);
796
797 var line = document.getElementById(id+'_line');
798 previousColor = line.getAttributeNS(null,'stroke');
799 previousOpacity = line.getAttributeNS(null,'opacity');
800 line.setAttributeNS(null,'stroke', 'blue');
801 line.setAttributeNS(null,'opacity', '1');
802
803 var slope = document.getElementById(id+'_linear');
804 if (slope) {
805 slope.setAttributeNS(null,'visibility', 'visible');
806 }
807 }
808 }
809//]]></script>"""
810 for label, line in lines.items():
811 print '<g id=%s>' % qa(label)
812 r = 128
813 g = 128
814 b = 128
815 a = .10
816 if label in regressions:
817 regression = regressions[label]
818 min_slope = regression.find_min_slope()
819 if min_slope < 0:
820 d = max(0, (min_slope / min_down_slope))
821 g += int(d*128)
822 a += d*0.9
823 elif min_slope > 0:
824 d = max(0, (min_slope / max_up_slope))
825 r += int(d*128)
826 a += d*0.9
827
828 slope = regression.slope
829 intercept = regression.intercept
830 min_x = regression.min_x
831 max_x = regression.max_x
832 print '<polyline id=%s' % qa(str(label)+'_linear'),
833 print 'fill="none" stroke="yellow"',
834 print 'stroke-width=%s' % qa(abs(ch(regression.serror*2))),
835 print 'opacity="0.5" pointer-events="none" visibility="hidden"',
836 print 'points="',
837 print '%s,%s' % (str(cx(min_x)), str(cy(slope*min_x + intercept))),
838 print '%s,%s' % (str(cx(max_x)), str(cy(slope*max_x + intercept))),
839 print '"/>'
840
841 print '<polyline id=%s' % qa(str(label)+'_line'),
842 print 'onmouseover=%s' % qa(
843 "var event = arguments[0] || window.event;"
844 " if (event.ctrlKey) {"
845 " highlight('"+str(label).replace("'", "\\'")+"');"
846 " return false;"
847 " }"),
848 print 'fill="none" stroke="rgb(%s,%s,%s)"' % (str(r), str(g), str(b)),
849 print 'stroke-width=%s' % qa(line_width),
850 print 'opacity=%s' % qa(a),
851 print 'points="',
852 for point in line:
853 print '%s,%s' % (str(cx(point[0])), str(cy(point[1]))),
854 print '"/>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000855
bungeman@google.com85669f92011-06-17 13:58:14 +0000856 print '</g>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000857
bungeman@google.com85669f92011-06-17 13:58:14 +0000858 #output the labels
859 print '<text id="label" x="0" y=%s' % qa(font_size),
860 print 'font-size=%s> </text>' % qa(font_size)
epoger@google.comc71174d2011-08-08 17:19:23 +0000861
862 print '<a id="rev_link" xlink:href="" target="_top">'
863 print '<text id="revision" x="0" y=%s style="' % qa(font_size*2)
864 print 'font-size: %s; ' % qe(font_size)
865 print 'stroke: #0000dd; text-decoration: underline; '
866 print '"> </text></a>'
867
bungeman@google.com85669f92011-06-17 13:58:14 +0000868 print '</svg>'
epoger@google.comc71174d2011-08-08 17:19:23 +0000869
bungeman@google.com85669f92011-06-17 13:58:14 +0000870if __name__ == "__main__":
871 main()