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, BinaryProto 18from python.generators.diff_tests.testing import DiffTestBlueprint 19from python.generators.diff_tests.testing import TestSuite 20from google.protobuf import text_format 21 22 23class Timestamps(TestSuite): 24 25 def test_to_time(self): 26 return DiffTestBlueprint( 27 trace=TextProto(""), 28 query=""" 29 INCLUDE PERFETTO MODULE time.conversion; 30 31 WITH data(unit, time) AS ( 32 VALUES 33 ('ns', time_to_ns(cast_int!(1e14))), 34 ('us', time_to_us(cast_int!(1e14))), 35 ('ms', time_to_ms(cast_int!(1e14))), 36 ('s', time_to_s(cast_int!(1e14))), 37 ('min', time_to_min(cast_int!(1e14))), 38 ('h', time_to_hours(cast_int!(1e14))), 39 ('days', time_to_days(cast_int!(1e14))) 40 ) 41 SELECT * FROM data 42 """, 43 out=Csv(""" 44 "unit","time" 45 "ns",100000000000000 46 "us",100000000000 47 "ms",100000000 48 "s",100000 49 "min",1666 50 "h",27 51 "days",1 52 """)) 53 54 def test_from_time(self): 55 return DiffTestBlueprint( 56 trace=TextProto(""), 57 query=""" 58 INCLUDE PERFETTO MODULE time.conversion; 59 60 WITH data(unit, time) AS ( 61 VALUES 62 ('ns', time_from_ns(1)), 63 ('us', time_from_us(1)), 64 ('ms', time_from_ms(1)), 65 ('s', time_from_s(1)), 66 ('min', time_from_min(1)), 67 ('h', time_from_hours(1)), 68 ('days', time_from_days(1)) 69 ) 70 SELECT * FROM data 71 """, 72 out=Csv(""" 73 "unit","time" 74 "ns",1 75 "us",1000 76 "ms",1000000 77 "s",1000000000 78 "min",60000000000 79 "h",3600000000000 80 "days",86400000000000 81 """))