xref: /aosp_15_r20/external/perfetto/src/base/time_unittest.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2017 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 
17 #include "perfetto/base/time.h"
18 
19 #include "perfetto/ext/base/utils.h"
20 #include "test/gtest_and_gmock.h"
21 
22 namespace perfetto {
23 namespace base {
24 namespace {
25 
TEST(TimeTest,Conversions)26 TEST(TimeTest, Conversions) {
27   TimeMillis ms = GetWallTimeMs();
28   TimeNanos ns = GetWallTimeNs();
29   EXPECT_NEAR(static_cast<double>(ms.count()),
30               static_cast<double>(ns.count()) / 1000000, 1000);
31 
32   {
33     struct timespec ts = ToPosixTimespec(TimeMillis(0));
34     EXPECT_EQ(0, ts.tv_sec);
35     EXPECT_EQ(0, ts.tv_nsec);
36   }
37   {
38     struct timespec ts = ToPosixTimespec(TimeMillis(1));
39     EXPECT_EQ(0, ts.tv_sec);
40     EXPECT_EQ(1000000, ts.tv_nsec);
41   }
42   {
43     struct timespec ts = ToPosixTimespec(TimeMillis(12345));
44     EXPECT_EQ(12, ts.tv_sec);
45     EXPECT_EQ(345000000, ts.tv_nsec);
46   }
47   {
48     struct timespec ts = ToPosixTimespec(TimeMillis(1000000000001LL));
49     EXPECT_EQ(1000000000, ts.tv_sec);
50     EXPECT_EQ(1000000, ts.tv_nsec);
51   }
52 }
53 
TEST(TimeTest,GetTime)54 TEST(TimeTest, GetTime) {
55   const auto start_time = GetWallTimeNs();
56   const auto start_cputime = GetThreadCPUTimeNs();
57 
58   const unsigned ns_in_ms = 1000000;
59 
60   for (;;) {
61     auto cur_time = GetWallTimeNs();
62     auto elapsed = cur_time - start_time;
63     // Spin for a little while.
64     if (elapsed > TimeNanos(20 * ns_in_ms))
65       break;
66   }
67 
68   auto end_cputime = GetThreadCPUTimeNs();
69   auto elapsed_cputime = end_cputime - start_cputime;
70   // Check that we're not burning much more CPU time than the length of time
71   // that we spun in the loop. We may burn much less, depending on what else is
72   // happening on the test machine.
73   EXPECT_LE(elapsed_cputime.count(), 50 * ns_in_ms);
74 }
75 
76 // This test can work only on Posix platforms which respect the TZ env var.
77 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||   \
78     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
79     PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
TEST(TimeTest,GetTimezoneOffsetMins)80 TEST(TimeTest, GetTimezoneOffsetMins) {
81   const char* tz = getenv("TZ");
82   std::string tz_save(tz ? tz : "");
83   auto reset_tz_on_exit = OnScopeExit([&] {
84     if (!tz_save.empty())
85       base::SetEnv("TZ", tz_save.c_str());
86   });
87 
88   // Note: the sign is reversed in the semantic of the TZ env var.
89   // UTC+2 means "2 hours to reach UTC", not "2 hours ahead of UTC".
90 
91   base::SetEnv("TZ", "UTC+2");
92   EXPECT_EQ(GetTimezoneOffsetMins(), -2 * 60);
93 
94   base::SetEnv("TZ", "UTC-2");
95   EXPECT_EQ(GetTimezoneOffsetMins(), 2 * 60);
96 
97   base::SetEnv("TZ", "UTC-07:45");
98   EXPECT_EQ(GetTimezoneOffsetMins(), 7 * 60 + 45);
99 }
100 #endif
101 
102 }  // namespace
103 }  // namespace base
104 }  // namespace perfetto
105