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
|
// Copyright 2015 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 "components/metrics/profiler/tracking_synchronizer.h"
#include <utility>
#include "base/bind.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/tracked_objects.h"
#include "components/metrics/profiler/tracking_synchronizer_delegate.h"
#include "components/metrics/profiler/tracking_synchronizer_observer.h"
#include "testing/gtest/include/gtest/gtest.h"
using tracked_objects::ProcessDataPhaseSnapshot;
using tracked_objects::TaskSnapshot;
namespace metrics {
namespace {
class TestDelegate : public TrackingSynchronizerDelegate {
public:
~TestDelegate() override {}
static scoped_ptr<TrackingSynchronizerDelegate> Create(
TrackingSynchronizer* synchronizer) {
return make_scoped_ptr(new TestDelegate());
}
private:
TestDelegate() {}
// TrackingSynchronizerDelegate:
void GetProfilerDataForChildProcesses(int sequence_number,
int current_profiling_phase) override {}
void OnProfilingPhaseCompleted(int profiling_phase) override {}
};
class TestObserver : public TrackingSynchronizerObserver {
public:
TestObserver() {}
~TestObserver() override {
EXPECT_TRUE(got_phase_0_);
EXPECT_TRUE(got_phase_1_);
}
void ReceivedProfilerData(
const ProfilerDataAttributes& attributes,
const tracked_objects::ProcessDataPhaseSnapshot& process_data_phase,
const ProfilerEvents& past_events) override {
EXPECT_EQ(static_cast<base::ProcessId>(239), attributes.process_id);
EXPECT_EQ(ProfilerEventProto::TrackedObject::PLUGIN,
attributes.process_type);
ASSERT_EQ(1u, process_data_phase.tasks.size());
switch (attributes.profiling_phase) {
case 0:
EXPECT_FALSE(got_phase_0_);
got_phase_0_ = true;
EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(111),
attributes.phase_start);
EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(333),
attributes.phase_finish);
EXPECT_EQ("death_thread0",
process_data_phase.tasks[0].death_thread_name);
EXPECT_EQ(0u, past_events.size());
break;
case 1:
EXPECT_FALSE(got_phase_1_);
got_phase_1_ = true;
EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(333),
attributes.phase_start);
EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(777),
attributes.phase_finish);
EXPECT_EQ("death_thread1",
process_data_phase.tasks[0].death_thread_name);
ASSERT_EQ(1u, past_events.size());
EXPECT_EQ(ProfilerEventProto::EVENT_FIRST_NONEMPTY_PAINT,
past_events[0]);
break;
default:
bool profiling_phase_is_neither_0_nor_1 = true;
EXPECT_FALSE(profiling_phase_is_neither_0_nor_1);
}
}
private:
bool got_phase_0_ = false;
bool got_phase_1_ = false;
DISALLOW_COPY_AND_ASSIGN(TestObserver);
};
class TestTrackingSynchronizer : public TrackingSynchronizer {
public:
explicit TestTrackingSynchronizer(scoped_ptr<base::TickClock> clock)
: TrackingSynchronizer(std::move(clock),
base::Bind(&TestDelegate::Create)) {}
using TrackingSynchronizer::RegisterPhaseCompletion;
using TrackingSynchronizer::SendData;
private:
~TestTrackingSynchronizer() override {}
};
} // namespace
TEST(TrackingSynchronizerTest, ProfilerData) {
// Testing how TrackingSynchronizer reports 2 phases of profiling.
auto clock = new base::SimpleTestTickClock(); // Will be owned by
// |tracking_synchronizer|.
clock->Advance(base::TimeDelta::FromMilliseconds(111));
scoped_refptr<TestTrackingSynchronizer> tracking_synchronizer =
new TestTrackingSynchronizer(make_scoped_ptr(clock));
clock->Advance(base::TimeDelta::FromMilliseconds(222));
tracking_synchronizer->RegisterPhaseCompletion(
ProfilerEventProto::EVENT_FIRST_NONEMPTY_PAINT);
tracked_objects::ProcessDataSnapshot profiler_data;
ProcessDataPhaseSnapshot snapshot0;
tracked_objects::TaskSnapshot task_snapshot0;
task_snapshot0.death_thread_name = "death_thread0";
snapshot0.tasks.push_back(task_snapshot0);
ProcessDataPhaseSnapshot snapshot1;
profiler_data.phased_snapshots[0] = snapshot0;
tracked_objects::TaskSnapshot task_snapshot1;
task_snapshot1.death_thread_name = "death_thread1";
snapshot1.tasks.push_back(task_snapshot1);
profiler_data.phased_snapshots[1] = snapshot1;
profiler_data.process_id = 239;
clock->Advance(base::TimeDelta::FromMilliseconds(444));
TestObserver test_observer;
tracking_synchronizer->SendData(
profiler_data, ProfilerEventProto::TrackedObject::PLUGIN, &test_observer);
}
} // namespace metrics
|