OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import json | |
6 import logging | |
7 import os | |
8 import pickle | |
9 import re | |
10 | |
11 from pylib import constants | |
12 from pylib.base import base_test_result | |
13 from pylib.base import test_instance | |
14 from pylib.constants import exit_codes | |
15 from pylib.constants import host_paths | |
16 from devil.utils import cmd_helper | |
17 | |
18 | |
19 _GIT_CR_POS_RE = re.compile(r'^Cr-Commit-Position: refs/heads/master@{#(\d+)}$') | |
20 | |
21 | |
22 def _GetPersistedResult(test_name): | |
23 file_name = os.path.join(constants.PERF_OUTPUT_DIR, test_name) | |
24 if not os.path.exists(file_name): | |
25 logging.error('File not found %s', file_name) | |
26 return None | |
27 | |
28 with file(file_name, 'r') as f: | |
29 return pickle.loads(f.read()) | |
30 | |
31 | |
32 def _GetChromiumRevision(): | |
33 # pylint: disable=line-too-long | |
34 """Get the git hash and commit position of the chromium master branch. | |
35 | |
36 See: | |
37 https://chromium.googlesource.com/chromium/tools/build/+/master/scripts/slave/ runtest.py#212 | |
38 | |
39 Returns: | |
40 A dictionary with 'revision' and 'commit_pos' keys. | |
41 """ | |
42 # pylint: enable=line-too-long | |
43 status, output = cmd_helper.GetCmdStatusAndOutput( | |
44 ['git', 'log', '-n', '1', '--pretty=format:%H%n%B', 'HEAD'], | |
45 cwd=host_paths.DIR_SOURCE_ROOT) | |
46 revision = None | |
47 commit_pos = None | |
48 if not status: | |
49 lines = output.splitlines() | |
50 revision = lines[0] | |
51 for line in reversed(lines): | |
52 m = _GIT_CR_POS_RE.match(line.strip()) | |
53 if m: | |
54 commit_pos = int(m.group(1)) | |
55 break | |
56 return {'revision': revision, 'commit_pos': commit_pos} | |
57 | |
58 | |
59 class PerfTestInstance(test_instance.TestInstance): | |
60 def __init__(self, args, _): | |
61 super(PerfTestInstance, self).__init__() | |
62 | |
63 if args.single_step: | |
64 args.single_step = ' '.join(args.single_step_command) | |
65 | |
66 self._collect_chartjson_data = args.collect_chartjson_data | |
67 self._dry_run = args.dry_run | |
68 self._flaky_steps = args.flaky_steps | |
69 self._output_dir_archive_path = args.output_dir_archive_path | |
70 self._known_devices_file = args.known_devices_file | |
71 self._max_battery_temp = args.max_battery_temp | |
72 self._min_battery_level = args.min_battery_level | |
73 self._no_timeout = args.no_timeout | |
74 self._output_chartjson_data = args.output_chartjson_data | |
75 self._output_json_list = args.output_json_list | |
76 self._print_step = args.print_step | |
77 self._single_step = args.single_step | |
78 self._steps = args.steps | |
79 self._test_filter = args.test_filter | |
80 self._write_buildbot_json = args.write_buildbot_json | |
81 | |
82 def SetUp(self): | |
83 pass | |
84 | |
85 def TearDown(self): | |
86 pass | |
87 | |
88 def OutputJsonList(self): | |
89 try: | |
90 with file(self._steps, 'r') as i: | |
91 all_steps = json.load(i) | |
92 | |
93 step_values = [] | |
94 for k, v in all_steps['steps'].iteritems(): | |
95 data = {'test': k, 'device_affinity': v['device_affinity']} | |
96 | |
97 persisted_result = _GetPersistedResult(k) | |
98 if persisted_result: | |
99 data['start_time'] = persisted_result['start_time'] | |
100 data['end_time'] = persisted_result['end_time'] | |
101 data['total_time'] = persisted_result['total_time'] | |
102 data['has_archive'] = persisted_result['archive_bytes'] is not None | |
103 step_values.append(data) | |
104 | |
105 with file(self._output_json_list, 'w') as o: | |
106 o.write(json.dumps(step_values)) | |
107 return 0 | |
108 except Exception: # pylint: disable=broad-except | |
jbudorick
2016/07/01 14:20:10
similar to the previous file: What is this catchin
rnephew (Reviews Here)
2016/07/01 22:09:32
Just using what the old one had; I think KeyError
| |
109 return constants.ERROR_EXIT_CODE | |
110 | |
111 def PrintTestOutput(self): | |
112 """Helper method to print the output of previously executed test_name. | |
113 | |
114 Test_name is passed from the command line as print_step | |
115 | |
116 Returns: | |
117 exit code generated by the test step. | |
118 """ | |
119 persisted_result = _GetPersistedResult(self._print_step) | |
120 if not persisted_result: | |
121 return exit_codes.INFRA | |
122 logging.info('*' * 80) | |
123 logging.info('Output from:') | |
124 logging.info(persisted_result['cmd']) | |
125 logging.info('*' * 80) | |
126 | |
127 output_formatted = '' | |
128 persisted_outputs = persisted_result['output'] | |
129 for i in xrange(len(persisted_outputs)): | |
130 output_formatted += '\n\nOutput from run #%d:\n\n%s' % ( | |
131 i, persisted_outputs[i]) | |
132 print output_formatted | |
133 | |
134 if self._output_chartjson_data: | |
135 with file(self._output_chartjson_data, 'w') as f: | |
136 f.write(persisted_result['chartjson']) | |
137 | |
138 if self._output_dir_archive_path: | |
139 if persisted_result['archive_bytes'] is not None: | |
140 with file(self._output_dir_archive_path, 'wb') as f: | |
141 f.write(persisted_result['archive_bytes']) | |
142 else: | |
143 logging.error('The output dir was not archived.') | |
144 | |
145 return persisted_result['exit_code'] | |
146 | |
147 #override | |
148 def TestType(self): | |
149 return 'perf' | |
150 | |
151 @staticmethod | |
152 def ReadChartjsonOutput(output_dir): | |
153 if not output_dir: | |
154 return '' | |
155 json_output_path = os.path.join(output_dir, 'results-chart.json') | |
156 try: | |
157 with open(json_output_path) as f: | |
158 return f.read() | |
159 except IOError: | |
160 logging.exception('Exception when reading chartjson.') | |
161 logging.error('This usually means that telemetry did not run, so it could' | |
162 ' not generate the file. Please check the device running' | |
163 ' the test.') | |
164 return '' | |
165 | |
166 def WriteBuildBotJson(self, output_dir): | |
167 """Write metadata about the buildbot environment to the output dir.""" | |
168 if not output_dir or not self._write_buildbot_json: | |
169 return | |
170 data = { | |
171 'chromium': _GetChromiumRevision(), | |
172 'environment': dict(os.environ) | |
173 } | |
174 with open(os.path.join(output_dir, 'buildbot.json'), 'w') as f: | |
175 json.dump(data, f, sort_keys=True, separators=(',', ': ')) | |
176 | |
177 def RunOutputJsonList(self): | |
178 if self.OutputJsonList() == 0: | |
179 result_type = base_test_result.ResultType.PASS | |
180 else: | |
181 result_type = base_test_result.ResultType.FAIL | |
182 result = base_test_result.TestRunResults() | |
183 result.AddResult( | |
184 base_test_result.BaseTestResult('OutputJsonList', result_type)) | |
185 return [result] | |
186 | |
187 def RunPrintStep(self): | |
188 if self.PrintTestOutput() == 0: | |
189 result_type = base_test_result.ResultType.PASS | |
190 else: | |
191 result_type = base_test_result.ResultType.FAIL | |
192 result = base_test_result.TestRunResults() | |
193 result.AddResult( | |
194 base_test_result.BaseTestResult('PrintStep', result_type)) | |
195 return [result] | |
196 | |
197 @property | |
198 def collect_chartjson_data(self): | |
199 return self._collect_chartjson_data | |
200 | |
201 @property | |
202 def dry_run(self): | |
203 return self._dry_run | |
204 | |
205 @property | |
206 def flaky_steps(self): | |
207 return self._flaky_steps | |
208 | |
209 @property | |
210 def known_devices_file(self): | |
211 return self._known_devices_file | |
212 | |
213 @property | |
214 def max_battery_temp(self): | |
215 return self._max_battery_temp | |
216 | |
217 @property | |
218 def min_battery_level(self): | |
219 return self._min_battery_level | |
220 | |
221 @property | |
222 def no_timeout(self): | |
223 return self._no_timeout | |
224 | |
225 @property | |
226 def output_chartjson_data(self): | |
227 return self._output_chartjson_data | |
228 | |
229 @property | |
230 def output_dir_archive_path(self): | |
231 return self._output_dir_archive_path | |
232 | |
233 @property | |
234 def output_json_list(self): | |
235 return self._output_json_list | |
236 | |
237 @property | |
238 def print_step(self): | |
239 return self._print_step | |
240 | |
241 @property | |
242 def single_step(self): | |
243 return self._single_step | |
244 | |
245 @property | |
246 def steps(self): | |
247 return self._steps | |
248 | |
249 @property | |
250 def test_filter(self): | |
251 return self._test_filter | |
OLD | NEW |