1 // Copyright 2015 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.
5 #include "base/memory/ref_counted.h"
6 #include "base/test/simple_test_tick_clock.h"
7 #include "base/tracked_objects.h"
8 #include "components/metrics/profiler/tracking_synchronizer.h"
9 #include "components/metrics/profiler/tracking_synchronizer_observer.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using tracked_objects::ProcessDataPhaseSnapshot
;
14 using tracked_objects::TaskSnapshot
;
20 class TestObserver
: public TrackingSynchronizerObserver
{
24 ~TestObserver() override
{
25 EXPECT_TRUE(got_phase_0_
);
26 EXPECT_TRUE(got_phase_1_
);
29 void ReceivedProfilerData(
30 const ProfilerDataAttributes
& attributes
,
31 const tracked_objects::ProcessDataPhaseSnapshot
& process_data_phase
,
32 const ProfilerEvents
& past_events
) override
{
33 EXPECT_EQ(static_cast<base::ProcessId
>(239), attributes
.process_id
);
34 EXPECT_EQ(content::ProcessType::PROCESS_TYPE_PLUGIN
,
35 attributes
.process_type
);
36 ASSERT_EQ(1u, process_data_phase
.tasks
.size());
38 switch (attributes
.profiling_phase
) {
40 EXPECT_FALSE(got_phase_0_
);
43 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(111),
44 attributes
.phase_start
);
45 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(333),
46 attributes
.phase_finish
);
48 EXPECT_EQ("death_thread0",
49 process_data_phase
.tasks
[0].death_thread_name
);
50 EXPECT_EQ(0u, past_events
.size());
54 EXPECT_FALSE(got_phase_1_
);
57 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(333),
58 attributes
.phase_start
);
59 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(777),
60 attributes
.phase_finish
);
62 EXPECT_EQ("death_thread1",
63 process_data_phase
.tasks
[0].death_thread_name
);
64 ASSERT_EQ(1u, past_events
.size());
65 EXPECT_EQ(ProfilerEventProto::EVENT_FIRST_NONEMPTY_PAINT
,
70 bool profiling_phase_is_neither_0_nor_1
= true;
71 EXPECT_FALSE(profiling_phase_is_neither_0_nor_1
);
76 bool got_phase_0_
= false;
77 bool got_phase_1_
= false;
79 DISALLOW_COPY_AND_ASSIGN(TestObserver
);
82 class TestTrackingSynchronizer
: public TrackingSynchronizer
{
84 explicit TestTrackingSynchronizer(scoped_ptr
<base::TickClock
> clock
)
85 : TrackingSynchronizer(clock
.Pass()) {}
87 using TrackingSynchronizer::RegisterPhaseCompletion
;
88 using TrackingSynchronizer::SendData
;
91 ~TestTrackingSynchronizer() override
{}
96 TEST(TrackingSynchronizerTest
, ProfilerData
) {
97 // Testing how TrackingSynchronizer reports 2 phases of profiling.
99 content::TestBrowserThreadBundle thread_bundle
;
102 auto clock
= new base::SimpleTestTickClock(); // Will be owned by
103 // |tracking_synchronizer|.
104 clock
->Advance(base::TimeDelta::FromMilliseconds(111));
106 scoped_refptr
<TestTrackingSynchronizer
> tracking_synchronizer
=
107 new TestTrackingSynchronizer(make_scoped_ptr(clock
));
109 clock
->Advance(base::TimeDelta::FromMilliseconds(222));
111 tracking_synchronizer
->RegisterPhaseCompletion(
112 ProfilerEventProto::EVENT_FIRST_NONEMPTY_PAINT
);
114 tracked_objects::ProcessDataSnapshot profiler_data
;
115 ProcessDataPhaseSnapshot snapshot0
;
116 tracked_objects::TaskSnapshot task_snapshot0
;
117 task_snapshot0
.death_thread_name
= "death_thread0";
118 snapshot0
.tasks
.push_back(task_snapshot0
);
119 ProcessDataPhaseSnapshot snapshot1
;
120 profiler_data
.phased_snapshots
[0] = snapshot0
;
121 tracked_objects::TaskSnapshot task_snapshot1
;
122 task_snapshot1
.death_thread_name
= "death_thread1";
123 snapshot1
.tasks
.push_back(task_snapshot1
);
124 profiler_data
.phased_snapshots
[1] = snapshot1
;
125 profiler_data
.process_id
= 239;
127 clock
->Advance(base::TimeDelta::FromMilliseconds(444));
128 TestObserver test_observer
;
129 tracking_synchronizer
->SendData(
130 profiler_data
, content::ProcessType::PROCESS_TYPE_PLUGIN
, &test_observer
);
133 } // namespace metrics