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

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 Sprang@ 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
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
18 namespace {
19 const int kAllowedErrorMillisecs = 30;
20 const int kProcessingTimeMillisecs = 300;
21
22 // Consumes approximately kProcessingTimeMillisecs of CPU time.
23 bool WorkingFunction(void* counter_pointer) {
24 int64_t* counter = reinterpret_cast<int64_t*>(counter_pointer);
25 *counter = 0;
26 int64_t stop_time = rtc::SystemTimeNanos() +
27 kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec;
28 while (rtc::SystemTimeNanos() < stop_time) {
29 (*counter)++;
30 }
31 return false;
32 }
33 } // namespace
34
35 namespace rtc {
36
37 TEST(GetProcessCpuTimeTest, SingleThread) {
38 int64_t start_time_nanos = GetProcessCpuTime();
39 std::unique_ptr<int64_t> counter(new int64_t);
Taylor Brandstetter 2017/02/16 18:49:19 Is this because the int64_t needs to be on the hea
ilnik 2017/02/17 09:17:10 I was getting ASAN errors, but it wasn't because c
40 WorkingFunction(reinterpret_cast<void*>(counter.get()));
41 EXPECT_GT(*counter, 0);
42 int64_t duration_nanos = GetProcessCpuTime() - start_time_nanos;
43 // Should be about kProcessingTimeMillisecs.
44 EXPECT_NEAR(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
Taylor Brandstetter 2017/02/16 18:49:19 nit: I'd prefer no cast to double
ilnik 2017/02/17 09:17:10 Done.
45 kProcessingTimeMillisecs, kAllowedErrorMillisecs);
46 }
47
48 TEST(GetProcessCpuTimeTest, TwoThreads) {
49 int64_t start_time_nanos = GetProcessCpuTime();
50 std::unique_ptr<int64_t> counter1(new int64_t);
51 std::unique_ptr<int64_t> counter2(new int64_t);
52 PlatformThread thread1(WorkingFunction,
53 reinterpret_cast<void*>(counter1.get()), "Thread1");
54 PlatformThread thread2(WorkingFunction,
55 reinterpret_cast<void*>(counter2.get()), "Thread2");
56 thread1.Start();
57 thread2.Start();
58 thread1.Stop();
59 thread2.Stop();
60
61 EXPECT_GE(*counter1, 0);
62 EXPECT_GE(*counter2, 0);
63 int64_t duration_nanos = GetProcessCpuTime() - start_time_nanos;
64 EXPECT_NEAR(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
65 2 * kProcessingTimeMillisecs, 2 * kAllowedErrorMillisecs);
Taylor Brandstetter 2017/02/16 18:49:19 This test will fail if the system doesn't have mul
ilnik 2017/02/17 09:17:10 Done.
66 }
67
68 TEST(GetThreadCpuTimeTest, SingleThread) {
69 int64_t start_times_nanos = GetThreadCpuTime();
70 std::unique_ptr<int64_t> counter(new int64_t);
71 WorkingFunction(reinterpret_cast<void*>(counter.get()));
72 EXPECT_GT(*counter, 0);
73 int64_t duration_nanos = GetThreadCpuTime() - start_times_nanos;
74 // Should be about kProcessingTimeMillisecs.
75 EXPECT_NEAR(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
76 kProcessingTimeMillisecs, kAllowedErrorMillisecs);
77 }
78
79 TEST(GetThreadCpuTimeTest, TwoThreads) {
80 int64_t start_time_nanos = GetThreadCpuTime();
81 std::unique_ptr<int64_t> counter1(new int64_t);
82 std::unique_ptr<int64_t> counter2(new int64_t);
83 PlatformThread thread1(WorkingFunction,
84 reinterpret_cast<void*>(counter1.get()), "Thread1");
85 PlatformThread thread2(WorkingFunction,
86 reinterpret_cast<void*>(counter2.get()), "Thread2");
87 thread1.Start();
88 thread2.Start();
89 thread1.Stop();
90 thread2.Stop();
91
92 EXPECT_GE(*counter1, 0);
93 EXPECT_GE(*counter2, 0);
94 int64_t duration_nanos = GetThreadCpuTime() - start_time_nanos;
95 // Should be about 0.
96 EXPECT_NEAR(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec, 0,
97 kAllowedErrorMillisecs);
98 }
99
100 } // 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