| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 | 6 |
| 7 import hashlib | 7 import hashlib |
| 8 import math | 8 import math |
| 9 import optparse | 9 import optparse |
| 10 import re | 10 import re |
| 11 import simplejson | |
| 12 import subprocess | 11 import subprocess |
| 13 import sys | 12 import sys |
| 14 import time | 13 import time |
| 15 import urllib2 | 14 import urllib2 |
| 16 | 15 |
| 17 | 16 |
| 17 try: |
| 18 import json |
| 19 except ImportError: |
| 20 import simplejson as json |
| 21 |
| 22 |
| 18 __version__ = '1.0' | 23 __version__ = '1.0' |
| 19 DEFAULT_EXPECTATIONS_FILE = 'perf_expectations.json' | 24 DEFAULT_EXPECTATIONS_FILE = 'perf_expectations.json' |
| 20 DEFAULT_VARIANCE = 0.05 | 25 DEFAULT_VARIANCE = 0.05 |
| 21 USAGE = '' | 26 USAGE = '' |
| 22 | 27 |
| 23 | 28 |
| 24 def ReadFile(filename): | 29 def ReadFile(filename): |
| 25 try: | 30 try: |
| 26 file = open(filename, 'r') | 31 file = open(filename, 'r') |
| 27 except IOError, e: | 32 except IOError, e: |
| 28 print >> sys.stderr, ('I/O Error reading file %s(%s): %s' % | 33 print >> sys.stderr, ('I/O Error reading file %s(%s): %s' % |
| 29 (filename, e.errno, e.strerror)) | 34 (filename, e.errno, e.strerror)) |
| 30 raise e | 35 raise e |
| 31 contents = file.read() | 36 contents = file.read() |
| 32 file.close() | 37 file.close() |
| 33 return contents | 38 return contents |
| 34 | 39 |
| 35 | 40 |
| 36 def ConvertJsonIntoDict(string): | 41 def ConvertJsonIntoDict(string): |
| 37 """Read a JSON string and convert its contents into a Python datatype.""" | 42 """Read a JSON string and convert its contents into a Python datatype.""" |
| 38 if len(string) == 0: | 43 if len(string) == 0: |
| 39 print >> sys.stderr, ('Error could not parse empty string') | 44 print >> sys.stderr, ('Error could not parse empty string') |
| 40 raise Exception('JSON data missing') | 45 raise Exception('JSON data missing') |
| 41 | 46 |
| 42 try: | 47 try: |
| 43 json = simplejson.loads(string) | 48 jsondata = json.loads(string) |
| 44 except ValueError, e: | 49 except ValueError, e: |
| 45 print >> sys.stderr, ('Error parsing string: "%s"' % string) | 50 print >> sys.stderr, ('Error parsing string: "%s"' % string) |
| 46 raise e | 51 raise e |
| 47 return json | 52 return jsondata |
| 48 | 53 |
| 49 | 54 |
| 50 # Floating point representation of last time we fetched a URL. | 55 # Floating point representation of last time we fetched a URL. |
| 51 last_fetched_at = None | 56 last_fetched_at = None |
| 52 def FetchUrlContents(url): | 57 def FetchUrlContents(url): |
| 53 global last_fetched_at | 58 global last_fetched_at |
| 54 if last_fetched_at and ((time.time() - last_fetched_at) <= 0.5): | 59 if last_fetched_at and ((time.time() - last_fetched_at) <= 0.5): |
| 55 # Sleep for half a second to avoid overloading the server. | 60 # Sleep for half a second to avoid overloading the server. |
| 56 time.sleep(0.5) | 61 time.sleep(0.5) |
| 57 try: | 62 try: |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 except IOError, e: | 95 except IOError, e: |
| 91 print >> sys.stderr, ('I/O Error writing file %s(%s): %s' % | 96 print >> sys.stderr, ('I/O Error writing file %s(%s): %s' % |
| 92 (filename, e.errno, e.strerror)) | 97 (filename, e.errno, e.strerror)) |
| 93 return False | 98 return False |
| 94 jsondata = [] | 99 jsondata = [] |
| 95 for key in keys: | 100 for key in keys: |
| 96 rowdata = GetRowData(data, key) | 101 rowdata = GetRowData(data, key) |
| 97 | 102 |
| 98 # Include an updated checksum. | 103 # Include an updated checksum. |
| 99 sha1 = hashlib.sha1() | 104 sha1 = hashlib.sha1() |
| 105 rowdata = [str(possibly_unicode_string).encode('ascii') |
| 106 for possibly_unicode_string in rowdata] |
| 100 sha1.update(str(rowdata) + key) | 107 sha1.update(str(rowdata) + key) |
| 101 rowdata.append('"sha1": "%s"' % sha1.hexdigest()[0:8]) | 108 rowdata.append('"sha1": "%s"' % sha1.hexdigest()[0:8]) |
| 102 | 109 |
| 103 jsondata.append('"%s": {%s}' % (key, ', '.join(rowdata))) | 110 jsondata.append('"%s": {%s}' % (key, ', '.join(rowdata))) |
| 104 jsondata.append('"load": true') | 111 jsondata.append('"load": true') |
| 105 json = '{%s\n}' % ',\n '.join(jsondata) | 112 jsontext = '{%s\n}' % ',\n '.join(jsondata) |
| 106 file.write(json + '\n') | 113 file.write(jsontext + '\n') |
| 107 file.close() | 114 file.close() |
| 108 return True | 115 return True |
| 109 | 116 |
| 110 | 117 |
| 111 last_key_printed = None | 118 last_key_printed = None |
| 112 def Main(args): | 119 def Main(args): |
| 113 def OutputMessage(message, verbose_message=True): | 120 def OutputMessage(message, verbose_message=True): |
| 114 global last_key_printed | 121 global last_key_printed |
| 115 if not options.verbose and verbose_message: | 122 if not options.verbose and verbose_message: |
| 116 return | 123 return |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 trace_values = {} | 201 trace_values = {} |
| 195 traces = [tracename] | 202 traces = [tracename] |
| 196 if value_type == 'relative': | 203 if value_type == 'relative': |
| 197 traces += [reftracename] | 204 traces += [reftracename] |
| 198 for trace in traces: | 205 for trace in traces: |
| 199 trace_values.setdefault(trace, {}) | 206 trace_values.setdefault(trace, {}) |
| 200 | 207 |
| 201 # Find the high and low values for each of the traces. | 208 # Find the high and low values for each of the traces. |
| 202 scanning = False | 209 scanning = False |
| 203 for line in summarylist: | 210 for line in summarylist: |
| 204 json = ConvertJsonIntoDict(line) | 211 jsondata = ConvertJsonIntoDict(line) |
| 205 if int(json['rev']) <= revb: | 212 if int(jsondata['rev']) <= revb: |
| 206 scanning = True | 213 scanning = True |
| 207 if int(json['rev']) < reva: | 214 if int(jsondata['rev']) < reva: |
| 208 break | 215 break |
| 209 | 216 |
| 210 # We found the upper revision in the range. Scan for trace data until we | 217 # We found the upper revision in the range. Scan for trace data until we |
| 211 # find the lower revision in the range. | 218 # find the lower revision in the range. |
| 212 if scanning: | 219 if scanning: |
| 213 for trace in traces: | 220 for trace in traces: |
| 214 if trace not in json['traces']: | 221 if trace not in jsondata['traces']: |
| 215 OutputMessage('trace %s missing' % trace) | 222 OutputMessage('trace %s missing' % trace) |
| 216 continue | 223 continue |
| 217 if type(json['traces'][trace]) != type([]): | 224 if type(jsondata['traces'][trace]) != type([]): |
| 218 OutputMessage('trace %s format not recognized' % trace) | 225 OutputMessage('trace %s format not recognized' % trace) |
| 219 continue | 226 continue |
| 220 try: | 227 try: |
| 221 tracevalue = float(json['traces'][trace][0]) | 228 tracevalue = float(jsondata['traces'][trace][0]) |
| 222 except ValueError: | 229 except ValueError: |
| 223 OutputMessage('trace %s value error: %s' % ( | 230 OutputMessage('trace %s value error: %s' % ( |
| 224 trace, str(json['traces'][trace][0]))) | 231 trace, str(jsondata['traces'][trace][0]))) |
| 225 continue | 232 continue |
| 226 | 233 |
| 227 for bound in ['high', 'low']: | 234 for bound in ['high', 'low']: |
| 228 trace_values[trace].setdefault(bound, tracevalue) | 235 trace_values[trace].setdefault(bound, tracevalue) |
| 229 | 236 |
| 230 trace_values[trace]['high'] = max(trace_values[trace]['high'], | 237 trace_values[trace]['high'] = max(trace_values[trace]['high'], |
| 231 tracevalue) | 238 tracevalue) |
| 232 trace_values[trace]['low'] = min(trace_values[trace]['low'], | 239 trace_values[trace]['low'] = min(trace_values[trace]['low'], |
| 233 tracevalue) | 240 tracevalue) |
| 234 | 241 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 WriteJson(DEFAULT_EXPECTATIONS_FILE, perf, perfkeys) | 288 WriteJson(DEFAULT_EXPECTATIONS_FILE, perf, perfkeys) |
| 282 print 'done' | 289 print 'done' |
| 283 else: | 290 else: |
| 284 if options.verbose: | 291 if options.verbose: |
| 285 print '' | 292 print '' |
| 286 print 'No changes.' | 293 print 'No changes.' |
| 287 | 294 |
| 288 | 295 |
| 289 if __name__ == '__main__': | 296 if __name__ == '__main__': |
| 290 sys.exit(Main(sys.argv)) | 297 sys.exit(Main(sys.argv)) |
| OLD | NEW |