OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/trace_event/blame_context.h" | 5 #include "base/trace_event/blame_context.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/memory/ptr_util.h" |
8 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
10 #include "base/test/trace_event_analyzer.h" | 11 #include "base/test/trace_event_analyzer.h" |
11 #include "base/trace_event/trace_buffer.h" | 12 #include "base/trace_event/trace_buffer.h" |
12 #include "base/trace_event/trace_event_argument.h" | 13 #include "base/trace_event/trace_event_argument.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 namespace trace_event { | 17 namespace trace_event { |
17 namespace { | 18 namespace { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 bool has_more_events) { | 65 bool has_more_events) { |
65 buffer->AddFragment(json->data()); | 66 buffer->AddFragment(json->data()); |
66 if (!has_more_events) | 67 if (!has_more_events) |
67 quit_closure.Run(); | 68 quit_closure.Run(); |
68 } | 69 } |
69 | 70 |
70 class BlameContextTest : public testing::Test { | 71 class BlameContextTest : public testing::Test { |
71 public: | 72 public: |
72 void StartTracing(); | 73 void StartTracing(); |
73 void StopTracing(); | 74 void StopTracing(); |
74 scoped_ptr<trace_analyzer::TraceAnalyzer> CreateTraceAnalyzer(); | 75 std::unique_ptr<trace_analyzer::TraceAnalyzer> CreateTraceAnalyzer(); |
75 }; | 76 }; |
76 | 77 |
77 void BlameContextTest::StartTracing() { | 78 void BlameContextTest::StartTracing() { |
78 trace_event::TraceLog::GetInstance()->SetEnabled( | 79 trace_event::TraceLog::GetInstance()->SetEnabled( |
79 trace_event::TraceConfig("*"), trace_event::TraceLog::RECORDING_MODE); | 80 trace_event::TraceConfig("*"), trace_event::TraceLog::RECORDING_MODE); |
80 } | 81 } |
81 | 82 |
82 void BlameContextTest::StopTracing() { | 83 void BlameContextTest::StopTracing() { |
83 trace_event::TraceLog::GetInstance()->SetDisabled(); | 84 trace_event::TraceLog::GetInstance()->SetDisabled(); |
84 } | 85 } |
85 | 86 |
86 scoped_ptr<trace_analyzer::TraceAnalyzer> | 87 std::unique_ptr<trace_analyzer::TraceAnalyzer> |
87 BlameContextTest::CreateTraceAnalyzer() { | 88 BlameContextTest::CreateTraceAnalyzer() { |
88 trace_event::TraceResultBuffer buffer; | 89 trace_event::TraceResultBuffer buffer; |
89 trace_event::TraceResultBuffer::SimpleOutput trace_output; | 90 trace_event::TraceResultBuffer::SimpleOutput trace_output; |
90 buffer.SetOutputCallback(trace_output.GetCallback()); | 91 buffer.SetOutputCallback(trace_output.GetCallback()); |
91 RunLoop run_loop; | 92 RunLoop run_loop; |
92 buffer.Start(); | 93 buffer.Start(); |
93 trace_event::TraceLog::GetInstance()->Flush( | 94 trace_event::TraceLog::GetInstance()->Flush( |
94 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer))); | 95 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer))); |
95 run_loop.Run(); | 96 run_loop.Run(); |
96 buffer.Finish(); | 97 buffer.Finish(); |
97 | 98 |
98 return make_scoped_ptr( | 99 return WrapUnique( |
99 trace_analyzer::TraceAnalyzer::Create(trace_output.json_output)); | 100 trace_analyzer::TraceAnalyzer::Create(trace_output.json_output)); |
100 } | 101 } |
101 | 102 |
102 TEST_F(BlameContextTest, EnterAndLeave) { | 103 TEST_F(BlameContextTest, EnterAndLeave) { |
103 using trace_analyzer::Query; | 104 using trace_analyzer::Query; |
104 StartTracing(); | 105 StartTracing(); |
105 { | 106 { |
106 TestBlameContext blame_context(0x1234); | 107 TestBlameContext blame_context(0x1234); |
107 blame_context.Initialize(); | 108 blame_context.Initialize(); |
108 blame_context.Enter(); | 109 blame_context.Enter(); |
109 blame_context.Leave(); | 110 blame_context.Leave(); |
110 } | 111 } |
111 StopTracing(); | 112 StopTracing(); |
112 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer(); | 113 std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer = |
| 114 CreateTraceAnalyzer(); |
113 | 115 |
114 trace_analyzer::TraceEventVector events; | 116 trace_analyzer::TraceEventVector events; |
115 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) || | 117 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) || |
116 Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT); | 118 Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT); |
117 analyzer->FindEvents(q, &events); | 119 analyzer->FindEvents(q, &events); |
118 | 120 |
119 EXPECT_EQ(2u, events.size()); | 121 EXPECT_EQ(2u, events.size()); |
120 EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase); | 122 EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase); |
121 EXPECT_EQ(kTestBlameContextCategory, events[0]->category); | 123 EXPECT_EQ(kTestBlameContextCategory, events[0]->category); |
122 EXPECT_EQ(kTestBlameContextName, events[0]->name); | 124 EXPECT_EQ(kTestBlameContextName, events[0]->name); |
(...skipping 13 matching lines...) Expand all Loading... |
136 TestBlameContext blame_context(0x1234); | 138 TestBlameContext blame_context(0x1234); |
137 DisabledTestBlameContext disabled_blame_context(0x5678); | 139 DisabledTestBlameContext disabled_blame_context(0x5678); |
138 blame_context.Initialize(); | 140 blame_context.Initialize(); |
139 blame_context.Enter(); | 141 blame_context.Enter(); |
140 blame_context.Leave(); | 142 blame_context.Leave(); |
141 disabled_blame_context.Initialize(); | 143 disabled_blame_context.Initialize(); |
142 disabled_blame_context.Enter(); | 144 disabled_blame_context.Enter(); |
143 disabled_blame_context.Leave(); | 145 disabled_blame_context.Leave(); |
144 } | 146 } |
145 StopTracing(); | 147 StopTracing(); |
146 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer(); | 148 std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer = |
| 149 CreateTraceAnalyzer(); |
147 | 150 |
148 trace_analyzer::TraceEventVector events; | 151 trace_analyzer::TraceEventVector events; |
149 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) || | 152 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) || |
150 Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT); | 153 Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT); |
151 analyzer->FindEvents(q, &events); | 154 analyzer->FindEvents(q, &events); |
152 | 155 |
153 // None of the events from the disabled-by-default category should show up. | 156 // None of the events from the disabled-by-default category should show up. |
154 EXPECT_EQ(2u, events.size()); | 157 EXPECT_EQ(2u, events.size()); |
155 EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase); | 158 EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase); |
156 EXPECT_EQ(kTestBlameContextCategory, events[0]->category); | 159 EXPECT_EQ(kTestBlameContextCategory, events[0]->category); |
157 EXPECT_EQ(kTestBlameContextName, events[0]->name); | 160 EXPECT_EQ(kTestBlameContextName, events[0]->name); |
158 EXPECT_EQ("0x1234", events[0]->id); | 161 EXPECT_EQ("0x1234", events[0]->id); |
159 EXPECT_EQ(TRACE_EVENT_PHASE_LEAVE_CONTEXT, events[1]->phase); | 162 EXPECT_EQ(TRACE_EVENT_PHASE_LEAVE_CONTEXT, events[1]->phase); |
160 EXPECT_EQ(kTestBlameContextCategory, events[1]->category); | 163 EXPECT_EQ(kTestBlameContextCategory, events[1]->category); |
161 EXPECT_EQ(kTestBlameContextName, events[1]->name); | 164 EXPECT_EQ(kTestBlameContextName, events[1]->name); |
162 EXPECT_EQ("0x1234", events[1]->id); | 165 EXPECT_EQ("0x1234", events[1]->id); |
163 } | 166 } |
164 | 167 |
165 TEST_F(BlameContextTest, TakeSnapshot) { | 168 TEST_F(BlameContextTest, TakeSnapshot) { |
166 using trace_analyzer::Query; | 169 using trace_analyzer::Query; |
167 StartTracing(); | 170 StartTracing(); |
168 { | 171 { |
169 TestBlameContext parent_blame_context(0x5678); | 172 TestBlameContext parent_blame_context(0x5678); |
170 TestBlameContext blame_context(0x1234, parent_blame_context); | 173 TestBlameContext blame_context(0x1234, parent_blame_context); |
171 parent_blame_context.Initialize(); | 174 parent_blame_context.Initialize(); |
172 blame_context.Initialize(); | 175 blame_context.Initialize(); |
173 blame_context.TakeSnapshot(); | 176 blame_context.TakeSnapshot(); |
174 } | 177 } |
175 StopTracing(); | 178 StopTracing(); |
176 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer(); | 179 std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer = |
| 180 CreateTraceAnalyzer(); |
177 | 181 |
178 trace_analyzer::TraceEventVector events; | 182 trace_analyzer::TraceEventVector events; |
179 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT); | 183 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT); |
180 analyzer->FindEvents(q, &events); | 184 analyzer->FindEvents(q, &events); |
181 | 185 |
182 // We should have 3 snapshots: one for both calls to Initialize() and one from | 186 // We should have 3 snapshots: one for both calls to Initialize() and one from |
183 // the explicit call to TakeSnapshot(). | 187 // the explicit call to TakeSnapshot(). |
184 EXPECT_EQ(3u, events.size()); | 188 EXPECT_EQ(3u, events.size()); |
185 EXPECT_EQ(kTestBlameContextCategory, events[0]->category); | 189 EXPECT_EQ(kTestBlameContextCategory, events[0]->category); |
186 EXPECT_EQ(kTestBlameContextType, events[0]->name); | 190 EXPECT_EQ(kTestBlameContextType, events[0]->name); |
(...skipping 20 matching lines...) Expand all Loading... |
207 "}"; | 211 "}"; |
208 | 212 |
209 std::string snapshot_json; | 213 std::string snapshot_json; |
210 JSONWriter::Write(*events[2]->GetKnownArgAsValue("snapshot"), &snapshot_json); | 214 JSONWriter::Write(*events[2]->GetKnownArgAsValue("snapshot"), &snapshot_json); |
211 EXPECT_EQ(kExpectedSnapshotJson, snapshot_json); | 215 EXPECT_EQ(kExpectedSnapshotJson, snapshot_json); |
212 } | 216 } |
213 | 217 |
214 } // namepace | 218 } // namepace |
215 } // namespace trace_event | 219 } // namespace trace_event |
216 } // namespace base | 220 } // namespace base |
OLD | NEW |