blob: 8f84a859f0b076c17f1031650e2011fe87dc66f2 [file] [log] [blame]
Lalit Magantidf500092020-07-16 23:56:24 +01001#!/usr/bin/env python3
Lalit Maganti21160e82018-10-16 09:40:29 +01002# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Florian Mayercdc44b32020-06-29 17:18:29 +010016from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
Lalit Maganti21160e82018-10-16 09:40:29 +010020import argparse
Lalit Maganti467a8ef2019-12-05 15:54:20 +000021import datetime
Lalit Maganti21160e82018-10-16 09:40:29 +010022import difflib
23import glob
Lalit Magantidfe69ca2018-10-30 12:18:23 +000024import importlib
Lalit Maganti46a0a532019-07-15 15:09:22 +010025import json
Lalit Maganti21160e82018-10-16 09:40:29 +010026import os
Lalit Maganti4be82e12019-12-05 15:33:09 +000027import re
Lalit Maganti21160e82018-10-16 09:40:29 +010028import subprocess
29import sys
Lalit Magantidfe69ca2018-10-30 12:18:23 +000030import tempfile
Lalit Maganti21160e82018-10-16 09:40:29 +010031
Lalit Magantic8d1d2c2019-08-12 16:10:45 +010032from itertools import chain
Lalit Magantida1c1d02019-05-08 11:55:50 +010033from google.protobuf import reflection, text_format
Lalit Maganti21160e82018-10-16 09:40:29 +010034
Lalit Maganti5eacf422020-06-30 13:51:23 +010035from proto_utils import create_message_factory, serialize_textproto_trace, serialize_python_trace
36
Lalit Magantida1c1d02019-05-08 11:55:50 +010037ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Florian Mayercee13fa2021-01-05 19:05:23 +000038ENV = {
39 'PERFETTO_BINARY_PATH': os.path.join(ROOT_DIR, 'test', 'data'),
40}
41if sys.platform.startswith('linux'):
42 ENV['PATH'] = os.path.join(ROOT_DIR, 'buildtools', 'linux64', 'clang', 'bin')
43elif sys.platform.startswith('dawin'):
44 # Sadly, on macOS we need to check out the Android deps to get
45 # llvm symbolizer.
46 ENV['PATH'] = os.path.join(ROOT_DIR, 'buildtools', 'ndk', 'toolchains',
47 'llvm', 'prebuilt', 'darwin-x86_64', 'bin')
48elif sys.platform.startswith('win32'):
49 ENV['PATH'] = os.path.join(ROOT_DIR, 'buildtools', 'win', 'clang', 'bin')
Lalit Magantida1c1d02019-05-08 11:55:50 +010050
Primiano Tucci834fdc72019-10-04 11:33:44 +010051
Lalit Maganti467a8ef2019-12-05 15:54:20 +000052class Test(object):
53
Lalit Maganti0ec47c62020-02-12 16:24:08 +000054 def __init__(self, type, trace_path, query_path_or_metric, expected_path):
55 self.type = type
Lalit Maganti1642e1d2020-02-12 16:23:38 +000056 self.trace_path = trace_path
57 self.query_path_or_metric = query_path_or_metric
58 self.expected_path = expected_path
Lalit Maganti467a8ef2019-12-05 15:54:20 +000059
60
Lalit Magantic8d1d2c2019-08-12 16:10:45 +010061class PerfResult(object):
Primiano Tucci834fdc72019-10-04 11:33:44 +010062
Lalit Maganti0ec47c62020-02-12 16:24:08 +000063 def __init__(self, test_type, trace_path, query_path_or_metric,
64 ingest_time_ns_str, real_time_ns_str):
65 self.test_type = test_type
Lalit Maganti1642e1d2020-02-12 16:23:38 +000066 self.trace_path = trace_path
67 self.query_path_or_metric = query_path_or_metric
Lalit Maganti467a8ef2019-12-05 15:54:20 +000068 self.ingest_time_ns = int(ingest_time_ns_str)
69 self.real_time_ns = int(real_time_ns_str)
Lalit Magantic8d1d2c2019-08-12 16:10:45 +010070
Primiano Tucci834fdc72019-10-04 11:33:44 +010071
Lalit Maganti04a450a2019-07-03 16:04:19 +010072class TestResult(object):
Primiano Tucci834fdc72019-10-04 11:33:44 +010073
Lalit Maganti467a8ef2019-12-05 15:54:20 +000074 def __init__(self, test_type, input_name, trace, cmd, expected, actual,
Lalit Magantib6fd42a2020-03-31 14:34:36 +010075 stderr, exit_code):
Lalit Maganti04a450a2019-07-03 16:04:19 +010076 self.test_type = test_type
Lalit Maganti637589a2019-07-04 17:25:29 +010077 self.input_name = input_name
Lalit Maganti467a8ef2019-12-05 15:54:20 +000078 self.trace = trace
Lalit Maganti04a450a2019-07-03 16:04:19 +010079 self.cmd = cmd
80 self.expected = expected
81 self.actual = actual
Lalit Maganti467a8ef2019-12-05 15:54:20 +000082 self.stderr = stderr
Lalit Magantib6fd42a2020-03-31 14:34:36 +010083 self.exit_code = exit_code
Lalit Maganti04a450a2019-07-03 16:04:19 +010084
Primiano Tucci834fdc72019-10-04 11:33:44 +010085
Deepanjan Roy5d56d4c2021-02-10 10:53:36 -050086def create_metrics_message_factory(metrics_descriptor_paths):
87 return create_message_factory(metrics_descriptor_paths,
Lalit Magantia951d3d2020-07-16 23:57:00 +010088 'perfetto.protos.TraceMetrics')
89
90
91def write_diff(expected, actual):
92 expected_lines = expected.splitlines(True)
93 actual_lines = actual.splitlines(True)
94 diff = difflib.unified_diff(
95 expected_lines, actual_lines, fromfile='expected', tofile='actual')
96 for line in diff:
97 sys.stderr.write(line)
98
99
Lalit Maganti04a450a2019-07-03 16:04:19 +0100100def run_metrics_test(trace_processor_path, gen_trace_path, metric,
Lalit Maganti75869e62019-07-12 16:55:38 +0100101 expected_path, perf_path, metrics_message_factory):
Lalit Maganti46a0a532019-07-15 15:09:22 +0100102 with open(expected_path, 'r') as expected_file:
Lalit Magantida1c1d02019-05-08 11:55:50 +0100103 expected = expected_file.read()
104
Lalit Maganti25863f72019-08-28 22:14:25 +0100105 json_output = os.path.basename(expected_path).endswith('.json.out')
Lalit Maganti58e24922019-05-30 18:56:14 +0100106 cmd = [
Primiano Tucci834fdc72019-10-04 11:33:44 +0100107 trace_processor_path,
108 '--run-metrics',
109 metric,
110 '--metrics-output=%s' % ('json' if json_output else 'binary'),
Primiano Tucci834fdc72019-10-04 11:33:44 +0100111 '--perf-file',
112 perf_path,
Primiano Tucci4656ba12021-01-05 19:55:15 +0100113 gen_trace_path,
Lalit Maganti58e24922019-05-30 18:56:14 +0100114 ]
Florian Mayercee13fa2021-01-05 19:05:23 +0000115 tp = subprocess.Popen(
116 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=ENV)
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000117 (stdout, stderr) = tp.communicate()
Lalit Magantida1c1d02019-05-08 11:55:50 +0100118
Lalit Maganti25863f72019-08-28 22:14:25 +0100119 if json_output:
120 expected_text = expected
Florian Mayercdc44b32020-06-29 17:18:29 +0100121 actual_text = stdout.decode('utf8')
Lalit Maganti25863f72019-08-28 22:14:25 +0100122 else:
123 # Expected will be in text proto format and we'll need to parse it to a real
124 # proto.
125 expected_message = metrics_message_factory()
126 text_format.Merge(expected, expected_message)
Lalit Magantida1c1d02019-05-08 11:55:50 +0100127
Lalit Maganti25863f72019-08-28 22:14:25 +0100128 # Actual will be the raw bytes of the proto and we'll need to parse it into
129 # a message.
130 actual_message = metrics_message_factory()
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000131 actual_message.ParseFromString(stdout)
Lalit Magantida1c1d02019-05-08 11:55:50 +0100132
Lalit Maganti25863f72019-08-28 22:14:25 +0100133 # Convert both back to text format.
134 expected_text = text_format.MessageToString(expected_message)
135 actual_text = text_format.MessageToString(actual_message)
Lalit Maganti770167a2019-05-08 14:32:50 +0100136
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000137 return TestResult('metric', metric, gen_trace_path, cmd, expected_text,
Florian Mayercdc44b32020-06-29 17:18:29 +0100138 actual_text, stderr.decode('utf8'), tp.returncode)
Lalit Magantida1c1d02019-05-08 11:55:50 +0100139
Primiano Tucci834fdc72019-10-04 11:33:44 +0100140
141def run_query_test(trace_processor_path, gen_trace_path, query_path,
142 expected_path, perf_path):
Lalit Maganti46a0a532019-07-15 15:09:22 +0100143 with open(expected_path, 'r') as expected_file:
Lalit Magantida1c1d02019-05-08 11:55:50 +0100144 expected = expected_file.read()
145
Lalit Maganti75869e62019-07-12 16:55:38 +0100146 cmd = [
Primiano Tucci834fdc72019-10-04 11:33:44 +0100147 trace_processor_path,
148 '-q',
149 query_path,
Primiano Tucci834fdc72019-10-04 11:33:44 +0100150 '--perf-file',
151 perf_path,
Primiano Tucci4656ba12021-01-05 19:55:15 +0100152 gen_trace_path,
Lalit Maganti75869e62019-07-12 16:55:38 +0100153 ]
Lalit Magantida1c1d02019-05-08 11:55:50 +0100154
Florian Mayercee13fa2021-01-05 19:05:23 +0000155 tp = subprocess.Popen(
156 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=ENV)
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000157 (stdout, stderr) = tp.communicate()
Florian Mayercdc44b32020-06-29 17:18:29 +0100158 return TestResult('query', query_path, gen_trace_path, cmd, expected,
159 stdout.decode('utf8'), stderr.decode('utf8'), tp.returncode)
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000160
161
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000162def run_all_tests(trace_processor, trace_descriptor_path,
Andrew Shulaev94d1bf22021-01-15 15:30:42 +0000163 extension_descriptor_paths, metrics_message_factory, tests,
164 keep_input, rebase):
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000165 perf_data = []
166 test_failure = 0
Dan Elphicke603bf72020-12-21 16:01:06 +0000167 rebased = 0
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000168 for test in tests:
Lalit Maganti1642e1d2020-02-12 16:23:38 +0000169 trace_path = test.trace_path
170 expected_path = test.expected_path
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000171 if not os.path.exists(trace_path):
172 sys.stderr.write('Trace file not found {}\n'.format(trace_path))
173 test_failure += 1
174 continue
175 elif not os.path.exists(expected_path):
176 sys.stderr.write('Expected file not found {}\n'.format(expected_path))
177 test_failure += 1
178 continue
179
Lalit Magantidf500092020-07-16 23:56:24 +0100180 is_generated_trace = trace_path.endswith('.py') or trace_path.endswith(
181 '.textproto')
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000182 if trace_path.endswith('.py'):
Eric Secklerfbd9aed2020-03-10 18:07:38 +0000183 gen_trace_file = tempfile.NamedTemporaryFile(delete=False)
Lalit Maganti5eacf422020-06-30 13:51:23 +0100184 serialize_python_trace(trace_descriptor_path, trace_path, gen_trace_file)
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000185 gen_trace_path = os.path.realpath(gen_trace_file.name)
186 elif trace_path.endswith('.textproto'):
Eric Secklerfbd9aed2020-03-10 18:07:38 +0000187 gen_trace_file = tempfile.NamedTemporaryFile(delete=False)
Andrew Shulaev94d1bf22021-01-15 15:30:42 +0000188 serialize_textproto_trace(trace_descriptor_path,
189 extension_descriptor_paths, trace_path,
Lalit Maganti5eacf422020-06-30 13:51:23 +0100190 gen_trace_file)
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000191 gen_trace_path = os.path.realpath(gen_trace_file.name)
192 else:
193 gen_trace_file = None
194 gen_trace_path = trace_path
195
Primiano Tucci4656ba12021-01-05 19:55:15 +0100196 # We can't use delete=True here. When using that on Windwows, the resulting
197 # file is opened in exclusive mode (in turn that's a subtle side-effect of
198 # the underlying CreateFile(FILE_ATTRIBUTE_TEMPORARY)) and TP fails to open
199 # the passed path.
200 tmp_perf_file = tempfile.NamedTemporaryFile(delete=False)
201 sys.stderr.write('[ RUN ] {} {}\n'.format(
202 os.path.basename(test.query_path_or_metric),
203 os.path.basename(trace_path)))
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000204
Primiano Tucci4656ba12021-01-05 19:55:15 +0100205 tmp_perf_path = tmp_perf_file.name
206 if test.type == 'queries':
207 query_path = test.query_path_or_metric
Lalit Maganti1642e1d2020-02-12 16:23:38 +0000208
Primiano Tucci4656ba12021-01-05 19:55:15 +0100209 if not os.path.exists(test.query_path_or_metric):
210 print('Query file not found {}'.format(query_path))
211 test_failure += 1
212 continue
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000213
Primiano Tucci4656ba12021-01-05 19:55:15 +0100214 result = run_query_test(trace_processor, gen_trace_path, query_path,
215 expected_path, tmp_perf_path)
216 elif test.type == 'metrics':
217 result = run_metrics_test(trace_processor, gen_trace_path,
218 test.query_path_or_metric, expected_path,
219 tmp_perf_path, metrics_message_factory)
220 else:
221 assert False
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000222
Primiano Tucci4656ba12021-01-05 19:55:15 +0100223 perf_lines = [line.decode('utf8') for line in tmp_perf_file.readlines()]
224 tmp_perf_file.close()
225 os.remove(tmp_perf_file.name)
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000226
227 if gen_trace_file:
Eric Secklerfbd9aed2020-03-10 18:07:38 +0000228 if keep_input:
Lalit Magantidf500092020-07-16 23:56:24 +0100229 sys.stderr.write(
230 "Saving generated input trace: {}\n".format(gen_trace_path))
Eric Secklerfbd9aed2020-03-10 18:07:38 +0000231 else:
232 gen_trace_file.close()
233 os.remove(gen_trace_path)
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000234
Lalit Magantidf500092020-07-16 23:56:24 +0100235 def write_cmdlines():
236 if is_generated_trace:
237 sys.stderr.write(
238 'Command to generate trace:\n'
239 'tools/serialize_test_trace.py --descriptor {} {} > {}\n'.format(
240 os.path.relpath(trace_descriptor_path, ROOT_DIR),
Lalit Magantia951d3d2020-07-16 23:57:00 +0100241 os.path.relpath(trace_path, ROOT_DIR),
Lalit Magantidf500092020-07-16 23:56:24 +0100242 os.path.relpath(gen_trace_path, ROOT_DIR)))
243 sys.stderr.write('Command line:\n{}\n'.format(' '.join(result.cmd)))
244
Primiano Tucci4656ba12021-01-05 19:55:15 +0100245 contents_equal = (
246 result.expected.replace('\r\n',
247 '\n') == result.actual.replace('\r\n', '\n'))
248 if result.exit_code != 0 or not contents_equal:
Lalit Magantib6fd42a2020-03-31 14:34:36 +0100249 sys.stderr.write(result.stderr)
250
251 if result.exit_code == 0:
252 sys.stderr.write(
253 'Expected did not match actual for trace {} and {} {}\n'.format(
254 trace_path, result.test_type, result.input_name))
255 sys.stderr.write('Expected file: {}\n'.format(expected_path))
Lalit Magantidf500092020-07-16 23:56:24 +0100256 write_cmdlines()
Lalit Magantib6fd42a2020-03-31 14:34:36 +0100257 write_diff(result.expected, result.actual)
258 else:
Lalit Magantidf500092020-07-16 23:56:24 +0100259 write_cmdlines()
Lalit Magantib6fd42a2020-03-31 14:34:36 +0100260
261 sys.stderr.write('[ FAIL ] {} {}\n'.format(
262 os.path.basename(test.query_path_or_metric),
263 os.path.basename(trace_path)))
264
Dan Elphicke603bf72020-12-21 16:01:06 +0000265 if rebase:
266 if result.exit_code == 0:
267 sys.stderr.write('Rebasing {}\n'.format(expected_path))
268 with open(expected_path, 'w') as f:
269 f.write(result.actual)
270 rebase += 1
271 else:
272 sys.stderr.write(
273 'Rebase failed for {} as query failed\n'.format(expected_path))
274
Lalit Magantib6fd42a2020-03-31 14:34:36 +0100275 test_failure += 1
276 else:
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000277 assert len(perf_lines) == 1
278 perf_numbers = perf_lines[0].split(',')
279
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000280 assert len(perf_numbers) == 2
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000281 perf_result = PerfResult(test.type, trace_path, test.query_path_or_metric,
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000282 perf_numbers[0], perf_numbers[1])
283 perf_data.append(perf_result)
284
285 sys.stderr.write(
286 '[ OK ] {} {} (ingest: {} ms, query: {} ms)\n'.format(
Lalit Maganti1642e1d2020-02-12 16:23:38 +0000287 os.path.basename(test.query_path_or_metric),
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000288 os.path.basename(trace_path),
289 perf_result.ingest_time_ns / 1000000,
290 perf_result.real_time_ns / 1000000))
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000291
Dan Elphicke603bf72020-12-21 16:01:06 +0000292 return test_failure, perf_data, rebased
Lalit Magantidfe69ca2018-10-30 12:18:23 +0000293
Primiano Tucci834fdc72019-10-04 11:33:44 +0100294
Lalit Magantia951d3d2020-07-16 23:57:00 +0100295def read_all_tests_from_index(index_path, query_metric_pattern, trace_pattern):
Lalit Maganti244200e2020-07-15 11:39:07 +0100296 index_dir = os.path.dirname(index_path)
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000297
Lalit Maganti244200e2020-07-15 11:39:07 +0100298 with open(index_path, 'r') as index_file:
299 index_lines = index_file.readlines()
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000300
301 tests = []
302 for line in index_lines:
303 stripped = line.strip()
304 if stripped.startswith('#'):
305 continue
306 elif not stripped:
307 continue
308
309 [trace_fname, query_fname_or_metric, expected_fname] = stripped.split(' ')
310 if not query_metric_pattern.match(os.path.basename(query_fname_or_metric)):
311 continue
312
313 if not trace_pattern.match(os.path.basename(trace_fname)):
314 continue
315
316 trace_path = os.path.abspath(os.path.join(index_dir, trace_fname))
317 expected_path = os.path.abspath(os.path.join(index_dir, expected_fname))
318
Lalit Magantia951d3d2020-07-16 23:57:00 +0100319 if query_fname_or_metric.endswith('.sql'):
320 test_type = 'queries'
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000321 query_path_or_metric = os.path.abspath(
322 os.path.join(index_dir, query_fname_or_metric))
Lalit Magantia951d3d2020-07-16 23:57:00 +0100323 else:
324 test_type = 'metrics'
325 query_path_or_metric = query_fname_or_metric
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000326
327 tests.append(
328 Test(test_type, trace_path, query_path_or_metric, expected_path))
Lalit Maganti244200e2020-07-15 11:39:07 +0100329 return tests
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000330
Lalit Maganti244200e2020-07-15 11:39:07 +0100331
Lalit Magantia951d3d2020-07-16 23:57:00 +0100332def read_all_tests(query_metric_pattern, trace_pattern):
333 include_index_dir = os.path.join(ROOT_DIR, 'test', 'trace_processor')
334 include_index = os.path.join(include_index_dir, 'include_index')
Lalit Maganti244200e2020-07-15 11:39:07 +0100335 tests = []
336 with open(include_index, 'r') as include_file:
337 for index_relpath in include_file.readlines():
338 index_path = os.path.join(include_index_dir, index_relpath.strip())
339 tests.extend(
Lalit Magantia951d3d2020-07-16 23:57:00 +0100340 read_all_tests_from_index(index_path, query_metric_pattern,
Lalit Maganti244200e2020-07-15 11:39:07 +0100341 trace_pattern))
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000342 return tests
343
344
Lalit Maganti21160e82018-10-16 09:40:29 +0100345def main():
346 parser = argparse.ArgumentParser()
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000347 parser.add_argument('--test-type', type=str, default='all')
Lalit Magantida1c1d02019-05-08 11:55:50 +0100348 parser.add_argument('--trace-descriptor', type=str)
349 parser.add_argument('--metrics-descriptor', type=str)
Lalit Maganti75869e62019-07-12 16:55:38 +0100350 parser.add_argument('--perf-file', type=str)
Primiano Tucci834fdc72019-10-04 11:33:44 +0100351 parser.add_argument(
Lalit Maganti4be82e12019-12-05 15:33:09 +0000352 '--query-metric-filter',
353 default='.*',
354 type=str,
355 help=
356 'Filter the name of query files or metrics to diff test (regex syntax)')
357 parser.add_argument(
358 '--trace-filter',
359 default='.*',
360 type=str,
361 help='Filter the name of trace files to diff test (regex syntax)')
362 parser.add_argument(
Eric Secklerfbd9aed2020-03-10 18:07:38 +0000363 '--keep-input',
364 action='store_true',
365 help='Save the (generated) input pb file for debugging')
366 parser.add_argument(
Dan Elphicke603bf72020-12-21 16:01:06 +0000367 '--rebase',
368 action='store_true',
369 help='Update the expected output file with the actual result')
370 parser.add_argument(
Primiano Tucci834fdc72019-10-04 11:33:44 +0100371 'trace_processor', type=str, help='location of trace processor binary')
Lalit Maganti21160e82018-10-16 09:40:29 +0100372 args = parser.parse_args()
373
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000374 query_metric_pattern = re.compile(args.query_metric_filter)
375 trace_pattern = re.compile(args.trace_filter)
376
Lalit Magantia951d3d2020-07-16 23:57:00 +0100377 tests = read_all_tests(query_metric_pattern, trace_pattern)
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000378 sys.stderr.write('[==========] Running {} tests.\n'.format(len(tests)))
Lalit Maganti21160e82018-10-16 09:40:29 +0100379
Andrew Shulaev94d1bf22021-01-15 15:30:42 +0000380 out_path = os.path.dirname(args.trace_processor)
Lalit Magantidfe69ca2018-10-30 12:18:23 +0000381 if args.trace_descriptor:
382 trace_descriptor_path = args.trace_descriptor
383 else:
Lalit Magantie0986f32020-09-17 15:35:47 +0100384 def find_trace_descriptor(parent):
385 trace_protos_path = os.path.join(parent, 'gen', 'protos', 'perfetto',
386 'trace')
387 return os.path.join(trace_protos_path, 'trace.descriptor')
388
389 trace_descriptor_path = find_trace_descriptor(out_path)
390 if not os.path.exists(trace_descriptor_path):
391 trace_descriptor_path = find_trace_descriptor(
392 os.path.join(out_path, 'gcc_like_host'))
Lalit Magantidfe69ca2018-10-30 12:18:23 +0000393
Deepanjan Roy5d56d4c2021-02-10 10:53:36 -0500394
Lalit Magantida1c1d02019-05-08 11:55:50 +0100395 if args.metrics_descriptor:
Deepanjan Roy5d56d4c2021-02-10 10:53:36 -0500396 metrics_descriptor_paths = [args.metrics_descriptor]
Lalit Magantida1c1d02019-05-08 11:55:50 +0100397 else:
Primiano Tucci834fdc72019-10-04 11:33:44 +0100398 metrics_protos_path = os.path.join(out_path, 'gen', 'protos', 'perfetto',
399 'metrics')
Deepanjan Roy5d56d4c2021-02-10 10:53:36 -0500400 metrics_descriptor_paths = [
401 os.path.join(metrics_protos_path, 'metrics.descriptor'),
402 os.path.join(metrics_protos_path, 'chrome',
403 'all_chrome_metrics.descriptor')
404 ]
Lalit Magantida1c1d02019-05-08 11:55:50 +0100405
Andrew Shulaev94d1bf22021-01-15 15:30:42 +0000406 chrome_extensions = os.path.join(out_path, 'gen', 'protos', 'third_party',
407 'chromium', 'chrome_track_event.descriptor')
408 test_extensions = os.path.join(out_path, 'gen', 'protos', 'perfetto', 'trace',
409 'test_extensions.descriptor')
410
Lalit Magantida1c1d02019-05-08 11:55:50 +0100411 metrics_message_factory = create_metrics_message_factory(
Deepanjan Roy5d56d4c2021-02-10 10:53:36 -0500412 metrics_descriptor_paths)
Lalit Magantida1c1d02019-05-08 11:55:50 +0100413
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000414 test_run_start = datetime.datetime.now()
Andrew Shulaev94d1bf22021-01-15 15:30:42 +0000415 test_failure, perf_data, rebased = run_all_tests(
416 args.trace_processor, trace_descriptor_path,
417 [chrome_extensions, test_extensions], metrics_message_factory, tests,
418 args.keep_input, args.rebase)
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000419 test_run_end = datetime.datetime.now()
Hector Dearmanf1f51a32018-10-22 11:09:41 +0100420
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000421 sys.stderr.write('[==========] {} tests ran. ({} ms total)\n'.format(
422 len(tests), int((test_run_end - test_run_start).total_seconds() * 1000)))
423 sys.stderr.write('[ PASSED ] {} tests.\n'.format(len(tests) - test_failure))
Dan Elphicke603bf72020-12-21 16:01:06 +0000424 if args.rebase:
425 sys.stderr.write('{} tests rebased.\n'.format(rebased))
Lalit Maganti04a450a2019-07-03 16:04:19 +0100426
Lalit Maganti21160e82018-10-16 09:40:29 +0100427 if test_failure == 0:
Lalit Maganti75869e62019-07-12 16:55:38 +0100428 if args.perf_file:
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000429 test_dir = os.path.join(ROOT_DIR, 'test')
430 trace_processor_dir = os.path.join(test_dir, 'trace_processor')
431
432 metrics = []
Lalit Magantidf500092020-07-16 23:56:24 +0100433 sorted_data = sorted(
434 perf_data,
435 key=lambda x: (x.test_type, x.trace_path, x.query_path_or_metric))
436 for perf_args in sorted_data:
Lalit Maganti0ec47c62020-02-12 16:24:08 +0000437 trace_short_path = os.path.relpath(perf_args.trace_path, test_dir)
438
439 query_short_path_or_metric = perf_args.query_path_or_metric
440 if perf_args.test_type == 'queries':
441 query_short_path_or_metric = os.path.relpath(
442 perf_args.query_path_or_metric, trace_processor_dir)
443
444 metrics.append({
445 'metric': 'tp_perf_test_ingest_time',
446 'value': float(perf_args.ingest_time_ns) / 1.0e9,
447 'unit': 's',
448 'tags': {
449 'test_name':
450 '{}-{}'.format(trace_short_path,
451 query_short_path_or_metric),
452 'test_type':
453 perf_args.test_type,
454 },
455 'labels': {},
456 })
457 metrics.append({
458 'metric': 'perf_test_real_time',
459 'value': float(perf_args.real_time_ns) / 1.0e9,
460 'unit': 's',
461 'tags': {
462 'test_name':
463 '{}-{}'.format(
464 os.path.relpath(perf_args.trace_path, test_dir),
465 query_short_path_or_metric),
466 'test_type':
467 perf_args.test_type,
468 },
469 'labels': {},
470 })
471
472 output_data = {'metrics': metrics}
Lalit Maganti75869e62019-07-12 16:55:38 +0100473 with open(args.perf_file, 'w+') as perf_file:
Lalit Maganti46a0a532019-07-15 15:09:22 +0100474 perf_file.write(json.dumps(output_data, indent=2))
Lalit Maganti21160e82018-10-16 09:40:29 +0100475 return 0
476 else:
Lalit Maganti467a8ef2019-12-05 15:54:20 +0000477 sys.stderr.write('[ FAILED ] {} tests.\n'.format(test_failure))
Lalit Maganti21160e82018-10-16 09:40:29 +0100478 return 1
479
Primiano Tucci834fdc72019-10-04 11:33:44 +0100480
Lalit Maganti21160e82018-10-16 09:40:29 +0100481if __name__ == '__main__':
482 sys.exit(main())