1/* 2 * libjingle 3 * Copyright 2004--2013, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include "talk/base/gunit.h" 29#include "talk/base/profiler.h" 30#include "talk/base/thread.h" 31 32namespace { 33 34const int kWaitMs = 250; 35const double kWaitSec = 0.250; 36const double kTolerance = 0.1; 37 38const char* TestFunc() { 39 PROFILE_F(); 40 talk_base::Thread::SleepMs(kWaitMs); 41 return __FUNCTION__; 42} 43 44} // namespace 45 46namespace talk_base { 47 48TEST(ProfilerTest, TestFunction) { 49 ASSERT_TRUE(Profiler::Instance()->Clear()); 50 // Profile a long-running function. 51 const char* function_name = TestFunc(); 52 const ProfilerEvent* event = Profiler::Instance()->GetEvent(function_name); 53 ASSERT_TRUE(event != NULL); 54 EXPECT_FALSE(event->is_started()); 55 EXPECT_EQ(1, event->event_count()); 56 EXPECT_NEAR(kWaitSec, event->mean(), kTolerance); 57 // Run it a second time. 58 TestFunc(); 59 EXPECT_FALSE(event->is_started()); 60 EXPECT_EQ(2, event->event_count()); 61 EXPECT_NEAR(kWaitSec, event->mean(), kTolerance); 62 EXPECT_NEAR(kWaitSec * 2, event->total_time(), kTolerance * 2); 63 EXPECT_DOUBLE_EQ(event->mean(), event->total_time() / event->event_count()); 64} 65 66TEST(ProfilerTest, TestScopedEvents) { 67 const std::string kEvent1Name = "Event 1"; 68 const std::string kEvent2Name = "Event 2"; 69 const int kEvent2WaitMs = 150; 70 const double kEvent2WaitSec = 0.150; 71 const ProfilerEvent* event1; 72 const ProfilerEvent* event2; 73 ASSERT_TRUE(Profiler::Instance()->Clear()); 74 { // Profile a scope. 75 PROFILE(kEvent1Name); 76 event1 = Profiler::Instance()->GetEvent(kEvent1Name); 77 ASSERT_TRUE(event1 != NULL); 78 EXPECT_TRUE(event1->is_started()); 79 EXPECT_EQ(0, event1->event_count()); 80 talk_base::Thread::SleepMs(kWaitMs); 81 EXPECT_TRUE(event1->is_started()); 82 } 83 // Check the result. 84 EXPECT_FALSE(event1->is_started()); 85 EXPECT_EQ(1, event1->event_count()); 86 EXPECT_NEAR(kWaitSec, event1->mean(), kTolerance); 87 { // Profile a second event. 88 PROFILE(kEvent2Name); 89 event2 = Profiler::Instance()->GetEvent(kEvent2Name); 90 ASSERT_TRUE(event2 != NULL); 91 EXPECT_FALSE(event1->is_started()); 92 EXPECT_TRUE(event2->is_started()); 93 talk_base::Thread::SleepMs(kEvent2WaitMs); 94 } 95 // Check the result. 96 EXPECT_FALSE(event2->is_started()); 97 EXPECT_EQ(1, event2->event_count()); 98 EXPECT_NEAR(kEvent2WaitSec, event2->mean(), kTolerance); 99 // Make sure event1 is unchanged. 100 EXPECT_FALSE(event1->is_started()); 101 EXPECT_EQ(1, event1->event_count()); 102 { // Run another event 1. 103 PROFILE(kEvent1Name); 104 EXPECT_TRUE(event1->is_started()); 105 talk_base::Thread::SleepMs(kWaitMs); 106 } 107 // Check the result. 108 EXPECT_FALSE(event1->is_started()); 109 EXPECT_EQ(2, event1->event_count()); 110 EXPECT_NEAR(kWaitSec, event1->mean(), kTolerance); 111 EXPECT_NEAR(kWaitSec * 2, event1->total_time(), kTolerance * 2); 112 EXPECT_DOUBLE_EQ(event1->mean(), 113 event1->total_time() / event1->event_count()); 114} 115 116TEST(ProfilerTest, Clear) { 117 ASSERT_TRUE(Profiler::Instance()->Clear()); 118 PROFILE_START("event"); 119 EXPECT_FALSE(Profiler::Instance()->Clear()); 120 EXPECT_TRUE(Profiler::Instance()->GetEvent("event") != NULL); 121 PROFILE_STOP("event"); 122 EXPECT_TRUE(Profiler::Instance()->Clear()); 123 EXPECT_EQ(NULL, Profiler::Instance()->GetEvent("event")); 124} 125 126} // namespace talk_base 127