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 StdlibIntervals(TestSuite): 23 24 def test_intervals_overlap_count(self): 25 return DiffTestBlueprint( 26 trace=TextProto(""), 27 query=""" 28 INCLUDE PERFETTO MODULE intervals.overlap; 29 30 WITH data(ts, dur) AS ( 31 VALUES 32 (10, 40), 33 (20, 10), 34 (25, 10), 35 (60, 10), 36 (70, 20), 37 (80, -1) 38 ) 39 SELECT * 40 FROM intervals_overlap_count!(data, ts, dur) 41 """, 42 out=Csv(""" 43 "ts","value" 44 10,1 45 20,2 46 25,3 47 30,2 48 35,1 49 50,0 50 60,1 51 70,1 52 80,2 53 90,1 54 """)) 55 56 def test_intervals_overlap_in_table(self): 57 return DiffTestBlueprint( 58 trace=TextProto(""), 59 query=""" 60 INCLUDE PERFETTO MODULE intervals.overlap; 61 62 WITH data_no_overlaps(ts, dur) AS ( 63 VALUES 64 (10, 10), 65 (30, 10) 66 ), 67 data_with_overlaps(ts, dur) AS ( 68 VALUES 69 (10, 10), 70 (15, 10) 71 ) 72 SELECT * FROM ( 73 SELECT * 74 FROM _intervals_overlap_in_table!(data_no_overlaps) 75 UNION 76 SELECT * 77 FROM _intervals_overlap_in_table!(data_with_overlaps) 78 ) 79 """, 80 out=Csv(""" 81 "has_overlaps" 82 0 83 1 84 """)) 85 86 def test_intervals_flatten(self): 87 return DiffTestBlueprint( 88 trace=TextProto(""), 89 query=""" 90 INCLUDE PERFETTO MODULE intervals.overlap; 91 92 WITH roots_data (id, ts, dur) AS ( 93 VALUES 94 (0, 0, 7), 95 (1, 8, 1) 96 ), children_data (root_id, id, parent_id, ts, dur) AS ( 97 VALUES 98 (0, 2, 0, 1, 3), 99 (0, 3, 0, 5, 1), 100 (0, 4, 2, 2, 1) 101 ) 102 SELECT ts, dur, id, parent_id, root_id 103 FROM _intervals_flatten!(roots_data, children_data) ORDER BY ts 104 """, 105 out=Csv(""" 106 "ts","dur","id","parent_id","root_id" 107 0,1,0,"[NULL]",0 108 1,1,2,0,0 109 2,1,4,2,0 110 3,1,2,0,0 111 4,1,0,"[NULL]",0 112 5,1,3,0,0 113 6,1,0,"[NULL]",0 114 8,1,1,"[NULL]",1 115 """)) 116