Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: webrtc/base/cpu_time_unittest.cc

Issue 2695743003: Added GetCpuTime to base/ to get total CPU time consumed by process for perf tests. (Closed)
Patch Set: fixing asan second attempt Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« webrtc/base/cpu_time.cc ('K') | « webrtc/base/cpu_time.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 double start = CpuTime::GetCpuTime();
36 int64_t *counter = new int64_t;
37 ::internal::WaitingFunction(reinterpret_cast<void*>(counter));
38 EXPECT_GT(*counter, 0);
39 delete counter;
40 double duration = CpuTime::GetCpuTime() - start;
41 EXPECT_GE(duration,
42 ::internal::kProcessingTimeSec - ::internal::kAllowedErrorSec);
43 EXPECT_LE(duration,
44 ::internal::kProcessingTimeSec + ::internal::kAllowedErrorSec);
45 }
46
47 TEST(GetCpuTimeTest, TwoThreads) {
48 double start = CpuTime::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 double duration = CpuTime::GetCpuTime() - start;
65 EXPECT_GE(duration,
66 2 * ::internal::kProcessingTimeSec - ::internal::kAllowedErrorSec);
67 // If .Stop() also consumes CPU time, there will be 3 active threads.
68 EXPECT_LE(duration,
69 3 * ::internal::kProcessingTimeSec + ::internal::kAllowedErrorSec);
70 }
71
72 } // namespace rtc
OLDNEW
« webrtc/base/cpu_time.cc ('K') | « webrtc/base/cpu_time.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698