| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2004 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 "webrtc/base/fakeclock.h" | |
| 12 #include "webrtc/base/gunit.h" | |
| 13 #include "webrtc/base/profiler.h" | |
| 14 #include "webrtc/base/timedelta.h" | |
| 15 #include "webrtc/base/thread.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 const int kWaitMs = 250; | |
| 20 const double kWaitSec = 0.250; | |
| 21 | |
| 22 const char* TestFunc(rtc::FakeClock* clock) { | |
| 23 PROFILE_F(); | |
| 24 clock->AdvanceTime(rtc::TimeDelta::FromMilliseconds(kWaitMs)); | |
| 25 return __FUNCTION__; | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 namespace rtc { | |
| 31 | |
| 32 TEST(ProfilerTest, TestFunction) { | |
| 33 rtc::ScopedFakeClock fake_clock; | |
| 34 ASSERT_TRUE(Profiler::Instance()->Clear()); | |
| 35 | |
| 36 // Profile a long-running function. | |
| 37 const char* function_name = TestFunc(&fake_clock); | |
| 38 const ProfilerEvent* event = Profiler::Instance()->GetEvent(function_name); | |
| 39 ASSERT_TRUE(event != NULL); | |
| 40 EXPECT_FALSE(event->is_started()); | |
| 41 EXPECT_EQ(1, event->event_count()); | |
| 42 EXPECT_EQ(kWaitSec, event->mean()); | |
| 43 | |
| 44 // Run it a second time. | |
| 45 TestFunc(&fake_clock); | |
| 46 EXPECT_FALSE(event->is_started()); | |
| 47 EXPECT_EQ(2, event->event_count()); | |
| 48 EXPECT_EQ(kWaitSec, event->mean()); | |
| 49 EXPECT_EQ(kWaitSec * 2, event->total_time()); | |
| 50 EXPECT_DOUBLE_EQ(event->mean(), event->total_time() / event->event_count()); | |
| 51 } | |
| 52 | |
| 53 TEST(ProfilerTest, TestScopedEvents) { | |
| 54 rtc::ScopedFakeClock fake_clock; | |
| 55 const std::string kEvent1Name = "Event 1"; | |
| 56 const std::string kEvent2Name = "Event 2"; | |
| 57 const int kEvent2WaitMs = 150; | |
| 58 const double kEvent2WaitSec = 0.150; | |
| 59 const ProfilerEvent* event1; | |
| 60 const ProfilerEvent* event2; | |
| 61 ASSERT_TRUE(Profiler::Instance()->Clear()); | |
| 62 { // Profile a scope. | |
| 63 PROFILE(kEvent1Name); | |
| 64 event1 = Profiler::Instance()->GetEvent(kEvent1Name); | |
| 65 ASSERT_TRUE(event1 != NULL); | |
| 66 EXPECT_TRUE(event1->is_started()); | |
| 67 EXPECT_EQ(0, event1->event_count()); | |
| 68 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(kWaitMs)); | |
| 69 EXPECT_TRUE(event1->is_started()); | |
| 70 } | |
| 71 // Check the result. | |
| 72 EXPECT_FALSE(event1->is_started()); | |
| 73 EXPECT_EQ(1, event1->event_count()); | |
| 74 EXPECT_EQ(kWaitSec, event1->mean()); | |
| 75 { // Profile a second event. | |
| 76 PROFILE(kEvent2Name); | |
| 77 event2 = Profiler::Instance()->GetEvent(kEvent2Name); | |
| 78 ASSERT_TRUE(event2 != NULL); | |
| 79 EXPECT_FALSE(event1->is_started()); | |
| 80 EXPECT_TRUE(event2->is_started()); | |
| 81 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(kEvent2WaitMs)); | |
| 82 } | |
| 83 // Check the result. | |
| 84 EXPECT_FALSE(event2->is_started()); | |
| 85 EXPECT_EQ(1, event2->event_count()); | |
| 86 | |
| 87 EXPECT_EQ(kEvent2WaitSec, event2->mean()); | |
| 88 // Make sure event1 is unchanged. | |
| 89 EXPECT_FALSE(event1->is_started()); | |
| 90 EXPECT_EQ(1, event1->event_count()); | |
| 91 { // Run another event 1. | |
| 92 PROFILE(kEvent1Name); | |
| 93 EXPECT_TRUE(event1->is_started()); | |
| 94 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(kWaitMs)); | |
| 95 } | |
| 96 // Check the result. | |
| 97 EXPECT_FALSE(event1->is_started()); | |
| 98 EXPECT_EQ(2, event1->event_count()); | |
| 99 EXPECT_EQ(kWaitSec, event1->mean()); | |
| 100 EXPECT_EQ(kWaitSec * 2, event1->total_time()); | |
| 101 EXPECT_DOUBLE_EQ(event1->mean(), | |
| 102 event1->total_time() / event1->event_count()); | |
| 103 } | |
| 104 | |
| 105 TEST(ProfilerTest, Clear) { | |
| 106 ASSERT_TRUE(Profiler::Instance()->Clear()); | |
| 107 PROFILE_START("event"); | |
| 108 EXPECT_FALSE(Profiler::Instance()->Clear()); | |
| 109 EXPECT_TRUE(Profiler::Instance()->GetEvent("event") != NULL); | |
| 110 PROFILE_STOP("event"); | |
| 111 EXPECT_TRUE(Profiler::Instance()->Clear()); | |
| 112 EXPECT_EQ(NULL, Profiler::Instance()->GetEvent("event")); | |
| 113 } | |
| 114 | |
| 115 } // namespace rtc | |
| OLD | NEW |