Chromium Code Reviews| 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 | |
|
jbudorick
2016/07/06 19:12:10
nit: this should precede pylib
rnephew (Reviews Here)
2016/07/06 21:47:06
Done.
| |
| 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): | |
|
jbudorick
2016/07/06 19:12:10
Both this and PrintTestOutput are still dealing wi
rnephew (Reviews Here)
2016/07/06 21:47:06
Done.
| |
| 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 KeyError: # pylint: disable=broad-except | |
|
jbudorick
2016/07/06 19:12:10
nit: is broad-except still necessary here?
rnephew (Reviews Here)
2016/07/06 21:47:06
Done.
| |
| 109 logging.exception('Persistent results file missing key.') | |
| 110 return constants.ERROR_EXIT_CODE | |
| 111 | |
| 112 def PrintTestOutput(self): | |
| 113 """Helper method to print the output of previously executed test_name. | |
| 114 | |
| 115 Test_name is passed from the command line as print_step | |
| 116 | |
| 117 Returns: | |
| 118 exit code generated by the test step. | |
| 119 """ | |
| 120 persisted_result = _GetPersistedResult(self._print_step) | |
| 121 if not persisted_result: | |
| 122 return exit_codes.INFRA | |
| 123 logging.info('*' * 80) | |
| 124 logging.info('Output from:') | |
| 125 logging.info(persisted_result['cmd']) | |
| 126 logging.info('*' * 80) | |
| 127 | |
| 128 output_formatted = '' | |
| 129 persisted_outputs = persisted_result['output'] | |
| 130 for i in xrange(len(persisted_outputs)): | |
| 131 output_formatted += '\n\nOutput from run #%d:\n\n%s' % ( | |
| 132 i, persisted_outputs[i]) | |
| 133 print output_formatted | |
| 134 | |
| 135 if self._output_chartjson_data: | |
| 136 with file(self._output_chartjson_data, 'w') as f: | |
| 137 f.write(persisted_result['chartjson']) | |
| 138 | |
| 139 if self._output_dir_archive_path: | |
| 140 if persisted_result['archive_bytes'] is not None: | |
| 141 with file(self._output_dir_archive_path, 'wb') as f: | |
| 142 f.write(persisted_result['archive_bytes']) | |
| 143 else: | |
| 144 logging.error('The output dir was not archived.') | |
| 145 | |
| 146 return persisted_result['exit_code'] | |
| 147 | |
| 148 #override | |
| 149 def TestType(self): | |
| 150 return 'perf' | |
| 151 | |
| 152 @staticmethod | |
| 153 def ReadChartjsonOutput(output_dir): | |
| 154 if not output_dir: | |
| 155 return '' | |
| 156 json_output_path = os.path.join(output_dir, 'results-chart.json') | |
| 157 try: | |
| 158 with open(json_output_path) as f: | |
| 159 return f.read() | |
| 160 except IOError: | |
| 161 logging.exception('Exception when reading chartjson.') | |
| 162 logging.error('This usually means that telemetry did not run, so it could' | |
| 163 ' not generate the file. Please check the device running' | |
| 164 ' the test.') | |
| 165 return '' | |
| 166 | |
| 167 def WriteBuildBotJson(self, output_dir): | |
| 168 """Write metadata about the buildbot environment to the output dir.""" | |
| 169 if not output_dir or not self._write_buildbot_json: | |
| 170 return | |
| 171 data = { | |
| 172 'chromium': _GetChromiumRevision(), | |
| 173 'environment': dict(os.environ) | |
| 174 } | |
| 175 with open(os.path.join(output_dir, 'buildbot.json'), 'w') as f: | |
| 176 json.dump(data, f, sort_keys=True, separators=(',', ': ')) | |
| 177 | |
| 178 def RunOutputJsonList(self): | |
| 179 if self.OutputJsonList() == 0: | |
| 180 result_type = base_test_result.ResultType.PASS | |
| 181 else: | |
| 182 result_type = base_test_result.ResultType.FAIL | |
| 183 result = base_test_result.TestRunResults() | |
| 184 result.AddResult( | |
| 185 base_test_result.BaseTestResult('OutputJsonList', result_type)) | |
| 186 return [result] | |
| 187 | |
| 188 def RunPrintStep(self): | |
| 189 if self.PrintTestOutput() == 0: | |
| 190 result_type = base_test_result.ResultType.PASS | |
| 191 else: | |
| 192 result_type = base_test_result.ResultType.FAIL | |
| 193 result = base_test_result.TestRunResults() | |
| 194 result.AddResult( | |
| 195 base_test_result.BaseTestResult('PrintStep', result_type)) | |
| 196 return [result] | |
| 197 | |
| 198 @property | |
| 199 def collect_chartjson_data(self): | |
| 200 return self._collect_chartjson_data | |
| 201 | |
| 202 @property | |
| 203 def dry_run(self): | |
| 204 return self._dry_run | |
| 205 | |
| 206 @property | |
| 207 def flaky_steps(self): | |
| 208 return self._flaky_steps | |
| 209 | |
| 210 @property | |
| 211 def known_devices_file(self): | |
| 212 return self._known_devices_file | |
| 213 | |
| 214 @property | |
| 215 def max_battery_temp(self): | |
| 216 return self._max_battery_temp | |
| 217 | |
| 218 @property | |
| 219 def min_battery_level(self): | |
| 220 return self._min_battery_level | |
| 221 | |
| 222 @property | |
| 223 def no_timeout(self): | |
| 224 return self._no_timeout | |
| 225 | |
| 226 @property | |
| 227 def output_chartjson_data(self): | |
| 228 return self._output_chartjson_data | |
| 229 | |
| 230 @property | |
| 231 def output_dir_archive_path(self): | |
| 232 return self._output_dir_archive_path | |
| 233 | |
| 234 @property | |
| 235 def output_json_list(self): | |
| 236 return self._output_json_list | |
| 237 | |
| 238 @property | |
| 239 def print_step(self): | |
| 240 return self._print_step | |
| 241 | |
| 242 @property | |
| 243 def single_step(self): | |
| 244 return self._single_step | |
| 245 | |
| 246 @property | |
| 247 def steps(self): | |
| 248 return self._steps | |
| 249 | |
| 250 @property | |
| 251 def test_filter(self): | |
| 252 return self._test_filter | |
| OLD | NEW |