Fixed cpu time unittest to be less flaky
BUG=webrtc:7095
Review-Url: https://codereview.webrtc.org/2725553002
Cr-Commit-Position: refs/heads/master@{#16897}
diff --git a/webrtc/base/cpu_time_unittest.cc b/webrtc/base/cpu_time_unittest.cc
index cb025ea..6a82cfd 100644
--- a/webrtc/base/cpu_time_unittest.cc
+++ b/webrtc/base/cpu_time_unittest.cc
@@ -15,18 +15,21 @@
#include "webrtc/base/timeutils.h"
#include "webrtc/test/gtest.h"
#include "webrtc/system_wrappers/include/cpu_info.h"
+#include "webrtc/system_wrappers/include/sleep.h"
namespace {
const int kAllowedErrorMillisecs = 30;
const int kProcessingTimeMillisecs = 300;
+const int kWorkingThreads = 2;
-// Consumes approximately kProcessingTimeMillisecs of CPU time.
+// Consumes approximately kProcessingTimeMillisecs of CPU time in single thread.
bool WorkingFunction(void* counter_pointer) {
int64_t* counter = reinterpret_cast<int64_t*>(counter_pointer);
*counter = 0;
- int64_t stop_time = rtc::SystemTimeNanos() +
- kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec;
- while (rtc::SystemTimeNanos() < stop_time) {
+ int64_t stop_cpu_time =
+ rtc::GetThreadCpuTimeNanos() +
+ kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec;
+ while (rtc::GetThreadCpuTimeNanos() < stop_cpu_time) {
(*counter)++;
}
return false;
@@ -35,20 +38,9 @@
namespace rtc {
-TEST(GetProcessCpuTimeTest, SingleThread) {
- int64_t start_time_nanos = GetProcessCpuTimeNanos();
- int64_t counter;
- WorkingFunction(reinterpret_cast<void*>(&counter));
- EXPECT_GT(counter, 0);
- int64_t duration_nanos = GetProcessCpuTimeNanos() - start_time_nanos;
- // Should be about kProcessingTimeMillisecs.
- EXPECT_NEAR(duration_nanos,
- kProcessingTimeMillisecs * kNumNanosecsPerMillisec,
- kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
-}
-
-TEST(GetProcessCpuTimeTest, TwoThreads) {
- int64_t start_time_nanos = GetProcessCpuTimeNanos();
+TEST(CpuTimeTest, TwoThreads) {
+ int64_t process_start_time_nanos = GetProcessCpuTimeNanos();
+ int64_t thread_start_time_nanos = GetThreadCpuTimeNanos();
int64_t counter1;
int64_t counter2;
PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1),
@@ -62,47 +54,31 @@
EXPECT_GE(counter1, 0);
EXPECT_GE(counter2, 0);
- int64_t duration_nanos = GetProcessCpuTimeNanos() - start_time_nanos;
- const uint32_t kWorkingThreads = 2;
- uint32_t used_cores =
- std::min(webrtc::CpuInfo::DetectNumberOfCores(), kWorkingThreads);
- // Two working threads for kProcessingTimeMillisecs consume double CPU time
- // if there are at least 2 cores.
- EXPECT_NEAR(duration_nanos,
- used_cores * kProcessingTimeMillisecs * kNumNanosecsPerMillisec,
- used_cores * kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
+ int64_t process_duration_nanos =
+ GetProcessCpuTimeNanos() - process_start_time_nanos;
+ int64_t thread_duration_nanos =
+ GetThreadCpuTimeNanos() - thread_start_time_nanos;
+ // This thread did almost nothing.
+ // Therefore GetThreadCpuTime is not a wall clock.
+ EXPECT_LE(thread_duration_nanos,
+ kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
+ // Total process time is twice working threads' CPU time.
+ // Therefore process and thread times are correctly related.
+ EXPECT_NEAR(
+ process_duration_nanos,
+ kWorkingThreads * kProcessingTimeMillisecs * kNumNanosecsPerMillisec,
+ kWorkingThreads * kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
}
-TEST(GetThreadCpuTimeTest, SingleThread) {
- int64_t start_times_nanos = GetThreadCpuTimeNanos();
- int64_t counter;
- WorkingFunction(reinterpret_cast<void*>(&counter));
- EXPECT_GT(counter, 0);
- int64_t duration_nanos = GetThreadCpuTimeNanos() - start_times_nanos;
- EXPECT_NEAR(duration_nanos,
- kProcessingTimeMillisecs * kNumNanosecsPerMillisec,
- kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
-}
-
-TEST(GetThreadCpuTimeTest, TwoThreads) {
- int64_t start_time_nanos = GetThreadCpuTimeNanos();
- int64_t counter1;
- int64_t counter2;
- PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1),
- "Thread1");
- PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2),
- "Thread2");
- thread1.Start();
- thread2.Start();
- thread1.Stop();
- thread2.Stop();
-
- EXPECT_GE(counter1, 0);
- EXPECT_GE(counter2, 0);
- int64_t duration_nanos = GetThreadCpuTimeNanos() - start_time_nanos;
- // This thread didn't do any work.
- EXPECT_NEAR(duration_nanos, 0,
- kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
+TEST(CpuTimeTest, Sleeping) {
+ int64_t process_start_time_nanos = GetProcessCpuTimeNanos();
+ webrtc::SleepMs(kProcessingTimeMillisecs);
+ int64_t process_duration_nanos =
+ GetProcessCpuTimeNanos() - process_start_time_nanos;
+ // Sleeping should not introduce any additional CPU time.
+ // Therefore GetProcessCpuTime is not a wall clock.
+ EXPECT_LE(process_duration_nanos,
+ kWorkingThreads * kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
}
} // namespace rtc