OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 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 |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/base/cpu_time.h" |
| 12 #include "webrtc/base/platform_thread.h" |
| 13 #include "webrtc/base/timeutils.h" |
| 14 #include "webrtc/test/gtest.h" |
| 15 |
| 16 namespace internal { |
| 17 const double kAllowedErrorSec = 0.1; |
| 18 const int kProcessingTimeSec = 1; |
| 19 |
| 20 bool WaitingFunction(void* counter_pointer) { |
| 21 int64_t& counter = *reinterpret_cast<int64_t*>(counter_pointer); |
| 22 counter = 0; |
| 23 int64_t stop_time = |
| 24 rtc::SystemTimeNanos() + kProcessingTimeSec * rtc::kNumNanosecsPerSec; |
| 25 while (rtc::SystemTimeNanos() < stop_time) { |
| 26 counter++; |
| 27 } |
| 28 return true; |
| 29 } |
| 30 } // namespace internal |
| 31 |
| 32 namespace rtc { |
| 33 |
| 34 TEST(GetCpuTimeTest, SingleThread) { |
| 35 int64_t start = GetCpuTime(); |
| 36 int64_t* counter = new int64_t; |
| 37 ::internal::WaitingFunction(reinterpret_cast<void*>(counter)); |
| 38 EXPECT_GT(*counter, 0); |
| 39 delete counter; |
| 40 int64_t duration = GetCpuTime() - start; |
| 41 EXPECT_GE(static_cast<double>(duration) / kNumNanosecsPerSec, |
| 42 ::internal::kProcessingTimeSec - ::internal::kAllowedErrorSec); |
| 43 EXPECT_LE(static_cast<double>(duration) / kNumNanosecsPerSec, |
| 44 ::internal::kProcessingTimeSec + ::internal::kAllowedErrorSec); |
| 45 } |
| 46 |
| 47 TEST(GetCpuTimeTest, TwoThreads) { |
| 48 int64_t start = GetCpuTime(); |
| 49 int64_t* counter1 = new int64_t; |
| 50 int64_t* counter2 = new int64_t; |
| 51 PlatformThread thread1(::internal::WaitingFunction, |
| 52 reinterpret_cast<void*>(counter1), "Thread1"); |
| 53 PlatformThread thread2(::internal::WaitingFunction, |
| 54 reinterpret_cast<void*>(counter2), "Thread2"); |
| 55 thread1.Start(); |
| 56 thread2.Start(); |
| 57 thread1.Stop(); |
| 58 thread2.Stop(); |
| 59 |
| 60 EXPECT_GE(*counter1, 0); |
| 61 EXPECT_GE(*counter2, 0); |
| 62 delete counter1; |
| 63 delete counter2; |
| 64 int64_t duration = GetCpuTime() - start; |
| 65 EXPECT_GE(static_cast<double>(duration) / kNumNanosecsPerSec, |
| 66 2 * ::internal::kProcessingTimeSec - ::internal::kAllowedErrorSec); |
| 67 // If .Stop() also consumes CPU time, there will be 3 active threads. |
| 68 EXPECT_LE(static_cast<double>(duration) / kNumNanosecsPerSec, |
| 69 3 * ::internal::kProcessingTimeSec + ::internal::kAllowedErrorSec); |
| 70 } |
| 71 |
| 72 } // namespace rtc |
OLD | NEW |