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/tabs/tab_strip_model.h" |
19 #include "chrome/browser/ui/browser_tabstrip.h" | 20 #include "chrome/browser/ui/browser_tabstrip.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 | 29 |
29 using extensions::Extension; | 30 using extensions::Extension; |
30 using performance_monitor::Event; | 31 using performance_monitor::Event; |
31 | 32 |
32 namespace { | 33 namespace { |
| 34 |
| 35 // Used in PerformanceMonitorBrowserTest.GatherStatistics to consume CPU cycles. |
| 36 int kSpinCount = 1000000000; |
| 37 |
33 // Helper struct to store the information of an extension; this is needed if the | 38 // Helper struct to store the information of an extension; this is needed if the |
34 // pointer to the extension ever becomes invalid (e.g., if we uninstall the | 39 // pointer to the extension ever becomes invalid (e.g., if we uninstall the |
35 // extension). | 40 // extension). |
36 struct ExtensionBasicInfo { | 41 struct ExtensionBasicInfo { |
37 // Empty constructor for stl-container-happiness. | 42 // Empty constructor for stl-container-happiness. |
38 ExtensionBasicInfo() { | 43 ExtensionBasicInfo() { |
39 } | 44 } |
40 explicit ExtensionBasicInfo(const Extension* extension) | 45 explicit ExtensionBasicInfo(const Extension* extension) |
41 : description(extension->description()), | 46 : description(extension->description()), |
42 id(extension->id()), | 47 id(extension->id()), |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 Database::kDatabaseSequenceToken, | 169 Database::kDatabaseSequenceToken, |
165 FROM_HERE, | 170 FROM_HERE, |
166 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, | 171 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, |
167 base::Unretained(this), | 172 base::Unretained(this), |
168 &events)); | 173 &events)); |
169 | 174 |
170 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 175 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
171 return events; | 176 return events; |
172 } | 177 } |
173 | 178 |
| 179 // Retrieves stats from the database, given |activity| and |metric|. Does the |
| 180 // retrieval on a background thread. |
| 181 Database::MetricInfoVector GetStatsForActivityAndMetric( |
| 182 const std::string& activity, |
| 183 const std::string& metric) { |
| 184 Database::MetricInfoVector stats; |
| 185 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 186 Database::kDatabaseSequenceToken, |
| 187 FROM_HERE, |
| 188 base::Bind(&PerformanceMonitorBrowserTest:: |
| 189 GetStatsForActivityAndMetricOnBackgroundThread, |
| 190 base::Unretained(this), |
| 191 activity, |
| 192 metric, |
| 193 &stats)); |
| 194 |
| 195 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 196 return stats; |
| 197 } |
| 198 |
| 199 void GetStatsForActivityAndMetricOnBackgroundThread( |
| 200 const std::string& activity, |
| 201 const std::string& metric, |
| 202 Database::MetricInfoVector* stats) { |
| 203 *stats = performance_monitor_->database()-> |
| 204 GetStatsForActivityAndMetric(activity, metric); |
| 205 } |
| 206 |
174 // A handle for inserting a state value into the database, which must be done | 207 // A handle for inserting a state value into the database, which must be done |
175 // on the background thread. This is useful for mocking up a scenario in which | 208 // on the background thread. This is useful for mocking up a scenario in which |
176 // the database has prior data stored. We mock synchronicity with | 209 // the database has prior data stored. We mock synchronicity with |
177 // FlushForTesting(). | 210 // FlushForTesting(). |
178 void AddStateValue(const std::string& key, const std::string& value) { | 211 void AddStateValue(const std::string& key, const std::string& value) { |
179 content::BrowserThread::PostBlockingPoolSequencedTask( | 212 content::BrowserThread::PostBlockingPoolSequencedTask( |
180 Database::kDatabaseSequenceToken, | 213 Database::kDatabaseSequenceToken, |
181 FROM_HERE, | 214 FROM_HERE, |
182 base::Bind(base::IgnoreResult(&Database::AddStateValue), | 215 base::Bind(base::IgnoreResult(&Database::AddStateValue), |
183 base::Unretained(performance_monitor()->database()), | 216 base::Unretained(performance_monitor()->database()), |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 | 421 |
389 std::string previous_version; | 422 std::string previous_version; |
390 std::string current_version; | 423 std::string current_version; |
391 | 424 |
392 ASSERT_TRUE(value->GetString("previousVersion", &previous_version)); | 425 ASSERT_TRUE(value->GetString("previousVersion", &previous_version)); |
393 ASSERT_EQ(kOldVersion, previous_version); | 426 ASSERT_EQ(kOldVersion, previous_version); |
394 ASSERT_TRUE(value->GetString("currentVersion", ¤t_version)); | 427 ASSERT_TRUE(value->GetString("currentVersion", ¤t_version)); |
395 ASSERT_EQ(version_string, current_version); | 428 ASSERT_EQ(version_string, current_version); |
396 } | 429 } |
397 | 430 |
| 431 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, GatherStatistics) { |
| 432 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 433 Database::kDatabaseSequenceToken, |
| 434 FROM_HERE, |
| 435 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread, |
| 436 base::Unretained(performance_monitor()))); |
| 437 |
| 438 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 439 |
| 440 // Gather CPU usage. No stats should be recorded because this was the first |
| 441 // call to GatherStatistics. |
| 442 Database::MetricInfoVector stats = GetStatsForActivityAndMetric( |
| 443 performance_monitor::kProcessChromeAggregate, |
| 444 performance_monitor::kMetricCPUUsage); |
| 445 ASSERT_EQ(0u, stats.size()); |
| 446 |
| 447 // Gather private memory usage. |
| 448 stats = GetStatsForActivityAndMetric( |
| 449 performance_monitor::kProcessChromeAggregate, |
| 450 performance_monitor::kMetricPrivateMemoryUsage); |
| 451 ASSERT_EQ(1u, stats.size()); |
| 452 EXPECT_GT(stats[0].value, 0); |
| 453 |
| 454 // Gather shared memory usage. |
| 455 stats = GetStatsForActivityAndMetric( |
| 456 performance_monitor::kProcessChromeAggregate, |
| 457 performance_monitor::kMetricSharedMemoryUsage); |
| 458 ASSERT_EQ(1u, stats.size()); |
| 459 EXPECT_GT(stats[0].value, 0); |
| 460 |
| 461 // TODO(mwrosen) something less barbaric? |
| 462 // Spin for a while, so CPU usage isn't 0. |
| 463 int i = 0; |
| 464 for (; i < kSpinCount; ++i) { |
| 465 } |
| 466 ASSERT_EQ(kSpinCount, i); |
| 467 |
| 468 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 469 Database::kDatabaseSequenceToken, |
| 470 FROM_HERE, |
| 471 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread, |
| 472 base::Unretained(performance_monitor()))); |
| 473 |
| 474 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 475 |
| 476 // Gather CPU usage a second time and verify a stat was recorded. |
| 477 stats = GetStatsForActivityAndMetric( |
| 478 performance_monitor::kProcessChromeAggregate, |
| 479 performance_monitor::kMetricCPUUsage); |
| 480 ASSERT_EQ(1u, stats.size()); |
| 481 EXPECT_GT(stats[0].value, 0); |
| 482 |
| 483 // Gather private memory usage a second time and verify a second stat was |
| 484 // recorded. |
| 485 stats = GetStatsForActivityAndMetric( |
| 486 performance_monitor::kProcessChromeAggregate, |
| 487 performance_monitor::kMetricPrivateMemoryUsage); |
| 488 ASSERT_EQ(2u, stats.size()); |
| 489 EXPECT_GT(stats[1].value, 0); |
| 490 |
| 491 // Gather shared memory usage a second time and verify a second stat was |
| 492 // recorded. |
| 493 stats = GetStatsForActivityAndMetric( |
| 494 performance_monitor::kProcessChromeAggregate, |
| 495 performance_monitor::kMetricSharedMemoryUsage); |
| 496 ASSERT_EQ(2u, stats.size()); |
| 497 EXPECT_GT(stats[1].value, 0); |
| 498 } |
| 499 |
398 #if !defined(OS_WIN) | 500 #if !defined(OS_WIN) |
399 // Disabled on Windows due to a bug where Windows will return a normal exit | 501 // Disabled on Windows due to a bug where Windows will return a normal exit |
400 // code in the testing environment, even if the process died (this is not the | 502 // code in the testing environment, even if the process died (this is not the |
401 // case when hand-testing). This code can be traced to MSDN functions in | 503 // case when hand-testing). This code can be traced to MSDN functions in |
402 // base::GetTerminationStatus(), so there's not much we can do. | 504 // base::GetTerminationStatus(), so there's not much we can do. |
403 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) { | 505 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) { |
404 ui_test_utils::CrashTab(chrome::GetActiveWebContents(browser())); | 506 ui_test_utils::CrashTab(chrome::GetActiveWebContents(browser())); |
405 | 507 |
406 std::vector<linked_ptr<Event> > events = GetEvents(); | 508 std::vector<linked_ptr<Event> > events = GetEvents(); |
407 | 509 |
408 ASSERT_EQ(1u, events.size()); | 510 ASSERT_EQ(1u, events.size()); |
409 CheckEventType(EVENT_KILLED_BY_OS_CRASH, events[0]); | 511 CheckEventType(EVENT_KILLED_BY_OS_CRASH, events[0]); |
410 } | 512 } |
411 #endif // !defined(OS_WIN) | 513 #endif // !defined(OS_WIN) |
412 | 514 |
413 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, | 515 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, |
414 DISABLED_RendererCrashEvent) { | 516 DISABLED_RendererCrashEvent) { |
415 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL)); | 517 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL)); |
416 | 518 |
417 std::vector<linked_ptr<Event> > events = GetEvents(); | 519 std::vector<linked_ptr<Event> > events = GetEvents(); |
418 ASSERT_EQ(1u, events.size()); | 520 ASSERT_EQ(1u, events.size()); |
419 | 521 |
420 CheckEventType(EVENT_RENDERER_CRASH, events[0]); | 522 CheckEventType(EVENT_RENDERER_CRASH, events[0]); |
421 } | 523 } |
422 | 524 |
423 } // namespace performance_monitor | 525 } // namespace performance_monitor |
OLD | NEW |