Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: build/android/pylib/perf/perf_test_instance.py

Issue 2012323002: [Android] Implement perf tests to platform mode. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Unify RunTestsInPlatformMode and move adbd restart Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 test_instance
13 from pylib.constants import exit_codes
14 from pylib.constants import host_paths
15 from devil.utils import cmd_helper
16
17
18 _GIT_CR_POS_RE = re.compile(r'^Cr-Commit-Position: refs/heads/master@{#(\d+)}$')
19
20
21 def _GetPersistedResult(test_name):
22 file_name = os.path.join(constants.PERF_OUTPUT_DIR, test_name)
23 if not os.path.exists(file_name):
24 logging.error('File not found %s', file_name)
25 return None
26
27 with file(file_name, 'r') as f:
28 return pickle.loads(f.read())
29
30
31 def _GetChromiumRevision():
jbudorick 2016/06/07 15:29:28 I'm not sure this is something the test runner sho
rnephew (Reviews Here) 2016/06/09 22:06:39 Any suggestion on where to move it since the build
32 # pylint: disable=line-too-long
33 """Get the git hash and commit position of the chromium master branch.
34
35 See: https://chromium.googlesource.com/chromium/tools/build/+/master/scripts/s lave/runtest.py#212
jbudorick 2016/06/07 15:29:28 Drop the URL onto its own line and see if you stil
rnephew (Reviews Here) 2016/06/09 22:06:39 Yep.
36
37 Returns:
38 A dictionary with 'revision' and 'commit_pos' keys.
39 """
40 # pylint: enable=line-too-long
41 status, output = cmd_helper.GetCmdStatusAndOutput(
42 ['git', 'log', '-n', '1', '--pretty=format:%H%n%B', 'HEAD'],
43 cwd=host_paths.DIR_SOURCE_ROOT)
44 revision = None
45 commit_pos = None
46 if not status:
47 lines = output.splitlines()
48 revision = lines[0]
49 for line in reversed(lines):
50 m = _GIT_CR_POS_RE.match(line.strip())
51 if m:
52 commit_pos = int(m.group(1))
53 break
54 return {'revision': revision, 'commit_pos': commit_pos}
55
56
57 class PerfTestInstance(test_instance.TestInstance):
58 def __init__(self, args, _):
59 super(PerfTestInstance, self).__init__()
60
61 if args.single_step:
62 args.single_step = ' '.join(args.single_step_command)
63
64 self._collect_chartjson_data = args.collect_chartjson_data
65 self._dry_run = args.dry_run
66 self._flaky_steps = args.flaky_steps
67 self._get_output_dir_archive = args.get_output_dir_archive
jbudorick 2016/06/07 15:29:28 If this argument is the path to the output directo
rnephew (Reviews Here) 2016/06/09 22:06:39 Done.
68 self._known_devices_file = args.known_devices_file
69 self._max_battery_temp = args.max_battery_temp
70 self._min_battery_level = args.min_battery_level
71 self._no_timeout = args.no_timeout
72 self._output_chartjson_data = args.output_chartjson_data
73 self._output_json_list = args.output_json_list
74 self._print_step = args.print_step
75 self._single_step = args.single_step
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 0
106 except Exception: # pylint: disable=broad-except
107 return constants.ERROR_EXIT_CODE
108
109 def PrintTestOutput(self):
110 """Helper method to print the output of previously executed test_name.
111
112 Test_name is passed from the command line as print_step
113
114 Returns:
115 exit code generated by the test step.
116 """
117 persisted_result = _GetPersistedResult(self._print_step)
118 if not persisted_result:
119 return exit_codes.INFRA
120 logging.info('*' * 80)
121 logging.info('Output from:')
122 logging.info(persisted_result['cmd'])
123 logging.info('*' * 80)
124
125 output_formatted = ''
126 persisted_outputs = persisted_result['output']
127 for i in xrange(len(persisted_outputs)):
128 output_formatted += '\n\nOutput from run #%d:\n\n%s' % (
129 i, persisted_outputs[i])
130 print output_formatted
131
132 if self._output_chartjson_data:
133 with file(self._output_chartjson_data, 'w') as f:
134 f.write(persisted_result['chartjson'])
135
136 if self._get_output_dir_archive:
137 if persisted_result['archive_bytes'] is not None:
138 with file(self._get_output_dir_archive, 'wb') as f:
139 f.write(persisted_result['archive_bytes'])
140 else:
141 logging.error('The output dir was not archived.')
142
143 return persisted_result['exit_code']
144
145 #override
146 def TestType(self):
147 return 'perf'
148
149 @staticmethod
150 def ReadChartjsonOutput(output_dir):
151 if not output_dir:
152 return ''
153 json_output_path = os.path.join(output_dir, 'results-chart.json')
154 try:
155 with open(json_output_path) as f:
156 return f.read()
157 except IOError:
158 logging.exception('Exception when reading chartjson.')
159 logging.error('This usually means that telemetry did not run, so it could'
160 ' not generate the file. Please check the device running'
161 ' the test.')
162 return ''
163
164 def WriteBuildBotJson(self, output_dir):
165 """Write metadata about the buildbot environment to the output dir."""
166 if not output_dir or not self._write_buildbot_json:
167 return
168 data = {
169 'chromium': _GetChromiumRevision(),
170 'environment': dict(os.environ)
171 }
172 with open(os.path.join(output_dir, 'buildbot.json'), 'w') as f:
173 json.dump(data, f, sort_keys=True, indent=2, separators=(',', ': '))
174
175
176 @property
177 def collect_chartjson_data(self):
178 return self._collect_chartjson_data
179
180 @property
181 def dry_run(self):
182 return self._dry_run
183
184 @property
185 def flaky_steps(self):
186 return self._flaky_steps
187
188 @property
189 def get_output_dir_archive(self):
190 return self._get_output_dir_archive
191
192 @property
193 def known_devices_file(self):
194 return self._known_devices_file
195
196 @property
197 def max_battery_temp(self):
198 return self._max_battery_temp
199
200 @property
201 def min_battery_level(self):
202 return self._min_battery_level
203
204 @property
205 def no_timeout(self):
206 return self._no_timeout
207
208 @property
209 def output_chartjson_data(self):
210 return self._output_chartjson_data
211
212 @property
213 def output_json_list(self):
214 return self._output_json_list
215
216 @property
217 def print_step(self):
218 return self._print_step
219
220 @property
221 def single_step(self):
222 return self._single_step
223
224 @property
225 def steps(self):
226 return self._steps
227
228 @property
229 def test_filter(self):
230 return self._test_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698