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..0bde1dfe31428184e4d8d59be94f166a048e3114 |
| --- /dev/null |
| +++ b/webrtc/base/cpu_time_unittest.cc |
| @@ -0,0 +1,72 @@ |
| +/* |
| + * 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 "webrtc/base/cpu_time.h" |
| +#include "webrtc/base/platform_thread.h" |
| +#include "webrtc/base/timeutils.h" |
| +#include "webrtc/test/gtest.h" |
| + |
| +namespace internal { |
|
nisse-webrtc
2017/02/15 14:27:35
Just use the unnamed namespace. My understanding i
ilnik
2017/02/15 14:40:45
Done.
|
| +const double kAllowedErrorSec = 0.1; |
| +const int kProcessingTimeSec = 1; |
| + |
| +bool WaitingFunction(void* counter_pointer) { |
| + int64_t& counter = *reinterpret_cast<int64_t*>(counter_pointer); |
| + counter = 0; |
| + int64_t stop_time = |
| + rtc::SystemTimeNanos() + kProcessingTimeSec * rtc::kNumNanosecsPerSec; |
| + while (rtc::SystemTimeNanos() < stop_time) { |
| + counter++; |
| + } |
| + return true; |
| +} |
| +} // namespace internal |
| + |
| +namespace rtc { |
| + |
| +TEST(GetCpuTimeTest, SingleThread) { |
| + int64_t start = GetCpuTime(); |
| + int64_t* counter = new int64_t; |
| + ::internal::WaitingFunction(reinterpret_cast<void*>(counter)); |
| + EXPECT_GT(*counter, 0); |
| + delete counter; |
| + int64_t duration = GetCpuTime() - start; |
| + EXPECT_GE(static_cast<double>(duration) / kNumNanosecsPerSec, |
| + ::internal::kProcessingTimeSec - ::internal::kAllowedErrorSec); |
|
nisse-webrtc
2017/02/15 14:27:35
Is there anyway you can rearrange this test to not
ilnik
2017/02/15 14:40:45
Unfortunately, these CPU counters are not very pre
nisse-webrtc
2017/02/15 14:48:33
I'm more concerned that tests may be flaky, i.e.,
ilnik
2017/02/15 15:07:10
That's the great thing about CPU time - it should
ilnik
2017/02/15 15:19:16
Also, I just realized, that's the problem with the
nisse-webrtc
2017/02/15 15:34:35
I think you could do a meaningful test like this:
|
| + EXPECT_LE(static_cast<double>(duration) / kNumNanosecsPerSec, |
| + ::internal::kProcessingTimeSec + ::internal::kAllowedErrorSec); |
| +} |
| + |
| +TEST(GetCpuTimeTest, TwoThreads) { |
|
nisse-webrtc
2017/02/15 14:27:35
This makes me wonder what the cputime is going to
ilnik
2017/02/15 14:40:45
I am going to measure total CPU time consumed by t
nisse-webrtc
2017/02/15 14:48:33
I see. Then I have two suggestions:
1. Rename it
ilnik
2017/02/15 15:07:10
1. is GetProcessCpuTime() ok?
2. I could just inc
nisse-webrtc
2017/02/15 15:34:35
I think so.
|
| + int64_t start = GetCpuTime(); |
| + int64_t* counter1 = new int64_t; |
| + int64_t* counter2 = new int64_t; |
| + PlatformThread thread1(::internal::WaitingFunction, |
| + reinterpret_cast<void*>(counter1), "Thread1"); |
| + PlatformThread thread2(::internal::WaitingFunction, |
| + reinterpret_cast<void*>(counter2), "Thread2"); |
| + thread1.Start(); |
| + thread2.Start(); |
| + thread1.Stop(); |
| + thread2.Stop(); |
| + |
| + EXPECT_GE(*counter1, 0); |
| + EXPECT_GE(*counter2, 0); |
| + delete counter1; |
| + delete counter2; |
| + int64_t duration = GetCpuTime() - start; |
| + EXPECT_GE(static_cast<double>(duration) / kNumNanosecsPerSec, |
| + 2 * ::internal::kProcessingTimeSec - ::internal::kAllowedErrorSec); |
| + // If .Stop() also consumes CPU time, there will be 3 active threads. |
| + EXPECT_LE(static_cast<double>(duration) / kNumNanosecsPerSec, |
| + 3 * ::internal::kProcessingTimeSec + ::internal::kAllowedErrorSec); |
| +} |
| + |
| +} // namespace rtc |