| 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 9bc3965395f3f1a771b17e1e18ef42d57c241a69..a95c8158d031de7dc1e01ec2882e6bc46727c08d 100644
|
| --- a/chrome/browser/performance_monitor/performance_monitor_browsertest.cc
|
| +++ b/chrome/browser/performance_monitor/performance_monitor_browsertest.cc
|
| @@ -21,6 +21,7 @@
|
| #include "chrome/browser/profiles/profile_manager.h"
|
| #include "chrome/browser/ui/browser.h"
|
| #include "chrome/browser/ui/browser_tabstrip.h"
|
| +#include "chrome/browser/ui/tabs/tab_strip_model.h"
|
| #include "chrome/common/chrome_constants.h"
|
| #include "chrome/common/chrome_notification_types.h"
|
| #include "chrome/common/chrome_paths.h"
|
| @@ -38,6 +39,7 @@ using extensions::Extension;
|
| using performance_monitor::Event;
|
|
|
| namespace {
|
| +
|
| // 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).
|
| @@ -148,6 +150,19 @@ class PerformanceMonitorBrowserTest : public ExtensionBrowserTest {
|
| windowed_observer.Wait();
|
| }
|
|
|
| + // A handle for gathering statistics from the database, which must be done on
|
| + // the background thread. Since we are testing, we can mock synchronicity with
|
| + // FlushForTesting().
|
| + void GatherStatistics() {
|
| + content::BrowserThread::PostBlockingPoolSequencedTask(
|
| + Database::kDatabaseSequenceToken,
|
| + FROM_HERE,
|
| + base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread,
|
| + base::Unretained(performance_monitor())));
|
| +
|
| + content::BrowserThread::GetBlockingPool()->FlushForTesting();
|
| + }
|
| +
|
| void GetEventsOnBackgroundThread(std::vector<linked_ptr<Event> >* events) {
|
| // base::Time is potentially flaky in that there is no guarantee that it
|
| // won't actually decrease between successive calls. If we call GetEvents
|
| @@ -179,6 +194,31 @@ class PerformanceMonitorBrowserTest : public ExtensionBrowserTest {
|
| return events;
|
| }
|
|
|
| + void GetStatsOnBackgroundThread(Database::MetricInfoVector* metrics,
|
| + MetricType type) {
|
| + *metrics = performance_monitor_->database()->GetStatsForActivityAndMetric(
|
| + type, base::Time(), base::Time::FromInternalValue(kint64max));
|
| + }
|
| +
|
| + // A handle for getting statistics from the database (see previous comments on
|
| + // GetEvents() and GetEventsOnBackgroundThread).
|
| + Database::MetricInfoVector GetStats(MetricType type) {
|
| + content::BrowserThread::GetBlockingPool()->FlushForTesting();
|
| + content::RunAllPendingInMessageLoop();
|
| +
|
| + Database::MetricInfoVector metrics;
|
| + content::BrowserThread::PostBlockingPoolSequencedTask(
|
| + Database::kDatabaseSequenceToken,
|
| + FROM_HERE,
|
| + base::Bind(&PerformanceMonitorBrowserTest::GetStatsOnBackgroundThread,
|
| + base::Unretained(this),
|
| + &metrics,
|
| + type));
|
| +
|
| + content::BrowserThread::GetBlockingPool()->FlushForTesting();
|
| + return metrics;
|
| + }
|
| +
|
| // A handle for inserting a state value into the database, which must be done
|
| // on the background thread. This is useful for mocking up a scenario in which
|
| // the database has prior data stored. We mock synchronicity with
|
| @@ -462,6 +502,51 @@ IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, NewVersionEvent) {
|
| ASSERT_EQ(version_string, current_version);
|
| }
|
|
|
| +IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, GatherStatistics) {
|
| + GatherStatistics();
|
| +
|
| + // Gather CPU usage. No stats should be recorded because this was the first
|
| + // call to GatherStatistics.
|
| + Database::MetricInfoVector stats = GetStats(METRIC_CPU_USAGE);
|
| + ASSERT_EQ(0u, stats.size());
|
| +
|
| + // Gather private memory usage.
|
| + stats = GetStats(METRIC_PRIVATE_MEMORY_USAGE);
|
| + ASSERT_EQ(1u, stats.size());
|
| + EXPECT_GT(stats[0].value, 0);
|
| +
|
| + // Gather shared memory usage.
|
| + stats = GetStats(METRIC_SHARED_MEMORY_USAGE);
|
| + ASSERT_EQ(1u, stats.size());
|
| + EXPECT_GT(stats[0].value, 0);
|
| +
|
| + // Spin for a while, so CPU usage isn't 0.
|
| + const int kSpinCount = 1000000000;
|
| + int i = 0;
|
| + for (; i < kSpinCount; ++i) {
|
| + }
|
| + ASSERT_EQ(kSpinCount, i);
|
| +
|
| + GatherStatistics();
|
| +
|
| + // Gather CPU usage a second time and verify a stat was recorded.
|
| + stats = GetStats(METRIC_CPU_USAGE);
|
| + ASSERT_EQ(1u, stats.size());
|
| + EXPECT_GT(stats[0].value, 0);
|
| +
|
| + // Gather private memory usage a second time and verify a second stat was
|
| + // recorded.
|
| + stats = GetStats(METRIC_PRIVATE_MEMORY_USAGE);
|
| + ASSERT_EQ(2u, stats.size());
|
| + EXPECT_GT(stats[1].value, 0);
|
| +
|
| + // Gather shared memory usage a second time and verify a second stat was
|
| + // recorded.
|
| + stats = GetStats(METRIC_SHARED_MEMORY_USAGE);
|
| + ASSERT_EQ(2u, stats.size());
|
| + EXPECT_GT(stats[1].value, 0);
|
| +}
|
| +
|
| #if !defined(OS_WIN)
|
| // Disabled on Windows due to a bug where Windows will return a normal exit
|
| // code in the testing environment, even if the process died (this is not the
|
|
|