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