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

Unified 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 side-by-side diff with in-line comments
Download patch
« webrtc/base/cpu_time.cc ('K') | « webrtc/base/cpu_time.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/cpu_time_unittest.cc
diff --git a/webrtc/base/cpu_time_unittest.cc b/webrtc/base/cpu_time_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a48f2e98f680e50d6cbaf9b7c95abdb2518a1019
--- /dev/null
+++ b/webrtc/base/cpu_time_unittest.cc
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "webrtc/base/cpu_time.h"
+#include "webrtc/base/platform_thread.h"
+#include "webrtc/base/timeutils.h"
+#include "webrtc/test/gtest.h"
+
+namespace {
+const int kAllowedErrorMillisecs = 30;
+const int kProcessingTimeMillisecs = 300;
+
+// Consumes approximately kProcessingTimeMillisecs of CPU time.
sprang_webrtc 2017/02/16 12:20:18 nit: formatting
+bool WorkingFunction(void* counter_pointer) {
+ 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.
+ counter = 0;
+ int64_t stop_time = rtc::SystemTimeNanos() +
+ kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec;
+ while (rtc::SystemTimeNanos() < stop_time) {
+ counter++;
+ }
+ return false;
+}
+} // namespace
+
+namespace rtc {
+
+TEST(GetProcessCpuTimeTest, SingleThread) {
+ int64_t start_time_nanos = GetProcessCpuTime();
+ 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
+ WorkingFunction(reinterpret_cast<void*>(counter));
+ EXPECT_GT(*counter, 0);
+ delete counter;
+ int64_t duration_nanos = GetProcessCpuTime() - start_time_nanos;
+ // Should be about kProcessingTimeMillisecs.
+ EXPECT_GE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
+ kProcessingTimeMillisecs - kAllowedErrorMillisecs);
+ EXPECT_LE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
+ 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.
+}
+
+TEST(GetProcessCpuTimeTest, TwoThreads) {
+ int64_t start_time_nanos = GetProcessCpuTime();
+ int64_t* counter1 = new int64_t;
+ int64_t* counter2 = new int64_t;
+ PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(counter1),
+ "Thread1");
+ PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(counter2),
+ "Thread2");
+ thread1.Start();
+ thread2.Start();
+ thread1.Stop();
+ thread2.Stop();
+
+ EXPECT_GE(*counter1, 0);
+ EXPECT_GE(*counter2, 0);
+ delete counter1;
+ delete counter2;
+ int64_t duration_nanos = GetProcessCpuTime() - start_time_nanos;
+ // Should be about 2*kProcessingTimeMillisecs.
+ EXPECT_GE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
+ 2 * (kProcessingTimeMillisecs - kAllowedErrorMillisecs));
+ EXPECT_LE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
+ 2 * (kProcessingTimeMillisecs + kAllowedErrorMillisecs));
+}
+
+TEST(GetThreadCpuTimeTest, SingleThread) {
+ int64_t start_times_nanos = GetThreadCpuTime();
+ int64_t* counter = new int64_t;
+ WorkingFunction(reinterpret_cast<void*>(counter));
+ EXPECT_GT(*counter, 0);
+ delete counter;
+ int64_t duration_nanos = GetThreadCpuTime() - start_times_nanos;
+ // Should be about kProcessingTimeMillisecs.
+ EXPECT_GE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
+ kProcessingTimeMillisecs - kAllowedErrorMillisecs);
+ EXPECT_LE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
+ kProcessingTimeMillisecs + kAllowedErrorMillisecs);
+}
+
+TEST(GetThreadCpuTimeTest, TwoThreads) {
+ int64_t start_time_nanos = GetThreadCpuTime();
+ int64_t* counter1 = new int64_t;
+ int64_t* counter2 = new int64_t;
+ PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(counter1),
+ "Thread1");
+ PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(counter2),
+ "Thread2");
+ thread1.Start();
+ thread2.Start();
+ thread1.Stop();
+ thread2.Stop();
+
+ EXPECT_GE(*counter1, 0);
+ EXPECT_GE(*counter2, 0);
+ delete counter1;
+ delete counter2;
+ int64_t duration_nanos = GetThreadCpuTime() - start_time_nanos;
+ // Should be about 0.
+ EXPECT_GE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec, 0);
+ EXPECT_LE(static_cast<double>(duration_nanos) / kNumNanosecsPerMillisec,
+ kAllowedErrorMillisecs);
+}
+
+} // namespace rtc
« 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