OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2009 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Verify perf_expectations.json can be loaded using simplejson. | 7 """Verify perf_expectations.json can be loaded using simplejson. |
8 | 8 |
9 perf_expectations.json is a JSON-formatted file. This script verifies | 9 perf_expectations.json is a JSON-formatted file. This script verifies |
10 that simplejson can load it correctly. It should catch most common | 10 that simplejson can load it correctly. It should catch most common |
11 formatting problems. | 11 formatting problems. |
12 """ | 12 """ |
13 | 13 |
14 import sys | 14 import sys |
15 import os | 15 import os |
16 import unittest | 16 import unittest |
| 17 import re |
17 | 18 |
18 simplejson = None | 19 simplejson = None |
19 | 20 |
20 def OnTestsLoad(): | 21 def OnTestsLoad(): |
21 old_path = sys.path | 22 old_path = sys.path |
22 script_path = os.path.dirname(sys.argv[0]) | 23 script_path = os.path.dirname(sys.argv[0]) |
23 load_path = None | 24 load_path = None |
24 global simplejson | 25 global simplejson |
25 | 26 |
26 # This test script should be stored in src/tools/perf_expectations/. That | 27 # This test script should be stored in src/tools/perf_expectations/. That |
(...skipping 24 matching lines...) Expand all Loading... |
51 import simplejson as Simplejson | 52 import simplejson as Simplejson |
52 simplejson = Simplejson | 53 simplejson = Simplejson |
53 except ImportError, e: | 54 except ImportError, e: |
54 msg = "%s expects to live within a Chromium checkout" % sys.argv[0] | 55 msg = "%s expects to live within a Chromium checkout" % sys.argv[0] |
55 raise Exception, "Error trying to import simplejson from %s (%s)" % \ | 56 raise Exception, "Error trying to import simplejson from %s (%s)" % \ |
56 (load_path, msg) | 57 (load_path, msg) |
57 finally: | 58 finally: |
58 sys.path = old_path | 59 sys.path = old_path |
59 return True | 60 return True |
60 | 61 |
| 62 def LoadData(): |
| 63 perf_file = open(PERF_EXPECTATIONS, 'r') |
| 64 try: |
| 65 perf_data = simplejson.load(perf_file) |
| 66 except ValueError, e: |
| 67 perf_file.seek(0) |
| 68 print "Error reading %s:\n%s" % (PERF_EXPECTATIONS, |
| 69 perf_file.read()[:50]+'...') |
| 70 raise e |
| 71 return perf_data |
| 72 |
61 OnTestsLoad() | 73 OnTestsLoad() |
62 | 74 |
63 PERF_EXPECTATIONS = os.path.join(os.path.dirname(sys.argv[0]), | 75 PERF_EXPECTATIONS = os.path.join(os.path.dirname(sys.argv[0]), |
64 '../perf_expectations.json') | 76 '../perf_expectations.json') |
65 | 77 |
66 class SimplejsonUnittest(unittest.TestCase): | 78 class PerfExpectationsUnittest(unittest.TestCase): |
67 def testFormat(self): | 79 def testPerfExpectations(self): |
68 perf_file = open(PERF_EXPECTATIONS, 'r') | 80 perf_data = LoadData() |
69 try: | 81 |
70 perf_data = simplejson.load(perf_file) | 82 # Test data is dictionary. |
71 except ValueError, e: | 83 perf_data = LoadData() |
72 perf_file.seek(0) | 84 if not isinstance(perf_data, dict): |
73 print "Error reading %s:\n%s" % (PERF_EXPECTATIONS, | 85 raise Exception('perf expectations is not a dict') |
74 perf_file.read()[:50]+'...') | 86 |
75 raise e | 87 # Test the 'load' key. |
76 print ("Successfully loaded perf_expectations: %d keys found." % | 88 if not 'load' in perf_data: |
77 len(perf_data)) | 89 raise Exception("perf expectations is missing a load key") |
78 return | 90 if not isinstance(perf_data['load'], bool): |
| 91 raise Exception("perf expectations load key has non-bool value") |
| 92 |
| 93 # Test all key values are dictionaries. |
| 94 bad_keys = [] |
| 95 for key in perf_data: |
| 96 if key == 'load': |
| 97 continue |
| 98 if not isinstance(perf_data[key], dict): |
| 99 bad_keys.append(key) |
| 100 if len(bad_keys) > 0: |
| 101 msg = "perf expectations keys have non-dict values" |
| 102 raise Exception("%s: %s" % (msg, bad_keys)) |
| 103 |
| 104 # Test all key values have delta and var keys. |
| 105 for key in perf_data: |
| 106 if key == 'load': |
| 107 continue |
| 108 if 'delta' not in perf_data[key] or 'var' not in perf_data[key]: |
| 109 bad_keys.append(key) |
| 110 if (not isinstance(perf_data[key]['delta'], int) and |
| 111 not isinstance(perf_data[key]['delta'], float)): |
| 112 bad_keys.append(key) |
| 113 if (not isinstance(perf_data[key]['var'], int) and |
| 114 not isinstance(perf_data[key]['var'], float)): |
| 115 bad_keys.append(key) |
| 116 if len(bad_keys) > 0: |
| 117 msg = "perf expectations key values missing or invalid delta/var" |
| 118 raise Exception("%s: %s" % (msg, bad_keys)) |
| 119 |
| 120 # Test all keys have the correct format. |
| 121 for key in perf_data: |
| 122 if key == 'load': |
| 123 continue |
| 124 if not re.match(r"^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)$", key): |
| 125 bad_keys.append(key) |
| 126 if len(bad_keys) > 0: |
| 127 msg = "perf expectations keys in bad format, expected a/b/c/d" |
| 128 raise Exception("%s: %s" % (msg, bad_keys)) |
79 | 129 |
80 if __name__ == '__main__': | 130 if __name__ == '__main__': |
81 unittest.main() | 131 unittest.main() |
OLD | NEW |