xref: /aosp_15_r20/external/webrtc/rtc_base/cpu_time_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/cpu_time.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/platform_thread.h"
14*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/time_utils.h"
15*d9f75844SAndroid Build Coastguard Worker #include "system_wrappers/include/sleep.h"
16*d9f75844SAndroid Build Coastguard Worker #include "test/gtest.h"
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker // Only run these tests on non-instrumented builds, because timing on
19*d9f75844SAndroid Build Coastguard Worker // instrumented builds is unreliable, causing the test to be flaky.
20*d9f75844SAndroid Build Coastguard Worker #if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
21*d9f75844SAndroid Build Coastguard Worker     defined(ADDRESS_SANITIZER)
22*d9f75844SAndroid Build Coastguard Worker #define MAYBE_TEST(test_name) DISABLED_##test_name
23*d9f75844SAndroid Build Coastguard Worker #else
24*d9f75844SAndroid Build Coastguard Worker #define MAYBE_TEST(test_name) test_name
25*d9f75844SAndroid Build Coastguard Worker #endif
26*d9f75844SAndroid Build Coastguard Worker 
27*d9f75844SAndroid Build Coastguard Worker namespace {
28*d9f75844SAndroid Build Coastguard Worker const int kAllowedErrorMillisecs = 30;
29*d9f75844SAndroid Build Coastguard Worker const int kProcessingTimeMillisecs = 500;
30*d9f75844SAndroid Build Coastguard Worker const int kWorkingThreads = 2;
31*d9f75844SAndroid Build Coastguard Worker 
32*d9f75844SAndroid Build Coastguard Worker // Consumes approximately kProcessingTimeMillisecs of CPU time in single thread.
WorkingFunction(int64_t * counter)33*d9f75844SAndroid Build Coastguard Worker void WorkingFunction(int64_t* counter) {
34*d9f75844SAndroid Build Coastguard Worker   *counter = 0;
35*d9f75844SAndroid Build Coastguard Worker   int64_t stop_cpu_time =
36*d9f75844SAndroid Build Coastguard Worker       rtc::GetThreadCpuTimeNanos() +
37*d9f75844SAndroid Build Coastguard Worker       kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec;
38*d9f75844SAndroid Build Coastguard Worker   while (rtc::GetThreadCpuTimeNanos() < stop_cpu_time) {
39*d9f75844SAndroid Build Coastguard Worker     (*counter)++;
40*d9f75844SAndroid Build Coastguard Worker   }
41*d9f75844SAndroid Build Coastguard Worker }
42*d9f75844SAndroid Build Coastguard Worker }  // namespace
43*d9f75844SAndroid Build Coastguard Worker 
44*d9f75844SAndroid Build Coastguard Worker namespace rtc {
45*d9f75844SAndroid Build Coastguard Worker 
46*d9f75844SAndroid Build Coastguard Worker // A minimal test which can be run on instrumented builds, so that they're at
47*d9f75844SAndroid Build Coastguard Worker // least exercising the code to check for memory leaks/etc.
TEST(CpuTimeTest,BasicTest)48*d9f75844SAndroid Build Coastguard Worker TEST(CpuTimeTest, BasicTest) {
49*d9f75844SAndroid Build Coastguard Worker   int64_t process_start_time_nanos = GetProcessCpuTimeNanos();
50*d9f75844SAndroid Build Coastguard Worker   int64_t thread_start_time_nanos = GetThreadCpuTimeNanos();
51*d9f75844SAndroid Build Coastguard Worker   int64_t process_duration_nanos =
52*d9f75844SAndroid Build Coastguard Worker       GetProcessCpuTimeNanos() - process_start_time_nanos;
53*d9f75844SAndroid Build Coastguard Worker   int64_t thread_duration_nanos =
54*d9f75844SAndroid Build Coastguard Worker       GetThreadCpuTimeNanos() - thread_start_time_nanos;
55*d9f75844SAndroid Build Coastguard Worker   EXPECT_GE(process_duration_nanos, 0);
56*d9f75844SAndroid Build Coastguard Worker   EXPECT_GE(thread_duration_nanos, 0);
57*d9f75844SAndroid Build Coastguard Worker }
58*d9f75844SAndroid Build Coastguard Worker 
TEST(CpuTimeTest,MAYBE_TEST (TwoThreads))59*d9f75844SAndroid Build Coastguard Worker TEST(CpuTimeTest, MAYBE_TEST(TwoThreads)) {
60*d9f75844SAndroid Build Coastguard Worker   int64_t process_start_time_nanos = GetProcessCpuTimeNanos();
61*d9f75844SAndroid Build Coastguard Worker   int64_t thread_start_time_nanos = GetThreadCpuTimeNanos();
62*d9f75844SAndroid Build Coastguard Worker   int64_t counter1;
63*d9f75844SAndroid Build Coastguard Worker   int64_t counter2;
64*d9f75844SAndroid Build Coastguard Worker   auto thread1 = PlatformThread::SpawnJoinable(
65*d9f75844SAndroid Build Coastguard Worker       [&counter1] { WorkingFunction(&counter1); }, "Thread1");
66*d9f75844SAndroid Build Coastguard Worker   auto thread2 = PlatformThread::SpawnJoinable(
67*d9f75844SAndroid Build Coastguard Worker       [&counter2] { WorkingFunction(&counter2); }, "Thread2");
68*d9f75844SAndroid Build Coastguard Worker   thread1.Finalize();
69*d9f75844SAndroid Build Coastguard Worker   thread2.Finalize();
70*d9f75844SAndroid Build Coastguard Worker 
71*d9f75844SAndroid Build Coastguard Worker   EXPECT_GE(counter1, 0);
72*d9f75844SAndroid Build Coastguard Worker   EXPECT_GE(counter2, 0);
73*d9f75844SAndroid Build Coastguard Worker   int64_t process_duration_nanos =
74*d9f75844SAndroid Build Coastguard Worker       GetProcessCpuTimeNanos() - process_start_time_nanos;
75*d9f75844SAndroid Build Coastguard Worker   int64_t thread_duration_nanos =
76*d9f75844SAndroid Build Coastguard Worker       GetThreadCpuTimeNanos() - thread_start_time_nanos;
77*d9f75844SAndroid Build Coastguard Worker   // This thread did almost nothing. Definetly less work than kProcessingTime.
78*d9f75844SAndroid Build Coastguard Worker   // Therefore GetThreadCpuTime is not a wall clock.
79*d9f75844SAndroid Build Coastguard Worker   EXPECT_LE(thread_duration_nanos,
80*d9f75844SAndroid Build Coastguard Worker             (kProcessingTimeMillisecs - kAllowedErrorMillisecs) *
81*d9f75844SAndroid Build Coastguard Worker                 kNumNanosecsPerMillisec);
82*d9f75844SAndroid Build Coastguard Worker   // Total process time is at least twice working threads' CPU time.
83*d9f75844SAndroid Build Coastguard Worker   // Therefore process and thread times are correctly related.
84*d9f75844SAndroid Build Coastguard Worker   EXPECT_GE(process_duration_nanos,
85*d9f75844SAndroid Build Coastguard Worker             kWorkingThreads *
86*d9f75844SAndroid Build Coastguard Worker                 (kProcessingTimeMillisecs - kAllowedErrorMillisecs) *
87*d9f75844SAndroid Build Coastguard Worker                 kNumNanosecsPerMillisec);
88*d9f75844SAndroid Build Coastguard Worker }
89*d9f75844SAndroid Build Coastguard Worker 
TEST(CpuTimeTest,MAYBE_TEST (Sleeping))90*d9f75844SAndroid Build Coastguard Worker TEST(CpuTimeTest, MAYBE_TEST(Sleeping)) {
91*d9f75844SAndroid Build Coastguard Worker   int64_t process_start_time_nanos = GetProcessCpuTimeNanos();
92*d9f75844SAndroid Build Coastguard Worker   webrtc::SleepMs(kProcessingTimeMillisecs);
93*d9f75844SAndroid Build Coastguard Worker   int64_t process_duration_nanos =
94*d9f75844SAndroid Build Coastguard Worker       GetProcessCpuTimeNanos() - process_start_time_nanos;
95*d9f75844SAndroid Build Coastguard Worker   // Sleeping should not introduce any additional CPU time.
96*d9f75844SAndroid Build Coastguard Worker   // Therefore GetProcessCpuTime is not a wall clock.
97*d9f75844SAndroid Build Coastguard Worker   EXPECT_LE(process_duration_nanos,
98*d9f75844SAndroid Build Coastguard Worker             (kProcessingTimeMillisecs - kAllowedErrorMillisecs) *
99*d9f75844SAndroid Build Coastguard Worker                 kNumNanosecsPerMillisec);
100*d9f75844SAndroid Build Coastguard Worker }
101*d9f75844SAndroid Build Coastguard Worker 
102*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
103