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