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 devil import base_error |
| 12 from devil.utils import cmd_helper |
| 13 from pylib import constants |
| 14 from pylib.base import base_test_result |
| 15 from pylib.base import test_instance |
| 16 from pylib.constants import host_paths |
| 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/+/387e3cf3/scripts/slav
e/runtest.py#211 |
| 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 self._collect_chartjson_data = args.collect_chartjson_data |
| 64 self._dry_run = args.dry_run |
| 65 self._flaky_steps = args.flaky_steps |
| 66 self._output_dir_archive_path = args.output_dir_archive_path |
| 67 self._known_devices_file = args.known_devices_file |
| 68 self._max_battery_temp = args.max_battery_temp |
| 69 self._min_battery_level = args.min_battery_level |
| 70 self._no_timeout = args.no_timeout |
| 71 self._output_chartjson_data = args.output_chartjson_data |
| 72 self._output_json_list = args.output_json_list |
| 73 self._print_step = args.print_step |
| 74 self._single_step = ( |
| 75 ' '.join(args.single_step_command) if args.single_step else None) |
| 76 self._steps = args.steps |
| 77 self._test_filter = args.test_filter |
| 78 self._write_buildbot_json = args.write_buildbot_json |
| 79 |
| 80 def SetUp(self): |
| 81 pass |
| 82 |
| 83 def TearDown(self): |
| 84 pass |
| 85 |
| 86 def OutputJsonList(self): |
| 87 try: |
| 88 with file(self._steps, 'r') as i: |
| 89 all_steps = json.load(i) |
| 90 |
| 91 step_values = [] |
| 92 for k, v in all_steps['steps'].iteritems(): |
| 93 data = {'test': k, 'device_affinity': v['device_affinity']} |
| 94 |
| 95 persisted_result = _GetPersistedResult(k) |
| 96 if persisted_result: |
| 97 data['start_time'] = persisted_result['start_time'] |
| 98 data['end_time'] = persisted_result['end_time'] |
| 99 data['total_time'] = persisted_result['total_time'] |
| 100 data['has_archive'] = persisted_result['archive_bytes'] is not None |
| 101 step_values.append(data) |
| 102 |
| 103 with file(self._output_json_list, 'w') as o: |
| 104 o.write(json.dumps(step_values)) |
| 105 return base_test_result.ResultType.PASS |
| 106 except KeyError: |
| 107 logging.exception('Persistent results file missing key.') |
| 108 return base_test_result.ResultType.FAIL |
| 109 |
| 110 def PrintTestOutput(self): |
| 111 """Helper method to print the output of previously executed test_name. |
| 112 |
| 113 Test_name is passed from the command line as print_step |
| 114 |
| 115 Returns: |
| 116 exit code generated by the test step. |
| 117 """ |
| 118 persisted_result = _GetPersistedResult(self._print_step) |
| 119 if not persisted_result: |
| 120 raise PersistentDataError('No data for test %s found.' % self._print_step) |
| 121 logging.info('*' * 80) |
| 122 logging.info('Output from:') |
| 123 logging.info(persisted_result['cmd']) |
| 124 logging.info('*' * 80) |
| 125 |
| 126 output_formatted = '' |
| 127 persisted_outputs = persisted_result['output'] |
| 128 for i in xrange(len(persisted_outputs)): |
| 129 output_formatted += '\n\nOutput from run #%d:\n\n%s' % ( |
| 130 i, persisted_outputs[i]) |
| 131 print output_formatted |
| 132 |
| 133 if self._output_chartjson_data: |
| 134 with file(self._output_chartjson_data, 'w') as f: |
| 135 f.write(persisted_result['chartjson']) |
| 136 |
| 137 if self._output_dir_archive_path: |
| 138 if persisted_result['archive_bytes'] is not None: |
| 139 with file(self._output_dir_archive_path, 'wb') as f: |
| 140 f.write(persisted_result['archive_bytes']) |
| 141 else: |
| 142 logging.error('The output dir was not archived.') |
| 143 if persisted_result['exit_code'] == 0: |
| 144 return base_test_result.ResultType.PASS |
| 145 return base_test_result.ResultType.FAIL |
| 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 @property |
| 178 def collect_chartjson_data(self): |
| 179 return self._collect_chartjson_data |
| 180 |
| 181 @property |
| 182 def dry_run(self): |
| 183 return self._dry_run |
| 184 |
| 185 @property |
| 186 def flaky_steps(self): |
| 187 return self._flaky_steps |
| 188 |
| 189 @property |
| 190 def known_devices_file(self): |
| 191 return self._known_devices_file |
| 192 |
| 193 @property |
| 194 def max_battery_temp(self): |
| 195 return self._max_battery_temp |
| 196 |
| 197 @property |
| 198 def min_battery_level(self): |
| 199 return self._min_battery_level |
| 200 |
| 201 @property |
| 202 def no_timeout(self): |
| 203 return self._no_timeout |
| 204 |
| 205 @property |
| 206 def output_chartjson_data(self): |
| 207 return self._output_chartjson_data |
| 208 |
| 209 @property |
| 210 def output_dir_archive_path(self): |
| 211 return self._output_dir_archive_path |
| 212 |
| 213 @property |
| 214 def output_json_list(self): |
| 215 return self._output_json_list |
| 216 |
| 217 @property |
| 218 def print_step(self): |
| 219 return self._print_step |
| 220 |
| 221 @property |
| 222 def single_step(self): |
| 223 return self._single_step |
| 224 |
| 225 @property |
| 226 def steps(self): |
| 227 return self._steps |
| 228 |
| 229 @property |
| 230 def test_filter(self): |
| 231 return self._test_filter |
| 232 |
| 233 |
| 234 class PersistentDataError(base_error.BaseError): |
| 235 def __init__(self, message): |
| 236 super(PersistentDataError, self).__init__(message) |
| 237 self._is_infra_error = True |
OLD | NEW |