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

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: refactor tests 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 {
17 const int kAllowedErrorMillisecs = 30;
18 const int kProcessingTimeMillisecs = 300;
19
20 // Consumes approximately kProcessingTimeMillisecs of CPU time.
sprang_webrtc 2017/02/16 12:20:18 nit: formatting
21 bool WorkingFunction(void* counter_pointer) {
22 int64_t& counter = *reinterpret_cast<int64_t*>(counter_pointer);
sprang_webrtc 2017/02/16 12:20:18 Avoid non-const refs, just keep it after casting t
ilnik 2017/02/16 12:38:55 Done.
23 counter = 0;
24 int64_t stop_time = rtc::SystemTimeNanos() +
25 kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec;
26 while (rtc::SystemTimeNanos() < stop_time) {
27 counter++;
28 }
29 return false;
30 }
31 } // namespace
32
33 namespace rtc {
34
35 TEST(GetProcessCpuTimeTest, SingleThread) {
36 int64_t start_time_nanos = GetProcessCpuTime();
37 int64_t* counter = new int64_t;
sprang_webrtc 2017/02/16 12:20:18 No need to heap-allocate this.
ilnik 2017/02/16 12:38:55 _asan tests were mistaking this access for stack b
nisse-webrtc 2017/02/16 12:49:46 Odd. Passing pointers to stack allocated objects i
ilnik 2017/02/16 12:55:41 Maybe ASAN were confused because there was another
38 WorkingFunction(reinterpret_cast<void*>(counter));
39 EXPECT_GT(*counter, 0);
40 delete counter;
41 int64_t duration_nanos = GetProcessCpuTime() - start_time_nanos;
42 // Should be about kProcessingTimeMillisecs.
43 EXPECT_GE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
44 kProcessingTimeMillisecs - kAllowedErrorMillisecs);
45 EXPECT_LE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
46 kProcessingTimeMillisecs + kAllowedErrorMillisecs);
sprang_webrtc 2017/02/16 12:20:19 EXPECT_NEAR ?
ilnik 2017/02/16 12:38:54 Did not know about that. Should use it.
47 }
48
49 TEST(GetProcessCpuTimeTest, TwoThreads) {
50 int64_t start_time_nanos = GetProcessCpuTime();
51 int64_t* counter1 = new int64_t;
52 int64_t* counter2 = new int64_t;
53 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(counter1),
54 "Thread1");
55 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(counter2),
56 "Thread2");
57 thread1.Start();
58 thread2.Start();
59 thread1.Stop();
60 thread2.Stop();
61
62 EXPECT_GE(*counter1, 0);
63 EXPECT_GE(*counter2, 0);
64 delete counter1;
65 delete counter2;
66 int64_t duration_nanos = GetProcessCpuTime() - start_time_nanos;
67 // Should be about 2*kProcessingTimeMillisecs.
68 EXPECT_GE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
69 2 * (kProcessingTimeMillisecs - kAllowedErrorMillisecs));
70 EXPECT_LE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
71 2 * (kProcessingTimeMillisecs + kAllowedErrorMillisecs));
72 }
73
74 TEST(GetThreadCpuTimeTest, SingleThread) {
75 int64_t start_times_nanos = GetThreadCpuTime();
76 int64_t* counter = new int64_t;
77 WorkingFunction(reinterpret_cast<void*>(counter));
78 EXPECT_GT(*counter, 0);
79 delete counter;
80 int64_t duration_nanos = GetThreadCpuTime() - start_times_nanos;
81 // Should be about kProcessingTimeMillisecs.
82 EXPECT_GE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
83 kProcessingTimeMillisecs - kAllowedErrorMillisecs);
84 EXPECT_LE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
85 kProcessingTimeMillisecs + kAllowedErrorMillisecs);
86 }
87
88 TEST(GetThreadCpuTimeTest, TwoThreads) {
89 int64_t start_time_nanos = GetThreadCpuTime();
90 int64_t* counter1 = new int64_t;
91 int64_t* counter2 = new int64_t;
92 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(counter1),
93 "Thread1");
94 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(counter2),
95 "Thread2");
96 thread1.Start();
97 thread2.Start();
98 thread1.Stop();
99 thread2.Stop();
100
101 EXPECT_GE(*counter1, 0);
102 EXPECT_GE(*counter2, 0);
103 delete counter1;
104 delete counter2;
105 int64_t duration_nanos = GetThreadCpuTime() - start_time_nanos;
106 // Should be about 0.
107 EXPECT_GE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec, 0);
108 EXPECT_LE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
109 kAllowedErrorMillisecs);
110 }
111
112 } // 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