Chromium Code Reviews| Index: chrome/browser/performance_monitor/performance_monitor_browsertest.cc |
| diff --git a/chrome/browser/performance_monitor/performance_monitor_browsertest.cc b/chrome/browser/performance_monitor/performance_monitor_browsertest.cc |
| index 9867d1cd517adb44676ff79a1313605d6f43c2d3..70c6c74cbd4e9e1f88b806dba3b4afd0e1dad711 100644 |
| --- a/chrome/browser/performance_monitor/performance_monitor_browsertest.cc |
| +++ b/chrome/browser/performance_monitor/performance_monitor_browsertest.cc |
| @@ -16,6 +16,7 @@ |
| #include "chrome/browser/extensions/unpacked_installer.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| #include "chrome/common/chrome_notification_types.h" |
| #include "chrome/common/chrome_paths.h" |
| #include "chrome/common/chrome_version_info.h" |
| @@ -29,6 +30,9 @@ using performance_monitor::Event; |
| namespace { |
| +// Used in PerformanceMonitorBrowserTest.GatherStatistics to consume CPU cycles. |
| +int kSpinCount = 1000000000; |
| + |
| // Helper struct to store the information of an extension; this is needed if the |
| // pointer to the extension ever becomes invalid (e.g., if we uninstall the |
| // extension). |
| @@ -131,6 +135,34 @@ class PerformanceMonitorBrowserTest : public ExtensionBrowserTest { |
| return events; |
| } |
| + // Retrieves stats from the database, given |activity| and |metric|. Does the |
| + // retrieval on a background thread. |
| + Database::MetricInfoVector GetStatsForActivityAndMetric( |
| + const std::string& activity, |
| + const std::string& metric) { |
| + Database::MetricInfoVector stats; |
| + content::BrowserThread::PostBlockingPoolSequencedTask( |
| + Database::kDatabaseSequenceToken, |
| + FROM_HERE, |
| + base::Bind(&PerformanceMonitorBrowserTest:: |
| + GetStatsForActivityAndMetricOnBackgroundThread, |
| + base::Unretained(this), |
| + activity, |
| + metric, |
| + &stats)); |
| + |
| + content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| + return stats; |
| + } |
| + |
| + void GetStatsForActivityAndMetricOnBackgroundThread( |
| + const std::string& activity, |
| + const std::string& metric, |
| + Database::MetricInfoVector* stats) { |
| + *stats = performance_monitor_->database()-> |
| + GetStatsForActivityAndMetric(activity, metric); |
| + } |
| + |
| PerformanceMonitor* performance_monitor() const { |
| return performance_monitor_; |
| } |
| @@ -317,4 +349,69 @@ IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, MAYBE_NewVersionEvent) { |
| ASSERT_EQ(version_string, current_version); |
| } |
| +IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, GatherStatistics) { |
| + content::BrowserThread::PostBlockingPoolSequencedTask( |
| + Database::kDatabaseSequenceToken, |
| + FROM_HERE, |
| + base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread, |
| + base::Unretained(performance_monitor()))); |
| + |
| + content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| + |
| + // CPU Usage -- no stats recorded the first time |
| + Database::MetricInfoVector stats = GetStatsForActivityAndMetric( |
| + performance_monitor::kProcessChromeAggregate, |
| + performance_monitor::kMetricCPUUsage); |
| + ASSERT_EQ(0u, stats.size()); |
| + |
| + // Private memory usage |
| + stats = GetStatsForActivityAndMetric( |
| + performance_monitor::kProcessChromeAggregate, |
| + performance_monitor::kMetricPrivateMemoryUsage); |
| + ASSERT_EQ(1u, stats.size()); |
| + EXPECT_GT(stats[0].value, 0); |
| + |
| + // Shared memory usage |
| + stats = GetStatsForActivityAndMetric( |
| + performance_monitor::kProcessChromeAggregate, |
| + performance_monitor::kMetricSharedMemoryUsage); |
| + ASSERT_EQ(1u, stats.size()); |
| + EXPECT_GT(stats[0].value, 0); |
| + |
| + // TODO(mwrosen) something less barbaric? |
| + // Spin for a while, so CPU usage isn't 0 |
|
Devlin
2012/07/17 18:07:58
add a period.
mitchellwrosen
2012/07/20 19:42:36
Done.
|
| + int i = 0; |
| + for (; i < kSpinCount; ++i) { |
| + } |
| + ASSERT_EQ(kSpinCount, i); |
|
Devlin
2012/07/17 18:07:58
Is this just testing that a for-loop runs? If so,
mitchellwrosen
2012/07/20 19:42:36
See yoz's previous comment
|
| + |
| + content::BrowserThread::PostBlockingPoolSequencedTask( |
| + Database::kDatabaseSequenceToken, |
| + FROM_HERE, |
| + base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread, |
| + base::Unretained(performance_monitor()))); |
| + |
| + content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| + |
| + stats = GetStatsForActivityAndMetric( |
| + performance_monitor::kProcessChromeAggregate, |
| + performance_monitor::kMetricCPUUsage); |
| + ASSERT_EQ(1u, stats.size()); |
| + EXPECT_GT(stats[0].value, 0); |
| + |
| + // Private memory usage #2 |
| + stats = GetStatsForActivityAndMetric( |
| + performance_monitor::kProcessChromeAggregate, |
| + performance_monitor::kMetricPrivateMemoryUsage); |
| + ASSERT_EQ(2u, stats.size()); |
| + EXPECT_GT(stats[1].value, 0); |
| + |
| + // Shared memory usage #2 |
| + stats = GetStatsForActivityAndMetric( |
| + performance_monitor::kProcessChromeAggregate, |
| + performance_monitor::kMetricSharedMemoryUsage); |
| + ASSERT_EQ(2u, stats.size()); |
| + EXPECT_GT(stats[1].value, 0); |
| +} |
| + |
| } // namespace performance_monitor |