OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
| 6 #include "base/test/simple_test_tick_clock.h" |
6 #include "base/tracked_objects.h" | 7 #include "base/tracked_objects.h" |
7 #include "components/metrics/profiler/tracking_synchronizer.h" | 8 #include "components/metrics/profiler/tracking_synchronizer.h" |
8 #include "components/metrics/profiler/tracking_synchronizer_observer.h" | 9 #include "components/metrics/profiler/tracking_synchronizer_observer.h" |
9 #include "content/public/test/test_browser_thread_bundle.h" | 10 #include "content/public/test/test_browser_thread_bundle.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 using tracked_objects::ProcessDataPhaseSnapshot; | 13 using tracked_objects::ProcessDataPhaseSnapshot; |
13 using tracked_objects::TaskSnapshot; | 14 using tracked_objects::TaskSnapshot; |
14 | 15 |
15 namespace metrics { | 16 namespace metrics { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 class TestObserver : public TrackingSynchronizerObserver { | 20 class TestObserver : public TrackingSynchronizerObserver { |
20 public: | 21 public: |
21 TestObserver() {} | 22 TestObserver() {} |
22 | 23 |
23 ~TestObserver() override { | 24 ~TestObserver() override { |
24 EXPECT_TRUE(got_phase_0_); | 25 EXPECT_TRUE(got_phase_0_); |
25 EXPECT_TRUE(got_phase_1_); | 26 EXPECT_TRUE(got_phase_1_); |
26 } | 27 } |
27 | 28 |
28 void ReceivedProfilerData( | 29 void ReceivedProfilerData( |
29 const tracked_objects::ProcessDataPhaseSnapshot& process_data_phase, | 30 const tracked_objects::ProcessDataPhaseSnapshot& process_data_phase, |
30 base::ProcessId process_id, | 31 base::ProcessId process_id, |
31 content::ProcessType process_type, | 32 content::ProcessType process_type, |
32 int profiling_phase, | 33 int profiling_phase, |
33 base::TimeDelta phase_start, | 34 base::TimeTicks phase_start, |
34 base::TimeDelta phase_finish, | 35 base::TimeTicks phase_finish, |
35 const ProfilerEvents& past_events) override { | 36 const ProfilerEvents& past_events) override { |
36 EXPECT_EQ(static_cast<base::ProcessId>(239), process_id); | 37 EXPECT_EQ(static_cast<base::ProcessId>(239), process_id); |
37 EXPECT_EQ(content::ProcessType::PROCESS_TYPE_PLUGIN, process_type); | 38 EXPECT_EQ(content::ProcessType::PROCESS_TYPE_PLUGIN, process_type); |
38 ASSERT_EQ(1u, process_data_phase.tasks.size()); | 39 ASSERT_EQ(1u, process_data_phase.tasks.size()); |
39 | 40 |
40 switch (profiling_phase) { | 41 switch (profiling_phase) { |
41 case 0: | 42 case 0: |
42 EXPECT_FALSE(got_phase_0_); | 43 EXPECT_FALSE(got_phase_0_); |
43 got_phase_0_ = true; | 44 got_phase_0_ = true; |
44 | 45 |
45 EXPECT_EQ(base::TimeDelta::FromMilliseconds(0), phase_start); | 46 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(111), |
46 EXPECT_EQ(base::TimeDelta::FromMilliseconds(222), phase_finish); | 47 phase_start); |
| 48 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(333), |
| 49 phase_finish); |
47 | 50 |
48 EXPECT_EQ("death_thread0", | 51 EXPECT_EQ("death_thread0", |
49 process_data_phase.tasks[0].death_thread_name); | 52 process_data_phase.tasks[0].death_thread_name); |
50 EXPECT_EQ(0u, past_events.size()); | 53 EXPECT_EQ(0u, past_events.size()); |
51 break; | 54 break; |
52 | 55 |
53 case 1: | 56 case 1: |
54 EXPECT_FALSE(got_phase_1_); | 57 EXPECT_FALSE(got_phase_1_); |
55 got_phase_1_ = true; | 58 got_phase_1_ = true; |
56 | 59 |
57 EXPECT_EQ(base::TimeDelta::FromMilliseconds(222), phase_start); | 60 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(333), |
58 EXPECT_EQ(base::TimeDelta::FromMilliseconds(666), phase_finish); | 61 phase_start); |
| 62 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(777), |
| 63 phase_finish); |
59 | 64 |
60 EXPECT_EQ("death_thread1", | 65 EXPECT_EQ("death_thread1", |
61 process_data_phase.tasks[0].death_thread_name); | 66 process_data_phase.tasks[0].death_thread_name); |
62 ASSERT_EQ(1u, past_events.size()); | 67 ASSERT_EQ(1u, past_events.size()); |
63 EXPECT_EQ(ProfilerEventProto::EVENT_FIRST_NONEMPTY_PAINT, | 68 EXPECT_EQ(ProfilerEventProto::EVENT_FIRST_NONEMPTY_PAINT, |
64 past_events[0]); | 69 past_events[0]); |
65 break; | 70 break; |
66 | 71 |
67 default: | 72 default: |
68 bool profiling_phase_is_neither_0_nor_1 = true; | 73 bool profiling_phase_is_neither_0_nor_1 = true; |
69 EXPECT_FALSE(profiling_phase_is_neither_0_nor_1); | 74 EXPECT_FALSE(profiling_phase_is_neither_0_nor_1); |
70 } | 75 } |
71 } | 76 } |
72 | 77 |
73 private: | 78 private: |
74 bool got_phase_0_ = false; | 79 bool got_phase_0_ = false; |
75 bool got_phase_1_ = false; | 80 bool got_phase_1_ = false; |
76 | 81 |
77 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 82 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
78 }; | 83 }; |
79 | 84 |
80 base::TimeTicks TestTimeFromMs(int64 ms) { | 85 class TestTrackingSynchronizer : public TrackingSynchronizer { |
81 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(ms); | 86 public: |
82 } | 87 explicit TestTrackingSynchronizer(scoped_ptr<base::TickClock> clock) |
| 88 : TrackingSynchronizer(clock.Pass()) {} |
| 89 |
| 90 ~TestTrackingSynchronizer() {} |
| 91 |
| 92 using TrackingSynchronizer::RegisterPhaseCompletion; |
| 93 using TrackingSynchronizer::SendData; |
| 94 }; |
83 | 95 |
84 } // namespace | 96 } // namespace |
85 | 97 |
86 TEST(TrackingSynchronizerTest, ProfilerData) { | 98 TEST(TrackingSynchronizerTest, ProfilerData) { |
87 // Testing how TrackingSynchronizer reports 2 phases of profiling. | 99 // Testing how TrackingSynchronizer reports 2 phases of profiling. |
88 #if !defined(OS_IOS) | 100 #if !defined(OS_IOS) |
89 content::TestBrowserThreadBundle thread_bundle; | 101 content::TestBrowserThreadBundle thread_bundle; |
90 #endif | 102 #endif |
91 scoped_refptr<TrackingSynchronizer> tracking_synchronizer = | |
92 new TrackingSynchronizer(TestTimeFromMs(111)); | |
93 | 103 |
94 // Mimic a phase change event. | 104 auto clock = new base::SimpleTestTickClock(); // Will be owned by |
95 tracking_synchronizer->phase_completion_events_sequence_.push_back( | 105 // |tracking_synchronizer|. |
| 106 clock->Advance(base::TimeDelta::FromMilliseconds(111)); |
| 107 |
| 108 scoped_refptr<TestTrackingSynchronizer> tracking_synchronizer = |
| 109 new TestTrackingSynchronizer(make_scoped_ptr(clock)); |
| 110 |
| 111 clock->Advance(base::TimeDelta::FromMilliseconds(222)); |
| 112 |
| 113 tracking_synchronizer->RegisterPhaseCompletion( |
96 ProfilerEventProto::EVENT_FIRST_NONEMPTY_PAINT); | 114 ProfilerEventProto::EVENT_FIRST_NONEMPTY_PAINT); |
97 tracking_synchronizer->phase_start_times_.push_back(TestTimeFromMs(333)); | |
98 | 115 |
99 tracked_objects::ProcessDataSnapshot profiler_data; | 116 tracked_objects::ProcessDataSnapshot profiler_data; |
100 ProcessDataPhaseSnapshot snapshot0; | 117 ProcessDataPhaseSnapshot snapshot0; |
101 tracked_objects::TaskSnapshot task_snapshot0; | 118 tracked_objects::TaskSnapshot task_snapshot0; |
102 task_snapshot0.death_thread_name = "death_thread0"; | 119 task_snapshot0.death_thread_name = "death_thread0"; |
103 snapshot0.tasks.push_back(task_snapshot0); | 120 snapshot0.tasks.push_back(task_snapshot0); |
104 ProcessDataPhaseSnapshot snapshot1; | 121 ProcessDataPhaseSnapshot snapshot1; |
105 profiler_data.phased_process_data_snapshots[0] = snapshot0; | 122 profiler_data.phased_process_data_snapshots[0] = snapshot0; |
106 tracked_objects::TaskSnapshot task_snapshot1; | 123 tracked_objects::TaskSnapshot task_snapshot1; |
107 task_snapshot1.death_thread_name = "death_thread1"; | 124 task_snapshot1.death_thread_name = "death_thread1"; |
108 snapshot1.tasks.push_back(task_snapshot1); | 125 snapshot1.tasks.push_back(task_snapshot1); |
109 profiler_data.phased_process_data_snapshots[1] = snapshot1; | 126 profiler_data.phased_process_data_snapshots[1] = snapshot1; |
110 profiler_data.process_id = 239; | 127 profiler_data.process_id = 239; |
111 | 128 |
| 129 clock->Advance(base::TimeDelta::FromMilliseconds(444)); |
112 TestObserver test_observer; | 130 TestObserver test_observer; |
113 tracking_synchronizer->SendData(profiler_data, | 131 tracking_synchronizer->SendData( |
114 content::ProcessType::PROCESS_TYPE_PLUGIN, | 132 profiler_data, content::ProcessType::PROCESS_TYPE_PLUGIN, &test_observer); |
115 TestTimeFromMs(777), &test_observer); | |
116 } | 133 } |
117 | 134 |
118 } // namespace metrics | 135 } // namespace metrics |
OLD | NEW |