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 TablesCounters(TestSuite): 23 # Counters table 24 def test_synth_1_filter_counter(self): 25 return DiffTestBlueprint( 26 trace=Path('../common/synth_1.py'), 27 query=""" 28 SELECT COUNT(*) 29 FROM counter 30 WHERE 31 track_id = 0; 32 """, 33 out=Csv(""" 34 "COUNT(*)" 35 2 36 """)) 37 38 def test_memory_counters_b120278869_neg_ts_end(self): 39 return DiffTestBlueprint( 40 trace=DataPath('memory_counters.pb'), 41 query=""" 42 SELECT count(*) FROM counters WHERE -1 < ts; 43 """, 44 out=Csv(""" 45 "count(*)" 46 98688 47 """)) 48 49 def test_counters_where_cpu_counters_where_cpu(self): 50 return DiffTestBlueprint( 51 trace=Path('counters_where_cpu.py'), 52 query=""" 53 SELECT 54 ts, 55 lead(ts, 1, ts) OVER (PARTITION BY name ORDER BY ts) - ts AS dur, 56 value 57 FROM counter c 58 JOIN cpu_counter_track t ON t.id = c.track_id 59 WHERE cpu = 1; 60 """, 61 out=Csv(""" 62 "ts","dur","value" 63 1000,1,3000.000000 64 1001,0,4000.000000 65 """)) 66 67 def test_counters_group_by_freq_counters_group_by_freq(self): 68 return DiffTestBlueprint( 69 trace=Path('counters_group_by_freq.py'), 70 query=""" 71 SELECT 72 value, 73 sum(dur) AS dur_sum 74 FROM ( 75 SELECT value, 76 lead(ts) OVER (PARTITION BY name, track_id ORDER BY ts) - ts AS dur 77 FROM counter 78 JOIN counter_track ON counter.track_id = counter_track.id 79 ) 80 WHERE value > 0 81 GROUP BY value 82 ORDER BY dur_sum DESC; 83 """, 84 out=Csv(""" 85 "value","dur_sum" 86 4000.000000,2 87 3000.000000,1 88 """)) 89 90 def test_filter_row_vector_example_android_trace_30s(self): 91 return DiffTestBlueprint( 92 trace=DataPath('example_android_trace_30s.pb'), 93 query=""" 94 SELECT ts 95 FROM counter 96 WHERE 97 ts > 72563651549 98 AND track_id = ( 99 SELECT t.id 100 FROM process_counter_track t 101 JOIN process p USING (upid) 102 WHERE 103 t.name = 'Heap size (KB)' 104 AND p.pid = 1204 105 ) 106 AND value != 17952.000000 107 LIMIT 20; 108 """, 109 out=Path('filter_row_vector_example_android_trace_30s.out')) 110 111 def test_counter_dur_example_android_trace_30s(self): 112 return DiffTestBlueprint( 113 trace=DataPath('example_android_trace_30s.pb'), 114 query=Path('counter_dur_test.sql'), 115 out=Csv(""" 116 "ts","dur" 117 100351738640,-1 118 100351738640,-1 119 100351738640,-1 120 70731059648,19510835 121 70731059648,19510835 122 70731059648,19510835 123 73727335051,23522762 124 73727335051,23522762 125 73727335051,23522762 126 86726132752,24487554 127 """)) 128