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

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

Issue 1447563002: Implement frame attribution (FrameBlamer) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleanup. 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/debug/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.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace base {
16 namespace debug {
17 namespace {
18
19 const char kTestBlameCategory[] = "test";
20 const char kDisabledTestBlameCategory[] = "disabled-by-default-test";
21 const char kTestBlameName[] = "TestBlameContext";
22 const char kTestBlameScope[] = "TestBlameScope";
23
24 class TestBlameContext
25 : public BlameContext<kTestBlameCategory, kTestBlameName, kTestBlameScope> {
26 public:
27 explicit TestBlameContext(int id)
28 : BlameContext<kTestBlameCategory, kTestBlameName, kTestBlameScope>(id) {}
29
30 TestBlameContext(int id, const TestBlameContext& parent)
31 : BlameContext<kTestBlameCategory, kTestBlameName, kTestBlameScope>(
32 id,
33 parent) {}
34
35 protected:
36 void AsValueInto(trace_event::TracedValue* state) override {
37 BlameContext<kTestBlameCategory, kTestBlameName,
38 kTestBlameScope>::AsValueInto(state);
39 state->SetBoolean("crossStreams", false);
40 }
41 };
42
43 class DisabledTestBlameContext : public BlameContext<kDisabledTestBlameCategory,
44 kTestBlameName,
45 kTestBlameScope> {
46 public:
47 explicit DisabledTestBlameContext(int id)
48 : BlameContext<kDisabledTestBlameCategory,
49 kTestBlameName,
50 kTestBlameScope>(id) {}
51 };
52
53 void OnTraceDataCollected(Closure quit_closure,
54 trace_event::TraceResultBuffer* buffer,
55 const scoped_refptr<RefCountedString>& json,
56 bool has_more_events) {
57 buffer->AddFragment(json->data());
58 if (!has_more_events)
59 quit_closure.Run();
60 }
61
62 class BlameContextTest : public testing::Test {
63 public:
64 void StartTracing();
65 void StopTracing();
66 scoped_ptr<trace_analyzer::TraceAnalyzer> CreateTraceAnalyzer();
67 };
68
69 void BlameContextTest::StartTracing() {
70 trace_event::TraceLog::GetInstance()->SetEnabled(
71 trace_event::TraceConfig("*"), trace_event::TraceLog::RECORDING_MODE);
72 }
73
74 void BlameContextTest::StopTracing() {
75 trace_event::TraceLog::GetInstance()->SetDisabled();
76 }
77
78 scoped_ptr<trace_analyzer::TraceAnalyzer>
79 BlameContextTest::CreateTraceAnalyzer() {
80 trace_event::TraceResultBuffer buffer;
81 trace_event::TraceResultBuffer::SimpleOutput trace_output;
82 buffer.SetOutputCallback(trace_output.GetCallback());
83 RunLoop run_loop;
84 buffer.Start();
85 trace_event::TraceLog::GetInstance()->Flush(
86 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer)));
87 run_loop.Run();
88 buffer.Finish();
89
90 return make_scoped_ptr(
91 trace_analyzer::TraceAnalyzer::Create(trace_output.json_output));
92 }
93
94 TEST_F(BlameContextTest, EnterAndLeave) {
95 using trace_analyzer::Query;
96 StartTracing();
97 {
98 TestBlameContext blame_context(0x1234);
99 blame_context.Enter();
100 blame_context.Leave();
101 }
102 StopTracing();
103 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer();
104
105 trace_analyzer::TraceEventVector events;
106 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) ||
107 Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT);
108 analyzer->FindEvents(q, &events);
109
110 EXPECT_EQ(2u, events.size());
111 EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase);
112 EXPECT_EQ(kTestBlameCategory, events[0]->category);
113 EXPECT_EQ(kTestBlameName, events[0]->name);
114 EXPECT_EQ("0x1234", events[0]->id);
115 EXPECT_EQ(TRACE_EVENT_PHASE_LEAVE_CONTEXT, events[1]->phase);
116 EXPECT_EQ(kTestBlameCategory, events[1]->category);
117 EXPECT_EQ(kTestBlameName, events[1]->name);
118 EXPECT_EQ("0x1234", events[1]->id);
119 }
120
121 TEST_F(BlameContextTest, DifferentCategories) {
122 // Ensure there is no cross talk between blame contexts from different
123 // categories.
124 using trace_analyzer::Query;
125 StartTracing();
126 {
127 TestBlameContext blame_context(0x1234);
128 DisabledTestBlameContext disabled_blame_context(0x5678);
129 blame_context.Enter();
130 blame_context.Leave();
131 disabled_blame_context.Enter();
132 disabled_blame_context.Leave();
133 }
134 StopTracing();
135 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer();
136
137 trace_analyzer::TraceEventVector events;
138 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) ||
139 Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT);
140 analyzer->FindEvents(q, &events);
141
142 // None of the events from the disabled-by-default category should show up.
143 EXPECT_EQ(2u, events.size());
144 EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase);
145 EXPECT_EQ(kTestBlameCategory, events[0]->category);
146 EXPECT_EQ(kTestBlameName, events[0]->name);
147 EXPECT_EQ("0x1234", events[0]->id);
148 EXPECT_EQ(TRACE_EVENT_PHASE_LEAVE_CONTEXT, events[1]->phase);
149 EXPECT_EQ(kTestBlameCategory, events[1]->category);
150 EXPECT_EQ(kTestBlameName, events[1]->name);
151 EXPECT_EQ("0x1234", events[1]->id);
152 }
153
154 TEST_F(BlameContextTest, TakeSnapshot) {
155 using trace_analyzer::Query;
156 StartTracing();
157 {
158 TestBlameContext parent_blame_context(0x5678);
159 TestBlameContext blame_context(0x1234, parent_blame_context);
160 blame_context.TakeSnapshot();
161 }
162 StopTracing();
163 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer();
164
165 trace_analyzer::TraceEventVector events;
166 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT);
167 analyzer->FindEvents(q, &events);
168
169 EXPECT_EQ(1u, events.size());
170 EXPECT_EQ(kTestBlameCategory, events[0]->category);
171 EXPECT_EQ(kTestBlameName, events[0]->name);
172 EXPECT_EQ("0x1234", events[0]->id);
173 EXPECT_TRUE(events[0]->HasArg("snapshot"));
174
175 const char kExpectedSnapshotJson[] =
176 "{\n"
177 " \"crossStreams\": false,\n"
178 " \"parent\": {\n"
179 " \"id_ref\": \"0x5678\",\n"
180 " \"scope\": \"TestBlameScope\"\n"
181 " }\n"
182 "}\n";
183
184 std::string snapshot_json;
185 JSONWriter::WriteWithOptions(*events[0]->GetKnownArgAsValue("snapshot"),
186 JSONWriter::OPTIONS_PRETTY_PRINT,
187 &snapshot_json);
188 EXPECT_EQ(kExpectedSnapshotJson, snapshot_json);
189 }
190
191 } // namepace
192 } // namespace debug
193 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698