Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # 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 | |
| 16 | import argparse |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 17 | import datetime |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 18 | import difflib |
| 19 | import glob |
Lalit Maganti | dfe69ca | 2018-10-30 12:18:23 +0000 | [diff] [blame] | 20 | import importlib |
Lalit Maganti | 46a0a53 | 2019-07-15 15:09:22 +0100 | [diff] [blame] | 21 | import json |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 22 | import os |
Lalit Maganti | 4be82e1 | 2019-12-05 15:33:09 +0000 | [diff] [blame^] | 23 | import re |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 24 | import subprocess |
| 25 | import sys |
Lalit Maganti | dfe69ca | 2018-10-30 12:18:23 +0000 | [diff] [blame] | 26 | import tempfile |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 27 | |
Lalit Maganti | c8d1d2c | 2019-08-12 16:10:45 +0100 | [diff] [blame] | 28 | from itertools import chain |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 29 | from google.protobuf import descriptor, descriptor_pb2, message_factory |
| 30 | from google.protobuf import reflection, text_format |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 31 | |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 32 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 33 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 34 | |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 35 | class Test(object): |
| 36 | |
| 37 | def __init__(self, trace_fname, query_fname_or_metric, expected_fname): |
| 38 | self.trace_fname = trace_fname |
| 39 | self.query_fname_or_metric = query_fname_or_metric |
| 40 | self.expected_fname = expected_fname |
| 41 | |
| 42 | |
Lalit Maganti | c8d1d2c | 2019-08-12 16:10:45 +0100 | [diff] [blame] | 43 | class PerfResult(object): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 44 | |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 45 | def __init__(self, trace_name, query_or_metric, ingest_time_ns_str, |
| 46 | real_time_ns_str): |
Lalit Maganti | c8d1d2c | 2019-08-12 16:10:45 +0100 | [diff] [blame] | 47 | self.trace_name = trace_name |
| 48 | self.query_or_metric = query_or_metric |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 49 | self.ingest_time_ns = int(ingest_time_ns_str) |
| 50 | self.real_time_ns = int(real_time_ns_str) |
Lalit Maganti | c8d1d2c | 2019-08-12 16:10:45 +0100 | [diff] [blame] | 51 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 52 | |
Ioannis Ilkos | 3c6a914 | 2019-08-14 00:39:27 +0100 | [diff] [blame] | 53 | def create_message_factory(descriptor_file_path, proto_type): |
| 54 | with open(descriptor_file_path, 'rb') as descriptor_file: |
| 55 | descriptor_content = descriptor_file.read() |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 56 | |
| 57 | file_desc_set_pb2 = descriptor_pb2.FileDescriptorSet() |
Ioannis Ilkos | 3c6a914 | 2019-08-14 00:39:27 +0100 | [diff] [blame] | 58 | file_desc_set_pb2.MergeFromString(descriptor_content) |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 59 | |
| 60 | desc_by_path = {} |
| 61 | for f_desc_pb2 in file_desc_set_pb2.file: |
| 62 | f_desc_pb2_encode = f_desc_pb2.SerializeToString() |
| 63 | f_desc = descriptor.FileDescriptor( |
| 64 | name=f_desc_pb2.name, |
| 65 | package=f_desc_pb2.package, |
| 66 | serialized_pb=f_desc_pb2_encode) |
| 67 | |
| 68 | for desc in f_desc.message_types_by_name.values(): |
| 69 | desc_by_path[desc.full_name] = desc |
| 70 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 71 | return message_factory.MessageFactory().GetPrototype(desc_by_path[proto_type]) |
| 72 | |
Ioannis Ilkos | 3c6a914 | 2019-08-14 00:39:27 +0100 | [diff] [blame] | 73 | |
| 74 | def create_trace_message_factory(trace_descriptor_path): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 75 | return create_message_factory(trace_descriptor_path, 'perfetto.protos.Trace') |
| 76 | |
Ioannis Ilkos | 3c6a914 | 2019-08-14 00:39:27 +0100 | [diff] [blame] | 77 | |
| 78 | def create_metrics_message_factory(metrics_descriptor_path): |
| 79 | return create_message_factory(metrics_descriptor_path, |
| 80 | 'perfetto.protos.TraceMetrics') |
| 81 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 82 | |
Ioannis Ilkos | 3c6a914 | 2019-08-14 00:39:27 +0100 | [diff] [blame] | 83 | def serialize_text_proto_to_file(proto_descriptor_path, text_proto_path, |
| 84 | output_file): |
| 85 | trace_message_factory = create_trace_message_factory(proto_descriptor_path) |
| 86 | proto = trace_message_factory() |
| 87 | with open(text_proto_path, 'r') as text_proto_file: |
| 88 | text_format.Merge(text_proto_file.read(), proto) |
| 89 | output_file.write(proto.SerializeToString()) |
| 90 | output_file.flush() |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 91 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 92 | |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 93 | def write_diff(expected, actual): |
| 94 | expected_lines = expected.splitlines(True) |
| 95 | actual_lines = actual.splitlines(True) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 96 | diff = difflib.unified_diff( |
| 97 | expected_lines, actual_lines, fromfile='expected', tofile='actual') |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 98 | for line in diff: |
| 99 | sys.stderr.write(line) |
| 100 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 101 | |
Lalit Maganti | 04a450a | 2019-07-03 16:04:19 +0100 | [diff] [blame] | 102 | class TestResult(object): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 103 | |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 104 | def __init__(self, test_type, input_name, trace, cmd, expected, actual, |
| 105 | stderr): |
Lalit Maganti | 04a450a | 2019-07-03 16:04:19 +0100 | [diff] [blame] | 106 | self.test_type = test_type |
Lalit Maganti | 637589a | 2019-07-04 17:25:29 +0100 | [diff] [blame] | 107 | self.input_name = input_name |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 108 | self.trace = trace |
Lalit Maganti | 04a450a | 2019-07-03 16:04:19 +0100 | [diff] [blame] | 109 | self.cmd = cmd |
| 110 | self.expected = expected |
| 111 | self.actual = actual |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 112 | self.stderr = stderr |
Lalit Maganti | 04a450a | 2019-07-03 16:04:19 +0100 | [diff] [blame] | 113 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 114 | |
Lalit Maganti | 04a450a | 2019-07-03 16:04:19 +0100 | [diff] [blame] | 115 | def run_metrics_test(trace_processor_path, gen_trace_path, metric, |
Lalit Maganti | 75869e6 | 2019-07-12 16:55:38 +0100 | [diff] [blame] | 116 | expected_path, perf_path, metrics_message_factory): |
Lalit Maganti | 46a0a53 | 2019-07-15 15:09:22 +0100 | [diff] [blame] | 117 | with open(expected_path, 'r') as expected_file: |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 118 | expected = expected_file.read() |
| 119 | |
Lalit Maganti | 25863f7 | 2019-08-28 22:14:25 +0100 | [diff] [blame] | 120 | json_output = os.path.basename(expected_path).endswith('.json.out') |
Lalit Maganti | 58e2492 | 2019-05-30 18:56:14 +0100 | [diff] [blame] | 121 | cmd = [ |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 122 | trace_processor_path, |
| 123 | '--run-metrics', |
| 124 | metric, |
| 125 | '--metrics-output=%s' % ('json' if json_output else 'binary'), |
| 126 | gen_trace_path, |
| 127 | '--perf-file', |
| 128 | perf_path, |
Lalit Maganti | 58e2492 | 2019-05-30 18:56:14 +0100 | [diff] [blame] | 129 | ] |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 130 | tp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 131 | (stdout, stderr) = tp.communicate() |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 132 | |
Lalit Maganti | 25863f7 | 2019-08-28 22:14:25 +0100 | [diff] [blame] | 133 | if json_output: |
| 134 | expected_text = expected |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 135 | actual_text = stdout |
Lalit Maganti | 25863f7 | 2019-08-28 22:14:25 +0100 | [diff] [blame] | 136 | else: |
| 137 | # Expected will be in text proto format and we'll need to parse it to a real |
| 138 | # proto. |
| 139 | expected_message = metrics_message_factory() |
| 140 | text_format.Merge(expected, expected_message) |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 141 | |
Lalit Maganti | 25863f7 | 2019-08-28 22:14:25 +0100 | [diff] [blame] | 142 | # Actual will be the raw bytes of the proto and we'll need to parse it into |
| 143 | # a message. |
| 144 | actual_message = metrics_message_factory() |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 145 | actual_message.ParseFromString(stdout) |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 146 | |
Lalit Maganti | 25863f7 | 2019-08-28 22:14:25 +0100 | [diff] [blame] | 147 | # Convert both back to text format. |
| 148 | expected_text = text_format.MessageToString(expected_message) |
| 149 | actual_text = text_format.MessageToString(actual_message) |
Lalit Maganti | 770167a | 2019-05-08 14:32:50 +0100 | [diff] [blame] | 150 | |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 151 | return TestResult('metric', metric, gen_trace_path, cmd, expected_text, |
| 152 | actual_text, stderr) |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 153 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 154 | |
| 155 | def run_query_test(trace_processor_path, gen_trace_path, query_path, |
| 156 | expected_path, perf_path): |
Lalit Maganti | 46a0a53 | 2019-07-15 15:09:22 +0100 | [diff] [blame] | 157 | with open(expected_path, 'r') as expected_file: |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 158 | expected = expected_file.read() |
| 159 | |
Lalit Maganti | 75869e6 | 2019-07-12 16:55:38 +0100 | [diff] [blame] | 160 | cmd = [ |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 161 | trace_processor_path, |
| 162 | '-q', |
| 163 | query_path, |
| 164 | gen_trace_path, |
| 165 | '--perf-file', |
| 166 | perf_path, |
Lalit Maganti | 75869e6 | 2019-07-12 16:55:38 +0100 | [diff] [blame] | 167 | ] |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 168 | |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 169 | tp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 170 | (stdout, stderr) = tp.communicate() |
| 171 | return TestResult('query', query_path, gen_trace_path, cmd, expected, stdout, |
| 172 | stderr) |
| 173 | |
| 174 | |
| 175 | def run_all_tests(args, test_dir, index_dir, trace_descriptor_path, |
| 176 | metrics_message_factory, tests): |
| 177 | perf_data = [] |
| 178 | test_failure = 0 |
| 179 | for test in tests: |
| 180 | trace_path = os.path.abspath(os.path.join(index_dir, test.trace_fname)) |
| 181 | expected_path = os.path.abspath( |
| 182 | os.path.join(index_dir, test.expected_fname)) |
| 183 | if not os.path.exists(trace_path): |
| 184 | sys.stderr.write('Trace file not found {}\n'.format(trace_path)) |
| 185 | test_failure += 1 |
| 186 | continue |
| 187 | elif not os.path.exists(expected_path): |
| 188 | sys.stderr.write('Expected file not found {}\n'.format(expected_path)) |
| 189 | test_failure += 1 |
| 190 | continue |
| 191 | |
| 192 | if trace_path.endswith('.py'): |
| 193 | gen_trace_file = tempfile.NamedTemporaryFile() |
| 194 | python_cmd = ['python', trace_path, trace_descriptor_path] |
| 195 | subprocess.check_call(python_cmd, stdout=gen_trace_file) |
| 196 | gen_trace_path = os.path.realpath(gen_trace_file.name) |
| 197 | elif trace_path.endswith('.textproto'): |
| 198 | gen_trace_file = tempfile.NamedTemporaryFile() |
| 199 | serialize_text_proto_to_file(trace_descriptor_path, trace_path, |
| 200 | gen_trace_file) |
| 201 | gen_trace_path = os.path.realpath(gen_trace_file.name) |
| 202 | else: |
| 203 | gen_trace_file = None |
| 204 | gen_trace_path = trace_path |
| 205 | |
| 206 | with tempfile.NamedTemporaryFile() as tmp_perf_file: |
| 207 | sys.stderr.write('[ RUN ] {} {}\n'.format( |
| 208 | os.path.basename(test.query_fname_or_metric), |
| 209 | os.path.basename(trace_path))) |
| 210 | |
| 211 | tmp_perf_path = tmp_perf_file.name |
| 212 | if args.test_type == 'queries': |
| 213 | query_path = os.path.abspath( |
| 214 | os.path.join(index_dir, test.query_fname_or_metric)) |
| 215 | if not os.path.exists(query_path): |
| 216 | print('Query file not found {}'.format(query_path)) |
| 217 | test_failure += 1 |
| 218 | continue |
| 219 | |
| 220 | result = run_query_test(args.trace_processor, gen_trace_path, |
| 221 | query_path, expected_path, tmp_perf_path) |
| 222 | elif args.test_type == 'metrics': |
| 223 | result = run_metrics_test(args.trace_processor, gen_trace_path, |
| 224 | test.query_fname_or_metric, expected_path, |
| 225 | tmp_perf_path, metrics_message_factory) |
| 226 | else: |
| 227 | assert False |
| 228 | |
| 229 | perf_lines = tmp_perf_file.readlines() |
| 230 | |
| 231 | if gen_trace_file: |
| 232 | gen_trace_file.close() |
| 233 | |
| 234 | if result.expected == result.actual: |
| 235 | assert len(perf_lines) == 1 |
| 236 | perf_numbers = perf_lines[0].split(',') |
| 237 | |
| 238 | trace_shortpath = os.path.relpath(trace_path, test_dir) |
| 239 | |
| 240 | assert len(perf_numbers) == 2 |
| 241 | perf_result = PerfResult(trace_shortpath, test.query_fname_or_metric, |
| 242 | perf_numbers[0], perf_numbers[1]) |
| 243 | perf_data.append(perf_result) |
| 244 | |
| 245 | sys.stderr.write( |
| 246 | '[ OK ] {} {} (ingest: {} ms, query: {} ms)\n'.format( |
| 247 | os.path.basename(test.query_fname_or_metric), |
| 248 | os.path.basename(trace_path), |
| 249 | perf_result.ingest_time_ns / 1000000, |
| 250 | perf_result.real_time_ns / 1000000)) |
| 251 | else: |
| 252 | sys.stderr.write(result.stderr) |
| 253 | |
| 254 | sys.stderr.write( |
| 255 | 'Expected did not match actual for trace {} and {} {}\n'.format( |
| 256 | trace_path, result.test_type, result.input_name)) |
| 257 | sys.stderr.write('Expected file: {}\n'.format(expected_path)) |
| 258 | sys.stderr.write('Command line: {}\n'.format(' '.join(result.cmd))) |
| 259 | |
| 260 | write_diff(result.expected, result.actual) |
| 261 | |
| 262 | sys.stderr.write('[ FAIL ] {} {}\n'.format( |
| 263 | os.path.basename(test.query_fname_or_metric), |
| 264 | os.path.basename(trace_path))) |
| 265 | |
| 266 | test_failure += 1 |
| 267 | |
| 268 | return test_failure, perf_data |
Lalit Maganti | dfe69ca | 2018-10-30 12:18:23 +0000 | [diff] [blame] | 269 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 270 | |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 271 | def main(): |
| 272 | parser = argparse.ArgumentParser() |
Lalit Maganti | 46a0a53 | 2019-07-15 15:09:22 +0100 | [diff] [blame] | 273 | parser.add_argument('--test-type', type=str, default='queries') |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 274 | parser.add_argument('--trace-descriptor', type=str) |
| 275 | parser.add_argument('--metrics-descriptor', type=str) |
Lalit Maganti | 75869e6 | 2019-07-12 16:55:38 +0100 | [diff] [blame] | 276 | parser.add_argument('--perf-file', type=str) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 277 | parser.add_argument( |
Lalit Maganti | 4be82e1 | 2019-12-05 15:33:09 +0000 | [diff] [blame^] | 278 | '--query-metric-filter', |
| 279 | default='.*', |
| 280 | type=str, |
| 281 | help= |
| 282 | 'Filter the name of query files or metrics to diff test (regex syntax)') |
| 283 | parser.add_argument( |
| 284 | '--trace-filter', |
| 285 | default='.*', |
| 286 | type=str, |
| 287 | help='Filter the name of trace files to diff test (regex syntax)') |
| 288 | parser.add_argument( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 289 | 'trace_processor', type=str, help='location of trace processor binary') |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 290 | args = parser.parse_args() |
| 291 | |
Lalit Maganti | 46a0a53 | 2019-07-15 15:09:22 +0100 | [diff] [blame] | 292 | test_dir = os.path.join(ROOT_DIR, 'test') |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 293 | if args.test_type == 'queries': |
Lalit Maganti | 46a0a53 | 2019-07-15 15:09:22 +0100 | [diff] [blame] | 294 | index = os.path.join(test_dir, 'trace_processor', 'index') |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 295 | elif args.test_type == 'metrics': |
Lalit Maganti | 46a0a53 | 2019-07-15 15:09:22 +0100 | [diff] [blame] | 296 | index = os.path.join(test_dir, 'metrics', 'index') |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 297 | else: |
Lalit Maganti | 46a0a53 | 2019-07-15 15:09:22 +0100 | [diff] [blame] | 298 | print('Unknown test type {}. Supported: queries, metircs'.format( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 299 | args.test_type)) |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 300 | return 1 |
| 301 | |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 302 | index_dir = os.path.dirname(index) |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 303 | with open(index, 'r') as file: |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 304 | index_lines = file.readlines() |
| 305 | |
Lalit Maganti | dfe69ca | 2018-10-30 12:18:23 +0000 | [diff] [blame] | 306 | if args.trace_descriptor: |
| 307 | trace_descriptor_path = args.trace_descriptor |
| 308 | else: |
| 309 | out_path = os.path.dirname(args.trace_processor) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 310 | trace_protos_path = os.path.join(out_path, 'gen', 'protos', 'perfetto', |
| 311 | 'trace') |
Lalit Maganti | 46a0a53 | 2019-07-15 15:09:22 +0100 | [diff] [blame] | 312 | trace_descriptor_path = os.path.join(trace_protos_path, 'trace.descriptor') |
Lalit Maganti | dfe69ca | 2018-10-30 12:18:23 +0000 | [diff] [blame] | 313 | |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 314 | if args.metrics_descriptor: |
| 315 | metrics_descriptor_path = args.metrics_descriptor |
| 316 | else: |
| 317 | out_path = os.path.dirname(args.trace_processor) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 318 | metrics_protos_path = os.path.join(out_path, 'gen', 'protos', 'perfetto', |
| 319 | 'metrics') |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 320 | metrics_descriptor_path = os.path.join(metrics_protos_path, |
Lalit Maganti | 46a0a53 | 2019-07-15 15:09:22 +0100 | [diff] [blame] | 321 | 'metrics.descriptor') |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 322 | |
| 323 | metrics_message_factory = create_metrics_message_factory( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 324 | metrics_descriptor_path) |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 325 | |
Lalit Maganti | 4be82e1 | 2019-12-05 15:33:09 +0000 | [diff] [blame^] | 326 | query_metric_pattern = re.compile(args.query_metric_filter) |
| 327 | trace_pattern = re.compile(args.trace_filter) |
| 328 | |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 329 | tests = [] |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 330 | for line in index_lines: |
Lalit Maganti | cc67422 | 2019-01-23 17:54:00 +0000 | [diff] [blame] | 331 | stripped = line.strip() |
| 332 | if stripped.startswith('#'): |
| 333 | continue |
Lalit Maganti | 6cf6edb | 2019-02-07 11:09:47 +0000 | [diff] [blame] | 334 | elif not stripped: |
| 335 | continue |
Lalit Maganti | cc67422 | 2019-01-23 17:54:00 +0000 | [diff] [blame] | 336 | |
Lalit Maganti | da1c1d0 | 2019-05-08 11:55:50 +0100 | [diff] [blame] | 337 | [trace_fname, query_fname_or_metric, expected_fname] = stripped.split(' ') |
Lalit Maganti | 4be82e1 | 2019-12-05 15:33:09 +0000 | [diff] [blame^] | 338 | if not query_metric_pattern.match(os.path.basename(query_fname_or_metric)): |
| 339 | continue |
| 340 | |
| 341 | if not trace_pattern.match(os.path.basename(trace_fname)): |
| 342 | continue |
| 343 | |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 344 | tests.append(Test(trace_fname, query_fname_or_metric, expected_fname)) |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 345 | |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 346 | sys.stderr.write('[==========] Running {} tests.\n'.format(len(tests))) |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 347 | |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 348 | test_run_start = datetime.datetime.now() |
| 349 | test_failure, perf_data = run_all_tests(args, test_dir, index_dir, |
| 350 | trace_descriptor_path, |
| 351 | metrics_message_factory, tests) |
| 352 | test_run_end = datetime.datetime.now() |
Hector Dearman | f1f51a3 | 2018-10-22 11:09:41 +0100 | [diff] [blame] | 353 | |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 354 | sys.stderr.write('[==========] {} tests ran. ({} ms total)\n'.format( |
| 355 | len(tests), int((test_run_end - test_run_start).total_seconds() * 1000))) |
| 356 | sys.stderr.write('[ PASSED ] {} tests.\n'.format(len(tests) - test_failure)) |
Lalit Maganti | 04a450a | 2019-07-03 16:04:19 +0100 | [diff] [blame] | 357 | |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 358 | if test_failure == 0: |
Lalit Maganti | 75869e6 | 2019-07-12 16:55:38 +0100 | [diff] [blame] | 359 | if args.perf_file: |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 360 | metrics = [[{ |
| 361 | 'metric': 'tp_perf_test_ingest_time', |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 362 | 'value': float(perf_args.ingest_time_ns) / 1.0e9, |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 363 | 'unit': 's', |
| 364 | 'tags': { |
| 365 | 'test_name': |
| 366 | '{}-{}'.format(perf_args.trace_name, |
| 367 | perf_args.query_or_metric), |
| 368 | 'test_type': |
| 369 | args.test_type, |
Lalit Maganti | c8d1d2c | 2019-08-12 16:10:45 +0100 | [diff] [blame] | 370 | }, |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 371 | 'labels': {}, |
| 372 | }, |
| 373 | { |
| 374 | 'metric': 'perf_test_real_time', |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 375 | 'value': float(perf_args.real_time_ns) / 1.0e9, |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 376 | 'unit': 's', |
| 377 | 'tags': { |
| 378 | 'test_name': |
| 379 | '{}-{}'.format(perf_args.trace_name, |
| 380 | perf_args.query_or_metric), |
| 381 | 'test_type': |
| 382 | args.test_type, |
| 383 | }, |
| 384 | 'labels': {}, |
| 385 | }] for perf_args in sorted(perf_data)] |
| 386 | output_data = {'metrics': list(chain.from_iterable(metrics))} |
Lalit Maganti | 75869e6 | 2019-07-12 16:55:38 +0100 | [diff] [blame] | 387 | with open(args.perf_file, 'w+') as perf_file: |
Lalit Maganti | 46a0a53 | 2019-07-15 15:09:22 +0100 | [diff] [blame] | 388 | perf_file.write(json.dumps(output_data, indent=2)) |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 389 | return 0 |
| 390 | else: |
Lalit Maganti | 467a8ef | 2019-12-05 15:54:20 +0000 | [diff] [blame] | 391 | sys.stderr.write('[ FAILED ] {} tests.\n'.format(test_failure)) |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 392 | return 1 |
| 393 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 394 | |
Lalit Maganti | 21160e8 | 2018-10-16 09:40:29 +0100 | [diff] [blame] | 395 | if __name__ == '__main__': |
| 396 | sys.exit(main()) |