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

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

Issue 2392613003: Switched flaky ProfilerTest to use fake clock (Closed)
Patch Set: Created 4 years, 2 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
« no previous file with comments | « no previous file | 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
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/base/fakeclock.h"
11 #include "webrtc/base/gunit.h" 12 #include "webrtc/base/gunit.h"
12 #include "webrtc/base/profiler.h" 13 #include "webrtc/base/profiler.h"
14 #include "webrtc/base/timedelta.h"
13 #include "webrtc/base/thread.h" 15 #include "webrtc/base/thread.h"
14 16
15 namespace { 17 namespace {
16 18
17 const int kWaitMs = 250; 19 const int kWaitMs = 250;
18 const double kWaitSec = 0.250; 20 const double kWaitSec = 0.250;
19 const double kTolerance = 0.1; 21 const double kTolerance = 0.1;
20 22
21 const char* TestFunc() { 23 const char* TestFunc(rtc::FakeClock* clock) {
22 PROFILE_F(); 24 PROFILE_F();
23 rtc::Thread::SleepMs(kWaitMs); 25 clock->AdvanceTime(rtc::TimeDelta::FromMilliseconds(kWaitMs));
24 return __FUNCTION__; 26 return __FUNCTION__;
25 } 27 }
26 28
27 } // namespace 29 } // namespace
28 30
29 namespace rtc { 31 namespace rtc {
30 32
31 // Disable this test due to flakiness; see bug 5947. 33 TEST(ProfilerTest, TestFunction) {
32 #if defined(WEBRTC_LINUX) 34 rtc::ScopedFakeClock fake_clock;
33 #define MAYBE_TestFunction DISABLED_TestFunction
34 #else
35 #define MAYBE_TestFunction TestFunction
36 #endif
37 TEST(ProfilerTest, MAYBE_TestFunction) {
38 ASSERT_TRUE(Profiler::Instance()->Clear()); 35 ASSERT_TRUE(Profiler::Instance()->Clear());
39 36
40 // Profile a long-running function. 37 // Profile a long-running function.
41 const char* function_name = TestFunc(); 38 const char* function_name = TestFunc(&fake_clock);
42 const ProfilerEvent* event = Profiler::Instance()->GetEvent(function_name); 39 const ProfilerEvent* event = Profiler::Instance()->GetEvent(function_name);
43 ASSERT_TRUE(event != NULL); 40 ASSERT_TRUE(event != NULL);
44 EXPECT_FALSE(event->is_started()); 41 EXPECT_FALSE(event->is_started());
45 EXPECT_EQ(1, event->event_count()); 42 EXPECT_EQ(1, event->event_count());
46 EXPECT_NEAR(kWaitSec, event->mean(), kTolerance * 3); 43 EXPECT_NEAR(kWaitSec, event->mean(), kTolerance * 3);
Taylor Brandstetter 2016/10/04 00:48:53 If the time is now 100% predictable, can we get ri
skvlad 2016/10/04 01:17:45 Good point. I've replaced them all with EXPECT_EQ.
47 44
48 // Run it a second time. 45 // Run it a second time.
49 TestFunc(); 46 TestFunc(&fake_clock);
50 EXPECT_FALSE(event->is_started()); 47 EXPECT_FALSE(event->is_started());
51 EXPECT_EQ(2, event->event_count()); 48 EXPECT_EQ(2, event->event_count());
52 EXPECT_NEAR(kWaitSec, event->mean(), kTolerance); 49 EXPECT_NEAR(kWaitSec, event->mean(), kTolerance);
53 EXPECT_NEAR(kWaitSec * 2, event->total_time(), kTolerance * 2); 50 EXPECT_NEAR(kWaitSec * 2, event->total_time(), kTolerance * 2);
54 EXPECT_DOUBLE_EQ(event->mean(), event->total_time() / event->event_count()); 51 EXPECT_DOUBLE_EQ(event->mean(), event->total_time() / event->event_count());
55 } 52 }
56 53
57 TEST(ProfilerTest, TestScopedEvents) { 54 TEST(ProfilerTest, TestScopedEvents) {
55 rtc::ScopedFakeClock fake_clock;
58 const std::string kEvent1Name = "Event 1"; 56 const std::string kEvent1Name = "Event 1";
59 const std::string kEvent2Name = "Event 2"; 57 const std::string kEvent2Name = "Event 2";
60 const int kEvent2WaitMs = 150; 58 const int kEvent2WaitMs = 150;
61 const double kEvent2WaitSec = 0.150; 59 const double kEvent2WaitSec = 0.150;
62 const ProfilerEvent* event1; 60 const ProfilerEvent* event1;
63 const ProfilerEvent* event2; 61 const ProfilerEvent* event2;
64 ASSERT_TRUE(Profiler::Instance()->Clear()); 62 ASSERT_TRUE(Profiler::Instance()->Clear());
65 { // Profile a scope. 63 { // Profile a scope.
66 PROFILE(kEvent1Name); 64 PROFILE(kEvent1Name);
67 event1 = Profiler::Instance()->GetEvent(kEvent1Name); 65 event1 = Profiler::Instance()->GetEvent(kEvent1Name);
68 ASSERT_TRUE(event1 != NULL); 66 ASSERT_TRUE(event1 != NULL);
69 EXPECT_TRUE(event1->is_started()); 67 EXPECT_TRUE(event1->is_started());
70 EXPECT_EQ(0, event1->event_count()); 68 EXPECT_EQ(0, event1->event_count());
71 rtc::Thread::SleepMs(kWaitMs); 69 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(kWaitMs));
72 EXPECT_TRUE(event1->is_started()); 70 EXPECT_TRUE(event1->is_started());
73 } 71 }
74 // Check the result. 72 // Check the result.
75 EXPECT_FALSE(event1->is_started()); 73 EXPECT_FALSE(event1->is_started());
76 EXPECT_EQ(1, event1->event_count()); 74 EXPECT_EQ(1, event1->event_count());
77 EXPECT_NEAR(kWaitSec, event1->mean(), kTolerance); 75 EXPECT_NEAR(kWaitSec, event1->mean(), kTolerance);
78 { // Profile a second event. 76 { // Profile a second event.
79 PROFILE(kEvent2Name); 77 PROFILE(kEvent2Name);
80 event2 = Profiler::Instance()->GetEvent(kEvent2Name); 78 event2 = Profiler::Instance()->GetEvent(kEvent2Name);
81 ASSERT_TRUE(event2 != NULL); 79 ASSERT_TRUE(event2 != NULL);
82 EXPECT_FALSE(event1->is_started()); 80 EXPECT_FALSE(event1->is_started());
83 EXPECT_TRUE(event2->is_started()); 81 EXPECT_TRUE(event2->is_started());
84 rtc::Thread::SleepMs(kEvent2WaitMs); 82 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(kEvent2WaitMs));
85 } 83 }
86 // Check the result. 84 // Check the result.
87 EXPECT_FALSE(event2->is_started()); 85 EXPECT_FALSE(event2->is_started());
88 EXPECT_EQ(1, event2->event_count()); 86 EXPECT_EQ(1, event2->event_count());
89 87
90 // The difference here can be as much as 0.33, so we need high tolerance. 88 // The difference here can be as much as 0.33, so we need high tolerance.
91 EXPECT_NEAR(kEvent2WaitSec, event2->mean(), kTolerance * 4); 89 EXPECT_NEAR(kEvent2WaitSec, event2->mean(), kTolerance * 4);
92 // Make sure event1 is unchanged. 90 // Make sure event1 is unchanged.
93 EXPECT_FALSE(event1->is_started()); 91 EXPECT_FALSE(event1->is_started());
94 EXPECT_EQ(1, event1->event_count()); 92 EXPECT_EQ(1, event1->event_count());
95 { // Run another event 1. 93 { // Run another event 1.
96 PROFILE(kEvent1Name); 94 PROFILE(kEvent1Name);
97 EXPECT_TRUE(event1->is_started()); 95 EXPECT_TRUE(event1->is_started());
98 rtc::Thread::SleepMs(kWaitMs); 96 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(kWaitMs));
99 } 97 }
100 // Check the result. 98 // Check the result.
101 EXPECT_FALSE(event1->is_started()); 99 EXPECT_FALSE(event1->is_started());
102 EXPECT_EQ(2, event1->event_count()); 100 EXPECT_EQ(2, event1->event_count());
103 EXPECT_NEAR(kWaitSec, event1->mean(), kTolerance); 101 EXPECT_NEAR(kWaitSec, event1->mean(), kTolerance);
104 EXPECT_NEAR(kWaitSec * 2, event1->total_time(), kTolerance * 2); 102 EXPECT_NEAR(kWaitSec * 2, event1->total_time(), kTolerance * 2);
105 EXPECT_DOUBLE_EQ(event1->mean(), 103 EXPECT_DOUBLE_EQ(event1->mean(),
106 event1->total_time() / event1->event_count()); 104 event1->total_time() / event1->event_count());
107 } 105 }
108 106
109 TEST(ProfilerTest, Clear) { 107 TEST(ProfilerTest, Clear) {
110 ASSERT_TRUE(Profiler::Instance()->Clear()); 108 ASSERT_TRUE(Profiler::Instance()->Clear());
111 PROFILE_START("event"); 109 PROFILE_START("event");
112 EXPECT_FALSE(Profiler::Instance()->Clear()); 110 EXPECT_FALSE(Profiler::Instance()->Clear());
113 EXPECT_TRUE(Profiler::Instance()->GetEvent("event") != NULL); 111 EXPECT_TRUE(Profiler::Instance()->GetEvent("event") != NULL);
114 PROFILE_STOP("event"); 112 PROFILE_STOP("event");
115 EXPECT_TRUE(Profiler::Instance()->Clear()); 113 EXPECT_TRUE(Profiler::Instance()->Clear());
116 EXPECT_EQ(NULL, Profiler::Instance()->GetEvent("event")); 114 EXPECT_EQ(NULL, Profiler::Instance()->GetEvent("event"));
117 } 115 }
118 116
119 } // namespace rtc 117 } // namespace rtc
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698