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