OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Test of classes in the tracked_objects.h classes. | 5 // Test of classes in the tracked_objects.h classes. |
6 | 6 |
7 #include "base/tracked_objects.h" | 7 #include "base/tracked_objects.h" |
8 | 8 |
9 #include "base/json/json_writer.h" | |
10 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/process_util.h" | |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace tracked_objects { | 14 namespace tracked_objects { |
15 | 15 |
16 class TrackedObjectsTest : public testing::Test { | 16 class TrackedObjectsTest : public testing::Test { |
17 protected: | 17 protected: |
18 TrackedObjectsTest() { | 18 TrackedObjectsTest() { |
19 // On entry, leak any database structures in case they are still in use by | 19 // On entry, leak any database structures in case they are still in use by |
20 // prior threads. | 20 // prior threads. |
21 ThreadData::ShutdownSingleThreadedCleanup(true); | 21 ThreadData::ShutdownSingleThreadedCleanup(true); |
22 } | 22 } |
23 | 23 |
24 ~TrackedObjectsTest() { | 24 ~TrackedObjectsTest() { |
25 // We should not need to leak any structures we create, since we are | 25 // We should not need to leak any structures we create, since we are |
26 // single threaded, and carefully accounting for items. | 26 // single threaded, and carefully accounting for items. |
27 ThreadData::ShutdownSingleThreadedCleanup(false); | 27 ThreadData::ShutdownSingleThreadedCleanup(false); |
28 } | 28 } |
29 | 29 |
30 // Provide access, since this class is a friend of ThreadData. | 30 // Reset the profiler state. |
31 void ShutdownSingleThreadedCleanup(bool leak) { | 31 void Reset() { |
jar (doing other things)
2012/04/09 23:32:59
Your name is much better... but I was worried that
Ilya Sherman
2012/04/10 00:37:42
Yeah, I agree that we don't want to propagate this
| |
32 ThreadData::ShutdownSingleThreadedCleanup(leak); | 32 ThreadData::ShutdownSingleThreadedCleanup(false); |
33 } | 33 } |
34 }; | 34 }; |
35 | 35 |
36 TEST_F(TrackedObjectsTest, MinimalStartupShutdown) { | 36 TEST_F(TrackedObjectsTest, MinimalStartupShutdown) { |
37 // Minimal test doesn't even create any tasks. | 37 // Minimal test doesn't even create any tasks. |
38 if (!ThreadData::InitializeAndSetTrackingStatus( | 38 if (!ThreadData::InitializeAndSetTrackingStatus( |
39 ThreadData::PROFILING_CHILDREN_ACTIVE)) | 39 ThreadData::PROFILING_CHILDREN_ACTIVE)) |
40 return; | 40 return; |
41 | 41 |
42 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. | 42 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. |
43 ThreadData* data = ThreadData::Get(); | 43 ThreadData* data = ThreadData::Get(); |
44 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. | 44 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. |
45 EXPECT_TRUE(data); | 45 ASSERT_TRUE(data); |
46 EXPECT_TRUE(!data->next()); | 46 EXPECT_FALSE(data->next()); |
47 EXPECT_EQ(data, ThreadData::Get()); | 47 EXPECT_EQ(data, ThreadData::Get()); |
48 ThreadData::BirthMap birth_map; | 48 ThreadData::BirthMap birth_map; |
49 ThreadData::DeathMap death_map; | 49 ThreadData::DeathMap death_map; |
50 ThreadData::ParentChildSet parent_child_set; | 50 ThreadData::ParentChildSet parent_child_set; |
51 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set); | 51 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set); |
52 EXPECT_EQ(0u, birth_map.size()); | 52 EXPECT_EQ(0u, birth_map.size()); |
53 EXPECT_EQ(0u, death_map.size()); | 53 EXPECT_EQ(0u, death_map.size()); |
54 EXPECT_EQ(0u, parent_child_set.size()); | 54 EXPECT_EQ(0u, parent_child_set.size()); |
55 // Cleanup with no leaking. | 55 |
56 ShutdownSingleThreadedCleanup(false); | 56 // Clean up with no leaking. |
57 Reset(); | |
57 | 58 |
58 // Do it again, just to be sure we reset state completely. | 59 // Do it again, just to be sure we reset state completely. |
59 ThreadData::InitializeAndSetTrackingStatus( | 60 EXPECT_TRUE(ThreadData::InitializeAndSetTrackingStatus( |
60 ThreadData::PROFILING_CHILDREN_ACTIVE); | 61 ThreadData::PROFILING_CHILDREN_ACTIVE)); |
61 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. | 62 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. |
62 data = ThreadData::Get(); | 63 data = ThreadData::Get(); |
63 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. | 64 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. |
64 EXPECT_TRUE(data); | 65 ASSERT_TRUE(data); |
65 EXPECT_TRUE(!data->next()); | 66 EXPECT_FALSE(data->next()); |
66 EXPECT_EQ(data, ThreadData::Get()); | 67 EXPECT_EQ(data, ThreadData::Get()); |
67 birth_map.clear(); | 68 birth_map.clear(); |
68 death_map.clear(); | 69 death_map.clear(); |
70 parent_child_set.clear(); | |
69 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set); | 71 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set); |
70 EXPECT_EQ(0u, birth_map.size()); | 72 EXPECT_EQ(0u, birth_map.size()); |
71 EXPECT_EQ(0u, death_map.size()); | 73 EXPECT_EQ(0u, death_map.size()); |
72 EXPECT_EQ(0u, parent_child_set.size()); | 74 EXPECT_EQ(0u, parent_child_set.size()); |
73 } | 75 } |
74 | 76 |
75 TEST_F(TrackedObjectsTest, TinyStartupShutdown) { | 77 TEST_F(TrackedObjectsTest, TinyStartupShutdown) { |
76 if (!ThreadData::InitializeAndSetTrackingStatus( | 78 if (!ThreadData::InitializeAndSetTrackingStatus( |
77 ThreadData::PROFILING_CHILDREN_ACTIVE)) | 79 ThreadData::PROFILING_CHILDREN_ACTIVE)) |
78 return; | 80 return; |
79 | 81 |
80 // Instigate tracking on a single tracked object, on our thread. | 82 // Instigate tracking on a single tracked object, on our thread. |
81 const Location& location = FROM_HERE; | 83 const Location& location = FROM_HERE; |
82 Births* first_birth = ThreadData::TallyABirthIfActive(location); | 84 Births* first_birth = ThreadData::TallyABirthIfActive(location); |
83 | 85 |
84 ThreadData* data = ThreadData::first(); | 86 ThreadData* data = ThreadData::first(); |
85 ASSERT_TRUE(data); | 87 ASSERT_TRUE(data); |
86 EXPECT_TRUE(!data->next()); | 88 EXPECT_FALSE(data->next()); |
87 EXPECT_EQ(data, ThreadData::Get()); | 89 EXPECT_EQ(data, ThreadData::Get()); |
88 ThreadData::BirthMap birth_map; | 90 ThreadData::BirthMap birth_map; |
89 ThreadData::DeathMap death_map; | 91 ThreadData::DeathMap death_map; |
90 ThreadData::ParentChildSet parent_child_set; | 92 ThreadData::ParentChildSet parent_child_set; |
91 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set); | 93 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set); |
92 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. | 94 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. |
93 EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth. | 95 EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth. |
94 EXPECT_EQ(0u, death_map.size()); // No deaths. | 96 EXPECT_EQ(0u, death_map.size()); // No deaths. |
95 EXPECT_EQ(0u, parent_child_set.size()); // No children. | 97 EXPECT_EQ(0u, parent_child_set.size()); // No children. |
96 | 98 |
97 | 99 |
98 // Now instigate another birth, while we are timing the run of the first | 100 // Now instigate another birth, while we are timing the run of the first |
99 // execution. | 101 // execution. |
100 TrackedTime start_time = | 102 TrackedTime start_time = ThreadData::NowForStartOfRun(first_birth); |
101 ThreadData::NowForStartOfRun(first_birth); | |
102 // Create a child (using the same birth location). | 103 // Create a child (using the same birth location). |
103 // TrackingInfo will call TallyABirth() during construction. | 104 // TrackingInfo will call TallyABirth() during construction. |
104 base::TimeTicks kBogusBirthTime; | 105 base::TimeTicks kBogusBirthTime; |
105 base::TrackingInfo pending_task(location, kBogusBirthTime); | 106 base::TrackingInfo pending_task(location, kBogusBirthTime); |
106 // Finally conclude the outer run. | 107 // Finally conclude the outer run. |
107 TrackedTime end_time = ThreadData::NowForEndOfRun(); | 108 TrackedTime end_time = ThreadData::NowForEndOfRun(); |
108 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, start_time, | 109 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, start_time, |
109 end_time); | 110 end_time); |
110 | 111 |
111 birth_map.clear(); | 112 birth_map.clear(); |
112 death_map.clear(); | 113 death_map.clear(); |
113 parent_child_set.clear(); | 114 parent_child_set.clear(); |
114 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set); | 115 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set); |
115 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. | 116 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. |
116 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births. | 117 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births. |
117 EXPECT_EQ(1u, death_map.size()); // 1 location. | 118 EXPECT_EQ(1u, death_map.size()); // 1 location. |
118 EXPECT_EQ(1, death_map.begin()->second.count()); // 1 death. | 119 EXPECT_EQ(1, death_map.begin()->second.count()); // 1 death. |
119 if (ThreadData::TrackingParentChildStatus()) { | 120 if (ThreadData::TrackingParentChildStatus()) { |
120 EXPECT_EQ(1u, parent_child_set.size()); // 1 child. | 121 EXPECT_EQ(1u, parent_child_set.size()); // 1 child. |
121 EXPECT_EQ(parent_child_set.begin()->first, | 122 EXPECT_EQ(parent_child_set.begin()->first, |
122 parent_child_set.begin()->second); | 123 parent_child_set.begin()->second); |
123 } else { | 124 } else { |
124 EXPECT_EQ(0u, parent_child_set.size()); // no stats. | 125 EXPECT_EQ(0u, parent_child_set.size()); // no stats. |
125 } | 126 } |
126 | 127 |
127 // The births were at the same location as the one known death. | 128 // The births were at the same location as the one known death. |
128 EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first); | 129 EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first); |
129 } | 130 } |
130 | 131 |
131 TEST_F(TrackedObjectsTest, ParentChildTest) { | 132 TEST_F(TrackedObjectsTest, ParentChildTest) { |
jar (doing other things)
2012/04/10 15:09:36
It looks like much of this test (and certainly its
Ilya Sherman
2012/04/10 20:34:40
This test just added additional EXPECTations to th
| |
132 if (!ThreadData::InitializeAndSetTrackingStatus( | 133 if (!ThreadData::InitializeAndSetTrackingStatus( |
133 ThreadData::PROFILING_CHILDREN_ACTIVE)) | 134 ThreadData::PROFILING_CHILDREN_ACTIVE)) |
134 return; | 135 return; |
135 if (!ThreadData::TrackingParentChildStatus()) | 136 if (!ThreadData::TrackingParentChildStatus()) |
136 return; // Feature not compiled in. | 137 return; // Feature not compiled in. |
137 | 138 |
138 // Instigate tracking on a single tracked object, on our thread. | 139 // Instigate tracking on a single tracked object, on our thread. |
139 const int kFakeLineNumber = 1776; | 140 const int kFakeLineNumber = 1776; |
140 const char* kFile = "FixedUnitTestFileName"; | 141 const char* kFile = "FixedUnitTestFileName"; |
141 const char* kFunction = "ParentChildTest"; | 142 const char* kFunction = "ParentChildTest"; |
142 Location location(kFunction, kFile, kFakeLineNumber, NULL); | 143 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
144 Births* first_birth = ThreadData::TallyABirthIfActive(location); | |
jar (doing other things)
2012/04/09 23:32:59
Hmm... it looks like I left this out :-(. Good ca
| |
143 | 145 |
144 // Now instigate another birth, while we are timing the run of the first | 146 // Now instigate another birth, while we are timing the run of the first |
145 // execution. | 147 // execution. |
146 | 148 TrackedTime start_time = ThreadData::NowForStartOfRun(first_birth); |
147 // Create a child (using the same birth location). | 149 // Create a child (using the same birth location). |
148 // TrackingInfo will call TallyABirth() during construction. | 150 // TrackingInfo will call TallyABirth() during construction. |
149 base::TimeTicks kBogusBirthTime; | 151 base::TimeTicks kBogusBirthTime; |
150 base::TrackingInfo pending_task(location, kBogusBirthTime); | 152 base::TrackingInfo pending_task(location, kBogusBirthTime); |
153 // Finally conclude the outer run. | |
154 TrackedTime end_time = ThreadData::NowForEndOfRun(); | |
155 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, start_time, | |
156 end_time); | |
157 | |
151 | 158 |
152 // Don't conclude the run, so that we don't use the actual timer that we | 159 // Don't conclude the run, so that we don't use the actual timer that we |
jar (doing other things)
2012/04/09 23:32:59
It looks like you concluded the run (contrary to t
Ilya Sherman
2012/04/10 00:37:42
Removed this comment in a more recent patch set :)
| |
153 // started for the outer profile. This way the JSON will not include some | 160 // started for the outer profile. This way the JSON will not include some |
154 // random time. | 161 // random time. |
155 ThreadData* data = ThreadData::first(); | 162 ThreadData* data = ThreadData::first(); |
156 ASSERT_TRUE(data); | 163 ASSERT_TRUE(data); |
157 EXPECT_TRUE(!data->next()); | 164 EXPECT_FALSE(data->next()); |
158 EXPECT_EQ(data, ThreadData::Get()); | 165 EXPECT_EQ(data, ThreadData::Get()); |
159 | 166 |
160 ThreadData::BirthMap birth_map; | 167 ThreadData::BirthMap birth_map; |
161 ThreadData::DeathMap death_map; | 168 ThreadData::DeathMap death_map; |
162 ThreadData::ParentChildSet parent_child_set; | 169 ThreadData::ParentChildSet parent_child_set; |
163 | 170 |
164 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set); | 171 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set); |
165 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. | 172 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. |
166 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births. | 173 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births. |
167 EXPECT_EQ(0u, death_map.size()); // No status yet. | 174 EXPECT_EQ(1u, death_map.size()); // No status yet. |
168 // Just like TinyStartupShutdown test. | 175 // Just like TinyStartupShutdown test. |
169 EXPECT_EQ(1u, parent_child_set.size()); // 1 child. | 176 EXPECT_EQ(1u, parent_child_set.size()); // 1 child. |
170 EXPECT_EQ(parent_child_set.begin()->first, | 177 EXPECT_EQ(parent_child_set.begin()->first, |
171 parent_child_set.begin()->second); | 178 parent_child_set.begin()->second); |
172 | 179 |
173 scoped_ptr<base::Value> value(ThreadData::ToValue(false)); | 180 ProcessDataSnapshot process_data; |
174 std::string json; | 181 ThreadData::Snapshot(false, &process_data); |
175 base::JSONWriter::Write(value.get(), &json); | 182 |
176 std::string birth_only_result = "{" | 183 ASSERT_EQ(1u, process_data.tasks.size()); |
177 "\"descendants\":[" | 184 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name); |
178 "{" | 185 EXPECT_EQ(kFunction, process_data.tasks[0].birth.location.function_name); |
179 "\"child_location\":{" | 186 EXPECT_EQ(kFakeLineNumber, process_data.tasks[0].birth.location.line_number); |
180 "\"file_name\":\"FixedUnitTestFileName\"," | 187 EXPECT_EQ("WorkerThread-1", process_data.tasks[0].birth.thread_name); |
181 "\"function_name\":\"ParentChildTest\"," | 188 EXPECT_EQ(1, process_data.tasks[0].death_data.count); |
182 "\"line_number\":1776" | 189 EXPECT_EQ(0, process_data.tasks[0].death_data.run_duration_sum); |
jar (doing other things)
2012/04/09 23:32:59
I *suspect* these times are 0 only because the dur
Ilya Sherman
2012/04/10 00:37:42
Good catch! Fixed.
| |
183 "}," | 190 EXPECT_EQ(0, process_data.tasks[0].death_data.run_duration_max); |
184 "\"child_thread\":\"WorkerThread-1\"," | 191 EXPECT_EQ(0, process_data.tasks[0].death_data.run_duration_sample); |
185 "\"parent_location\":{" | 192 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_sum); |
186 "\"file_name\":\"FixedUnitTestFileName\"," | 193 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_max); |
187 "\"function_name\":\"ParentChildTest\"," | 194 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_sample); |
188 "\"line_number\":1776" | 195 EXPECT_EQ("WorkerThread-1", process_data.tasks[0].death_thread_name); |
189 "}," | 196 |
190 "\"parent_thread\":\"WorkerThread-1\"" | 197 ASSERT_EQ(1u, process_data.descendants.size()); |
191 "}" | 198 EXPECT_EQ(kFile, process_data.descendants[0].parent.location.file_name); |
192 "]," | 199 EXPECT_EQ(kFunction, |
193 "\"list\":[" | 200 process_data.descendants[0].parent.location.function_name); |
194 "{" | 201 EXPECT_EQ(kFakeLineNumber, |
195 "\"birth_thread\":\"WorkerThread-1\"," | 202 process_data.descendants[0].parent.location.line_number); |
196 "\"death_data\":{" | 203 EXPECT_EQ("WorkerThread-1", process_data.descendants[0].parent.thread_name); |
197 "\"count\":2," | 204 EXPECT_EQ(kFile, process_data.descendants[0].child.location.file_name); |
198 "\"queue_ms\":0," | 205 EXPECT_EQ(kFunction, |
199 "\"queue_ms_max\":0," | 206 process_data.descendants[0].child.location.function_name); |
200 "\"queue_ms_sample\":0," | 207 EXPECT_EQ(kFakeLineNumber, |
201 "\"run_ms\":0," | 208 process_data.descendants[0].child.location.line_number); |
202 "\"run_ms_max\":0," | 209 EXPECT_EQ("WorkerThread-1", process_data.descendants[0].child.thread_name); |
203 "\"run_ms_sample\":0" | |
204 "}," | |
205 "\"death_thread\":\"Still_Alive\"," | |
206 "\"location\":{" | |
207 "\"file_name\":\"FixedUnitTestFileName\"," | |
208 "\"function_name\":\"ParentChildTest\"," | |
209 "\"line_number\":1776" | |
210 "}" | |
211 "}" | |
212 "]" | |
213 "}"; | |
214 EXPECT_EQ(json, birth_only_result); | |
215 } | 210 } |
216 | 211 |
217 TEST_F(TrackedObjectsTest, DeathDataTest) { | 212 TEST_F(TrackedObjectsTest, DeathDataTest) { |
218 if (!ThreadData::InitializeAndSetTrackingStatus( | 213 if (!ThreadData::InitializeAndSetTrackingStatus( |
219 ThreadData::PROFILING_CHILDREN_ACTIVE)) | 214 ThreadData::PROFILING_CHILDREN_ACTIVE)) |
220 return; | 215 return; |
221 | 216 |
222 scoped_ptr<DeathData> data(new DeathData()); | 217 scoped_ptr<DeathData> data(new DeathData()); |
223 ASSERT_NE(data, reinterpret_cast<DeathData*>(NULL)); | 218 ASSERT_NE(data, reinterpret_cast<DeathData*>(NULL)); |
224 EXPECT_EQ(data->run_duration_sum(), 0); | 219 EXPECT_EQ(data->run_duration_sum(), 0); |
225 EXPECT_EQ(data->run_duration_sample(), 0); | 220 EXPECT_EQ(data->run_duration_sample(), 0); |
226 EXPECT_EQ(data->queue_duration_sum(), 0); | 221 EXPECT_EQ(data->queue_duration_sum(), 0); |
227 EXPECT_EQ(data->queue_duration_sample(), 0); | 222 EXPECT_EQ(data->queue_duration_sample(), 0); |
228 EXPECT_EQ(data->count(), 0); | 223 EXPECT_EQ(data->count(), 0); |
229 | 224 |
230 int32 run_ms = 42; | 225 int32 run_ms = 42; |
231 int32 queue_ms = 8; | 226 int32 queue_ms = 8; |
232 | 227 |
233 const int kUnrandomInt = 0; // Fake random int that ensure we sample data. | 228 const int kUnrandomInt = 0; // Fake random int that ensure we sample data. |
234 data->RecordDeath(queue_ms, run_ms, kUnrandomInt); | 229 data->RecordDeath(queue_ms, run_ms, kUnrandomInt); |
235 EXPECT_EQ(data->run_duration_sum(), run_ms); | 230 EXPECT_EQ(data->run_duration_sum(), run_ms); |
236 EXPECT_EQ(data->run_duration_sample(), run_ms); | 231 EXPECT_EQ(data->run_duration_sample(), run_ms); |
237 EXPECT_EQ(data->queue_duration_sum(), queue_ms); | 232 EXPECT_EQ(data->queue_duration_sum(), queue_ms); |
238 EXPECT_EQ(data->queue_duration_sample(), queue_ms); | 233 EXPECT_EQ(data->queue_duration_sample(), queue_ms); |
239 EXPECT_EQ(data->count(), 1); | 234 EXPECT_EQ(data->count(), 1); |
240 | 235 |
241 data->RecordDeath(queue_ms, run_ms, kUnrandomInt); | 236 data->RecordDeath(queue_ms, run_ms, kUnrandomInt); |
242 EXPECT_EQ(data->run_duration_sum(), run_ms + run_ms); | 237 EXPECT_EQ(data->run_duration_sum(), run_ms + run_ms); |
243 EXPECT_EQ(data->run_duration_sample(), run_ms); | 238 EXPECT_EQ(data->run_duration_sample(), run_ms); |
244 EXPECT_EQ(data->queue_duration_sum(), queue_ms + queue_ms); | 239 EXPECT_EQ(data->queue_duration_sum(), queue_ms + queue_ms); |
245 EXPECT_EQ(data->queue_duration_sample(), queue_ms); | 240 EXPECT_EQ(data->queue_duration_sample(), queue_ms); |
246 EXPECT_EQ(data->count(), 2); | 241 EXPECT_EQ(data->count(), 2); |
247 | 242 |
248 scoped_ptr<base::DictionaryValue> dictionary(data->ToValue()); | 243 DeathDataSnapshot snapshot(*data); |
249 int integer; | 244 EXPECT_EQ(2, snapshot.count); |
250 EXPECT_TRUE(dictionary->GetInteger("run_ms", &integer)); | 245 EXPECT_EQ(2 * run_ms, snapshot.run_duration_sum); |
251 EXPECT_EQ(integer, 2 * run_ms); | 246 EXPECT_EQ(run_ms, snapshot.run_duration_max); |
252 EXPECT_TRUE(dictionary->GetInteger("run_ms_sample", &integer)); | 247 EXPECT_EQ(run_ms, snapshot.run_duration_sample); |
253 EXPECT_EQ(integer, run_ms); | 248 EXPECT_EQ(2 * queue_ms, snapshot.queue_duration_sum); |
254 EXPECT_TRUE(dictionary->GetInteger("queue_ms", &integer)); | 249 EXPECT_EQ(queue_ms, snapshot.queue_duration_max); |
255 EXPECT_EQ(integer, 2 * queue_ms); | 250 EXPECT_EQ(queue_ms, snapshot.queue_duration_sample); |
256 EXPECT_TRUE(dictionary->GetInteger("queue_ms_sample", &integer)); | |
257 EXPECT_EQ(integer, queue_ms); | |
258 EXPECT_TRUE(dictionary->GetInteger("count", &integer)); | |
259 EXPECT_EQ(integer, 2); | |
260 | |
261 scoped_ptr<base::Value> value(data->ToValue()); | |
262 std::string json; | |
263 base::JSONWriter::Write(value.get(), &json); | |
264 std::string birth_only_result = "{" | |
265 "\"count\":2," | |
266 "\"queue_ms\":16," | |
267 "\"queue_ms_max\":8," | |
268 "\"queue_ms_sample\":8," | |
269 "\"run_ms\":84," | |
270 "\"run_ms_max\":42," | |
271 "\"run_ms_sample\":42" | |
272 "}"; | |
273 EXPECT_EQ(birth_only_result, json); | |
274 } | 251 } |
275 | 252 |
276 TEST_F(TrackedObjectsTest, DeactivatedBirthOnlyToValueWorkerThread) { | 253 TEST_F(TrackedObjectsTest, DeactivatedBirthOnlyToSnapshotWorkerThread) { |
277 // Transition to Deactivated state before doing anything. | 254 // Transition to Deactivated state before doing anything. |
278 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) | 255 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) |
279 return; | 256 return; |
257 | |
280 // We don't initialize system with a thread name, so we're viewed as a worker | 258 // We don't initialize system with a thread name, so we're viewed as a worker |
281 // thread. | 259 // thread. |
282 const int kFakeLineNumber = 173; | 260 const int kFakeLineNumber = 173; |
283 const char* kFile = "FixedFileName"; | 261 const char* kFile = "FixedFileName"; |
284 const char* kFunction = "BirthOnlyToValueWorkerThread"; | 262 const char* kFunction = "BirthOnlyToSnapshotWorkerThread"; |
285 Location location(kFunction, kFile, kFakeLineNumber, NULL); | 263 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
286 Births* birth = ThreadData::TallyABirthIfActive(location); | 264 Births* birth = ThreadData::TallyABirthIfActive(location); |
287 // We should now see a NULL birth record. | 265 // We should now see a NULL birth record. |
288 EXPECT_EQ(birth, reinterpret_cast<Births*>(NULL)); | 266 EXPECT_EQ(reinterpret_cast<Births*>(NULL), birth); |
289 | 267 |
290 scoped_ptr<base::Value> value(ThreadData::ToValue(false)); | 268 ProcessDataSnapshot process_data; |
291 std::string json; | 269 ThreadData::Snapshot(false, &process_data); |
292 base::JSONWriter::Write(value.get(), &json); | 270 EXPECT_EQ(0u, process_data.tasks.size()); |
293 std::string birth_only_result = "{" | 271 EXPECT_EQ(0u, process_data.descendants.size()); |
294 "\"descendants\":[" | 272 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); |
295 "]," | |
296 "\"list\":[" | |
297 "]" | |
298 "}"; | |
299 EXPECT_EQ(json, birth_only_result); | |
300 } | 273 } |
301 | 274 |
302 TEST_F(TrackedObjectsTest, DeactivatedBirthOnlyToValueMainThread) { | 275 TEST_F(TrackedObjectsTest, DeactivatedBirthOnlyToSnapshotMainThread) { |
303 // Start in the deactivated state. | 276 // Start in the deactivated state. |
304 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) | 277 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) |
305 return; | 278 return; |
306 | 279 |
307 // Use a well named thread. | 280 // Use a well named thread. |
308 ThreadData::InitializeThreadContext("SomeMainThreadName"); | 281 ThreadData::InitializeThreadContext("SomeMainThreadName"); |
309 const int kFakeLineNumber = 173; | 282 const int kFakeLineNumber = 173; |
310 const char* kFile = "FixedFileName"; | 283 const char* kFile = "FixedFileName"; |
311 const char* kFunction = "BirthOnlyToValueMainThread"; | 284 const char* kFunction = "BirthOnlyToSnapshotMainThread"; |
312 Location location(kFunction, kFile, kFakeLineNumber, NULL); | 285 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
313 // Do not delete birth. We don't own it. | 286 // Do not delete birth. We don't own it. |
314 Births* birth = ThreadData::TallyABirthIfActive(location); | 287 Births* birth = ThreadData::TallyABirthIfActive(location); |
315 // We expect to not get a birth record. | 288 // We expect to not get a birth record. |
316 EXPECT_EQ(birth, reinterpret_cast<Births*>(NULL)); | 289 EXPECT_EQ(reinterpret_cast<Births*>(NULL), birth); |
317 | 290 |
318 scoped_ptr<base::Value> value(ThreadData::ToValue(false)); | 291 ProcessDataSnapshot process_data; |
319 std::string json; | 292 ThreadData::Snapshot(false, &process_data); |
320 base::JSONWriter::Write(value.get(), &json); | 293 EXPECT_EQ(0u, process_data.tasks.size()); |
321 std::string birth_only_result = "{" | 294 EXPECT_EQ(0u, process_data.descendants.size()); |
322 "\"descendants\":[" | 295 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); |
323 "]," | |
324 "\"list\":[" | |
325 "]" | |
326 "}"; | |
327 EXPECT_EQ(json, birth_only_result); | |
328 } | 296 } |
329 | 297 |
330 TEST_F(TrackedObjectsTest, BirthOnlyToValueWorkerThread) { | 298 TEST_F(TrackedObjectsTest, BirthOnlyToSnapshotWorkerThread) { |
331 if (!ThreadData::InitializeAndSetTrackingStatus( | 299 if (!ThreadData::InitializeAndSetTrackingStatus( |
332 ThreadData::PROFILING_CHILDREN_ACTIVE)) | 300 ThreadData::PROFILING_CHILDREN_ACTIVE)) |
333 return; | 301 return; |
302 | |
334 // We don't initialize system with a thread name, so we're viewed as a worker | 303 // We don't initialize system with a thread name, so we're viewed as a worker |
335 // thread. | 304 // thread. |
336 const int kFakeLineNumber = 173; | 305 const int kFakeLineNumber = 173; |
337 const char* kFile = "FixedFileName"; | 306 const char* kFile = "FixedFileName"; |
338 const char* kFunction = "BirthOnlyToValueWorkerThread"; | 307 const char* kFunction = "BirthOnlyToSnapshotWorkerThread"; |
339 Location location(kFunction, kFile, kFakeLineNumber, NULL); | 308 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
340 Births* birth = ThreadData::TallyABirthIfActive(location); | 309 Births* birth = ThreadData::TallyABirthIfActive(location); |
341 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); | 310 EXPECT_NE(reinterpret_cast<Births*>(NULL), birth); |
342 | 311 |
343 scoped_ptr<base::Value> value(ThreadData::ToValue(false)); | 312 ProcessDataSnapshot process_data; |
344 std::string json; | 313 ThreadData::Snapshot(false, &process_data); |
345 base::JSONWriter::Write(value.get(), &json); | 314 ASSERT_EQ(1u, process_data.tasks.size()); |
346 std::string birth_only_result = "{" | 315 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name); |
347 "\"descendants\":[" | 316 EXPECT_EQ(kFunction, process_data.tasks[0].birth.location.function_name); |
348 "]," | 317 EXPECT_EQ(kFakeLineNumber, process_data.tasks[0].birth.location.line_number); |
349 "\"list\":[" | 318 EXPECT_EQ("WorkerThread-1", process_data.tasks[0].birth.thread_name); |
350 "{" | 319 EXPECT_EQ(1, process_data.tasks[0].death_data.count); |
351 "\"birth_thread\":\"WorkerThread-1\"," | 320 EXPECT_EQ(0, process_data.tasks[0].death_data.run_duration_sum); |
352 "\"death_data\":{" | 321 EXPECT_EQ(0, process_data.tasks[0].death_data.run_duration_max); |
353 "\"count\":1," | 322 EXPECT_EQ(0, process_data.tasks[0].death_data.run_duration_sample); |
354 "\"queue_ms\":0," | 323 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_sum); |
355 "\"queue_ms_max\":0," | 324 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_max); |
356 "\"queue_ms_sample\":0," | 325 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_sample); |
357 "\"run_ms\":0," | 326 EXPECT_EQ("Still_Alive", process_data.tasks[0].death_thread_name); |
358 "\"run_ms_max\":0," | 327 EXPECT_EQ(0u, process_data.descendants.size()); |
359 "\"run_ms_sample\":0" | 328 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); |
360 "}," | |
361 "\"death_thread\":\"Still_Alive\"," | |
362 "\"location\":{" | |
363 "\"file_name\":\"FixedFileName\"," | |
364 "\"function_name\":\"BirthOnlyToValueWorkerThread\"," | |
365 "\"line_number\":173" | |
366 "}" | |
367 "}" | |
368 "]" | |
369 "}"; | |
370 EXPECT_EQ(json, birth_only_result); | |
371 } | 329 } |
372 | 330 |
373 TEST_F(TrackedObjectsTest, BirthOnlyToValueMainThread) { | 331 TEST_F(TrackedObjectsTest, BirthOnlyToSnapshotMainThread) { |
374 if (!ThreadData::InitializeAndSetTrackingStatus( | 332 if (!ThreadData::InitializeAndSetTrackingStatus( |
375 ThreadData::PROFILING_CHILDREN_ACTIVE)) | 333 ThreadData::PROFILING_CHILDREN_ACTIVE)) |
376 return; | 334 return; |
377 | 335 |
378 // Use a well named thread. | 336 // Use a well named thread. |
379 ThreadData::InitializeThreadContext("SomeMainThreadName"); | 337 ThreadData::InitializeThreadContext("SomeMainThreadName"); |
380 const int kFakeLineNumber = 173; | 338 const int kFakeLineNumber = 173; |
381 const char* kFile = "FixedFileName"; | 339 const char* kFile = "FixedFileName"; |
382 const char* kFunction = "BirthOnlyToValueMainThread"; | 340 const char* kFunction = "BirthOnlyToSnapshotMainThread"; |
383 Location location(kFunction, kFile, kFakeLineNumber, NULL); | 341 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
384 // Do not delete birth. We don't own it. | 342 // Do not delete birth. We don't own it. |
385 Births* birth = ThreadData::TallyABirthIfActive(location); | 343 Births* birth = ThreadData::TallyABirthIfActive(location); |
386 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); | 344 EXPECT_NE(reinterpret_cast<Births*>(NULL), birth); |
387 | 345 |
388 scoped_ptr<base::Value> value(ThreadData::ToValue(false)); | 346 ProcessDataSnapshot process_data; |
389 std::string json; | 347 ThreadData::Snapshot(false, &process_data); |
390 base::JSONWriter::Write(value.get(), &json); | 348 ASSERT_EQ(1u, process_data.tasks.size()); |
391 std::string birth_only_result = "{" | 349 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name); |
392 "\"descendants\":[" | 350 EXPECT_EQ(kFunction, process_data.tasks[0].birth.location.function_name); |
393 "]," | 351 EXPECT_EQ(kFakeLineNumber, process_data.tasks[0].birth.location.line_number); |
394 "\"list\":[" | 352 EXPECT_EQ("SomeMainThreadName", process_data.tasks[0].birth.thread_name); |
395 "{" | 353 EXPECT_EQ(1, process_data.tasks[0].death_data.count); |
396 "\"birth_thread\":\"SomeMainThreadName\"," | 354 EXPECT_EQ(0, process_data.tasks[0].death_data.run_duration_sum); |
397 "\"death_data\":{" | 355 EXPECT_EQ(0, process_data.tasks[0].death_data.run_duration_max); |
398 "\"count\":1," | 356 EXPECT_EQ(0, process_data.tasks[0].death_data.run_duration_sample); |
399 "\"queue_ms\":0," | 357 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_sum); |
400 "\"queue_ms_max\":0," | 358 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_max); |
401 "\"queue_ms_sample\":0," | 359 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_sample); |
402 "\"run_ms\":0," | 360 EXPECT_EQ("Still_Alive", process_data.tasks[0].death_thread_name); |
403 "\"run_ms_max\":0," | 361 EXPECT_EQ(0u, process_data.descendants.size()); |
404 "\"run_ms_sample\":0" | 362 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); |
405 "}," | |
406 "\"death_thread\":\"Still_Alive\"," | |
407 "\"location\":{" | |
408 "\"file_name\":\"FixedFileName\"," | |
409 "\"function_name\":\"BirthOnlyToValueMainThread\"," | |
410 "\"line_number\":173" | |
411 "}" | |
412 "}" | |
413 "]" | |
414 "}"; | |
415 EXPECT_EQ(json, birth_only_result); | |
416 } | 363 } |
417 | 364 |
418 TEST_F(TrackedObjectsTest, LifeCycleToValueMainThread) { | 365 TEST_F(TrackedObjectsTest, LifeCycleToSnapshotMainThread) { |
419 if (!ThreadData::InitializeAndSetTrackingStatus( | 366 if (!ThreadData::InitializeAndSetTrackingStatus( |
420 ThreadData::PROFILING_CHILDREN_ACTIVE)) | 367 ThreadData::PROFILING_CHILDREN_ACTIVE)) |
421 return; | 368 return; |
422 | 369 |
423 // Use a well named thread. | 370 // Use a well named thread. |
424 ThreadData::InitializeThreadContext("SomeMainThreadName"); | 371 ThreadData::InitializeThreadContext("SomeMainThreadName"); |
425 const int kFakeLineNumber = 236; | 372 const int kFakeLineNumber = 236; |
426 const char* kFile = "FixedFileName"; | 373 const char* kFile = "FixedFileName"; |
427 const char* kFunction = "LifeCycleToValueMainThread"; | 374 const char* kFunction = "LifeCycleToSnapshotMainThread"; |
428 Location location(kFunction, kFile, kFakeLineNumber, NULL); | 375 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
429 // Do not delete birth. We don't own it. | 376 // Do not delete birth. We don't own it. |
430 Births* birth = ThreadData::TallyABirthIfActive(location); | 377 Births* birth = ThreadData::TallyABirthIfActive(location); |
431 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); | 378 EXPECT_NE(reinterpret_cast<Births*>(NULL), birth); |
432 | 379 |
433 const base::TimeTicks kTimePosted = base::TimeTicks() | 380 const base::TimeTicks kTimePosted = base::TimeTicks() + |
434 + base::TimeDelta::FromMilliseconds(1); | 381 base::TimeDelta::FromMilliseconds(1); |
435 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); | 382 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); |
436 // TrackingInfo will call TallyABirth() during construction. | 383 // TrackingInfo will call TallyABirth() during construction. |
437 base::TrackingInfo pending_task(location, kDelayedStartTime); | 384 base::TrackingInfo pending_task(location, kDelayedStartTime); |
438 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). | 385 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). |
439 | 386 |
440 const TrackedTime kStartOfRun = TrackedTime() + | 387 const TrackedTime kStartOfRun = TrackedTime() + |
441 Duration::FromMilliseconds(5); | 388 Duration::FromMilliseconds(5); |
442 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); | 389 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); |
443 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, | 390 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, |
444 kStartOfRun, kEndOfRun); | 391 kStartOfRun, kEndOfRun); |
445 | 392 |
446 scoped_ptr<base::Value> value(ThreadData::ToValue(false)); | 393 ProcessDataSnapshot process_data; |
447 std::string json; | 394 ThreadData::Snapshot(false, &process_data); |
448 base::JSONWriter::Write(value.get(), &json); | 395 ASSERT_EQ(1u, process_data.tasks.size()); |
449 std::string one_line_result = "{" | 396 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name); |
450 "\"descendants\":[" | 397 EXPECT_EQ(kFunction, process_data.tasks[0].birth.location.function_name); |
451 "]," | 398 EXPECT_EQ(kFakeLineNumber, process_data.tasks[0].birth.location.line_number); |
452 "\"list\":[" | 399 EXPECT_EQ("SomeMainThreadName", process_data.tasks[0].birth.thread_name); |
453 "{" | 400 EXPECT_EQ(1, process_data.tasks[0].death_data.count); |
454 "\"birth_thread\":\"SomeMainThreadName\"," | 401 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sum); |
455 "\"death_data\":{" | 402 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_max); |
456 "\"count\":1," | 403 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sample); |
457 "\"queue_ms\":4," | 404 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sum); |
458 "\"queue_ms_max\":4," | 405 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_max); |
459 "\"queue_ms_sample\":4," | 406 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sample); |
460 "\"run_ms\":2," | 407 EXPECT_EQ("SomeMainThreadName", process_data.tasks[0].death_thread_name); |
461 "\"run_ms_max\":2," | 408 EXPECT_EQ(0u, process_data.descendants.size()); |
462 "\"run_ms_sample\":2" | 409 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); |
463 "}," | |
464 "\"death_thread\":\"SomeMainThreadName\"," | |
465 "\"location\":{" | |
466 "\"file_name\":\"FixedFileName\"," | |
467 "\"function_name\":\"LifeCycleToValueMainThread\"," | |
468 "\"line_number\":236" | |
469 "}" | |
470 "}" | |
471 "]" | |
472 "}"; | |
473 EXPECT_EQ(one_line_result, json); | |
474 } | 410 } |
475 | 411 |
476 // We will deactivate tracking after the birth, and before the death, and | 412 // We will deactivate tracking after the birth, and before the death, and |
477 // demonstrate that the lifecycle is completely tallied. This ensures that | 413 // demonstrate that the lifecycle is completely tallied. This ensures that |
478 // our tallied births are matched by tallied deaths (except for when the | 414 // our tallied births are matched by tallied deaths (except for when the |
479 // task is still running, or is queued). | 415 // task is still running, or is queued). |
480 TEST_F(TrackedObjectsTest, LifeCycleMidDeactivatedToValueMainThread) { | 416 TEST_F(TrackedObjectsTest, LifeCycleMidDeactivatedToSnapshotMainThread) { |
481 if (!ThreadData::InitializeAndSetTrackingStatus( | 417 if (!ThreadData::InitializeAndSetTrackingStatus( |
482 ThreadData::PROFILING_CHILDREN_ACTIVE)) | 418 ThreadData::PROFILING_CHILDREN_ACTIVE)) |
483 return; | 419 return; |
484 | 420 |
485 // Use a well named thread. | 421 // Use a well named thread. |
486 ThreadData::InitializeThreadContext("SomeMainThreadName"); | 422 ThreadData::InitializeThreadContext("SomeMainThreadName"); |
487 const int kFakeLineNumber = 236; | 423 const int kFakeLineNumber = 236; |
488 const char* kFile = "FixedFileName"; | 424 const char* kFile = "FixedFileName"; |
489 const char* kFunction = "LifeCycleToValueMainThread"; | 425 const char* kFunction = "LifeCycleToSnapshotMainThread"; |
490 Location location(kFunction, kFile, kFakeLineNumber, NULL); | 426 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
491 // Do not delete birth. We don't own it. | 427 // Do not delete birth. We don't own it. |
492 Births* birth = ThreadData::TallyABirthIfActive(location); | 428 Births* birth = ThreadData::TallyABirthIfActive(location); |
493 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); | 429 EXPECT_NE(reinterpret_cast<Births*>(NULL), birth); |
494 | 430 |
495 const base::TimeTicks kTimePosted = base::TimeTicks() | 431 const base::TimeTicks kTimePosted = base::TimeTicks() + |
496 + base::TimeDelta::FromMilliseconds(1); | 432 base::TimeDelta::FromMilliseconds(1); |
497 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); | 433 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); |
498 // TrackingInfo will call TallyABirth() during construction. | 434 // TrackingInfo will call TallyABirth() during construction. |
499 base::TrackingInfo pending_task(location, kDelayedStartTime); | 435 base::TrackingInfo pending_task(location, kDelayedStartTime); |
500 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). | 436 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). |
501 | 437 |
502 // Turn off tracking now that we have births. | 438 // Turn off tracking now that we have births. |
503 EXPECT_TRUE(ThreadData::InitializeAndSetTrackingStatus( | 439 EXPECT_TRUE(ThreadData::InitializeAndSetTrackingStatus( |
504 ThreadData::DEACTIVATED)); | 440 ThreadData::DEACTIVATED)); |
505 | 441 |
506 const TrackedTime kStartOfRun = TrackedTime() + | 442 const TrackedTime kStartOfRun = TrackedTime() + |
507 Duration::FromMilliseconds(5); | 443 Duration::FromMilliseconds(5); |
508 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); | 444 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); |
509 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, | 445 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, |
510 kStartOfRun, kEndOfRun); | 446 kStartOfRun, kEndOfRun); |
511 | 447 |
512 scoped_ptr<base::Value> value(ThreadData::ToValue(false)); | 448 ProcessDataSnapshot process_data; |
513 std::string json; | 449 ThreadData::Snapshot(false, &process_data); |
514 base::JSONWriter::Write(value.get(), &json); | 450 ASSERT_EQ(1u, process_data.tasks.size()); |
515 std::string one_line_result = "{" | 451 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name); |
516 "\"descendants\":[" | 452 EXPECT_EQ(kFunction, process_data.tasks[0].birth.location.function_name); |
517 "]," | 453 EXPECT_EQ(kFakeLineNumber, process_data.tasks[0].birth.location.line_number); |
518 "\"list\":[" | 454 EXPECT_EQ("SomeMainThreadName", process_data.tasks[0].birth.thread_name); |
519 "{" | 455 EXPECT_EQ(1, process_data.tasks[0].death_data.count); |
520 "\"birth_thread\":\"SomeMainThreadName\"," | 456 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sum); |
521 "\"death_data\":{" | 457 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_max); |
522 "\"count\":1," | 458 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sample); |
523 "\"queue_ms\":4," | 459 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sum); |
524 "\"queue_ms_max\":4," | 460 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_max); |
525 "\"queue_ms_sample\":4," | 461 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sample); |
526 "\"run_ms\":2," | 462 EXPECT_EQ("SomeMainThreadName", process_data.tasks[0].death_thread_name); |
527 "\"run_ms_max\":2," | 463 EXPECT_EQ(0u, process_data.descendants.size()); |
528 "\"run_ms_sample\":2" | 464 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); |
529 "}," | |
530 "\"death_thread\":\"SomeMainThreadName\"," | |
531 "\"location\":{" | |
532 "\"file_name\":\"FixedFileName\"," | |
533 "\"function_name\":\"LifeCycleToValueMainThread\"," | |
534 "\"line_number\":236" | |
535 "}" | |
536 "}" | |
537 "]" | |
538 "}"; | |
539 EXPECT_EQ(one_line_result, json); | |
540 } | 465 } |
541 | 466 |
542 // We will deactivate tracking before starting a life cycle, and neither | 467 // We will deactivate tracking before starting a life cycle, and neither |
543 // the birth nor the death will be recorded. | 468 // the birth nor the death will be recorded. |
544 TEST_F(TrackedObjectsTest, LifeCyclePreDeactivatedToValueMainThread) { | 469 TEST_F(TrackedObjectsTest, LifeCyclePreDeactivatedToSnapshotMainThread) { |
545 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) | 470 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) |
546 return; | 471 return; |
547 | 472 |
548 // Use a well named thread. | 473 // Use a well named thread. |
549 ThreadData::InitializeThreadContext("SomeMainThreadName"); | 474 ThreadData::InitializeThreadContext("SomeMainThreadName"); |
550 const int kFakeLineNumber = 236; | 475 const int kFakeLineNumber = 236; |
551 const char* kFile = "FixedFileName"; | 476 const char* kFile = "FixedFileName"; |
552 const char* kFunction = "LifeCycleToValueMainThread"; | 477 const char* kFunction = "LifeCycleToSnapshotMainThread"; |
553 Location location(kFunction, kFile, kFakeLineNumber, NULL); | 478 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
554 // Do not delete birth. We don't own it. | 479 // Do not delete birth. We don't own it. |
555 Births* birth = ThreadData::TallyABirthIfActive(location); | 480 Births* birth = ThreadData::TallyABirthIfActive(location); |
556 EXPECT_EQ(birth, reinterpret_cast<Births*>(NULL)); | 481 EXPECT_EQ(birth, reinterpret_cast<Births*>(NULL)); |
557 | 482 |
558 const base::TimeTicks kTimePosted = base::TimeTicks() | 483 const base::TimeTicks kTimePosted = base::TimeTicks() + |
559 + base::TimeDelta::FromMilliseconds(1); | 484 base::TimeDelta::FromMilliseconds(1); |
560 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); | 485 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); |
561 // TrackingInfo will call TallyABirth() during construction. | 486 // TrackingInfo will call TallyABirth() during construction. |
562 base::TrackingInfo pending_task(location, kDelayedStartTime); | 487 base::TrackingInfo pending_task(location, kDelayedStartTime); |
563 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). | 488 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). |
564 | 489 |
565 const TrackedTime kStartOfRun = TrackedTime() + | 490 const TrackedTime kStartOfRun = TrackedTime() + |
566 Duration::FromMilliseconds(5); | 491 Duration::FromMilliseconds(5); |
567 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); | 492 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); |
568 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, | 493 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, |
569 kStartOfRun, kEndOfRun); | 494 kStartOfRun, kEndOfRun); |
570 | 495 |
571 scoped_ptr<base::Value> value(ThreadData::ToValue(false)); | 496 ProcessDataSnapshot process_data; |
572 std::string json; | 497 ThreadData::Snapshot(false, &process_data); |
573 base::JSONWriter::Write(value.get(), &json); | 498 EXPECT_EQ(0u, process_data.tasks.size()); |
574 std::string one_line_result = "{" | 499 EXPECT_EQ(0u, process_data.descendants.size()); |
575 "\"descendants\":[" | 500 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); |
576 "]," | |
577 "\"list\":[" | |
578 "]" | |
579 "}"; | |
580 EXPECT_EQ(one_line_result, json); | |
581 } | 501 } |
582 | 502 |
583 TEST_F(TrackedObjectsTest, LifeCycleToValueWorkerThread) { | 503 TEST_F(TrackedObjectsTest, LifeCycleToSnapshotWorkerThread) { |
584 if (!ThreadData::InitializeAndSetTrackingStatus( | 504 if (!ThreadData::InitializeAndSetTrackingStatus( |
585 ThreadData::PROFILING_CHILDREN_ACTIVE)) | 505 ThreadData::PROFILING_CHILDREN_ACTIVE)) |
586 return; | 506 return; |
587 | 507 |
588 // Don't initialize thread, so that we appear as a worker thread. | 508 // Don't initialize thread, so that we appear as a worker thread. |
589 // ThreadData::InitializeThreadContext("SomeMainThreadName"); | 509 // ThreadData::InitializeThreadContext("SomeMainThreadName"); |
590 | 510 |
591 const int kFakeLineNumber = 236; | 511 const int kFakeLineNumber = 236; |
592 const char* kFile = "FixedFileName"; | 512 const char* kFile = "FixedFileName"; |
593 const char* kFunction = "LifeCycleToValueWorkerThread"; | 513 const char* kFunction = "LifeCycleToSnapshotWorkerThread"; |
594 Location location(kFunction, kFile, kFakeLineNumber, NULL); | 514 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
595 // Do not delete birth. We don't own it. | 515 // Do not delete birth. We don't own it. |
596 Births* birth = ThreadData::TallyABirthIfActive(location); | 516 Births* birth = ThreadData::TallyABirthIfActive(location); |
597 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); | 517 EXPECT_NE(reinterpret_cast<Births*>(NULL), birth); |
598 | 518 |
599 const TrackedTime kTimePosted = TrackedTime() + Duration::FromMilliseconds(1); | 519 const TrackedTime kTimePosted = TrackedTime() + Duration::FromMilliseconds(1); |
600 const TrackedTime kStartOfRun = TrackedTime() + | 520 const TrackedTime kStartOfRun = TrackedTime() + |
601 Duration::FromMilliseconds(5); | 521 Duration::FromMilliseconds(5); |
602 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); | 522 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); |
603 ThreadData::TallyRunOnWorkerThreadIfTracking(birth, kTimePosted, | 523 ThreadData::TallyRunOnWorkerThreadIfTracking(birth, kTimePosted, |
604 kStartOfRun, kEndOfRun); | 524 kStartOfRun, kEndOfRun); |
605 | 525 |
606 // Call for the ToValue, but tell it to not the maxes after scanning. | 526 // Call for the ToSnapshot, but tell it to not reset the maxes after scanning. |
607 scoped_ptr<base::Value> value(ThreadData::ToValue(false)); | 527 ProcessDataSnapshot process_data; |
608 std::string json; | 528 ThreadData::Snapshot(false, &process_data); |
609 base::JSONWriter::Write(value.get(), &json); | 529 ASSERT_EQ(1u, process_data.tasks.size()); |
610 std::string one_line_result = "{" | 530 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name); |
611 "\"descendants\":[" | 531 EXPECT_EQ(kFunction, process_data.tasks[0].birth.location.function_name); |
612 "]," | 532 EXPECT_EQ(kFakeLineNumber, process_data.tasks[0].birth.location.line_number); |
613 "\"list\":[" | 533 EXPECT_EQ("WorkerThread-1", process_data.tasks[0].birth.thread_name); |
614 "{" | 534 EXPECT_EQ(1, process_data.tasks[0].death_data.count); |
615 "\"birth_thread\":\"WorkerThread-1\"," | 535 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sum); |
616 "\"death_data\":{" | 536 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_max); |
617 "\"count\":1," | 537 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sample); |
618 "\"queue_ms\":4," | 538 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sum); |
619 "\"queue_ms_max\":4," | 539 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_max); |
620 "\"queue_ms_sample\":4," | 540 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sample); |
621 "\"run_ms\":2," | 541 EXPECT_EQ("WorkerThread-1", process_data.tasks[0].death_thread_name); |
622 "\"run_ms_max\":2," | 542 EXPECT_EQ(0u, process_data.descendants.size()); |
623 "\"run_ms_sample\":2" | 543 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); |
624 "}," | |
625 "\"death_thread\":\"WorkerThread-1\"," | |
626 "\"location\":{" | |
627 "\"file_name\":\"FixedFileName\"," | |
628 "\"function_name\":\"LifeCycleToValueWorkerThread\"," | |
629 "\"line_number\":236" | |
630 "}" | |
631 "}" | |
632 "]" | |
633 "}"; | |
634 EXPECT_EQ(one_line_result, json); | |
635 | 544 |
636 // Call for the ToValue, but tell it to reset the maxes after scanning. | 545 // Call for the ToSnapshot, but tell it to reset the maxes after scanning. |
637 // We'll still get the same values, but the data will be reset (which we'll | 546 // We'll still get the same values, but the data will be reset (which we'll |
638 // see in a moment). | 547 // see in a moment). |
639 value.reset(ThreadData::ToValue(true)); | 548 ProcessDataSnapshot process_data_pre_reset; |
640 base::JSONWriter::Write(value.get(), &json); | 549 ThreadData::Snapshot(true, &process_data_pre_reset); |
641 // Result should be unchanged. | 550 ASSERT_EQ(1u, process_data_pre_reset.tasks.size()); |
642 EXPECT_EQ(one_line_result, json); | 551 EXPECT_EQ(kFile, process_data_pre_reset.tasks[0].birth.location.file_name); |
552 EXPECT_EQ(kFunction, | |
553 process_data_pre_reset.tasks[0].birth.location.function_name); | |
554 EXPECT_EQ( | |
555 kFakeLineNumber, | |
556 process_data_pre_reset.tasks[0].birth.location.line_number); | |
557 EXPECT_EQ("WorkerThread-1", | |
558 process_data_pre_reset.tasks[0].birth.thread_name); | |
559 EXPECT_EQ(1, process_data_pre_reset.tasks[0].death_data.count); | |
560 EXPECT_EQ(2, process_data_pre_reset.tasks[0].death_data.run_duration_sum); | |
561 EXPECT_EQ(2, process_data_pre_reset.tasks[0].death_data.run_duration_max); | |
562 EXPECT_EQ(2, process_data_pre_reset.tasks[0].death_data.run_duration_sample); | |
563 EXPECT_EQ(4, process_data_pre_reset.tasks[0].death_data.queue_duration_sum); | |
564 EXPECT_EQ(4, process_data_pre_reset.tasks[0].death_data.queue_duration_max); | |
565 EXPECT_EQ(4, | |
566 process_data_pre_reset.tasks[0].death_data.queue_duration_sample); | |
567 EXPECT_EQ("WorkerThread-1", | |
568 process_data_pre_reset.tasks[0].death_thread_name); | |
569 EXPECT_EQ(0u, process_data_pre_reset.descendants.size()); | |
570 EXPECT_EQ(base::GetCurrentProcId(), process_data_pre_reset.process_id); | |
643 | 571 |
644 // Call for the ToValue, and now we'll see the result of the last translation, | 572 // Call for the ToSnapshot, and now we'll see the result of the last |
645 // as the max will have been pushed back to zero. | 573 // translation, as the max will have been pushed back to zero. |
646 value.reset(ThreadData::ToValue(false)); | 574 ProcessDataSnapshot process_data_post_reset; |
647 base::JSONWriter::Write(value.get(), &json); | 575 ThreadData::Snapshot(true, &process_data_post_reset); |
648 std::string one_line_result_with_zeros = "{" | 576 ASSERT_EQ(1u, process_data_post_reset.tasks.size()); |
649 "\"descendants\":[" | 577 EXPECT_EQ(kFile, process_data_post_reset.tasks[0].birth.location.file_name); |
650 "]," | 578 EXPECT_EQ(kFunction, |
651 "\"list\":[" | 579 process_data_post_reset.tasks[0].birth.location.function_name); |
652 "{" | 580 EXPECT_EQ( |
653 "\"birth_thread\":\"WorkerThread-1\"," | 581 kFakeLineNumber, |
654 "\"death_data\":{" | 582 process_data_post_reset.tasks[0].birth.location.line_number); |
655 "\"count\":1," | 583 EXPECT_EQ("WorkerThread-1", |
656 "\"queue_ms\":4," | 584 process_data_post_reset.tasks[0].birth.thread_name); |
657 "\"queue_ms_max\":0," // Note zero here. | 585 EXPECT_EQ(1, process_data_post_reset.tasks[0].death_data.count); |
658 "\"queue_ms_sample\":4," | 586 EXPECT_EQ(2, process_data_post_reset.tasks[0].death_data.run_duration_sum); |
659 "\"run_ms\":2," | 587 EXPECT_EQ(0, process_data_post_reset.tasks[0].death_data.run_duration_max); |
660 "\"run_ms_max\":0," // Note zero here. | 588 EXPECT_EQ(2, process_data_post_reset.tasks[0].death_data.run_duration_sample); |
661 "\"run_ms_sample\":2" | 589 EXPECT_EQ(4, process_data_post_reset.tasks[0].death_data.queue_duration_sum); |
662 "}," | 590 EXPECT_EQ(0, process_data_post_reset.tasks[0].death_data.queue_duration_max); |
663 "\"death_thread\":\"WorkerThread-1\"," | 591 EXPECT_EQ(4, |
664 "\"location\":{" | 592 process_data_post_reset.tasks[0].death_data.queue_duration_sample); |
665 "\"file_name\":\"FixedFileName\"," | 593 EXPECT_EQ("WorkerThread-1", |
666 "\"function_name\":\"LifeCycleToValueWorkerThread\"," | 594 process_data_post_reset.tasks[0].death_thread_name); |
667 "\"line_number\":236" | 595 EXPECT_EQ(0u, process_data_post_reset.descendants.size()); |
668 "}" | 596 EXPECT_EQ(base::GetCurrentProcId(), process_data_post_reset.process_id); |
669 "}" | |
670 "]" | |
671 "}"; | |
672 EXPECT_EQ(one_line_result_with_zeros, json); | |
673 } | 597 } |
674 | 598 |
675 TEST_F(TrackedObjectsTest, TwoLives) { | 599 TEST_F(TrackedObjectsTest, TwoLives) { |
676 if (!ThreadData::InitializeAndSetTrackingStatus( | 600 if (!ThreadData::InitializeAndSetTrackingStatus( |
677 ThreadData::PROFILING_CHILDREN_ACTIVE)) | 601 ThreadData::PROFILING_CHILDREN_ACTIVE)) |
678 return; | 602 return; |
679 | 603 |
680 // Use a well named thread. | 604 // Use a well named thread. |
681 ThreadData::InitializeThreadContext("SomeFileThreadName"); | 605 ThreadData::InitializeThreadContext("SomeFileThreadName"); |
682 const int kFakeLineNumber = 222; | 606 const int kFakeLineNumber = 222; |
683 const char* kFile = "AnotherFileName"; | 607 const char* kFile = "AnotherFileName"; |
684 const char* kFunction = "TwoLives"; | 608 const char* kFunction = "TwoLives"; |
685 Location location(kFunction, kFile, kFakeLineNumber, NULL); | 609 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
686 // Do not delete birth. We don't own it. | 610 // Do not delete birth. We don't own it. |
687 Births* birth = ThreadData::TallyABirthIfActive(location); | 611 Births* birth = ThreadData::TallyABirthIfActive(location); |
688 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); | 612 EXPECT_NE(reinterpret_cast<Births*>(NULL), birth); |
689 | 613 |
690 | 614 |
691 const base::TimeTicks kTimePosted = base::TimeTicks() | 615 const base::TimeTicks kTimePosted = base::TimeTicks() + |
692 + base::TimeDelta::FromMilliseconds(1); | 616 base::TimeDelta::FromMilliseconds(1); |
693 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); | 617 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); |
694 // TrackingInfo will call TallyABirth() during construction. | 618 // TrackingInfo will call TallyABirth() during construction. |
695 base::TrackingInfo pending_task(location, kDelayedStartTime); | 619 base::TrackingInfo pending_task(location, kDelayedStartTime); |
696 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). | 620 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). |
697 | 621 |
698 const TrackedTime kStartOfRun = TrackedTime() + | 622 const TrackedTime kStartOfRun = TrackedTime() + |
699 Duration::FromMilliseconds(5); | 623 Duration::FromMilliseconds(5); |
700 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); | 624 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); |
701 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, | 625 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, |
702 kStartOfRun, kEndOfRun); | 626 kStartOfRun, kEndOfRun); |
703 | 627 |
704 // TrackingInfo will call TallyABirth() during construction. | 628 // TrackingInfo will call TallyABirth() during construction. |
705 base::TrackingInfo pending_task2(location, kDelayedStartTime); | 629 base::TrackingInfo pending_task2(location, kDelayedStartTime); |
706 pending_task2.time_posted = kTimePosted; // Overwrite implied Now(). | 630 pending_task2.time_posted = kTimePosted; // Overwrite implied Now(). |
707 | 631 |
708 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task2, | 632 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task2, |
709 kStartOfRun, kEndOfRun); | 633 kStartOfRun, kEndOfRun); |
710 | 634 |
711 scoped_ptr<base::Value> value(ThreadData::ToValue(false)); | 635 ProcessDataSnapshot process_data; |
712 std::string json; | 636 ThreadData::Snapshot(false, &process_data); |
713 base::JSONWriter::Write(value.get(), &json); | 637 ASSERT_EQ(1u, process_data.tasks.size()); |
714 std::string one_line_result = "{" | 638 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name); |
715 "\"descendants\":[" | 639 EXPECT_EQ(kFunction, process_data.tasks[0].birth.location.function_name); |
716 "]," | 640 EXPECT_EQ(kFakeLineNumber, process_data.tasks[0].birth.location.line_number); |
717 "\"list\":[" | 641 EXPECT_EQ("SomeFileThreadName", process_data.tasks[0].birth.thread_name); |
718 "{" | 642 EXPECT_EQ(2, process_data.tasks[0].death_data.count); |
719 "\"birth_thread\":\"SomeFileThreadName\"," | 643 EXPECT_EQ(4, process_data.tasks[0].death_data.run_duration_sum); |
720 "\"death_data\":{" | 644 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_max); |
721 "\"count\":2," | 645 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sample); |
722 "\"queue_ms\":8," | 646 EXPECT_EQ(8, process_data.tasks[0].death_data.queue_duration_sum); |
723 "\"queue_ms_max\":4," | 647 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_max); |
724 "\"queue_ms_sample\":4," | 648 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sample); |
725 "\"run_ms\":4," | 649 EXPECT_EQ("SomeFileThreadName", process_data.tasks[0].death_thread_name); |
726 "\"run_ms_max\":2," | 650 EXPECT_EQ(0u, process_data.descendants.size()); |
727 "\"run_ms_sample\":2" | 651 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); |
728 "}," | |
729 "\"death_thread\":\"SomeFileThreadName\"," | |
730 "\"location\":{" | |
731 "\"file_name\":\"AnotherFileName\"," | |
732 "\"function_name\":\"TwoLives\"," | |
733 "\"line_number\":222" | |
734 "}" | |
735 "}" | |
736 "]" | |
737 "}"; | |
738 EXPECT_EQ(one_line_result, json); | |
739 } | 652 } |
740 | 653 |
741 TEST_F(TrackedObjectsTest, DifferentLives) { | 654 TEST_F(TrackedObjectsTest, DifferentLives) { |
742 if (!ThreadData::InitializeAndSetTrackingStatus( | 655 if (!ThreadData::InitializeAndSetTrackingStatus( |
743 ThreadData::PROFILING_CHILDREN_ACTIVE)) | 656 ThreadData::PROFILING_CHILDREN_ACTIVE)) |
744 return; | 657 return; |
745 | 658 |
746 // Use a well named thread. | 659 // Use a well named thread. |
747 ThreadData::InitializeThreadContext("SomeFileThreadName"); | 660 ThreadData::InitializeThreadContext("SomeFileThreadName"); |
748 const int kFakeLineNumber = 567; | 661 const int kFakeLineNumber = 567; |
749 const char* kFile = "AnotherFileName"; | 662 const char* kFile = "AnotherFileName"; |
750 const char* kFunction = "DifferentLives"; | 663 const char* kFunction = "DifferentLives"; |
751 Location location(kFunction, kFile, kFakeLineNumber, NULL); | 664 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
752 | 665 |
753 const base::TimeTicks kTimePosted = base::TimeTicks() | 666 const base::TimeTicks kTimePosted = base::TimeTicks() + |
754 + base::TimeDelta::FromMilliseconds(1); | 667 base::TimeDelta::FromMilliseconds(1); |
755 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); | 668 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); |
756 // TrackingInfo will call TallyABirth() during construction. | 669 // TrackingInfo will call TallyABirth() during construction. |
757 base::TrackingInfo pending_task(location, kDelayedStartTime); | 670 base::TrackingInfo pending_task(location, kDelayedStartTime); |
758 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). | 671 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). |
759 | 672 |
760 const TrackedTime kStartOfRun = TrackedTime() + | 673 const TrackedTime kStartOfRun = TrackedTime() + |
761 Duration::FromMilliseconds(5); | 674 Duration::FromMilliseconds(5); |
762 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); | 675 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); |
763 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, | 676 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, |
764 kStartOfRun, kEndOfRun); | 677 kStartOfRun, kEndOfRun); |
765 | 678 |
766 const int kSecondFakeLineNumber = 999; | 679 const int kSecondFakeLineNumber = 999; |
767 Location second_location(kFunction, kFile, kSecondFakeLineNumber, NULL); | 680 Location second_location(kFunction, kFile, kSecondFakeLineNumber, NULL); |
768 | 681 |
769 // TrackingInfo will call TallyABirth() during construction. | 682 // TrackingInfo will call TallyABirth() during construction. |
770 base::TrackingInfo pending_task2(second_location, kDelayedStartTime); | 683 base::TrackingInfo pending_task2(second_location, kDelayedStartTime); |
771 pending_task2.time_posted = kTimePosted; // Overwrite implied Now(). | 684 pending_task2.time_posted = kTimePosted; // Overwrite implied Now(). |
772 | 685 |
773 scoped_ptr<base::Value> value(ThreadData::ToValue(false)); | 686 ProcessDataSnapshot process_data; |
774 std::string json; | 687 ThreadData::Snapshot(false, &process_data); |
775 base::JSONWriter::Write(value.get(), &json); | 688 ASSERT_EQ(2u, process_data.tasks.size()); |
776 std::string one_line_result = "{" | 689 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name); |
777 "\"descendants\":[" | 690 EXPECT_EQ(kFunction, process_data.tasks[0].birth.location.function_name); |
778 "]," | 691 EXPECT_EQ(kFakeLineNumber, process_data.tasks[0].birth.location.line_number); |
779 "\"list\":[" | 692 EXPECT_EQ("SomeFileThreadName", process_data.tasks[0].birth.thread_name); |
780 "{" | 693 EXPECT_EQ(1, process_data.tasks[0].death_data.count); |
781 "\"birth_thread\":\"SomeFileThreadName\"," | 694 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sum); |
782 "\"death_data\":{" | 695 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_max); |
783 "\"count\":1," | 696 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sample); |
784 "\"queue_ms\":4," | 697 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sum); |
785 "\"queue_ms_max\":4," | 698 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_max); |
786 "\"queue_ms_sample\":4," | 699 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sample); |
787 "\"run_ms\":2," | 700 EXPECT_EQ("SomeFileThreadName", process_data.tasks[0].death_thread_name); |
788 "\"run_ms_max\":2," | 701 EXPECT_EQ(kFile, process_data.tasks[1].birth.location.file_name); |
789 "\"run_ms_sample\":2" | 702 EXPECT_EQ(kFunction, process_data.tasks[1].birth.location.function_name); |
790 "}," | 703 EXPECT_EQ(kSecondFakeLineNumber, |
791 "\"death_thread\":\"SomeFileThreadName\"," | 704 process_data.tasks[1].birth.location.line_number); |
792 "\"location\":{" | 705 EXPECT_EQ("SomeFileThreadName", process_data.tasks[1].birth.thread_name); |
793 "\"file_name\":\"AnotherFileName\"," | 706 EXPECT_EQ(1, process_data.tasks[1].death_data.count); |
794 "\"function_name\":\"DifferentLives\"," | 707 EXPECT_EQ(0, process_data.tasks[1].death_data.run_duration_sum); |
795 "\"line_number\":567" | 708 EXPECT_EQ(0, process_data.tasks[1].death_data.run_duration_max); |
796 "}" | 709 EXPECT_EQ(0, process_data.tasks[1].death_data.run_duration_sample); |
797 "}," | 710 EXPECT_EQ(0, process_data.tasks[1].death_data.queue_duration_sum); |
798 "{" | 711 EXPECT_EQ(0, process_data.tasks[1].death_data.queue_duration_max); |
799 "\"birth_thread\":\"SomeFileThreadName\"," | 712 EXPECT_EQ(0, process_data.tasks[1].death_data.queue_duration_sample); |
800 "\"death_data\":{" | 713 EXPECT_EQ("Still_Alive", process_data.tasks[1].death_thread_name); |
801 "\"count\":1," | 714 EXPECT_EQ(0u, process_data.descendants.size()); |
802 "\"queue_ms\":0," | 715 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); |
803 "\"queue_ms_max\":0," | |
804 "\"queue_ms_sample\":0," | |
805 "\"run_ms\":0," | |
806 "\"run_ms_max\":0," | |
807 "\"run_ms_sample\":0" | |
808 "}," | |
809 "\"death_thread\":\"Still_Alive\"," | |
810 "\"location\":{" | |
811 "\"file_name\":\"AnotherFileName\"," | |
812 "\"function_name\":\"DifferentLives\"," | |
813 "\"line_number\":999" | |
814 "}" | |
815 "}" | |
816 "]" | |
817 "}"; | |
818 EXPECT_EQ(one_line_result, json); | |
819 } | 716 } |
820 | 717 |
821 | |
822 } // namespace tracked_objects | 718 } // namespace tracked_objects |
OLD | NEW |