Chromium Code Reviews| Index: webrtc/base/cpu_time_unittest.cc |
| diff --git a/webrtc/base/cpu_time_unittest.cc b/webrtc/base/cpu_time_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8ffacbe99e2c9cf6eb9650bb79b22e37712afca8 |
| --- /dev/null |
| +++ b/webrtc/base/cpu_time_unittest.cc |
| @@ -0,0 +1,100 @@ |
| +/* |
| + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include <memory> |
| + |
| +#include "webrtc/base/cpu_time.h" |
| +#include "webrtc/base/platform_thread.h" |
| +#include "webrtc/base/timeutils.h" |
| +#include "webrtc/test/gtest.h" |
| + |
| +namespace { |
| +const int kAllowedErrorMillisecs = 30; |
| +const int kProcessingTimeMillisecs = 300; |
| + |
| +// Consumes approximately kProcessingTimeMillisecs of CPU time. |
| +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) { |
| + (*counter)++; |
| + } |
| + return false; |
| +} |
| +} // namespace |
| + |
| +namespace rtc { |
| + |
| +TEST(GetProcessCpuTimeTest, SingleThread) { |
| + int64_t start_time_nanos = GetProcessCpuTime(); |
| + std::unique_ptr<int64_t> counter(new int64_t); |
|
Taylor Brandstetter
2017/02/16 18:49:19
Is this because the int64_t needs to be on the hea
ilnik
2017/02/17 09:17:10
I was getting ASAN errors, but it wasn't because c
|
| + WorkingFunction(reinterpret_cast<void*>(counter.get())); |
| + EXPECT_GT(*counter, 0); |
| + int64_t duration_nanos = GetProcessCpuTime() - start_time_nanos; |
| + // Should be about kProcessingTimeMillisecs. |
| + EXPECT_NEAR(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec, |
|
Taylor Brandstetter
2017/02/16 18:49:19
nit: I'd prefer no cast to double
ilnik
2017/02/17 09:17:10
Done.
|
| + kProcessingTimeMillisecs, kAllowedErrorMillisecs); |
| +} |
| + |
| +TEST(GetProcessCpuTimeTest, TwoThreads) { |
| + int64_t start_time_nanos = GetProcessCpuTime(); |
| + std::unique_ptr<int64_t> counter1(new int64_t); |
| + std::unique_ptr<int64_t> counter2(new int64_t); |
| + PlatformThread thread1(WorkingFunction, |
| + reinterpret_cast<void*>(counter1.get()), "Thread1"); |
| + PlatformThread thread2(WorkingFunction, |
| + reinterpret_cast<void*>(counter2.get()), "Thread2"); |
| + thread1.Start(); |
| + thread2.Start(); |
| + thread1.Stop(); |
| + thread2.Stop(); |
| + |
| + EXPECT_GE(*counter1, 0); |
| + EXPECT_GE(*counter2, 0); |
| + int64_t duration_nanos = GetProcessCpuTime() - start_time_nanos; |
| + EXPECT_NEAR(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec, |
| + 2 * kProcessingTimeMillisecs, 2 * kAllowedErrorMillisecs); |
|
Taylor Brandstetter
2017/02/16 18:49:19
This test will fail if the system doesn't have mul
ilnik
2017/02/17 09:17:10
Done.
|
| +} |
| + |
| +TEST(GetThreadCpuTimeTest, SingleThread) { |
| + int64_t start_times_nanos = GetThreadCpuTime(); |
| + std::unique_ptr<int64_t> counter(new int64_t); |
| + WorkingFunction(reinterpret_cast<void*>(counter.get())); |
| + EXPECT_GT(*counter, 0); |
| + int64_t duration_nanos = GetThreadCpuTime() - start_times_nanos; |
| + // Should be about kProcessingTimeMillisecs. |
| + EXPECT_NEAR(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec, |
| + kProcessingTimeMillisecs, kAllowedErrorMillisecs); |
| +} |
| + |
| +TEST(GetThreadCpuTimeTest, TwoThreads) { |
| + int64_t start_time_nanos = GetThreadCpuTime(); |
| + std::unique_ptr<int64_t> counter1(new int64_t); |
| + std::unique_ptr<int64_t> counter2(new int64_t); |
| + PlatformThread thread1(WorkingFunction, |
| + reinterpret_cast<void*>(counter1.get()), "Thread1"); |
| + PlatformThread thread2(WorkingFunction, |
| + reinterpret_cast<void*>(counter2.get()), "Thread2"); |
| + thread1.Start(); |
| + thread2.Start(); |
| + thread1.Stop(); |
| + thread2.Stop(); |
| + |
| + EXPECT_GE(*counter1, 0); |
| + EXPECT_GE(*counter2, 0); |
| + int64_t duration_nanos = GetThreadCpuTime() - start_time_nanos; |
| + // Should be about 0. |
| + EXPECT_NEAR(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec, 0, |
| + kAllowedErrorMillisecs); |
| +} |
| + |
| +} // namespace rtc |