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 { | |
nisse-webrtc
2017/02/15 14:27:35
Just use the unnamed namespace. My understanding i
ilnik
2017/02/15 14:40:45
Done.
| |
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); | |
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:
| |
43 EXPECT_LE(static_cast<double>(duration) / kNumNanosecsPerSec, | |
44 ::internal::kProcessingTimeSec + ::internal::kAllowedErrorSec); | |
45 } | |
46 | |
47 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.
| |
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 |