| 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/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/performance_monitor/constants.h" | 14 #include "chrome/browser/performance_monitor/constants.h" |
| 15 #include "chrome/browser/performance_monitor/database.h" | 15 #include "chrome/browser/performance_monitor/database.h" |
| 16 #include "chrome/browser/performance_monitor/performance_monitor.h" | 16 #include "chrome/browser/performance_monitor/performance_monitor.h" |
| 17 #include "chrome/browser/extensions/extension_browsertest.h" | 17 #include "chrome/browser/extensions/extension_browsertest.h" |
| 18 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
| 19 #include "chrome/browser/extensions/unpacked_installer.h" | 19 #include "chrome/browser/extensions/unpacked_installer.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" | 21 #include "chrome/browser/profiles/profile_manager.h" |
| 22 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
| 23 #include "chrome/browser/ui/browser_tabstrip.h" | 23 #include "chrome/browser/ui/browser_tabstrip.h" |
| 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 24 #include "chrome/common/chrome_constants.h" | 25 #include "chrome/common/chrome_constants.h" |
| 25 #include "chrome/common/chrome_notification_types.h" | 26 #include "chrome/common/chrome_notification_types.h" |
| 26 #include "chrome/common/chrome_paths.h" | 27 #include "chrome/common/chrome_paths.h" |
| 27 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
| 28 #include "chrome/common/chrome_version_info.h" | 29 #include "chrome/common/chrome_version_info.h" |
| 29 #include "chrome/common/extensions/extension.h" | 30 #include "chrome/common/extensions/extension.h" |
| 30 #include "chrome/common/url_constants.h" | 31 #include "chrome/common/url_constants.h" |
| 31 #include "chrome/test/base/ui_test_utils.h" | 32 #include "chrome/test/base/ui_test_utils.h" |
| 32 #include "content/public/browser/notification_registrar.h" | 33 #include "content/public/browser/notification_registrar.h" |
| 33 #include "content/public/browser/notification_service.h" | 34 #include "content/public/browser/notification_service.h" |
| 34 #include "content/public/test/browser_test_utils.h" | 35 #include "content/public/test/browser_test_utils.h" |
| 35 #include "content/public/test/test_utils.h" | 36 #include "content/public/test/test_utils.h" |
| 36 | 37 |
| 37 using extensions::Extension; | 38 using extensions::Extension; |
| 38 using performance_monitor::Event; | 39 using performance_monitor::Event; |
| 39 | 40 |
| 40 namespace { | 41 namespace { |
| 42 |
| 41 // Helper struct to store the information of an extension; this is needed if the | 43 // Helper struct to store the information of an extension; this is needed if the |
| 42 // pointer to the extension ever becomes invalid (e.g., if we uninstall the | 44 // pointer to the extension ever becomes invalid (e.g., if we uninstall the |
| 43 // extension). | 45 // extension). |
| 44 struct ExtensionBasicInfo { | 46 struct ExtensionBasicInfo { |
| 45 // Empty constructor for stl-container-happiness. | 47 // Empty constructor for stl-container-happiness. |
| 46 ExtensionBasicInfo() { | 48 ExtensionBasicInfo() { |
| 47 } | 49 } |
| 48 explicit ExtensionBasicInfo(const Extension* extension) | 50 explicit ExtensionBasicInfo(const Extension* extension) |
| 49 : description(extension->description()), | 51 : description(extension->description()), |
| 50 id(extension->id()), | 52 id(extension->id()), |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 // to complete fully before proceeding with the test. | 143 // to complete fully before proceeding with the test. |
| 142 content::WindowedNotificationObserver windowed_observer( | 144 content::WindowedNotificationObserver windowed_observer( |
| 143 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, | 145 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, |
| 144 content::NotificationService::AllSources()); | 146 content::NotificationService::AllSources()); |
| 145 | 147 |
| 146 performance_monitor_->Start(); | 148 performance_monitor_->Start(); |
| 147 | 149 |
| 148 windowed_observer.Wait(); | 150 windowed_observer.Wait(); |
| 149 } | 151 } |
| 150 | 152 |
| 153 // A handle for gathering statistics from the database, which must be done on |
| 154 // the background thread. Since we are testing, we can mock synchronicity with |
| 155 // FlushForTesting(). |
| 156 void GatherStatistics() { |
| 157 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 158 Database::kDatabaseSequenceToken, |
| 159 FROM_HERE, |
| 160 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread, |
| 161 base::Unretained(performance_monitor()))); |
| 162 |
| 163 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 164 } |
| 165 |
| 151 void GetEventsOnBackgroundThread(std::vector<linked_ptr<Event> >* events) { | 166 void GetEventsOnBackgroundThread(std::vector<linked_ptr<Event> >* events) { |
| 152 // base::Time is potentially flaky in that there is no guarantee that it | 167 // base::Time is potentially flaky in that there is no guarantee that it |
| 153 // won't actually decrease between successive calls. If we call GetEvents | 168 // won't actually decrease between successive calls. If we call GetEvents |
| 154 // and the Database uses base::Time::Now() and gets a lesser time, then it | 169 // and the Database uses base::Time::Now() and gets a lesser time, then it |
| 155 // will return 0 events. Thus, we use a time that is guaranteed to be in the | 170 // will return 0 events. Thus, we use a time that is guaranteed to be in the |
| 156 // future (for at least the next couple hundred thousand years). | 171 // future (for at least the next couple hundred thousand years). |
| 157 *events = performance_monitor_->database()->GetEvents( | 172 *events = performance_monitor_->database()->GetEvents( |
| 158 base::Time(), base::Time::FromInternalValue(kint64max)); | 173 base::Time(), base::Time::FromInternalValue(kint64max)); |
| 159 } | 174 } |
| 160 | 175 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 172 Database::kDatabaseSequenceToken, | 187 Database::kDatabaseSequenceToken, |
| 173 FROM_HERE, | 188 FROM_HERE, |
| 174 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, | 189 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, |
| 175 base::Unretained(this), | 190 base::Unretained(this), |
| 176 &events)); | 191 &events)); |
| 177 | 192 |
| 178 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 193 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 179 return events; | 194 return events; |
| 180 } | 195 } |
| 181 | 196 |
| 197 void GetStatsOnBackgroundThread(Database::MetricInfoVector* metrics, |
| 198 MetricType type) { |
| 199 *metrics = performance_monitor_->database()->GetStatsForActivityAndMetric( |
| 200 type, base::Time(), base::Time::FromInternalValue(kint64max)); |
| 201 } |
| 202 |
| 203 // A handle for getting statistics from the database (see previous comments on |
| 204 // GetEvents() and GetEventsOnBackgroundThread). |
| 205 Database::MetricInfoVector GetStats(MetricType type) { |
| 206 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 207 content::RunAllPendingInMessageLoop(); |
| 208 |
| 209 Database::MetricInfoVector metrics; |
| 210 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 211 Database::kDatabaseSequenceToken, |
| 212 FROM_HERE, |
| 213 base::Bind(&PerformanceMonitorBrowserTest::GetStatsOnBackgroundThread, |
| 214 base::Unretained(this), |
| 215 &metrics, |
| 216 type)); |
| 217 |
| 218 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 219 return metrics; |
| 220 } |
| 221 |
| 182 // A handle for inserting a state value into the database, which must be done | 222 // A handle for inserting a state value into the database, which must be done |
| 183 // on the background thread. This is useful for mocking up a scenario in which | 223 // on the background thread. This is useful for mocking up a scenario in which |
| 184 // the database has prior data stored. We mock synchronicity with | 224 // the database has prior data stored. We mock synchronicity with |
| 185 // FlushForTesting(). | 225 // FlushForTesting(). |
| 186 void AddStateValue(const std::string& key, const std::string& value) { | 226 void AddStateValue(const std::string& key, const std::string& value) { |
| 187 content::BrowserThread::PostBlockingPoolSequencedTask( | 227 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 188 Database::kDatabaseSequenceToken, | 228 Database::kDatabaseSequenceToken, |
| 189 FROM_HERE, | 229 FROM_HERE, |
| 190 base::Bind(base::IgnoreResult(&Database::AddStateValue), | 230 base::Bind(base::IgnoreResult(&Database::AddStateValue), |
| 191 base::Unretained(performance_monitor()->database()), | 231 base::Unretained(performance_monitor()->database()), |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 | 495 |
| 456 std::string previous_version; | 496 std::string previous_version; |
| 457 std::string current_version; | 497 std::string current_version; |
| 458 | 498 |
| 459 ASSERT_TRUE(value->GetString("previousVersion", &previous_version)); | 499 ASSERT_TRUE(value->GetString("previousVersion", &previous_version)); |
| 460 ASSERT_EQ(kOldVersion, previous_version); | 500 ASSERT_EQ(kOldVersion, previous_version); |
| 461 ASSERT_TRUE(value->GetString("currentVersion", ¤t_version)); | 501 ASSERT_TRUE(value->GetString("currentVersion", ¤t_version)); |
| 462 ASSERT_EQ(version_string, current_version); | 502 ASSERT_EQ(version_string, current_version); |
| 463 } | 503 } |
| 464 | 504 |
| 505 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, GatherStatistics) { |
| 506 GatherStatistics(); |
| 507 |
| 508 // Gather CPU usage. No stats should be recorded because this was the first |
| 509 // call to GatherStatistics. |
| 510 Database::MetricInfoVector stats = GetStats(METRIC_CPU_USAGE); |
| 511 ASSERT_EQ(0u, stats.size()); |
| 512 |
| 513 // Gather private memory usage. |
| 514 stats = GetStats(METRIC_PRIVATE_MEMORY_USAGE); |
| 515 ASSERT_EQ(1u, stats.size()); |
| 516 EXPECT_GT(stats[0].value, 0); |
| 517 |
| 518 // Gather shared memory usage. |
| 519 stats = GetStats(METRIC_SHARED_MEMORY_USAGE); |
| 520 ASSERT_EQ(1u, stats.size()); |
| 521 EXPECT_GT(stats[0].value, 0); |
| 522 |
| 523 // Spin for a while, so CPU usage isn't 0. |
| 524 const int kSpinCount = 1000000000; |
| 525 int i = 0; |
| 526 for (; i < kSpinCount; ++i) { |
| 527 } |
| 528 ASSERT_EQ(kSpinCount, i); |
| 529 |
| 530 GatherStatistics(); |
| 531 |
| 532 // Gather CPU usage a second time and verify a stat was recorded. |
| 533 stats = GetStats(METRIC_CPU_USAGE); |
| 534 ASSERT_EQ(1u, stats.size()); |
| 535 EXPECT_GT(stats[0].value, 0); |
| 536 |
| 537 // Gather private memory usage a second time and verify a second stat was |
| 538 // recorded. |
| 539 stats = GetStats(METRIC_PRIVATE_MEMORY_USAGE); |
| 540 ASSERT_EQ(2u, stats.size()); |
| 541 EXPECT_GT(stats[1].value, 0); |
| 542 |
| 543 // Gather shared memory usage a second time and verify a second stat was |
| 544 // recorded. |
| 545 stats = GetStats(METRIC_SHARED_MEMORY_USAGE); |
| 546 ASSERT_EQ(2u, stats.size()); |
| 547 EXPECT_GT(stats[1].value, 0); |
| 548 } |
| 549 |
| 465 #if !defined(OS_WIN) | 550 #if !defined(OS_WIN) |
| 466 // Disabled on Windows due to a bug where Windows will return a normal exit | 551 // Disabled on Windows due to a bug where Windows will return a normal exit |
| 467 // code in the testing environment, even if the process died (this is not the | 552 // code in the testing environment, even if the process died (this is not the |
| 468 // case when hand-testing). This code can be traced to MSDN functions in | 553 // case when hand-testing). This code can be traced to MSDN functions in |
| 469 // base::GetTerminationStatus(), so there's not much we can do. | 554 // base::GetTerminationStatus(), so there's not much we can do. |
| 470 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) { | 555 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) { |
| 471 content::CrashTab(chrome::GetActiveWebContents(browser())); | 556 content::CrashTab(chrome::GetActiveWebContents(browser())); |
| 472 | 557 |
| 473 std::vector<linked_ptr<Event> > events = GetEvents(); | 558 std::vector<linked_ptr<Event> > events = GetEvents(); |
| 474 | 559 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 | 629 |
| 545 std::string event_profile; | 630 std::string event_profile; |
| 546 ASSERT_TRUE(events[0]->data()->GetString("profileName", &event_profile)); | 631 ASSERT_TRUE(events[0]->data()->GetString("profileName", &event_profile)); |
| 547 ASSERT_EQ(first_profile_name_, event_profile); | 632 ASSERT_EQ(first_profile_name_, event_profile); |
| 548 | 633 |
| 549 ASSERT_TRUE(events[1]->data()->GetString("profileName", &event_profile)); | 634 ASSERT_TRUE(events[1]->data()->GetString("profileName", &event_profile)); |
| 550 ASSERT_EQ(second_profile_name_, event_profile); | 635 ASSERT_EQ(second_profile_name_, event_profile); |
| 551 } | 636 } |
| 552 | 637 |
| 553 } // namespace performance_monitor | 638 } // namespace performance_monitor |
| OLD | NEW |