| OLD | NEW |
| 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; | |
| 20 | 21 |
| 21 const char* TestFunc() { | 22 const char* TestFunc(rtc::FakeClock* clock) { |
| 22 PROFILE_F(); | 23 PROFILE_F(); |
| 23 rtc::Thread::SleepMs(kWaitMs); | 24 clock->AdvanceTime(rtc::TimeDelta::FromMilliseconds(kWaitMs)); |
| 24 return __FUNCTION__; | 25 return __FUNCTION__; |
| 25 } | 26 } |
| 26 | 27 |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 namespace rtc { | 30 namespace rtc { |
| 30 | 31 |
| 31 // Disable this test due to flakiness; see bug 5947. | 32 TEST(ProfilerTest, TestFunction) { |
| 32 #if defined(WEBRTC_LINUX) | 33 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()); | 34 ASSERT_TRUE(Profiler::Instance()->Clear()); |
| 39 | 35 |
| 40 // Profile a long-running function. | 36 // Profile a long-running function. |
| 41 const char* function_name = TestFunc(); | 37 const char* function_name = TestFunc(&fake_clock); |
| 42 const ProfilerEvent* event = Profiler::Instance()->GetEvent(function_name); | 38 const ProfilerEvent* event = Profiler::Instance()->GetEvent(function_name); |
| 43 ASSERT_TRUE(event != NULL); | 39 ASSERT_TRUE(event != NULL); |
| 44 EXPECT_FALSE(event->is_started()); | 40 EXPECT_FALSE(event->is_started()); |
| 45 EXPECT_EQ(1, event->event_count()); | 41 EXPECT_EQ(1, event->event_count()); |
| 46 EXPECT_NEAR(kWaitSec, event->mean(), kTolerance * 3); | 42 EXPECT_EQ(kWaitSec, event->mean()); |
| 47 | 43 |
| 48 // Run it a second time. | 44 // Run it a second time. |
| 49 TestFunc(); | 45 TestFunc(&fake_clock); |
| 50 EXPECT_FALSE(event->is_started()); | 46 EXPECT_FALSE(event->is_started()); |
| 51 EXPECT_EQ(2, event->event_count()); | 47 EXPECT_EQ(2, event->event_count()); |
| 52 EXPECT_NEAR(kWaitSec, event->mean(), kTolerance); | 48 EXPECT_EQ(kWaitSec, event->mean()); |
| 53 EXPECT_NEAR(kWaitSec * 2, event->total_time(), kTolerance * 2); | 49 EXPECT_EQ(kWaitSec * 2, event->total_time()); |
| 54 EXPECT_DOUBLE_EQ(event->mean(), event->total_time() / event->event_count()); | 50 EXPECT_DOUBLE_EQ(event->mean(), event->total_time() / event->event_count()); |
| 55 } | 51 } |
| 56 | 52 |
| 57 TEST(ProfilerTest, TestScopedEvents) { | 53 TEST(ProfilerTest, TestScopedEvents) { |
| 54 rtc::ScopedFakeClock fake_clock; |
| 58 const std::string kEvent1Name = "Event 1"; | 55 const std::string kEvent1Name = "Event 1"; |
| 59 const std::string kEvent2Name = "Event 2"; | 56 const std::string kEvent2Name = "Event 2"; |
| 60 const int kEvent2WaitMs = 150; | 57 const int kEvent2WaitMs = 150; |
| 61 const double kEvent2WaitSec = 0.150; | 58 const double kEvent2WaitSec = 0.150; |
| 62 const ProfilerEvent* event1; | 59 const ProfilerEvent* event1; |
| 63 const ProfilerEvent* event2; | 60 const ProfilerEvent* event2; |
| 64 ASSERT_TRUE(Profiler::Instance()->Clear()); | 61 ASSERT_TRUE(Profiler::Instance()->Clear()); |
| 65 { // Profile a scope. | 62 { // Profile a scope. |
| 66 PROFILE(kEvent1Name); | 63 PROFILE(kEvent1Name); |
| 67 event1 = Profiler::Instance()->GetEvent(kEvent1Name); | 64 event1 = Profiler::Instance()->GetEvent(kEvent1Name); |
| 68 ASSERT_TRUE(event1 != NULL); | 65 ASSERT_TRUE(event1 != NULL); |
| 69 EXPECT_TRUE(event1->is_started()); | 66 EXPECT_TRUE(event1->is_started()); |
| 70 EXPECT_EQ(0, event1->event_count()); | 67 EXPECT_EQ(0, event1->event_count()); |
| 71 rtc::Thread::SleepMs(kWaitMs); | 68 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(kWaitMs)); |
| 72 EXPECT_TRUE(event1->is_started()); | 69 EXPECT_TRUE(event1->is_started()); |
| 73 } | 70 } |
| 74 // Check the result. | 71 // Check the result. |
| 75 EXPECT_FALSE(event1->is_started()); | 72 EXPECT_FALSE(event1->is_started()); |
| 76 EXPECT_EQ(1, event1->event_count()); | 73 EXPECT_EQ(1, event1->event_count()); |
| 77 EXPECT_NEAR(kWaitSec, event1->mean(), kTolerance); | 74 EXPECT_EQ(kWaitSec, event1->mean()); |
| 78 { // Profile a second event. | 75 { // Profile a second event. |
| 79 PROFILE(kEvent2Name); | 76 PROFILE(kEvent2Name); |
| 80 event2 = Profiler::Instance()->GetEvent(kEvent2Name); | 77 event2 = Profiler::Instance()->GetEvent(kEvent2Name); |
| 81 ASSERT_TRUE(event2 != NULL); | 78 ASSERT_TRUE(event2 != NULL); |
| 82 EXPECT_FALSE(event1->is_started()); | 79 EXPECT_FALSE(event1->is_started()); |
| 83 EXPECT_TRUE(event2->is_started()); | 80 EXPECT_TRUE(event2->is_started()); |
| 84 rtc::Thread::SleepMs(kEvent2WaitMs); | 81 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(kEvent2WaitMs)); |
| 85 } | 82 } |
| 86 // Check the result. | 83 // Check the result. |
| 87 EXPECT_FALSE(event2->is_started()); | 84 EXPECT_FALSE(event2->is_started()); |
| 88 EXPECT_EQ(1, event2->event_count()); | 85 EXPECT_EQ(1, event2->event_count()); |
| 89 | 86 |
| 90 // The difference here can be as much as 0.33, so we need high tolerance. | 87 EXPECT_EQ(kEvent2WaitSec, event2->mean()); |
| 91 EXPECT_NEAR(kEvent2WaitSec, event2->mean(), kTolerance * 4); | |
| 92 // Make sure event1 is unchanged. | 88 // Make sure event1 is unchanged. |
| 93 EXPECT_FALSE(event1->is_started()); | 89 EXPECT_FALSE(event1->is_started()); |
| 94 EXPECT_EQ(1, event1->event_count()); | 90 EXPECT_EQ(1, event1->event_count()); |
| 95 { // Run another event 1. | 91 { // Run another event 1. |
| 96 PROFILE(kEvent1Name); | 92 PROFILE(kEvent1Name); |
| 97 EXPECT_TRUE(event1->is_started()); | 93 EXPECT_TRUE(event1->is_started()); |
| 98 rtc::Thread::SleepMs(kWaitMs); | 94 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(kWaitMs)); |
| 99 } | 95 } |
| 100 // Check the result. | 96 // Check the result. |
| 101 EXPECT_FALSE(event1->is_started()); | 97 EXPECT_FALSE(event1->is_started()); |
| 102 EXPECT_EQ(2, event1->event_count()); | 98 EXPECT_EQ(2, event1->event_count()); |
| 103 EXPECT_NEAR(kWaitSec, event1->mean(), kTolerance); | 99 EXPECT_EQ(kWaitSec, event1->mean()); |
| 104 EXPECT_NEAR(kWaitSec * 2, event1->total_time(), kTolerance * 2); | 100 EXPECT_EQ(kWaitSec * 2, event1->total_time()); |
| 105 EXPECT_DOUBLE_EQ(event1->mean(), | 101 EXPECT_DOUBLE_EQ(event1->mean(), |
| 106 event1->total_time() / event1->event_count()); | 102 event1->total_time() / event1->event_count()); |
| 107 } | 103 } |
| 108 | 104 |
| 109 TEST(ProfilerTest, Clear) { | 105 TEST(ProfilerTest, Clear) { |
| 110 ASSERT_TRUE(Profiler::Instance()->Clear()); | 106 ASSERT_TRUE(Profiler::Instance()->Clear()); |
| 111 PROFILE_START("event"); | 107 PROFILE_START("event"); |
| 112 EXPECT_FALSE(Profiler::Instance()->Clear()); | 108 EXPECT_FALSE(Profiler::Instance()->Clear()); |
| 113 EXPECT_TRUE(Profiler::Instance()->GetEvent("event") != NULL); | 109 EXPECT_TRUE(Profiler::Instance()->GetEvent("event") != NULL); |
| 114 PROFILE_STOP("event"); | 110 PROFILE_STOP("event"); |
| 115 EXPECT_TRUE(Profiler::Instance()->Clear()); | 111 EXPECT_TRUE(Profiler::Instance()->Clear()); |
| 116 EXPECT_EQ(NULL, Profiler::Instance()->GetEvent("event")); | 112 EXPECT_EQ(NULL, Profiler::Instance()->GetEvent("event")); |
| 117 } | 113 } |
| 118 | 114 |
| 119 } // namespace rtc | 115 } // namespace rtc |
| OLD | NEW |