1#!/usr/bin/env python3 2# Copyright (C) 2023 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 a 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 16from python.generators.diff_tests.testing import Path, DataPath, Metric 17from python.generators.diff_tests.testing import Csv, Json, TextProto 18from python.generators.diff_tests.testing import DiffTestBlueprint 19from python.generators.diff_tests.testing import TestSuite 20 21 22class ChromeMetrics(TestSuite): 23 # Tests related to Chrome's use of Perfetto. Chrome histogram hashes 24 def test_chrome_histogram_hashes(self): 25 return DiffTestBlueprint( 26 trace=TextProto(r""" 27 packet { 28 trusted_packet_sequence_id: 1 29 timestamp: 0 30 incremental_state_cleared: true 31 track_event { 32 categories: "cat1" 33 type: 3 34 name_iid: 1 35 chrome_histogram_sample { 36 name_hash: 10 37 sample: 100 38 } 39 } 40 } 41 packet { 42 trusted_packet_sequence_id: 1 43 timestamp: 0 44 incremental_state_cleared: true 45 track_event { 46 categories: "cat2" 47 type: 3 48 name_iid: 2 49 chrome_histogram_sample { 50 name_hash: 20 51 } 52 } 53 } 54 """), 55 query=Metric('chrome_histogram_hashes'), 56 out=TextProto(r""" 57 [perfetto.protos.chrome_histogram_hashes]: { 58 hash: 10 59 hash: 20 60 } 61 """)) 62 63 # Chrome user events 64 def test_chrome_user_event_hashes(self): 65 return DiffTestBlueprint( 66 trace=TextProto(r""" 67 packet { 68 trusted_packet_sequence_id: 1 69 timestamp: 0 70 incremental_state_cleared: true 71 track_event { 72 categories: "cat1" 73 type: 3 74 name_iid: 1 75 chrome_user_event { 76 action_hash: 10 77 } 78 } 79 } 80 packet { 81 trusted_packet_sequence_id: 1 82 timestamp: 0 83 incremental_state_cleared: true 84 track_event { 85 categories: "cat2" 86 type: 3 87 name_iid: 2 88 chrome_user_event { 89 action_hash: 20 90 } 91 } 92 } 93 """), 94 query=Metric('chrome_user_event_hashes'), 95 out=TextProto(r""" 96 [perfetto.protos.chrome_user_event_hashes]: { 97 action_hash: 10 98 action_hash: 20 99 } 100 """)) 101 102 # Chrome performance mark 103 def test_chrome_performance_mark_hashes(self): 104 return DiffTestBlueprint( 105 trace=TextProto(r""" 106 packet { 107 trusted_packet_sequence_id: 1 108 timestamp: 0 109 incremental_state_cleared: true 110 track_event { 111 categories: "cat1" 112 type: 3 113 name: "name1" 114 [perfetto.protos.ChromeTrackEvent.chrome_hashed_performance_mark] { 115 site_hash: 10 116 mark_hash: 100 117 } 118 } 119 } 120 packet { 121 trusted_packet_sequence_id: 1 122 timestamp: 0 123 incremental_state_cleared: true 124 track_event { 125 categories: "cat2" 126 type: 3 127 name: "name2" 128 [perfetto.protos.ChromeTrackEvent.chrome_hashed_performance_mark] { 129 site_hash: 20 130 mark_hash: 200 131 } 132 } 133 } 134 """), 135 query=Metric('chrome_performance_mark_hashes'), 136 out=TextProto(r""" 137 [perfetto.protos.chrome_performance_mark_hashes]: { 138 site_hash: 10 139 site_hash: 20 140 mark_hash: 100 141 mark_hash: 200 142 } 143 """)) 144 145 # Chrome reliable range 146 def test_chrome_reliable_range(self): 147 return DiffTestBlueprint( 148 trace=Path('chrome_reliable_range.textproto'), 149 query=Path('chrome_reliable_range_test.sql'), 150 out=Csv(""" 151 "start","reason","debug_limiting_upid","debug_limiting_utid" 152 12,"First slice for utid=2","[NULL]",2 153 """)) 154 155 def test_chrome_reliable_range_cropping(self): 156 return DiffTestBlueprint( 157 trace=Path('chrome_reliable_range_cropping.textproto'), 158 query=Path('chrome_reliable_range_test.sql'), 159 out=Csv(""" 160 "start","reason","debug_limiting_upid","debug_limiting_utid" 161 10000,"Range of interest packet","[NULL]",2 162 """)) 163 164 def test_chrome_reliable_range_missing_processes(self): 165 return DiffTestBlueprint( 166 trace=Path('chrome_reliable_range_missing_processes.textproto'), 167 query=Path('chrome_reliable_range_test.sql'), 168 out=Csv(""" 169 "start","reason","debug_limiting_upid","debug_limiting_utid" 170 1011,"Missing process data for upid=2",2,1 171 """)) 172 173 def test_chrome_reliable_range_missing_browser_main(self): 174 return DiffTestBlueprint( 175 trace=Path('chrome_reliable_range_missing_browser_main.textproto'), 176 query=Path('chrome_reliable_range_test.sql'), 177 out=Csv(""" 178 "start","reason","debug_limiting_upid","debug_limiting_utid" 179 1011,"Missing main thread for upid=1",1,1 180 """)) 181 182 def test_chrome_reliable_range_missing_gpu_main(self): 183 return DiffTestBlueprint( 184 trace=Path('chrome_reliable_range_missing_gpu_main.textproto'), 185 query=Path('chrome_reliable_range_test.sql'), 186 out=Csv(""" 187 "start","reason","debug_limiting_upid","debug_limiting_utid" 188 1011,"Missing main thread for upid=1",1,1 189 """)) 190 191 def test_chrome_reliable_range_missing_renderer_main(self): 192 return DiffTestBlueprint( 193 trace=Path('chrome_reliable_range_missing_renderer_main.textproto'), 194 query=Path('chrome_reliable_range_test.sql'), 195 out=Csv(""" 196 "start","reason","debug_limiting_upid","debug_limiting_utid" 197 1011,"Missing main thread for upid=1",1,1 198 """)) 199 200 def test_chrome_reliable_range_non_chrome_process(self): 201 return DiffTestBlueprint( 202 # We need a trace with a large number of non-chrome slices, so that the 203 # reliable range is affected by their filtering. 204 trace=DataPath('example_android_trace_30s.pb'), 205 query=Path('chrome_reliable_range_test.sql'), 206 out=Csv(""" 207 "start","reason","debug_limiting_upid","debug_limiting_utid" 208 0,"[NULL]","[NULL]","[NULL]" 209 """)) 210 211 # Chrome slices 212 def test_chrome_slice_names(self): 213 return DiffTestBlueprint( 214 trace=TextProto(r""" 215 packet { 216 trusted_packet_sequence_id: 1 217 timestamp: 1000 218 track_event { 219 categories: "cat" 220 name: "Looper.Dispatch: class1" 221 type: 3 222 } 223 } 224 packet { 225 trusted_packet_sequence_id: 1 226 timestamp: 2000 227 track_event { 228 categories: "cat" 229 name: "name2" 230 type: 3 231 } 232 } 233 packet { 234 chrome_metadata { 235 chrome_version_code: 123 236 } 237 } 238 """), 239 query=Metric('chrome_slice_names'), 240 out=TextProto(r""" 241 [perfetto.protos.chrome_slice_names]: { 242 chrome_version_code: 123 243 slice_name: "Looper.Dispatch: class1" 244 slice_name: "name2" 245 } 246 """)) 247 248 # Chrome stack samples. 249 def test_chrome_stack_samples_for_task(self): 250 return DiffTestBlueprint( 251 trace=DataPath('chrome_stack_traces_symbolized_trace.pftrace'), 252 query=""" 253 SELECT RUN_METRIC('chrome/chrome_stack_samples_for_task.sql', 254 'target_duration_ms', '0.000001', 255 'thread_name', '"CrBrowserMain"', 256 'task_name', '"sendTouchEvent"'); 257 258 SELECT 259 sample.description, 260 sample.ts, 261 sample.depth 262 FROM chrome_stack_samples_for_task sample 263 JOIN ( 264 SELECT 265 ts, 266 dur 267 FROM slice 268 WHERE ts = 696373965001470 269 ) test_slice 270 ON sample.ts >= test_slice.ts 271 AND sample.ts <= test_slice.ts + test_slice.dur 272 ORDER BY sample.ts, sample.depth; 273 """, 274 out=Path('chrome_stack_samples_for_task_test.out')) 275 276 # Trace proto content 277 def test_proto_content(self): 278 return DiffTestBlueprint( 279 trace=DataPath('chrome_scroll_without_vsync.pftrace'), 280 query=Path('proto_content_test.sql'), 281 out=Path('proto_content.out')) 282 283 # TODO(mayzner): Uncomment when it works 284 # def test_proto_content_path(self): 285 # return DiffTestBlueprint( 286 # trace=DataPath('chrome_scroll_without_vsync.pftrace'), 287 # query=Path('proto_content_path_test.sql'), 288 # out=Csv(""" 289 # "total_size","field_type","field_name","parent_id","event_category","event_name" 290 # 137426,"TracePacket","[NULL]","[NULL]","[NULL]","[NULL]" 291 # 59475,"TrackEvent","#track_event",415,"[NULL]","[NULL]" 292 # 37903,"TrackEvent","#track_event",17,"[NULL]","[NULL]" 293 # 35904,"int32","#trusted_uid",17,"[NULL]","[NULL]" 294 # 35705,"TracePacket","[NULL]","[NULL]","input,benchmark","LatencyInfo.Flow" 295 # 29403,"TracePacket","[NULL]","[NULL]","cc,input","[NULL]" 296 # 24703,"ChromeLatencyInfo","#chrome_latency_info",18,"[NULL]","[NULL]" 297 # 22620,"uint64","#time_us",26,"[NULL]","[NULL]" 298 # 18711,"TrackEvent","#track_event",1467,"[NULL]","[NULL]" 299 # 15606,"uint64","#timestamp",17,"[NULL]","[NULL]" 300 # """)) 301