Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / profiler_unittest.cc
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
32 namespace {
33
34 const int kWaitMs = 250;
35 const double kWaitSec = 0.250;
36 const double kTolerance = 0.1;
37
38 const char* TestFunc() {
39   PROFILE_F();
40   talk_base::Thread::SleepMs(kWaitMs);
41   return __FUNCTION__;
42 }
43
44 }  // namespace
45
46 namespace talk_base {
47
48 TEST(ProfilerTest, TestFunction) {
49   ASSERT_TRUE(Profiler::Instance()->Clear());
50
51   // Profile a long-running function.
52   const char* function_name = TestFunc();
53   const ProfilerEvent* event = Profiler::Instance()->GetEvent(function_name);
54   ASSERT_TRUE(event != NULL);
55   EXPECT_FALSE(event->is_started());
56   EXPECT_EQ(1, event->event_count());
57   EXPECT_NEAR(kWaitSec, event->mean(), kTolerance * 3);
58
59   // Run it a second time.
60   TestFunc();
61   EXPECT_FALSE(event->is_started());
62   EXPECT_EQ(2, event->event_count());
63   EXPECT_NEAR(kWaitSec, event->mean(), kTolerance);
64   EXPECT_NEAR(kWaitSec * 2, event->total_time(), kTolerance * 2);
65   EXPECT_DOUBLE_EQ(event->mean(), event->total_time() / event->event_count());
66 }
67
68 TEST(ProfilerTest, TestScopedEvents) {
69   const std::string kEvent1Name = "Event 1";
70   const std::string kEvent2Name = "Event 2";
71   const int kEvent2WaitMs = 150;
72   const double kEvent2WaitSec = 0.150;
73   const ProfilerEvent* event1;
74   const ProfilerEvent* event2;
75   ASSERT_TRUE(Profiler::Instance()->Clear());
76   {  // Profile a scope.
77     PROFILE(kEvent1Name);
78     event1 = Profiler::Instance()->GetEvent(kEvent1Name);
79     ASSERT_TRUE(event1 != NULL);
80     EXPECT_TRUE(event1->is_started());
81     EXPECT_EQ(0, event1->event_count());
82     talk_base::Thread::SleepMs(kWaitMs);
83     EXPECT_TRUE(event1->is_started());
84   }
85   // Check the result.
86   EXPECT_FALSE(event1->is_started());
87   EXPECT_EQ(1, event1->event_count());
88   EXPECT_NEAR(kWaitSec, event1->mean(), kTolerance);
89   {  // Profile a second event.
90     PROFILE(kEvent2Name);
91     event2 = Profiler::Instance()->GetEvent(kEvent2Name);
92     ASSERT_TRUE(event2 != NULL);
93     EXPECT_FALSE(event1->is_started());
94     EXPECT_TRUE(event2->is_started());
95     talk_base::Thread::SleepMs(kEvent2WaitMs);
96   }
97   // Check the result.
98   EXPECT_FALSE(event2->is_started());
99   EXPECT_EQ(1, event2->event_count());
100
101   // The difference here can be as much as 0.33, so we need high tolerance.
102   EXPECT_NEAR(kEvent2WaitSec, event2->mean(), kTolerance * 4);
103   // Make sure event1 is unchanged.
104   EXPECT_FALSE(event1->is_started());
105   EXPECT_EQ(1, event1->event_count());
106   {  // Run another event 1.
107     PROFILE(kEvent1Name);
108     EXPECT_TRUE(event1->is_started());
109     talk_base::Thread::SleepMs(kWaitMs);
110   }
111   // Check the result.
112   EXPECT_FALSE(event1->is_started());
113   EXPECT_EQ(2, event1->event_count());
114   EXPECT_NEAR(kWaitSec, event1->mean(), kTolerance);
115   EXPECT_NEAR(kWaitSec * 2, event1->total_time(), kTolerance * 2);
116   EXPECT_DOUBLE_EQ(event1->mean(),
117                    event1->total_time() / event1->event_count());
118 }
119
120 TEST(ProfilerTest, Clear) {
121   ASSERT_TRUE(Profiler::Instance()->Clear());
122   PROFILE_START("event");
123   EXPECT_FALSE(Profiler::Instance()->Clear());
124   EXPECT_TRUE(Profiler::Instance()->GetEvent("event") != NULL);
125   PROFILE_STOP("event");
126   EXPECT_TRUE(Profiler::Instance()->Clear());
127   EXPECT_EQ(NULL, Profiler::Instance()->GetEvent("event"));
128 }
129
130 }  // namespace talk_base