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