Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(349)

Side by Side Diff: base/trace_event/blame_context_unittest.cc

Issue 1776673002: base: Add blame context (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Windows build fix. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/trace_event/blame_context.cc ('k') | base/trace_event/trace_event.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/trace_event/blame_context.h"
6
7 #include "base/json/json_writer.h"
8 #include "base/memory/ref_counted_memory.h"
9 #include "base/run_loop.h"
10 #include "base/test/trace_event_analyzer.h"
11 #include "base/trace_event/trace_buffer.h"
12 #include "base/trace_event/trace_event_argument.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace base {
16 namespace trace_event {
17 namespace {
18
19 const char kTestBlameContextCategory[] = "test";
20 const char kDisabledTestBlameContextCategory[] = "disabled-by-default-test";
21 const char kTestBlameContextName[] = "TestBlameContext";
22 const char kTestBlameContextType[] = "TestBlameContextType";
23 const char kTestBlameContextScope[] = "TestBlameContextScope";
24
25 class TestBlameContext : public BlameContext {
26 public:
27 explicit TestBlameContext(int id)
28 : BlameContext(kTestBlameContextCategory,
29 kTestBlameContextName,
30 kTestBlameContextType,
31 kTestBlameContextScope,
32 id,
33 nullptr) {}
34
35 TestBlameContext(int id, const TestBlameContext& parent)
36 : BlameContext(kTestBlameContextCategory,
37 kTestBlameContextName,
38 kTestBlameContextType,
39 kTestBlameContextScope,
40 id,
41 &parent) {}
42
43 protected:
44 void AsValueInto(trace_event::TracedValue* state) override {
45 BlameContext::AsValueInto(state);
46 state->SetBoolean("crossStreams", false);
47 }
48 };
49
50 class DisabledTestBlameContext : public BlameContext {
51 public:
52 explicit DisabledTestBlameContext(int id)
53 : BlameContext(kDisabledTestBlameContextCategory,
54 kTestBlameContextName,
55 kTestBlameContextType,
56 kTestBlameContextScope,
57 id,
58 nullptr) {}
59 };
60
61 void OnTraceDataCollected(Closure quit_closure,
62 trace_event::TraceResultBuffer* buffer,
63 const scoped_refptr<RefCountedString>& json,
64 bool has_more_events) {
65 buffer->AddFragment(json->data());
66 if (!has_more_events)
67 quit_closure.Run();
68 }
69
70 class BlameContextTest : public testing::Test {
71 public:
72 void StartTracing();
73 void StopTracing();
74 scoped_ptr<trace_analyzer::TraceAnalyzer> CreateTraceAnalyzer();
75 };
76
77 void BlameContextTest::StartTracing() {
78 trace_event::TraceLog::GetInstance()->SetEnabled(
79 trace_event::TraceConfig("*"), trace_event::TraceLog::RECORDING_MODE);
80 }
81
82 void BlameContextTest::StopTracing() {
83 trace_event::TraceLog::GetInstance()->SetDisabled();
84 }
85
86 scoped_ptr<trace_analyzer::TraceAnalyzer>
87 BlameContextTest::CreateTraceAnalyzer() {
88 trace_event::TraceResultBuffer buffer;
89 trace_event::TraceResultBuffer::SimpleOutput trace_output;
90 buffer.SetOutputCallback(trace_output.GetCallback());
91 RunLoop run_loop;
92 buffer.Start();
93 trace_event::TraceLog::GetInstance()->Flush(
94 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer)));
95 run_loop.Run();
96 buffer.Finish();
97
98 return make_scoped_ptr(
99 trace_analyzer::TraceAnalyzer::Create(trace_output.json_output));
100 }
101
102 TEST_F(BlameContextTest, EnterAndLeave) {
103 using trace_analyzer::Query;
104 StartTracing();
105 {
106 TestBlameContext blame_context(0x1234);
107 blame_context.Initialize();
108 blame_context.Enter();
109 blame_context.Leave();
110 }
111 StopTracing();
112 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer();
113
114 trace_analyzer::TraceEventVector events;
115 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) ||
116 Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT);
117 analyzer->FindEvents(q, &events);
118
119 EXPECT_EQ(2u, events.size());
120 EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase);
121 EXPECT_EQ(kTestBlameContextCategory, events[0]->category);
122 EXPECT_EQ(kTestBlameContextName, events[0]->name);
123 EXPECT_EQ("0x1234", events[0]->id);
124 EXPECT_EQ(TRACE_EVENT_PHASE_LEAVE_CONTEXT, events[1]->phase);
125 EXPECT_EQ(kTestBlameContextCategory, events[1]->category);
126 EXPECT_EQ(kTestBlameContextName, events[1]->name);
127 EXPECT_EQ("0x1234", events[1]->id);
128 }
129
130 TEST_F(BlameContextTest, DifferentCategories) {
131 // Ensure there is no cross talk between blame contexts from different
132 // categories.
133 using trace_analyzer::Query;
134 StartTracing();
135 {
136 TestBlameContext blame_context(0x1234);
137 DisabledTestBlameContext disabled_blame_context(0x5678);
138 blame_context.Initialize();
139 blame_context.Enter();
140 blame_context.Leave();
141 disabled_blame_context.Initialize();
142 disabled_blame_context.Enter();
143 disabled_blame_context.Leave();
144 }
145 StopTracing();
146 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer();
147
148 trace_analyzer::TraceEventVector events;
149 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) ||
150 Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT);
151 analyzer->FindEvents(q, &events);
152
153 // None of the events from the disabled-by-default category should show up.
154 EXPECT_EQ(2u, events.size());
155 EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase);
156 EXPECT_EQ(kTestBlameContextCategory, events[0]->category);
157 EXPECT_EQ(kTestBlameContextName, events[0]->name);
158 EXPECT_EQ("0x1234", events[0]->id);
159 EXPECT_EQ(TRACE_EVENT_PHASE_LEAVE_CONTEXT, events[1]->phase);
160 EXPECT_EQ(kTestBlameContextCategory, events[1]->category);
161 EXPECT_EQ(kTestBlameContextName, events[1]->name);
162 EXPECT_EQ("0x1234", events[1]->id);
163 }
164
165 TEST_F(BlameContextTest, TakeSnapshot) {
166 using trace_analyzer::Query;
167 StartTracing();
168 {
169 TestBlameContext parent_blame_context(0x5678);
170 TestBlameContext blame_context(0x1234, parent_blame_context);
171 parent_blame_context.Initialize();
172 blame_context.Initialize();
173 blame_context.TakeSnapshot();
174 }
175 StopTracing();
176 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer();
177
178 trace_analyzer::TraceEventVector events;
179 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT);
180 analyzer->FindEvents(q, &events);
181
182 // We should have 3 snapshots: one for both calls to Initialize() and one from
183 // the explicit call to TakeSnapshot().
184 EXPECT_EQ(3u, events.size());
185 EXPECT_EQ(kTestBlameContextCategory, events[0]->category);
186 EXPECT_EQ(kTestBlameContextType, events[0]->name);
187 EXPECT_EQ("0x5678", events[0]->id);
188 EXPECT_TRUE(events[0]->HasArg("snapshot"));
189
190 EXPECT_EQ(kTestBlameContextCategory, events[1]->category);
191 EXPECT_EQ(kTestBlameContextType, events[1]->name);
192 EXPECT_EQ("0x1234", events[1]->id);
193 EXPECT_TRUE(events[0]->HasArg("snapshot"));
194
195 EXPECT_EQ(kTestBlameContextCategory, events[2]->category);
196 EXPECT_EQ(kTestBlameContextType, events[2]->name);
197 EXPECT_EQ("0x1234", events[2]->id);
198 EXPECT_TRUE(events[0]->HasArg("snapshot"));
199
200 const char kExpectedSnapshotJson[] =
201 "{"
202 "\"crossStreams\":false,"
203 "\"parent\":{"
204 "\"id_ref\":\"0x5678\","
205 "\"scope\":\"TestBlameContextScope\""
206 "}"
207 "}";
208
209 std::string snapshot_json;
210 JSONWriter::Write(*events[2]->GetKnownArgAsValue("snapshot"), &snapshot_json);
211 EXPECT_EQ(kExpectedSnapshotJson, snapshot_json);
212 }
213
214 } // namepace
215 } // namespace trace_event
216 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/blame_context.cc ('k') | base/trace_event/trace_event.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698