OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" | 9 #include "base/json/json_writer.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.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 public: | 17 public: |
18 ~TrackedObjectsTest() { | 18 TrackedObjectsTest() { |
19 ThreadData::ShutdownSingleThreadedCleanup(); | 19 ThreadData::ShutdownSingleThreadedCleanup(); |
20 } | 20 } |
21 | 21 |
| 22 ~TrackedObjectsTest() { |
| 23 ThreadData::ShutdownSingleThreadedCleanup(); |
| 24 } |
22 }; | 25 }; |
23 | 26 |
24 TEST_F(TrackedObjectsTest, MinimalStartupShutdown) { | 27 TEST_F(TrackedObjectsTest, MinimalStartupShutdown) { |
25 // Minimal test doesn't even create any tasks. | 28 // Minimal test doesn't even create any tasks. |
26 if (!ThreadData::StartTracking(true)) | 29 if (!ThreadData::InitializeAndSetTrackingStatus(true)) |
27 return; | 30 return; |
28 | 31 |
29 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. | 32 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. |
30 ThreadData* data = ThreadData::Get(); | 33 ThreadData* data = ThreadData::Get(); |
31 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. | 34 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. |
32 EXPECT_TRUE(data); | 35 EXPECT_TRUE(data); |
33 EXPECT_TRUE(!data->next()); | 36 EXPECT_TRUE(!data->next()); |
34 EXPECT_EQ(data, ThreadData::Get()); | 37 EXPECT_EQ(data, ThreadData::Get()); |
35 ThreadData::BirthMap birth_map; | 38 ThreadData::BirthMap birth_map; |
36 data->SnapshotBirthMap(&birth_map); | 39 data->SnapshotBirthMap(&birth_map); |
37 EXPECT_EQ(0u, birth_map.size()); | 40 EXPECT_EQ(0u, birth_map.size()); |
38 ThreadData::DeathMap death_map; | 41 ThreadData::DeathMap death_map; |
39 data->SnapshotDeathMap(&death_map); | 42 data->SnapshotDeathMap(&death_map); |
40 EXPECT_EQ(0u, death_map.size()); | 43 EXPECT_EQ(0u, death_map.size()); |
41 ThreadData::ShutdownSingleThreadedCleanup(); | 44 ThreadData::ShutdownSingleThreadedCleanup(); |
42 | 45 |
43 // Do it again, just to be sure we reset state completely. | 46 // Do it again, just to be sure we reset state completely. |
44 ThreadData::StartTracking(true); | 47 ThreadData::InitializeAndSetTrackingStatus(true); |
45 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. | 48 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. |
46 data = ThreadData::Get(); | 49 data = ThreadData::Get(); |
47 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. | 50 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. |
48 EXPECT_TRUE(data); | 51 EXPECT_TRUE(data); |
49 EXPECT_TRUE(!data->next()); | 52 EXPECT_TRUE(!data->next()); |
50 EXPECT_EQ(data, ThreadData::Get()); | 53 EXPECT_EQ(data, ThreadData::Get()); |
51 birth_map.clear(); | 54 birth_map.clear(); |
52 data->SnapshotBirthMap(&birth_map); | 55 data->SnapshotBirthMap(&birth_map); |
53 EXPECT_EQ(0u, birth_map.size()); | 56 EXPECT_EQ(0u, birth_map.size()); |
54 death_map.clear(); | 57 death_map.clear(); |
55 data->SnapshotDeathMap(&death_map); | 58 data->SnapshotDeathMap(&death_map); |
56 EXPECT_EQ(0u, death_map.size()); | 59 EXPECT_EQ(0u, death_map.size()); |
57 } | 60 } |
58 | 61 |
59 TEST_F(TrackedObjectsTest, TinyStartupShutdown) { | 62 TEST_F(TrackedObjectsTest, TinyStartupShutdown) { |
60 if (!ThreadData::StartTracking(true)) | 63 if (!ThreadData::InitializeAndSetTrackingStatus(true)) |
61 return; | 64 return; |
62 | 65 |
63 // Instigate tracking on a single tracked object, on our thread. | 66 // Instigate tracking on a single tracked object, on our thread. |
64 const Location& location = FROM_HERE; | 67 const Location& location = FROM_HERE; |
65 ThreadData::TallyABirthIfActive(location); | 68 ThreadData::TallyABirthIfActive(location); |
66 | 69 |
67 const ThreadData* data = ThreadData::first(); | 70 const ThreadData* data = ThreadData::first(); |
68 ASSERT_TRUE(data); | 71 ASSERT_TRUE(data); |
69 EXPECT_TRUE(!data->next()); | 72 EXPECT_TRUE(!data->next()); |
70 EXPECT_EQ(data, ThreadData::Get()); | 73 EXPECT_EQ(data, ThreadData::Get()); |
71 ThreadData::BirthMap birth_map; | 74 ThreadData::BirthMap birth_map; |
72 data->SnapshotBirthMap(&birth_map); | 75 data->SnapshotBirthMap(&birth_map); |
73 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. | 76 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. |
74 EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth. | 77 EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth. |
75 ThreadData::DeathMap death_map; | 78 ThreadData::DeathMap death_map; |
76 data->SnapshotDeathMap(&death_map); | 79 data->SnapshotDeathMap(&death_map); |
77 EXPECT_EQ(0u, death_map.size()); // No deaths. | 80 EXPECT_EQ(0u, death_map.size()); // No deaths. |
78 | 81 |
79 | 82 |
80 // Now instigate a birth, and a death. | 83 // Now instigate another birth, and a first death at the same location. |
81 const Births* second_birth = ThreadData::TallyABirthIfActive(location); | 84 // TrackingInfo will call TallyABirth() during construction. |
82 ThreadData::TallyADeathIfActive( | 85 base::TimeTicks kBogusStartTime; |
83 second_birth, | 86 base::TrackingInfo pending_task(location, kBogusStartTime); |
84 base::TimeTicks(), /* Bogus post_time. */ | 87 TrackedTime kBogusStartRunTime; |
85 base::TimeTicks(), /* Bogus delayed_start_time. */ | 88 TrackedTime kBogusEndRunTime; |
86 base::TimeTicks(), /* Bogus start_run_time. */ | 89 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, kBogusStartRunTime, |
87 base::TimeTicks() /* Bogus end_run_time */ ); | 90 kBogusEndRunTime); |
88 | 91 |
89 birth_map.clear(); | 92 birth_map.clear(); |
90 data->SnapshotBirthMap(&birth_map); | 93 data->SnapshotBirthMap(&birth_map); |
91 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. | 94 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. |
92 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births. | 95 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births. |
93 death_map.clear(); | 96 death_map.clear(); |
94 data->SnapshotDeathMap(&death_map); | 97 data->SnapshotDeathMap(&death_map); |
95 EXPECT_EQ(1u, death_map.size()); // 1 location. | 98 EXPECT_EQ(1u, death_map.size()); // 1 location. |
96 EXPECT_EQ(1, death_map.begin()->second.count()); // 1 death. | 99 EXPECT_EQ(1, death_map.begin()->second.count()); // 1 death. |
97 | 100 |
98 // The births were at the same location as the one known death. | 101 // The births were at the same location as the one known death. |
99 EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first); | 102 EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first); |
100 } | 103 } |
101 | 104 |
102 TEST_F(TrackedObjectsTest, DeathDataTest) { | 105 TEST_F(TrackedObjectsTest, DeathDataTest) { |
103 if (!ThreadData::StartTracking(true)) | 106 if (!ThreadData::InitializeAndSetTrackingStatus(true)) |
104 return; | 107 return; |
105 | 108 |
106 scoped_ptr<DeathData> data(new DeathData()); | 109 scoped_ptr<DeathData> data(new DeathData()); |
107 ASSERT_NE(data, reinterpret_cast<DeathData*>(NULL)); | 110 ASSERT_NE(data, reinterpret_cast<DeathData*>(NULL)); |
108 EXPECT_EQ(data->run_duration(), base::TimeDelta()); | 111 EXPECT_EQ(data->run_duration(), Duration()); |
109 EXPECT_EQ(data->queue_duration(), base::TimeDelta()); | 112 EXPECT_EQ(data->queue_duration(), Duration()); |
110 EXPECT_EQ(data->AverageMsRunDuration(), 0); | 113 EXPECT_EQ(data->AverageMsRunDuration(), 0); |
111 EXPECT_EQ(data->AverageMsQueueDuration(), 0); | 114 EXPECT_EQ(data->AverageMsQueueDuration(), 0); |
112 EXPECT_EQ(data->count(), 0); | 115 EXPECT_EQ(data->count(), 0); |
113 | 116 |
114 int run_ms = 42; | 117 int run_ms = 42; |
115 int queue_ms = 8; | 118 int queue_ms = 8; |
116 | 119 |
117 base::TimeDelta run_duration = base::TimeDelta().FromMilliseconds(run_ms); | 120 Duration run_duration = Duration().FromMilliseconds(run_ms); |
118 base::TimeDelta queue_duration = base::TimeDelta().FromMilliseconds(queue_ms); | 121 Duration queue_duration = Duration().FromMilliseconds(queue_ms); |
119 data->RecordDeath(queue_duration, run_duration); | 122 data->RecordDeath(queue_duration, run_duration); |
120 EXPECT_EQ(data->run_duration(), run_duration); | 123 EXPECT_EQ(data->run_duration(), run_duration); |
121 EXPECT_EQ(data->queue_duration(), queue_duration); | 124 EXPECT_EQ(data->queue_duration(), queue_duration); |
122 EXPECT_EQ(data->AverageMsRunDuration(), run_ms); | 125 EXPECT_EQ(data->AverageMsRunDuration(), run_ms); |
123 EXPECT_EQ(data->AverageMsQueueDuration(), queue_ms); | 126 EXPECT_EQ(data->AverageMsQueueDuration(), queue_ms); |
124 EXPECT_EQ(data->count(), 1); | 127 EXPECT_EQ(data->count(), 1); |
125 | 128 |
126 data->RecordDeath(queue_duration, run_duration); | 129 data->RecordDeath(queue_duration, run_duration); |
127 EXPECT_EQ(data->run_duration(), run_duration + run_duration); | 130 EXPECT_EQ(data->run_duration(), run_duration + run_duration); |
128 EXPECT_EQ(data->queue_duration(), queue_duration + queue_duration); | 131 EXPECT_EQ(data->queue_duration(), queue_duration + queue_duration); |
(...skipping 16 matching lines...) Expand all Loading... |
145 EXPECT_EQ(output, results); | 148 EXPECT_EQ(output, results); |
146 | 149 |
147 scoped_ptr<base::Value> value(data->ToValue()); | 150 scoped_ptr<base::Value> value(data->ToValue()); |
148 std::string json; | 151 std::string json; |
149 base::JSONWriter::Write(value.get(), false, &json); | 152 base::JSONWriter::Write(value.get(), false, &json); |
150 std::string birth_only_result = "{\"count\":2,\"queue_ms\":16,\"run_ms\":84}"; | 153 std::string birth_only_result = "{\"count\":2,\"queue_ms\":16,\"run_ms\":84}"; |
151 EXPECT_EQ(json, birth_only_result); | 154 EXPECT_EQ(json, birth_only_result); |
152 } | 155 } |
153 | 156 |
154 TEST_F(TrackedObjectsTest, BirthOnlyToValueWorkerThread) { | 157 TEST_F(TrackedObjectsTest, BirthOnlyToValueWorkerThread) { |
155 if (!ThreadData::StartTracking(true)) | 158 if (!ThreadData::InitializeAndSetTrackingStatus(true)) |
156 return; | 159 return; |
157 // We don't initialize system with a thread name, so we're viewed as a worker | 160 // We don't initialize system with a thread name, so we're viewed as a worker |
158 // thread. | 161 // thread. |
159 int fake_line_number = 173; | 162 const int kFakeLineNumber = 173; |
160 const char* kFile = "FixedFileName"; | 163 const char* kFile = "FixedFileName"; |
161 const char* kFunction = "BirthOnlyToValueWorkerThread"; | 164 const char* kFunction = "BirthOnlyToValueWorkerThread"; |
162 Location location(kFunction, kFile, fake_line_number, NULL); | 165 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
163 Births* birth = ThreadData::TallyABirthIfActive(location); | 166 Births* birth = ThreadData::TallyABirthIfActive(location); |
164 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); | 167 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); |
165 | 168 |
166 int process_type = 3; | 169 scoped_ptr<base::Value> value(ThreadData::ToValue()); |
167 scoped_ptr<base::Value> value(ThreadData::ToValue(process_type)); | |
168 std::string json; | 170 std::string json; |
169 base::JSONWriter::Write(value.get(), false, &json); | 171 base::JSONWriter::Write(value.get(), false, &json); |
170 std::string birth_only_result = "{" | 172 std::string birth_only_result = "{" |
171 "\"list\":[" | 173 "\"list\":[" |
172 "{" | 174 "{" |
173 "\"birth_thread\":\"WorkerThread-1\"," | 175 "\"birth_thread\":\"WorkerThread-1\"," |
174 "\"death_data\":{" | 176 "\"death_data\":{" |
175 "\"count\":1," | 177 "\"count\":1," |
176 "\"queue_ms\":0," | 178 "\"queue_ms\":0," |
177 "\"run_ms\":0" | 179 "\"run_ms\":0" |
178 "}," | 180 "}," |
179 "\"death_thread\":\"Still_Alive\"," | 181 "\"death_thread\":\"Still_Alive\"," |
180 "\"location\":{" | 182 "\"location\":{" |
181 "\"file_name\":\"FixedFileName\"," | 183 "\"file_name\":\"FixedFileName\"," |
182 "\"function_name\":\"BirthOnlyToValueWorkerThread\"," | 184 "\"function_name\":\"BirthOnlyToValueWorkerThread\"," |
183 "\"line_number\":173" | 185 "\"line_number\":173" |
184 "}" | 186 "}" |
185 "}" | 187 "}" |
186 "]," | 188 "]" |
187 "\"process\":3" | |
188 "}"; | 189 "}"; |
189 EXPECT_EQ(json, birth_only_result); | 190 EXPECT_EQ(json, birth_only_result); |
190 } | 191 } |
191 | 192 |
192 TEST_F(TrackedObjectsTest, BirthOnlyToValueMainThread) { | 193 TEST_F(TrackedObjectsTest, BirthOnlyToValueMainThread) { |
193 if (!ThreadData::StartTracking(true)) | 194 if (!ThreadData::InitializeAndSetTrackingStatus(true)) |
194 return; | 195 return; |
195 | 196 |
196 // Use a well named thread. | 197 // Use a well named thread. |
197 ThreadData::InitializeThreadContext("SomeMainThreadName"); | 198 ThreadData::InitializeThreadContext("SomeMainThreadName"); |
198 int fake_line_number = 173; | 199 const int kFakeLineNumber = 173; |
199 const char* kFile = "FixedFileName"; | 200 const char* kFile = "FixedFileName"; |
200 const char* kFunction = "BirthOnlyToValueMainThread"; | 201 const char* kFunction = "BirthOnlyToValueMainThread"; |
201 Location location(kFunction, kFile, fake_line_number, NULL); | 202 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
202 // Do not delete birth. We don't own it. | 203 // Do not delete birth. We don't own it. |
203 Births* birth = ThreadData::TallyABirthIfActive(location); | 204 Births* birth = ThreadData::TallyABirthIfActive(location); |
204 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); | 205 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); |
205 | 206 |
206 int process_type = 34; | 207 scoped_ptr<base::Value> value(ThreadData::ToValue()); |
207 scoped_ptr<base::Value> value(ThreadData::ToValue(process_type)); | |
208 std::string json; | 208 std::string json; |
209 base::JSONWriter::Write(value.get(), false, &json); | 209 base::JSONWriter::Write(value.get(), false, &json); |
210 std::string birth_only_result = "{" | 210 std::string birth_only_result = "{" |
211 "\"list\":[" | 211 "\"list\":[" |
212 "{" | 212 "{" |
213 "\"birth_thread\":\"SomeMainThreadName\"," | 213 "\"birth_thread\":\"SomeMainThreadName\"," |
214 "\"death_data\":{" | 214 "\"death_data\":{" |
215 "\"count\":1," | 215 "\"count\":1," |
216 "\"queue_ms\":0," | 216 "\"queue_ms\":0," |
217 "\"run_ms\":0" | 217 "\"run_ms\":0" |
218 "}," | 218 "}," |
219 "\"death_thread\":\"Still_Alive\"," | 219 "\"death_thread\":\"Still_Alive\"," |
220 "\"location\":{" | 220 "\"location\":{" |
221 "\"file_name\":\"FixedFileName\"," | 221 "\"file_name\":\"FixedFileName\"," |
222 "\"function_name\":\"BirthOnlyToValueMainThread\"," | 222 "\"function_name\":\"BirthOnlyToValueMainThread\"," |
223 "\"line_number\":173" | 223 "\"line_number\":173" |
224 "}" | 224 "}" |
225 "}" | 225 "}" |
226 "]," | 226 "]" |
227 "\"process\":34" | |
228 "}"; | 227 "}"; |
229 EXPECT_EQ(json, birth_only_result); | 228 EXPECT_EQ(json, birth_only_result); |
230 } | 229 } |
231 | 230 |
232 TEST_F(TrackedObjectsTest, LifeCycleToValueMainThread) { | 231 TEST_F(TrackedObjectsTest, LifeCycleToValueMainThread) { |
233 if (!ThreadData::StartTracking(true)) | 232 if (!ThreadData::InitializeAndSetTrackingStatus(true)) |
234 return; | 233 return; |
235 | 234 |
236 // Use a well named thread. | 235 // Use a well named thread. |
237 ThreadData::InitializeThreadContext("SomeMainThreadName"); | 236 ThreadData::InitializeThreadContext("SomeMainThreadName"); |
238 int fake_line_number = 236; | 237 const int kFakeLineNumber = 236; |
239 const char* kFile = "FixedFileName"; | 238 const char* kFile = "FixedFileName"; |
240 const char* kFunction = "LifeCycleToValueMainThread"; | 239 const char* kFunction = "LifeCycleToValueMainThread"; |
241 Location location(kFunction, kFile, fake_line_number, NULL); | 240 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
242 // Do not delete birth. We don't own it. | 241 // Do not delete birth. We don't own it. |
243 Births* birth = ThreadData::TallyABirthIfActive(location); | 242 Births* birth = ThreadData::TallyABirthIfActive(location); |
244 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); | 243 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); |
245 | 244 |
246 // TimeTicks initializers ar ein microseconds. Durations are calculated in | 245 const base::TimeTicks kTimePosted = base::TimeTicks() |
247 // milliseconds, so we need to use 1000x. | 246 + base::TimeDelta::FromMilliseconds(1); |
248 const base::TimeTicks time_posted = base::TimeTicks() + | 247 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); |
249 base::TimeDelta::FromMilliseconds(1); | 248 // TrackingInfo will call TallyABirth() during construction. |
250 const base::TimeTicks delayed_start_time = base::TimeTicks(); | 249 base::TrackingInfo pending_task(location, kDelayedStartTime); |
251 const base::TimeTicks start_of_run = base::TimeTicks() + | 250 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). |
252 base::TimeDelta::FromMilliseconds(5); | |
253 const base::TimeTicks end_of_run = base::TimeTicks() + | |
254 base::TimeDelta::FromMilliseconds(7); | |
255 ThreadData::TallyADeathIfActive(birth, time_posted, delayed_start_time, | |
256 start_of_run, end_of_run); | |
257 | 251 |
258 int process_type = 7; | 252 const TrackedTime kStartOfRun = TrackedTime() + |
259 scoped_ptr<base::Value> value(ThreadData::ToValue(process_type)); | 253 Duration::FromMilliseconds(5); |
| 254 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); |
| 255 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, |
| 256 kStartOfRun, kEndOfRun); |
| 257 |
| 258 scoped_ptr<base::Value> value(ThreadData::ToValue()); |
260 std::string json; | 259 std::string json; |
261 base::JSONWriter::Write(value.get(), false, &json); | 260 base::JSONWriter::Write(value.get(), false, &json); |
262 std::string one_line_result = "{" | 261 std::string one_line_result = "{" |
263 "\"list\":[" | 262 "\"list\":[" |
264 "{" | 263 "{" |
265 "\"birth_thread\":\"SomeMainThreadName\"," | 264 "\"birth_thread\":\"SomeMainThreadName\"," |
266 "\"death_data\":{" | 265 "\"death_data\":{" |
267 "\"count\":1," | 266 "\"count\":1," |
268 "\"queue_ms\":4," | 267 "\"queue_ms\":4," |
269 "\"run_ms\":2" | 268 "\"run_ms\":2" |
270 "}," | 269 "}," |
271 "\"death_thread\":\"SomeMainThreadName\"," | 270 "\"death_thread\":\"SomeMainThreadName\"," |
272 "\"location\":{" | 271 "\"location\":{" |
273 "\"file_name\":\"FixedFileName\"," | 272 "\"file_name\":\"FixedFileName\"," |
274 "\"function_name\":\"LifeCycleToValueMainThread\"," | 273 "\"function_name\":\"LifeCycleToValueMainThread\"," |
275 "\"line_number\":236" | 274 "\"line_number\":236" |
276 "}" | 275 "}" |
277 "}" | 276 "}" |
278 "]," | 277 "]" |
279 "\"process\":7" | 278 "}"; |
| 279 EXPECT_EQ(json, one_line_result); |
| 280 } |
| 281 |
| 282 TEST_F(TrackedObjectsTest, LifeCycleToValueWorkerThread) { |
| 283 if (!ThreadData::InitializeAndSetTrackingStatus(true)) |
| 284 return; |
| 285 |
| 286 // Don't initialize thread, so that we appear as a worker thread. |
| 287 // ThreadData::InitializeThreadContext("SomeMainThreadName"); |
| 288 |
| 289 const int kFakeLineNumber = 236; |
| 290 const char* kFile = "FixedFileName"; |
| 291 const char* kFunction = "LifeCycleToValueWorkerThread"; |
| 292 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
| 293 // Do not delete birth. We don't own it. |
| 294 Births* birth = ThreadData::TallyABirthIfActive(location); |
| 295 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); |
| 296 |
| 297 const TrackedTime kTimePosted = TrackedTime() + Duration::FromMilliseconds(1); |
| 298 const TrackedTime kStartOfRun = TrackedTime() + |
| 299 Duration::FromMilliseconds(5); |
| 300 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); |
| 301 ThreadData::TallyRunOnWorkerThreadIfTracking(birth, kTimePosted, |
| 302 kStartOfRun, kEndOfRun); |
| 303 |
| 304 scoped_ptr<base::Value> value(ThreadData::ToValue()); |
| 305 std::string json; |
| 306 base::JSONWriter::Write(value.get(), false, &json); |
| 307 std::string one_line_result = "{" |
| 308 "\"list\":[" |
| 309 "{" |
| 310 "\"birth_thread\":\"WorkerThread-1\"," |
| 311 "\"death_data\":{" |
| 312 "\"count\":1," |
| 313 "\"queue_ms\":4," |
| 314 "\"run_ms\":2" |
| 315 "}," |
| 316 "\"death_thread\":\"WorkerThread-1\"," |
| 317 "\"location\":{" |
| 318 "\"file_name\":\"FixedFileName\"," |
| 319 "\"function_name\":\"LifeCycleToValueWorkerThread\"," |
| 320 "\"line_number\":236" |
| 321 "}" |
| 322 "}" |
| 323 "]" |
280 "}"; | 324 "}"; |
281 EXPECT_EQ(json, one_line_result); | 325 EXPECT_EQ(json, one_line_result); |
282 } | 326 } |
283 | 327 |
284 TEST_F(TrackedObjectsTest, TwoLives) { | 328 TEST_F(TrackedObjectsTest, TwoLives) { |
285 if (!ThreadData::StartTracking(true)) | 329 if (!ThreadData::InitializeAndSetTrackingStatus(true)) |
286 return; | 330 return; |
287 | 331 |
288 // Use a well named thread. | 332 // Use a well named thread. |
289 ThreadData::InitializeThreadContext("SomeFileThreadName"); | 333 ThreadData::InitializeThreadContext("SomeFileThreadName"); |
290 int fake_line_number = 222; | 334 const int kFakeLineNumber = 222; |
291 const char* kFile = "AnotherFileName"; | 335 const char* kFile = "AnotherFileName"; |
292 const char* kFunction = "TwoLives"; | 336 const char* kFunction = "TwoLives"; |
293 Location location(kFunction, kFile, fake_line_number, NULL); | 337 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
294 // Do not delete birth. We don't own it. | 338 // Do not delete birth. We don't own it. |
295 Births* birth = ThreadData::TallyABirthIfActive(location); | 339 Births* birth = ThreadData::TallyABirthIfActive(location); |
296 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); | 340 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); |
297 | 341 |
298 // TimeTicks initializers ar ein microseconds. Durations are calculated in | |
299 // milliseconds, so we need to use 1000x. | |
300 const base::TimeTicks time_posted = base::TimeTicks() + | |
301 base::TimeDelta::FromMilliseconds(1); | |
302 const base::TimeTicks delayed_start_time = base::TimeTicks(); | |
303 const base::TimeTicks start_of_run = base::TimeTicks() + | |
304 base::TimeDelta::FromMilliseconds(5); | |
305 const base::TimeTicks end_of_run = base::TimeTicks() + | |
306 base::TimeDelta::FromMilliseconds(7); | |
307 ThreadData::TallyADeathIfActive(birth, time_posted, delayed_start_time, | |
308 start_of_run, end_of_run); | |
309 | 342 |
310 birth = ThreadData::TallyABirthIfActive(location); | 343 const base::TimeTicks kTimePosted = base::TimeTicks() |
311 ThreadData::TallyADeathIfActive(birth, time_posted, delayed_start_time, | 344 + base::TimeDelta::FromMilliseconds(1); |
312 start_of_run, end_of_run); | 345 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); |
| 346 // TrackingInfo will call TallyABirth() during construction. |
| 347 base::TrackingInfo pending_task(location, kDelayedStartTime); |
| 348 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). |
313 | 349 |
314 int process_type = 7; | 350 const TrackedTime kStartOfRun = TrackedTime() + |
315 scoped_ptr<base::Value> value(ThreadData::ToValue(process_type)); | 351 Duration::FromMilliseconds(5); |
| 352 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); |
| 353 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, |
| 354 kStartOfRun, kEndOfRun); |
| 355 |
| 356 // TrackingInfo will call TallyABirth() during construction. |
| 357 base::TrackingInfo pending_task2(location, kDelayedStartTime); |
| 358 pending_task2.time_posted = kTimePosted; // Overwrite implied Now(). |
| 359 |
| 360 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task2, |
| 361 kStartOfRun, kEndOfRun); |
| 362 |
| 363 scoped_ptr<base::Value> value(ThreadData::ToValue()); |
316 std::string json; | 364 std::string json; |
317 base::JSONWriter::Write(value.get(), false, &json); | 365 base::JSONWriter::Write(value.get(), false, &json); |
318 std::string one_line_result = "{" | 366 std::string one_line_result = "{" |
319 "\"list\":[" | 367 "\"list\":[" |
320 "{" | 368 "{" |
321 "\"birth_thread\":\"SomeFileThreadName\"," | 369 "\"birth_thread\":\"SomeFileThreadName\"," |
322 "\"death_data\":{" | 370 "\"death_data\":{" |
323 "\"count\":2," | 371 "\"count\":2," |
324 "\"queue_ms\":8," | 372 "\"queue_ms\":8," |
325 "\"run_ms\":4" | 373 "\"run_ms\":4" |
326 "}," | 374 "}," |
327 "\"death_thread\":\"SomeFileThreadName\"," | 375 "\"death_thread\":\"SomeFileThreadName\"," |
328 "\"location\":{" | 376 "\"location\":{" |
329 "\"file_name\":\"AnotherFileName\"," | 377 "\"file_name\":\"AnotherFileName\"," |
330 "\"function_name\":\"TwoLives\"," | 378 "\"function_name\":\"TwoLives\"," |
331 "\"line_number\":222" | 379 "\"line_number\":222" |
332 "}" | 380 "}" |
333 "}" | 381 "}" |
334 "]," | 382 "]" |
335 "\"process\":7" | |
336 "}"; | 383 "}"; |
337 EXPECT_EQ(json, one_line_result); | 384 EXPECT_EQ(json, one_line_result); |
338 } | 385 } |
339 | 386 |
340 TEST_F(TrackedObjectsTest, DifferentLives) { | 387 TEST_F(TrackedObjectsTest, DifferentLives) { |
341 if (!ThreadData::StartTracking(true)) | 388 if (!ThreadData::InitializeAndSetTrackingStatus(true)) |
342 return; | 389 return; |
343 | 390 |
344 // Use a well named thread. | 391 // Use a well named thread. |
345 ThreadData::InitializeThreadContext("SomeFileThreadName"); | 392 ThreadData::InitializeThreadContext("SomeFileThreadName"); |
346 int fake_line_number = 567; | 393 const int kFakeLineNumber = 567; |
347 const char* kFile = "AnotherFileName"; | 394 const char* kFile = "AnotherFileName"; |
348 const char* kFunction = "DifferentLives"; | 395 const char* kFunction = "DifferentLives"; |
349 Location location(kFunction, kFile, fake_line_number, NULL); | 396 Location location(kFunction, kFile, kFakeLineNumber, NULL); |
350 // Do not delete birth. We don't own it. | |
351 Births* birth = ThreadData::TallyABirthIfActive(location); | |
352 EXPECT_NE(birth, reinterpret_cast<Births*>(NULL)); | |
353 | 397 |
354 // TimeTicks initializers ar ein microseconds. Durations are calculated in | 398 const base::TimeTicks kTimePosted = base::TimeTicks() |
355 // milliseconds, so we need to use 1000x. | 399 + base::TimeDelta::FromMilliseconds(1); |
356 const base::TimeTicks time_posted = base::TimeTicks() + | 400 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); |
357 base::TimeDelta::FromMilliseconds(1); | 401 // TrackingInfo will call TallyABirth() during construction. |
358 const base::TimeTicks delayed_start_time = base::TimeTicks(); | 402 base::TrackingInfo pending_task(location, kDelayedStartTime); |
359 const base::TimeTicks start_of_run = base::TimeTicks() + | 403 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). |
360 base::TimeDelta::FromMilliseconds(5); | |
361 const base::TimeTicks end_of_run = base::TimeTicks() + | |
362 base::TimeDelta::FromMilliseconds(7); | |
363 ThreadData::TallyADeathIfActive(birth, time_posted, delayed_start_time, | |
364 start_of_run, end_of_run); | |
365 | 404 |
366 int second_fake_line_number = 999; | 405 const TrackedTime kStartOfRun = TrackedTime() + |
367 Location second_location(kFunction, kFile, second_fake_line_number, NULL); | 406 Duration::FromMilliseconds(5); |
368 birth = ThreadData::TallyABirthIfActive(second_location); | 407 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7); |
| 408 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, |
| 409 kStartOfRun, kEndOfRun); |
369 | 410 |
370 int process_type = 2; | 411 const int kSecondFakeLineNumber = 999; |
371 scoped_ptr<base::Value> value(ThreadData::ToValue(process_type)); | 412 Location second_location(kFunction, kFile, kSecondFakeLineNumber, NULL); |
| 413 |
| 414 // TrackingInfo will call TallyABirth() during construction. |
| 415 base::TrackingInfo pending_task2(second_location, kDelayedStartTime); |
| 416 pending_task2.time_posted = kTimePosted; // Overwrite implied Now(). |
| 417 |
| 418 scoped_ptr<base::Value> value(ThreadData::ToValue()); |
372 std::string json; | 419 std::string json; |
373 base::JSONWriter::Write(value.get(), false, &json); | 420 base::JSONWriter::Write(value.get(), false, &json); |
374 std::string one_line_result = "{" | 421 std::string one_line_result = "{" |
375 "\"list\":[" | 422 "\"list\":[" |
376 "{" | 423 "{" |
377 "\"birth_thread\":\"SomeFileThreadName\"," | 424 "\"birth_thread\":\"SomeFileThreadName\"," |
378 "\"death_data\":{" | 425 "\"death_data\":{" |
379 "\"count\":1," | 426 "\"count\":1," |
380 "\"queue_ms\":4," | 427 "\"queue_ms\":4," |
381 "\"run_ms\":2" | 428 "\"run_ms\":2" |
(...skipping 12 matching lines...) Expand all Loading... |
394 "\"queue_ms\":0," | 441 "\"queue_ms\":0," |
395 "\"run_ms\":0" | 442 "\"run_ms\":0" |
396 "}," | 443 "}," |
397 "\"death_thread\":\"Still_Alive\"," | 444 "\"death_thread\":\"Still_Alive\"," |
398 "\"location\":{" | 445 "\"location\":{" |
399 "\"file_name\":\"AnotherFileName\"," | 446 "\"file_name\":\"AnotherFileName\"," |
400 "\"function_name\":\"DifferentLives\"," | 447 "\"function_name\":\"DifferentLives\"," |
401 "\"line_number\":999" | 448 "\"line_number\":999" |
402 "}" | 449 "}" |
403 "}" | 450 "}" |
404 "]," | 451 "]" |
405 "\"process\":2" | |
406 "}"; | 452 "}"; |
407 EXPECT_EQ(json, one_line_result); | 453 EXPECT_EQ(json, one_line_result); |
408 } | 454 } |
409 | 455 |
410 } // namespace tracked_objects | 456 } // namespace tracked_objects |
OLD | NEW |