forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblame_context_unittest.cc
222 lines (194 loc) · 7.39 KB
/
blame_context_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/trace_event/blame_context.h"
#include "base/json/json_writer.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted_memory.h"
#include "base/run_loop.h"
#include "base/test/trace_event_analyzer.h"
#include "base/trace_event/trace_buffer.h"
#include "base/trace_event/trace_event_argument.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace trace_event {
namespace {
const char kTestBlameContextCategory[] = "test";
const char kDisabledTestBlameContextCategory[] = "disabled-by-default-test";
const char kTestBlameContextName[] = "TestBlameContext";
const char kTestBlameContextType[] = "TestBlameContextType";
const char kTestBlameContextScope[] = "TestBlameContextScope";
class TestBlameContext : public BlameContext {
public:
explicit TestBlameContext(int id)
: BlameContext(kTestBlameContextCategory,
kTestBlameContextName,
kTestBlameContextType,
kTestBlameContextScope,
id,
nullptr) {}
TestBlameContext(int id, const TestBlameContext& parent)
: BlameContext(kTestBlameContextCategory,
kTestBlameContextName,
kTestBlameContextType,
kTestBlameContextScope,
id,
&parent) {}
protected:
void AsValueInto(trace_event::TracedValue* state) override {
BlameContext::AsValueInto(state);
state->SetBoolean("crossStreams", false);
}
};
class DisabledTestBlameContext : public BlameContext {
public:
explicit DisabledTestBlameContext(int id)
: BlameContext(kDisabledTestBlameContextCategory,
kTestBlameContextName,
kTestBlameContextType,
kTestBlameContextScope,
id,
nullptr) {}
};
void OnTraceDataCollected(Closure quit_closure,
trace_event::TraceResultBuffer* buffer,
const scoped_refptr<RefCountedString>& json,
bool has_more_events) {
buffer->AddFragment(json->data());
if (!has_more_events)
quit_closure.Run();
}
class BlameContextTest : public testing::Test {
public:
void StartTracing();
void StopTracing();
std::unique_ptr<trace_analyzer::TraceAnalyzer> CreateTraceAnalyzer();
protected:
MessageLoop loop_;
};
void BlameContextTest::StartTracing() {
trace_event::TraceLog::GetInstance()->SetEnabled(
trace_event::TraceConfig("*"), trace_event::TraceLog::RECORDING_MODE);
}
void BlameContextTest::StopTracing() {
trace_event::TraceLog::GetInstance()->SetDisabled();
}
std::unique_ptr<trace_analyzer::TraceAnalyzer>
BlameContextTest::CreateTraceAnalyzer() {
trace_event::TraceResultBuffer buffer;
trace_event::TraceResultBuffer::SimpleOutput trace_output;
buffer.SetOutputCallback(trace_output.GetCallback());
RunLoop run_loop;
buffer.Start();
trace_event::TraceLog::GetInstance()->Flush(
Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer)));
run_loop.Run();
buffer.Finish();
return WrapUnique(
trace_analyzer::TraceAnalyzer::Create(trace_output.json_output));
}
TEST_F(BlameContextTest, EnterAndLeave) {
using trace_analyzer::Query;
StartTracing();
{
TestBlameContext blame_context(0x1234);
blame_context.Initialize();
blame_context.Enter();
blame_context.Leave();
}
StopTracing();
std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer =
CreateTraceAnalyzer();
trace_analyzer::TraceEventVector events;
Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) ||
Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT);
analyzer->FindEvents(q, &events);
EXPECT_EQ(2u, events.size());
EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase);
EXPECT_EQ(kTestBlameContextCategory, events[0]->category);
EXPECT_EQ(kTestBlameContextName, events[0]->name);
EXPECT_EQ("0x1234", events[0]->id);
EXPECT_EQ(TRACE_EVENT_PHASE_LEAVE_CONTEXT, events[1]->phase);
EXPECT_EQ(kTestBlameContextCategory, events[1]->category);
EXPECT_EQ(kTestBlameContextName, events[1]->name);
EXPECT_EQ("0x1234", events[1]->id);
}
TEST_F(BlameContextTest, DifferentCategories) {
// Ensure there is no cross talk between blame contexts from different
// categories.
using trace_analyzer::Query;
StartTracing();
{
TestBlameContext blame_context(0x1234);
DisabledTestBlameContext disabled_blame_context(0x5678);
blame_context.Initialize();
blame_context.Enter();
blame_context.Leave();
disabled_blame_context.Initialize();
disabled_blame_context.Enter();
disabled_blame_context.Leave();
}
StopTracing();
std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer =
CreateTraceAnalyzer();
trace_analyzer::TraceEventVector events;
Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) ||
Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT);
analyzer->FindEvents(q, &events);
// None of the events from the disabled-by-default category should show up.
EXPECT_EQ(2u, events.size());
EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase);
EXPECT_EQ(kTestBlameContextCategory, events[0]->category);
EXPECT_EQ(kTestBlameContextName, events[0]->name);
EXPECT_EQ("0x1234", events[0]->id);
EXPECT_EQ(TRACE_EVENT_PHASE_LEAVE_CONTEXT, events[1]->phase);
EXPECT_EQ(kTestBlameContextCategory, events[1]->category);
EXPECT_EQ(kTestBlameContextName, events[1]->name);
EXPECT_EQ("0x1234", events[1]->id);
}
TEST_F(BlameContextTest, TakeSnapshot) {
using trace_analyzer::Query;
StartTracing();
{
TestBlameContext parent_blame_context(0x5678);
TestBlameContext blame_context(0x1234, parent_blame_context);
parent_blame_context.Initialize();
blame_context.Initialize();
blame_context.TakeSnapshot();
}
StopTracing();
std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer =
CreateTraceAnalyzer();
trace_analyzer::TraceEventVector events;
Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT);
analyzer->FindEvents(q, &events);
// We should have 3 snapshots: one for both calls to Initialize() and one from
// the explicit call to TakeSnapshot().
EXPECT_EQ(3u, events.size());
EXPECT_EQ(kTestBlameContextCategory, events[0]->category);
EXPECT_EQ(kTestBlameContextType, events[0]->name);
EXPECT_EQ("0x5678", events[0]->id);
EXPECT_TRUE(events[0]->HasArg("snapshot"));
EXPECT_EQ(kTestBlameContextCategory, events[1]->category);
EXPECT_EQ(kTestBlameContextType, events[1]->name);
EXPECT_EQ("0x1234", events[1]->id);
EXPECT_TRUE(events[0]->HasArg("snapshot"));
EXPECT_EQ(kTestBlameContextCategory, events[2]->category);
EXPECT_EQ(kTestBlameContextType, events[2]->name);
EXPECT_EQ("0x1234", events[2]->id);
EXPECT_TRUE(events[0]->HasArg("snapshot"));
const char kExpectedSnapshotJson[] =
"{"
"\"crossStreams\":false,"
"\"parent\":{"
"\"id_ref\":\"0x5678\","
"\"scope\":\"TestBlameContextScope\""
"}"
"}";
std::string snapshot_json;
JSONWriter::Write(*events[2]->GetKnownArgAsValue("snapshot"), &snapshot_json);
EXPECT_EQ(kExpectedSnapshotJson, snapshot_json);
}
} // namepace
} // namespace trace_event
} // namespace base