| 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..f295cf1127746445924e280858328aab6a0eea5d
|
| --- /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 {
|
| +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) {
|
| + double start = CpuTime::GetCpuTime();
|
| + int64_t *counter = new int64_t;
|
| + ::internal::WaitingFunction(reinterpret_cast<void*>(counter));
|
| + EXPECT_GT(*counter, 0);
|
| + delete counter;
|
| + double duration = CpuTime::GetCpuTime() - start;
|
| + EXPECT_GE(duration,
|
| + ::internal::kProcessingTimeSec - ::internal::kAllowedErrorSec);
|
| + EXPECT_LE(duration,
|
| + ::internal::kProcessingTimeSec + ::internal::kAllowedErrorSec);
|
| +}
|
| +
|
| +TEST(GetCpuTimeTest, TwoThreads) {
|
| + double start = CpuTime::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;
|
| + double duration = CpuTime::GetCpuTime() - start;
|
| + EXPECT_GE(duration,
|
| + 2 * ::internal::kProcessingTimeSec - ::internal::kAllowedErrorSec);
|
| + // If .Stop() also consumes CPU time, there will be 3 active threads.
|
| + EXPECT_LE(duration,
|
| + 3 * ::internal::kProcessingTimeSec + ::internal::kAllowedErrorSec);
|
| +}
|
| +
|
| +} // namespace rtc
|
|
|