Home | History | Annotate | Download | only in base
      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 
     34   // Profile a long-running function.
     35   const char* function_name = TestFunc();
     36   const ProfilerEvent* event = Profiler::Instance()->GetEvent(function_name);
     37   ASSERT_TRUE(event != NULL);
     38   EXPECT_FALSE(event->is_started());
     39   EXPECT_EQ(1, event->event_count());
     40   EXPECT_NEAR(kWaitSec, event->mean(), kTolerance * 3);
     41 
     42   // Run it a second time.
     43   TestFunc();
     44   EXPECT_FALSE(event->is_started());
     45   EXPECT_EQ(2, event->event_count());
     46   EXPECT_NEAR(kWaitSec, event->mean(), kTolerance);
     47   EXPECT_NEAR(kWaitSec * 2, event->total_time(), kTolerance * 2);
     48   EXPECT_DOUBLE_EQ(event->mean(), event->total_time() / event->event_count());
     49 }
     50 
     51 TEST(ProfilerTest, TestScopedEvents) {
     52   const std::string kEvent1Name = "Event 1";
     53   const std::string kEvent2Name = "Event 2";
     54   const int kEvent2WaitMs = 150;
     55   const double kEvent2WaitSec = 0.150;
     56   const ProfilerEvent* event1;
     57   const ProfilerEvent* event2;
     58   ASSERT_TRUE(Profiler::Instance()->Clear());
     59   {  // Profile a scope.
     60     PROFILE(kEvent1Name);
     61     event1 = Profiler::Instance()->GetEvent(kEvent1Name);
     62     ASSERT_TRUE(event1 != NULL);
     63     EXPECT_TRUE(event1->is_started());
     64     EXPECT_EQ(0, event1->event_count());
     65     rtc::Thread::SleepMs(kWaitMs);
     66     EXPECT_TRUE(event1->is_started());
     67   }
     68   // Check the result.
     69   EXPECT_FALSE(event1->is_started());
     70   EXPECT_EQ(1, event1->event_count());
     71   EXPECT_NEAR(kWaitSec, event1->mean(), kTolerance);
     72   {  // Profile a second event.
     73     PROFILE(kEvent2Name);
     74     event2 = Profiler::Instance()->GetEvent(kEvent2Name);
     75     ASSERT_TRUE(event2 != NULL);
     76     EXPECT_FALSE(event1->is_started());
     77     EXPECT_TRUE(event2->is_started());
     78     rtc::Thread::SleepMs(kEvent2WaitMs);
     79   }
     80   // Check the result.
     81   EXPECT_FALSE(event2->is_started());
     82   EXPECT_EQ(1, event2->event_count());
     83 
     84   // The difference here can be as much as 0.33, so we need high tolerance.
     85   EXPECT_NEAR(kEvent2WaitSec, event2->mean(), kTolerance * 4);
     86   // Make sure event1 is unchanged.
     87   EXPECT_FALSE(event1->is_started());
     88   EXPECT_EQ(1, event1->event_count());
     89   {  // Run another event 1.
     90     PROFILE(kEvent1Name);
     91     EXPECT_TRUE(event1->is_started());
     92     rtc::Thread::SleepMs(kWaitMs);
     93   }
     94   // Check the result.
     95   EXPECT_FALSE(event1->is_started());
     96   EXPECT_EQ(2, event1->event_count());
     97   EXPECT_NEAR(kWaitSec, event1->mean(), kTolerance);
     98   EXPECT_NEAR(kWaitSec * 2, event1->total_time(), kTolerance * 2);
     99   EXPECT_DOUBLE_EQ(event1->mean(),
    100                    event1->total_time() / event1->event_count());
    101 }
    102 
    103 TEST(ProfilerTest, Clear) {
    104   ASSERT_TRUE(Profiler::Instance()->Clear());
    105   PROFILE_START("event");
    106   EXPECT_FALSE(Profiler::Instance()->Clear());
    107   EXPECT_TRUE(Profiler::Instance()->GetEvent("event") != NULL);
    108   PROFILE_STOP("event");
    109   EXPECT_TRUE(Profiler::Instance()->Clear());
    110   EXPECT_EQ(NULL, Profiler::Instance()->GetEvent("event"));
    111 }
    112 
    113 }  // namespace rtc
    114