Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: webrtc/base/cpu_time_unittest.cc

Issue 2725553002: Fixed cpu time unittest to be less flaky (Closed)
Patch Set: Implemented Deadbeef@ comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 19
19 namespace { 20 namespace {
20 const int kAllowedErrorMillisecs = 30; 21 const int kAllowedErrorMillisecs = 30;
21 const int kProcessingTimeMillisecs = 300; 22 const int kProcessingTimeMillisecs = 300;
23 const int kWorkingThreads = 2;
22 24
23 // Consumes approximately kProcessingTimeMillisecs of CPU time. 25 // Consumes approximately kProcessingTimeMillisecs of CPU time in single thread.
24 bool WorkingFunction(void* counter_pointer) { 26 bool WorkingFunction(void* counter_pointer) {
25 int64_t* counter = reinterpret_cast<int64_t*>(counter_pointer); 27 int64_t* counter = reinterpret_cast<int64_t*>(counter_pointer);
26 *counter = 0; 28 *counter = 0;
27 int64_t stop_time = rtc::SystemTimeNanos() + 29 int64_t stop_cpu_time =
28 kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec; 30 rtc::GetThreadCpuTimeNanos() +
29 while (rtc::SystemTimeNanos() < stop_time) { 31 kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec;
32 while (rtc::GetThreadCpuTimeNanos() < stop_cpu_time) {
30 (*counter)++; 33 (*counter)++;
31 } 34 }
32 return false; 35 return false;
33 } 36 }
34 } // namespace 37 } // namespace
35 38
36 namespace rtc { 39 namespace rtc {
37 40
38 TEST(GetProcessCpuTimeTest, SingleThread) { 41 TEST(CpuTimeTest, TwoThreads) {
39 int64_t start_time_nanos = GetProcessCpuTimeNanos(); 42 int64_t process_start_time_nanos = GetProcessCpuTimeNanos();
40 int64_t counter; 43 int64_t thread_start_time_nanos = GetThreadCpuTimeNanos();
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; 44 int64_t counter1;
53 int64_t counter2; 45 int64_t counter2;
54 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1), 46 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1),
55 "Thread1"); 47 "Thread1");
56 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2), 48 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2),
57 "Thread2"); 49 "Thread2");
58 thread1.Start(); 50 thread1.Start();
59 thread2.Start(); 51 thread2.Start();
60 thread1.Stop(); 52 thread1.Stop();
61 thread2.Stop(); 53 thread2.Stop();
62 54
63 EXPECT_GE(counter1, 0); 55 EXPECT_GE(counter1, 0);
64 EXPECT_GE(counter2, 0); 56 EXPECT_GE(counter2, 0);
65 int64_t duration_nanos = GetProcessCpuTimeNanos() - start_time_nanos; 57 int64_t process_duration_nanos =
66 const uint32_t kWorkingThreads = 2; 58 GetProcessCpuTimeNanos() - process_start_time_nanos;
67 uint32_t used_cores = 59 int64_t thread_duration_nanos =
68 std::min(webrtc::CpuInfo::DetectNumberOfCores(), kWorkingThreads); 60 GetThreadCpuTimeNanos() - thread_start_time_nanos;
69 // Two working threads for kProcessingTimeMillisecs consume double CPU time 61 // This thread did almost nothing.
70 // if there are at least 2 cores. 62 // Therefore GetThreadCpuTime is not a wall clock.
71 EXPECT_NEAR(duration_nanos, 63 EXPECT_LE(thread_duration_nanos,
72 used_cores * kProcessingTimeMillisecs * kNumNanosecsPerMillisec, 64 kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
73 used_cores * kAllowedErrorMillisecs * kNumNanosecsPerMillisec); 65 // Total process time is twice working threads' CPU time.
66 // Therefore process and thread times are correctly related.
67 EXPECT_NEAR(
68 process_duration_nanos,
69 kWorkingThreads * kProcessingTimeMillisecs * kNumNanosecsPerMillisec,
70 kWorkingThreads * kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
74 } 71 }
75 72
76 TEST(GetThreadCpuTimeTest, SingleThread) { 73 TEST(CpuTimeTest, Sleeping) {
77 int64_t start_times_nanos = GetThreadCpuTimeNanos(); 74 int64_t process_start_time_nanos = GetProcessCpuTimeNanos();
78 int64_t counter; 75 webrtc::SleepMs(kProcessingTimeMillisecs);
79 WorkingFunction(reinterpret_cast<void*>(&counter)); 76 int64_t process_duration_nanos =
80 EXPECT_GT(counter, 0); 77 GetProcessCpuTimeNanos() - process_start_time_nanos;
81 int64_t duration_nanos = GetThreadCpuTimeNanos() - start_times_nanos; 78 // Sleeping should not introduce any additional CPU time.
82 EXPECT_NEAR(duration_nanos, 79 // Therefore GetProcessCpuTime is not a wall clock.
83 kProcessingTimeMillisecs * kNumNanosecsPerMillisec, 80 EXPECT_LE(process_duration_nanos,
84 kAllowedErrorMillisecs * kNumNanosecsPerMillisec); 81 kWorkingThreads * 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 } 82 }
107 83
108 } // namespace rtc 84 } // namespace rtc
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698