OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <algorithm> | 12 #include <algorithm> |
13 #include "webrtc/base/cpu_time.h" | 13 #include "webrtc/base/cpu_time.h" |
14 #include "webrtc/base/platform_thread.h" | 14 #include "webrtc/base/platform_thread.h" |
15 #include "webrtc/base/timeutils.h" | 15 #include "webrtc/base/timeutils.h" |
16 #include "webrtc/test/gtest.h" | 16 #include "webrtc/test/gtest.h" |
17 #include "webrtc/system_wrappers/include/cpu_info.h" | 17 #include "webrtc/system_wrappers/include/cpu_info.h" |
18 #include "webrtc/system_wrappers/include/sleep.h" | 18 #include "webrtc/system_wrappers/include/sleep.h" |
19 | 19 |
20 // Only run these tests on non-instrumented builds, because timing on | |
21 // instrumented builds is unreliable, causing the test to be flaky. | |
22 #if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \ | |
23 defined(ADDRESS_SANITIZER) | |
24 #define MAYBE_TEST(test_name) DISABLED_##test_name | |
25 #else | |
26 #define MAYBE_TEST(test_name) test_name | |
27 #endif | |
28 | |
20 namespace { | 29 namespace { |
21 const int kAllowedErrorMillisecs = 30; | 30 const int kAllowedErrorMillisecs = 30; |
22 const int kProcessingTimeMillisecs = 300; | 31 const int kProcessingTimeMillisecs = 300; |
23 const int kWorkingThreads = 2; | 32 const int kWorkingThreads = 2; |
24 | 33 |
25 // Consumes approximately kProcessingTimeMillisecs of CPU time in single thread. | 34 // Consumes approximately kProcessingTimeMillisecs of CPU time in single thread. |
26 bool WorkingFunction(void* counter_pointer) { | 35 bool WorkingFunction(void* counter_pointer) { |
27 int64_t* counter = reinterpret_cast<int64_t*>(counter_pointer); | 36 int64_t* counter = reinterpret_cast<int64_t*>(counter_pointer); |
28 *counter = 0; | 37 *counter = 0; |
29 int64_t stop_cpu_time = | 38 int64_t stop_cpu_time = |
30 rtc::GetThreadCpuTimeNanos() + | 39 rtc::GetThreadCpuTimeNanos() + |
31 kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec; | 40 kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec; |
32 while (rtc::GetThreadCpuTimeNanos() < stop_cpu_time) { | 41 while (rtc::GetThreadCpuTimeNanos() < stop_cpu_time) { |
33 (*counter)++; | 42 (*counter)++; |
34 } | 43 } |
35 return false; | 44 return false; |
36 } | 45 } |
37 } // namespace | 46 } // namespace |
38 | 47 |
39 namespace rtc { | 48 namespace rtc { |
40 | 49 |
41 TEST(CpuTimeTest, TwoThreads) { | 50 // A minimal test which can be run on instrumented builds, so that they're at |
51 // least exercising the code to check for memory leaks/etc. | |
52 TEST(CpuTimeTest, BasicTest) { | |
53 int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); | |
54 int64_t thread_start_time_nanos = GetThreadCpuTimeNanos(); | |
55 int64_t process_duration_nanos = | |
56 GetProcessCpuTimeNanos() - process_start_time_nanos; | |
57 int64_t thread_duration_nanos = | |
58 GetThreadCpuTimeNanos() - thread_start_time_nanos; | |
59 EXPECT_GT(process_duration_nanos, 0); | |
60 EXPECT_GT(thread_duration_nanos, 0); | |
ilnik
2017/05/01 11:16:19
nit: maybe use EXPECT_GE here?
Taylor Brandstetter
2017/05/01 17:15:29
Done.
| |
61 } | |
62 | |
63 TEST(CpuTimeTest, MAYBE_TEST(TwoThreads)) { | |
42 int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); | 64 int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); |
43 int64_t thread_start_time_nanos = GetThreadCpuTimeNanos(); | 65 int64_t thread_start_time_nanos = GetThreadCpuTimeNanos(); |
44 int64_t counter1; | 66 int64_t counter1; |
45 int64_t counter2; | 67 int64_t counter2; |
46 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1), | 68 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1), |
47 "Thread1"); | 69 "Thread1"); |
48 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2), | 70 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2), |
49 "Thread2"); | 71 "Thread2"); |
50 thread1.Start(); | 72 thread1.Start(); |
51 thread2.Start(); | 73 thread2.Start(); |
(...skipping 11 matching lines...) Expand all Loading... | |
63 EXPECT_LE(thread_duration_nanos, | 85 EXPECT_LE(thread_duration_nanos, |
64 kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | 86 kAllowedErrorMillisecs * kNumNanosecsPerMillisec); |
65 // Total process time is at least twice working threads' CPU time. | 87 // Total process time is at least twice working threads' CPU time. |
66 // Therefore process and thread times are correctly related. | 88 // Therefore process and thread times are correctly related. |
67 EXPECT_GE( | 89 EXPECT_GE( |
68 process_duration_nanos, | 90 process_duration_nanos, |
69 kWorkingThreads * (kProcessingTimeMillisecs - kAllowedErrorMillisecs) | 91 kWorkingThreads * (kProcessingTimeMillisecs - kAllowedErrorMillisecs) |
70 * kNumNanosecsPerMillisec); | 92 * kNumNanosecsPerMillisec); |
71 } | 93 } |
72 | 94 |
73 TEST(CpuTimeTest, Sleeping) { | 95 TEST(CpuTimeTest, MAYBE_TEST(Sleeping)) { |
74 int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); | 96 int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); |
75 webrtc::SleepMs(kProcessingTimeMillisecs); | 97 webrtc::SleepMs(kProcessingTimeMillisecs); |
76 int64_t process_duration_nanos = | 98 int64_t process_duration_nanos = |
77 GetProcessCpuTimeNanos() - process_start_time_nanos; | 99 GetProcessCpuTimeNanos() - process_start_time_nanos; |
78 // Sleeping should not introduce any additional CPU time. | 100 // Sleeping should not introduce any additional CPU time. |
79 // Therefore GetProcessCpuTime is not a wall clock. | 101 // Therefore GetProcessCpuTime is not a wall clock. |
80 EXPECT_LE(process_duration_nanos, | 102 EXPECT_LE(process_duration_nanos, |
81 kWorkingThreads * kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | 103 kWorkingThreads * kAllowedErrorMillisecs * kNumNanosecsPerMillisec); |
82 } | 104 } |
83 | 105 |
84 } // namespace rtc | 106 } // namespace rtc |
OLD | NEW |