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

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: implemented deadbeef@ comments. 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 <memory>
12 #include <algorithm>
13 #include "webrtc/base/cpu_time.h"
14 #include "webrtc/base/platform_thread.h"
15 #include "webrtc/base/timeutils.h"
16 #include "webrtc/test/gtest.h"
17 #include "webrtc/system_wrappers/include/cpu_info.h"
18
19
Taylor Brandstetter 2017/02/17 09:45:14 nit: extra newlines
ilnik 2017/02/17 09:52:08 Done.
20
21 namespace {
22 const int kAllowedErrorMillisecs = 30;
23 const int kProcessingTimeMillisecs = 300;
24
25 // Consumes approximately kProcessingTimeMillisecs of CPU time.
26 bool WorkingFunction(void* counter_pointer) {
27 int64_t* counter = reinterpret_cast<int64_t*>(counter_pointer);
28 *counter = 0;
29 int64_t stop_time = rtc::SystemTimeNanos() +
30 kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec;
31 while (rtc::SystemTimeNanos() < stop_time) {
32 (*counter)++;
33 }
34 return false;
35 }
36 } // namespace
37
38 namespace rtc {
39
40 TEST(GetProcessCpuTimeTest, SingleThread) {
41 int64_t start_time_nanos = GetProcessCpuTimeNanos();
42 int64_t counter;
43 WorkingFunction(reinterpret_cast<void*>(&counter));
44 EXPECT_GT(counter, 0);
45 int64_t duration_nanos = GetProcessCpuTimeNanos() - start_time_nanos;
46 // Should be about kProcessingTimeMillisecs.
47 EXPECT_NEAR(duration_nanos,
48 kProcessingTimeMillisecs * kNumNanosecsPerMillisec,
49 kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
50 }
51
52 TEST(GetProcessCpuTimeTest, TwoThreads) {
53 int64_t start_time_nanos = GetProcessCpuTimeNanos();
54 int64_t counter1;
55 int64_t counter2;
56 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1),
57 "Thread1");
58 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2),
59 "Thread2");
60 thread1.Start();
61 thread2.Start();
62 thread1.Stop();
63 thread2.Stop();
64
65 EXPECT_GE(counter1, 0);
66 EXPECT_GE(counter2, 0);
67 int64_t duration_nanos = GetProcessCpuTimeNanos() - start_time_nanos;
68 const uint32_t kWorkingThreads = 2;
69 uint32_t used_cores =
70 std::min(webrtc::CpuInfo::DetectNumberOfCores(), kWorkingThreads);
71 // Two working threads for kProcessingTimeMillisecs consume double CPU time
72 // if there are at least 2 cores.
73 EXPECT_NEAR(duration_nanos,
74 used_cores * kProcessingTimeMillisecs * kNumNanosecsPerMillisec,
75 used_cores * kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
76 }
77
78 TEST(GetThreadCpuTimeTest, SingleThread) {
79 int64_t start_times_nanos = GetThreadCpuTimeNanos();
80 int64_t counter;
81 WorkingFunction(reinterpret_cast<void*>(&counter));
82 EXPECT_GT(counter, 0);
83 int64_t duration_nanos = GetThreadCpuTimeNanos() - start_times_nanos;
84 EXPECT_NEAR(duration_nanos,
85 kProcessingTimeMillisecs * kNumNanosecsPerMillisec,
86 kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
87 }
88
89 TEST(GetThreadCpuTimeTest, TwoThreads) {
90 int64_t start_time_nanos = GetThreadCpuTimeNanos();
91 int64_t counter1;
92 int64_t counter2;
93 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1),
94 "Thread1");
95 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2),
96 "Thread2");
97 thread1.Start();
98 thread2.Start();
99 thread1.Stop();
100 thread2.Stop();
101
102 EXPECT_GE(counter1, 0);
103 EXPECT_GE(counter2, 0);
104 int64_t duration_nanos = GetThreadCpuTimeNanos() - start_time_nanos;
105 // This thread didn't do any work.
106 EXPECT_NEAR(duration_nanos, 0,
107 kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
108 }
109
110 } // 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