OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include <memory> | |
12 #include <algorithm> | |
13 #include "webrtc/base/cpu_time.h" | |
14 #include "webrtc/base/platform_thread.h" | |
15 #include "webrtc/base/timeutils.h" | |
16 #include "webrtc/test/gtest.h" | |
17 #include "webrtc/system_wrappers/include/cpu_info.h" | |
18 | |
19 namespace { | |
20 const int kAllowedErrorMillisecs = 30; | |
21 const int kProcessingTimeMillisecs = 300; | |
22 | |
23 // Consumes approximately kProcessingTimeMillisecs of CPU time. | |
24 bool WorkingFunction(void* counter_pointer) { | |
25 int64_t* counter = reinterpret_cast<int64_t*>(counter_pointer); | |
26 *counter = 0; | |
27 int64_t stop_time = rtc::SystemTimeNanos() + | |
28 kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec; | |
29 while (rtc::SystemTimeNanos() < stop_time) { | |
30 (*counter)++; | |
31 } | |
32 return false; | |
33 } | |
34 } // namespace | |
35 | |
36 namespace rtc { | |
37 | |
38 TEST(GetProcessCpuTimeTest, SingleThread) { | |
39 int64_t start_time_nanos = GetProcessCpuTimeNanos(); | |
40 int64_t counter; | |
41 WorkingFunction(reinterpret_cast<void*>(&counter)); | |
42 EXPECT_GT(counter, 0); | |
43 int64_t duration_nanos = GetProcessCpuTimeNanos() - start_time_nanos; | |
44 // Should be about kProcessingTimeMillisecs. | |
45 EXPECT_NEAR(duration_nanos, | |
46 kProcessingTimeMillisecs * kNumNanosecsPerMillisec, | |
47 kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | |
48 } | |
49 | |
50 TEST(GetProcessCpuTimeTest, TwoThreads) { | |
51 int64_t start_time_nanos = GetProcessCpuTimeNanos(); | |
52 int64_t counter1; | |
53 int64_t counter2; | |
54 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1), | |
55 "Thread1"); | |
56 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2), | |
57 "Thread2"); | |
58 thread1.Start(); | |
59 thread2.Start(); | |
60 thread1.Stop(); | |
61 thread2.Stop(); | |
62 | |
63 EXPECT_GE(counter1, 0); | |
64 EXPECT_GE(counter2, 0); | |
65 int64_t duration_nanos = GetProcessCpuTimeNanos() - start_time_nanos; | |
66 const uint32_t kWorkingThreads = 2; | |
67 uint32_t used_cores = | |
68 std::min(webrtc::CpuInfo::DetectNumberOfCores(), kWorkingThreads); | |
69 // Two working threads for kProcessingTimeMillisecs consume double CPU time | |
70 // if there are at least 2 cores. | |
71 EXPECT_NEAR(duration_nanos, | |
72 used_cores * kProcessingTimeMillisecs * kNumNanosecsPerMillisec, | |
73 used_cores * kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | |
74 } | |
75 | |
76 TEST(GetThreadCpuTimeTest, SingleThread) { | |
77 int64_t start_times_nanos = GetThreadCpuTimeNanos(); | |
78 int64_t counter; | |
79 WorkingFunction(reinterpret_cast<void*>(&counter)); | |
80 EXPECT_GT(counter, 0); | |
81 int64_t duration_nanos = GetThreadCpuTimeNanos() - start_times_nanos; | |
82 EXPECT_NEAR(duration_nanos, | |
83 kProcessingTimeMillisecs * kNumNanosecsPerMillisec, | |
84 kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | |
85 } | |
86 | |
87 TEST(GetThreadCpuTimeTest, TwoThreads) { | |
88 int64_t start_time_nanos = GetThreadCpuTimeNanos(); | |
89 int64_t counter1; | |
90 int64_t counter2; | |
91 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1), | |
92 "Thread1"); | |
93 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2), | |
94 "Thread2"); | |
95 thread1.Start(); | |
96 thread2.Start(); | |
97 thread1.Stop(); | |
98 thread2.Stop(); | |
99 | |
100 EXPECT_GE(counter1, 0); | |
101 EXPECT_GE(counter2, 0); | |
102 int64_t duration_nanos = GetThreadCpuTimeNanos() - start_time_nanos; | |
103 // This thread didn't do any work. | |
104 EXPECT_NEAR(duration_nanos, 0, | |
105 kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | |
106 } | |
107 | |
108 } // namespace rtc | |
OLD | NEW |