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