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 #include "chrome/test/base/in_process_browser_test.h" | 5 #include "chrome/test/base/in_process_browser_test.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
11 #include "chrome/browser/performance_monitor/constants.h" | 11 #include "chrome/browser/performance_monitor/constants.h" |
12 #include "chrome/browser/performance_monitor/database.h" | 12 #include "chrome/browser/performance_monitor/database.h" |
13 #include "chrome/browser/performance_monitor/performance_monitor.h" | 13 #include "chrome/browser/performance_monitor/performance_monitor.h" |
14 #include "chrome/browser/extensions/extension_browsertest.h" | 14 #include "chrome/browser/extensions/extension_browsertest.h" |
15 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
16 #include "chrome/browser/extensions/unpacked_installer.h" | 16 #include "chrome/browser/extensions/unpacked_installer.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
19 #include "chrome/browser/ui/browser_tabstrip.h" | 19 #include "chrome/browser/ui/browser_tabstrip.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
20 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
21 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
22 #include "chrome/common/chrome_version_info.h" | 23 #include "chrome/common/chrome_version_info.h" |
23 #include "chrome/common/extensions/extension.h" | 24 #include "chrome/common/extensions/extension.h" |
24 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
25 #include "chrome/test/base/ui_test_utils.h" | 26 #include "chrome/test/base/ui_test_utils.h" |
26 #include "content/public/browser/notification_registrar.h" | 27 #include "content/public/browser/notification_registrar.h" |
27 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
28 #include "content/public/test/browser_test_utils.h" | 29 #include "content/public/test/browser_test_utils.h" |
29 | 30 |
30 using extensions::Extension; | 31 using extensions::Extension; |
31 using performance_monitor::Event; | 32 using performance_monitor::Event; |
32 | 33 |
33 namespace { | 34 namespace { |
| 35 |
34 // Helper struct to store the information of an extension; this is needed if the | 36 // Helper struct to store the information of an extension; this is needed if the |
35 // pointer to the extension ever becomes invalid (e.g., if we uninstall the | 37 // pointer to the extension ever becomes invalid (e.g., if we uninstall the |
36 // extension). | 38 // extension). |
37 struct ExtensionBasicInfo { | 39 struct ExtensionBasicInfo { |
38 // Empty constructor for stl-container-happiness. | 40 // Empty constructor for stl-container-happiness. |
39 ExtensionBasicInfo() { | 41 ExtensionBasicInfo() { |
40 } | 42 } |
41 explicit ExtensionBasicInfo(const Extension* extension) | 43 explicit ExtensionBasicInfo(const Extension* extension) |
42 : description(extension->description()), | 44 : description(extension->description()), |
43 id(extension->id()), | 45 id(extension->id()), |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 // to complete fully before proceeding with the test. | 136 // to complete fully before proceeding with the test. |
135 content::WindowedNotificationObserver windowed_observer( | 137 content::WindowedNotificationObserver windowed_observer( |
136 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, | 138 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, |
137 content::NotificationService::AllSources()); | 139 content::NotificationService::AllSources()); |
138 | 140 |
139 performance_monitor_->Start(); | 141 performance_monitor_->Start(); |
140 | 142 |
141 windowed_observer.Wait(); | 143 windowed_observer.Wait(); |
142 } | 144 } |
143 | 145 |
| 146 // A handle for gathering statistics from the database, which must be done on |
| 147 // the background thread. Since we are testing, we can mock synchronicity with |
| 148 // FlushForTesting(). |
| 149 void GatherStatistics() { |
| 150 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 151 Database::kDatabaseSequenceToken, |
| 152 FROM_HERE, |
| 153 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread, |
| 154 base::Unretained(performance_monitor()))); |
| 155 |
| 156 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 157 } |
| 158 |
144 void GetEventsOnBackgroundThread(std::vector<linked_ptr<Event> >* events) { | 159 void GetEventsOnBackgroundThread(std::vector<linked_ptr<Event> >* events) { |
145 // base::Time is potentially flaky in that there is no guarantee that it | 160 // base::Time is potentially flaky in that there is no guarantee that it |
146 // won't actually decrease between successive calls. If we call GetEvents | 161 // won't actually decrease between successive calls. If we call GetEvents |
147 // and the Database uses base::Time::Now() and gets a lesser time, then it | 162 // and the Database uses base::Time::Now() and gets a lesser time, then it |
148 // will return 0 events. Thus, we use a time that is guaranteed to be in the | 163 // will return 0 events. Thus, we use a time that is guaranteed to be in the |
149 // future (for at least the next couple hundred thousand years). | 164 // future (for at least the next couple hundred thousand years). |
150 *events = performance_monitor_->database()->GetEvents( | 165 *events = performance_monitor_->database()->GetEvents( |
151 base::Time(), base::Time::FromInternalValue(kint64max)); | 166 base::Time(), base::Time::FromInternalValue(kint64max)); |
152 } | 167 } |
153 | 168 |
(...skipping 11 matching lines...) Expand all Loading... |
165 Database::kDatabaseSequenceToken, | 180 Database::kDatabaseSequenceToken, |
166 FROM_HERE, | 181 FROM_HERE, |
167 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, | 182 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, |
168 base::Unretained(this), | 183 base::Unretained(this), |
169 &events)); | 184 &events)); |
170 | 185 |
171 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 186 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
172 return events; | 187 return events; |
173 } | 188 } |
174 | 189 |
| 190 void GetStatsOnBackgroundThread(Database::MetricInfoVector* metrics, |
| 191 MetricType type) { |
| 192 *metrics = performance_monitor_->database()->GetStatsForActivityAndMetric( |
| 193 type, base::Time(), base::Time::FromInternalValue(kint64max)); |
| 194 } |
| 195 |
| 196 // A handle for getting statistics from the database (see previous comments on |
| 197 // GetEvents() and GetEventsOnBackgroundThread). |
| 198 Database::MetricInfoVector GetStats(MetricType type) { |
| 199 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 200 ui_test_utils::RunAllPendingInMessageLoop(); |
| 201 |
| 202 Database::MetricInfoVector metrics; |
| 203 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 204 Database::kDatabaseSequenceToken, |
| 205 FROM_HERE, |
| 206 base::Bind(&PerformanceMonitorBrowserTest::GetStatsOnBackgroundThread, |
| 207 base::Unretained(this), |
| 208 &metrics, |
| 209 type)); |
| 210 |
| 211 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 212 return metrics; |
| 213 } |
| 214 |
175 // A handle for inserting a state value into the database, which must be done | 215 // A handle for inserting a state value into the database, which must be done |
176 // on the background thread. This is useful for mocking up a scenario in which | 216 // on the background thread. This is useful for mocking up a scenario in which |
177 // the database has prior data stored. We mock synchronicity with | 217 // the database has prior data stored. We mock synchronicity with |
178 // FlushForTesting(). | 218 // FlushForTesting(). |
179 void AddStateValue(const std::string& key, const std::string& value) { | 219 void AddStateValue(const std::string& key, const std::string& value) { |
180 content::BrowserThread::PostBlockingPoolSequencedTask( | 220 content::BrowserThread::PostBlockingPoolSequencedTask( |
181 Database::kDatabaseSequenceToken, | 221 Database::kDatabaseSequenceToken, |
182 FROM_HERE, | 222 FROM_HERE, |
183 base::Bind(base::IgnoreResult(&Database::AddStateValue), | 223 base::Bind(base::IgnoreResult(&Database::AddStateValue), |
184 base::Unretained(performance_monitor()->database()), | 224 base::Unretained(performance_monitor()->database()), |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 | 429 |
390 std::string previous_version; | 430 std::string previous_version; |
391 std::string current_version; | 431 std::string current_version; |
392 | 432 |
393 ASSERT_TRUE(value->GetString("previousVersion", &previous_version)); | 433 ASSERT_TRUE(value->GetString("previousVersion", &previous_version)); |
394 ASSERT_EQ(kOldVersion, previous_version); | 434 ASSERT_EQ(kOldVersion, previous_version); |
395 ASSERT_TRUE(value->GetString("currentVersion", ¤t_version)); | 435 ASSERT_TRUE(value->GetString("currentVersion", ¤t_version)); |
396 ASSERT_EQ(version_string, current_version); | 436 ASSERT_EQ(version_string, current_version); |
397 } | 437 } |
398 | 438 |
| 439 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, GatherStatistics) { |
| 440 GatherStatistics(); |
| 441 |
| 442 // Gather CPU usage. No stats should be recorded because this was the first |
| 443 // call to GatherStatistics. |
| 444 Database::MetricInfoVector stats = GetStats(METRIC_CPU_USAGE); |
| 445 ASSERT_EQ(0u, stats.size()); |
| 446 |
| 447 // Gather private memory usage. |
| 448 stats = GetStats(METRIC_PRIVATE_MEMORY_USAGE); |
| 449 ASSERT_EQ(1u, stats.size()); |
| 450 EXPECT_GT(stats[0].value, 0); |
| 451 |
| 452 // Gather shared memory usage. |
| 453 stats = GetStats(METRIC_SHARED_MEMORY_USAGE); |
| 454 ASSERT_EQ(1u, stats.size()); |
| 455 EXPECT_GT(stats[0].value, 0); |
| 456 |
| 457 // Spin for a while, so CPU usage isn't 0. |
| 458 const int kSpinCount = 1000000000; |
| 459 int i = 0; |
| 460 for (; i < kSpinCount; ++i) { |
| 461 } |
| 462 ASSERT_EQ(kSpinCount, i); |
| 463 |
| 464 GatherStatistics(); |
| 465 |
| 466 // Gather CPU usage a second time and verify a stat was recorded. |
| 467 stats = GetStats(METRIC_CPU_USAGE); |
| 468 ASSERT_EQ(1u, stats.size()); |
| 469 EXPECT_GT(stats[0].value, 0); |
| 470 |
| 471 // Gather private memory usage a second time and verify a second stat was |
| 472 // recorded. |
| 473 stats = GetStats(METRIC_PRIVATE_MEMORY_USAGE); |
| 474 ASSERT_EQ(2u, stats.size()); |
| 475 EXPECT_GT(stats[1].value, 0); |
| 476 |
| 477 // Gather shared memory usage a second time and verify a second stat was |
| 478 // recorded. |
| 479 stats = GetStats(METRIC_SHARED_MEMORY_USAGE); |
| 480 ASSERT_EQ(2u, stats.size()); |
| 481 EXPECT_GT(stats[1].value, 0); |
| 482 } |
| 483 |
399 #if !defined(OS_WIN) | 484 #if !defined(OS_WIN) |
400 // Disabled on Windows due to a bug where Windows will return a normal exit | 485 // Disabled on Windows due to a bug where Windows will return a normal exit |
401 // code in the testing environment, even if the process died (this is not the | 486 // code in the testing environment, even if the process died (this is not the |
402 // case when hand-testing). This code can be traced to MSDN functions in | 487 // case when hand-testing). This code can be traced to MSDN functions in |
403 // base::GetTerminationStatus(), so there's not much we can do. | 488 // base::GetTerminationStatus(), so there's not much we can do. |
404 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) { | 489 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) { |
405 content::CrashTab(chrome::GetActiveWebContents(browser())); | 490 content::CrashTab(chrome::GetActiveWebContents(browser())); |
406 | 491 |
407 std::vector<linked_ptr<Event> > events = GetEvents(); | 492 std::vector<linked_ptr<Event> > events = GetEvents(); |
408 | 493 |
409 ASSERT_EQ(1u, events.size()); | 494 ASSERT_EQ(1u, events.size()); |
410 CheckEventType(EVENT_KILLED_BY_OS_CRASH, events[0]); | 495 CheckEventType(EVENT_KILLED_BY_OS_CRASH, events[0]); |
411 } | 496 } |
412 #endif // !defined(OS_WIN) | 497 #endif // !defined(OS_WIN) |
413 | 498 |
414 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, | 499 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, |
415 DISABLED_RendererCrashEvent) { | 500 DISABLED_RendererCrashEvent) { |
416 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL)); | 501 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL)); |
417 | 502 |
418 std::vector<linked_ptr<Event> > events = GetEvents(); | 503 std::vector<linked_ptr<Event> > events = GetEvents(); |
419 ASSERT_EQ(1u, events.size()); | 504 ASSERT_EQ(1u, events.size()); |
420 | 505 |
421 CheckEventType(EVENT_RENDERER_CRASH, events[0]); | 506 CheckEventType(EVENT_RENDERER_CRASH, events[0]); |
422 } | 507 } |
423 | 508 |
424 } // namespace performance_monitor | 509 } // namespace performance_monitor |
OLD | NEW |