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

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: Unprotect destructor. 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
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
34 TestBlameContext(int id, const TestBlameContext& parent)
35 : BlameContext(kTestBlameContextCategory,
36 kTestBlameContextName,
37 kTestBlameContextType,
38 kTestBlameContextScope,
39 id,
40 parent) {}
41
42 protected:
43 void AsValueInto(trace_event::TracedValue* state) override {
44 BlameContext::AsValueInto(state);
45 state->SetBoolean("crossStreams", false);
46 }
47 };
48
49 class DisabledTestBlameContext : public BlameContext {
50 public:
51 explicit DisabledTestBlameContext(int id)
52 : BlameContext(kDisabledTestBlameContextCategory,
53 kTestBlameContextName,
54 kTestBlameContextType,
55 kTestBlameContextScope,
56 id) {}
57 };
58
59 void OnTraceDataCollected(Closure quit_closure,
60 trace_event::TraceResultBuffer* buffer,
61 const scoped_refptr<RefCountedString>& json,
62 bool has_more_events) {
63 buffer->AddFragment(json->data());
64 if (!has_more_events)
65 quit_closure.Run();
66 }
67
68 class BlameContextTest : public testing::Test {
69 public:
70 void StartTracing();
71 void StopTracing();
72 scoped_ptr<trace_analyzer::TraceAnalyzer> CreateTraceAnalyzer();
73 };
74
75 void BlameContextTest::StartTracing() {
76 trace_event::TraceLog::GetInstance()->SetEnabled(
77 trace_event::TraceConfig("*"), trace_event::TraceLog::RECORDING_MODE);
78 }
79
80 void BlameContextTest::StopTracing() {
81 trace_event::TraceLog::GetInstance()->SetDisabled();
82 }
83
84 scoped_ptr<trace_analyzer::TraceAnalyzer>
85 BlameContextTest::CreateTraceAnalyzer() {
86 trace_event::TraceResultBuffer buffer;
87 trace_event::TraceResultBuffer::SimpleOutput trace_output;
88 buffer.SetOutputCallback(trace_output.GetCallback());
89 RunLoop run_loop;
90 buffer.Start();
91 trace_event::TraceLog::GetInstance()->Flush(
92 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer)));
93 run_loop.Run();
94 buffer.Finish();
95
96 return make_scoped_ptr(
97 trace_analyzer::TraceAnalyzer::Create(trace_output.json_output));
98 }
99
100 TEST_F(BlameContextTest, EnterAndLeave) {
101 using trace_analyzer::Query;
102 StartTracing();
103 {
104 TestBlameContext blame_context(0x1234);
105 blame_context.Enter();
106 blame_context.Leave();
107 }
108 StopTracing();
109 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer();
110
111 trace_analyzer::TraceEventVector events;
112 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) ||
113 Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT);
114 analyzer->FindEvents(q, &events);
115
116 EXPECT_EQ(2u, events.size());
117 EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase);
118 EXPECT_EQ(kTestBlameContextCategory, events[0]->category);
119 EXPECT_EQ(kTestBlameContextName, events[0]->name);
120 EXPECT_EQ("0x1234", events[0]->id);
121 EXPECT_EQ(TRACE_EVENT_PHASE_LEAVE_CONTEXT, events[1]->phase);
122 EXPECT_EQ(kTestBlameContextCategory, events[1]->category);
123 EXPECT_EQ(kTestBlameContextName, events[1]->name);
124 EXPECT_EQ("0x1234", events[1]->id);
125 }
Primiano Tucci (use gerrit) 2016/03/21 14:26:29 shouldn't you also look for the presence of the sn
Sami 2016/03/21 15:05:09 The automatic snapshotting is now covered by the T
126
127 TEST_F(BlameContextTest, DifferentCategories) {
128 // Ensure there is no cross talk between blame contexts from different
129 // categories.
130 using trace_analyzer::Query;
131 StartTracing();
132 {
133 TestBlameContext blame_context(0x1234);
134 DisabledTestBlameContext disabled_blame_context(0x5678);
135 blame_context.Enter();
136 blame_context.Leave();
137 disabled_blame_context.Enter();
138 disabled_blame_context.Leave();
139 }
140 StopTracing();
141 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer();
142
143 trace_analyzer::TraceEventVector events;
144 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) ||
145 Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT);
146 analyzer->FindEvents(q, &events);
147
148 // None of the events from the disabled-by-default category should show up.
149 EXPECT_EQ(2u, events.size());
150 EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase);
151 EXPECT_EQ(kTestBlameContextCategory, events[0]->category);
152 EXPECT_EQ(kTestBlameContextName, events[0]->name);
153 EXPECT_EQ("0x1234", events[0]->id);
154 EXPECT_EQ(TRACE_EVENT_PHASE_LEAVE_CONTEXT, events[1]->phase);
155 EXPECT_EQ(kTestBlameContextCategory, events[1]->category);
156 EXPECT_EQ(kTestBlameContextName, events[1]->name);
157 EXPECT_EQ("0x1234", events[1]->id);
158 }
159
160 TEST_F(BlameContextTest, TakeSnapshot) {
161 using trace_analyzer::Query;
162 StartTracing();
163 {
164 TestBlameContext parent_blame_context(0x5678);
165 TestBlameContext blame_context(0x1234, parent_blame_context);
166 blame_context.TakeSnapshot();
167 }
168 StopTracing();
169 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer();
170
171 trace_analyzer::TraceEventVector events;
172 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT);
173 analyzer->FindEvents(q, &events);
174
175 EXPECT_EQ(1u, events.size());
176 EXPECT_EQ(kTestBlameContextCategory, events[0]->category);
177 EXPECT_EQ(kTestBlameContextType, events[0]->name);
178 EXPECT_EQ("0x1234", events[0]->id);
179 EXPECT_TRUE(events[0]->HasArg("snapshot"));
180
181 const char kExpectedSnapshotJson[] =
182 "{"
183 "\"crossStreams\":false,"
184 "\"parent\":{"
185 "\"id_ref\":\"0x5678\","
186 "\"scope\":\"TestBlameContextScope\""
187 "}"
188 "}";
189
190 std::string snapshot_json;
191 JSONWriter::Write(*events[0]->GetKnownArgAsValue("snapshot"), &snapshot_json);
192 EXPECT_EQ(kExpectedSnapshotJson, snapshot_json);
193 }
194
195 } // namepace
196 } // namespace trace_event
197 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698