Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: chrome/browser/performance_monitor/performance_monitor_browsertest.cc

Issue 10656052: Performance monitor stats gathering. (Closed) Base URL: http://git.chromium.org/chromium/src.git@cpm_main
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
Devlin 2012/07/31 17:05:13 Sort.
mitchellwrosen 2012/07/31 17:15:36 Done.
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 #include "content/public/test/browser_test_utils.h" 29 #include "content/public/test/browser_test_utils.h"
29 30
30 using extensions::Extension; 31 using extensions::Extension;
31 using performance_monitor::Event; 32 using performance_monitor::Event;
32 33
33 namespace { 34 namespace {
35
34 // Helper struct to store the information of an extension; this is needed if the 36 // Helper struct to store the information of an extension; this is needed if the
35 // pointer to the extension ever becomes invalid (e.g., if we uninstall the 37 // pointer to the extension ever becomes invalid (e.g., if we uninstall the
36 // extension). 38 // extension).
37 struct ExtensionBasicInfo { 39 struct ExtensionBasicInfo {
38 // Empty constructor for stl-container-happiness. 40 // Empty constructor for stl-container-happiness.
39 ExtensionBasicInfo() { 41 ExtensionBasicInfo() {
40 } 42 }
41 explicit ExtensionBasicInfo(const Extension* extension) 43 explicit ExtensionBasicInfo(const Extension* extension)
42 : description(extension->description()), 44 : description(extension->description()),
43 id(extension->id()), 45 id(extension->id()),
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 // to complete fully before proceeding with the test. 136 // to complete fully before proceeding with the test.
135 content::WindowedNotificationObserver windowed_observer( 137 content::WindowedNotificationObserver windowed_observer(
136 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, 138 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED,
137 content::NotificationService::AllSources()); 139 content::NotificationService::AllSources());
138 140
139 performance_monitor_->Start(); 141 performance_monitor_->Start();
140 142
141 windowed_observer.Wait(); 143 windowed_observer.Wait();
142 } 144 }
143 145
146 void GatherStatistics() {
Devlin 2012/07/31 17:05:13 Document.
mitchellwrosen 2012/07/31 17:15:36 Done.
147 content::BrowserThread::PostBlockingPoolSequencedTask(
148 Database::kDatabaseSequenceToken,
149 FROM_HERE,
150 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread,
151 base::Unretained(performance_monitor())));
152
153 content::BrowserThread::GetBlockingPool()->FlushForTesting();
154 }
155
144 void GetEventsOnBackgroundThread(std::vector<linked_ptr<Event> >* events) { 156 void GetEventsOnBackgroundThread(std::vector<linked_ptr<Event> >* events) {
145 // base::Time is potentially flaky in that there is no guarantee that it 157 // base::Time is potentially flaky in that there is no guarantee that it
146 // won't actually decrease between successive calls. If we call GetEvents 158 // won't actually decrease between successive calls. If we call GetEvents
147 // and the Database uses base::Time::Now() and gets a lesser time, then it 159 // and the Database uses base::Time::Now() and gets a lesser time, then it
148 // will return 0 events. Thus, we use a time that is guaranteed to be in the 160 // will return 0 events. Thus, we use a time that is guaranteed to be in the
149 // future (for at least the next couple hundred thousand years). 161 // future (for at least the next couple hundred thousand years).
150 *events = performance_monitor_->database()->GetEvents( 162 *events = performance_monitor_->database()->GetEvents(
151 base::Time(), base::Time::FromInternalValue(kint64max)); 163 base::Time(), base::Time::FromInternalValue(kint64max));
152 } 164 }
153 165
(...skipping 11 matching lines...) Expand all
165 Database::kDatabaseSequenceToken, 177 Database::kDatabaseSequenceToken,
166 FROM_HERE, 178 FROM_HERE,
167 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, 179 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread,
168 base::Unretained(this), 180 base::Unretained(this),
169 &events)); 181 &events));
170 182
171 content::BrowserThread::GetBlockingPool()->FlushForTesting(); 183 content::BrowserThread::GetBlockingPool()->FlushForTesting();
172 return events; 184 return events;
173 } 185 }
174 186
187 void GetStatsOnBackgroundThread(Database::MetricInfoVector* metrics,
188 MetricType type) {
189 *metrics = performance_monitor_->database()->GetStatsForActivityAndMetric(
190 type, base::Time(), base::Time::FromInternalValue(kint64max));
191 }
192
193 // A handle for getting statistics from the database (see previous comments on
194 // GetEvents() and GetEventsOnBackgroundThread).
195 Database::MetricInfoVector GetStats(MetricType type) {
196 content::BrowserThread::GetBlockingPool()->FlushForTesting();
197 ui_test_utils::RunAllPendingInMessageLoop();
198
199 Database::MetricInfoVector metrics;
200 content::BrowserThread::PostBlockingPoolSequencedTask(
201 Database::kDatabaseSequenceToken,
202 FROM_HERE,
203 base::Bind(&PerformanceMonitorBrowserTest::GetStatsOnBackgroundThread,
204 base::Unretained(this),
205 &metrics,
206 type));
207
208 content::BrowserThread::GetBlockingPool()->FlushForTesting();
209 return metrics;
210 }
211
175 // A handle for inserting a state value into the database, which must be done 212 // A handle for inserting a state value into the database, which must be done
176 // on the background thread. This is useful for mocking up a scenario in which 213 // on the background thread. This is useful for mocking up a scenario in which
177 // the database has prior data stored. We mock synchronicity with 214 // the database has prior data stored. We mock synchronicity with
178 // FlushForTesting(). 215 // FlushForTesting().
179 void AddStateValue(const std::string& key, const std::string& value) { 216 void AddStateValue(const std::string& key, const std::string& value) {
180 content::BrowserThread::PostBlockingPoolSequencedTask( 217 content::BrowserThread::PostBlockingPoolSequencedTask(
181 Database::kDatabaseSequenceToken, 218 Database::kDatabaseSequenceToken,
182 FROM_HERE, 219 FROM_HERE,
183 base::Bind(base::IgnoreResult(&Database::AddStateValue), 220 base::Bind(base::IgnoreResult(&Database::AddStateValue),
184 base::Unretained(performance_monitor()->database()), 221 base::Unretained(performance_monitor()->database()),
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 426
390 std::string previous_version; 427 std::string previous_version;
391 std::string current_version; 428 std::string current_version;
392 429
393 ASSERT_TRUE(value->GetString("previousVersion", &previous_version)); 430 ASSERT_TRUE(value->GetString("previousVersion", &previous_version));
394 ASSERT_EQ(kOldVersion, previous_version); 431 ASSERT_EQ(kOldVersion, previous_version);
395 ASSERT_TRUE(value->GetString("currentVersion", &current_version)); 432 ASSERT_TRUE(value->GetString("currentVersion", &current_version));
396 ASSERT_EQ(version_string, current_version); 433 ASSERT_EQ(version_string, current_version);
397 } 434 }
398 435
436 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, GatherStatistics) {
437 const int kSpinCount = 1000000000;
Devlin 2012/07/31 17:05:13 This one's my fault - you're supposed to put this
mitchellwrosen 2012/07/31 17:15:36 Done.
438
439 GatherStatistics();
440
441 // Gather CPU usage. No stats should be recorded because this was the first
442 // call to GatherStatistics.
443 Database::MetricInfoVector stats = GetStats(METRIC_CPU_USAGE);
444 ASSERT_EQ(0u, stats.size());
445
446 // Gather private memory usage.
447 stats = GetStats(METRIC_PRIVATE_MEMORY_USAGE);
448 ASSERT_EQ(1u, stats.size());
449 EXPECT_GT(stats[0].value, 0);
450
451 // Gather shared memory usage.
452 stats = GetStats(METRIC_SHARED_MEMORY_USAGE);
453 ASSERT_EQ(1u, stats.size());
454 EXPECT_GT(stats[0].value, 0);
455
456 // Spin for a while, so CPU usage isn't 0.
457 int i = 0;
458 for (; i < kSpinCount; ++i) {
459 }
460 ASSERT_EQ(kSpinCount, i);
461
462 GatherStatistics();
463
464 // Gather CPU usage a second time and verify a stat was recorded.
465 stats = GetStats(METRIC_CPU_USAGE);
466 ASSERT_EQ(1u, stats.size());
467 EXPECT_GT(stats[0].value, 0);
468
469 // Gather private memory usage a second time and verify a second stat was
470 // recorded.
471 stats = GetStats(METRIC_PRIVATE_MEMORY_USAGE);
472 ASSERT_EQ(2u, stats.size());
473 EXPECT_GT(stats[1].value, 0);
474
475 // Gather shared memory usage a second time and verify a second stat was
476 // recorded.
477 stats = GetStats(METRIC_SHARED_MEMORY_USAGE);
478 ASSERT_EQ(2u, stats.size());
479 EXPECT_GT(stats[1].value, 0);
480 }
481
399 #if !defined(OS_WIN) 482 #if !defined(OS_WIN)
400 // Disabled on Windows due to a bug where Windows will return a normal exit 483 // Disabled on Windows due to a bug where Windows will return a normal exit
401 // code in the testing environment, even if the process died (this is not the 484 // code in the testing environment, even if the process died (this is not the
402 // case when hand-testing). This code can be traced to MSDN functions in 485 // case when hand-testing). This code can be traced to MSDN functions in
403 // base::GetTerminationStatus(), so there's not much we can do. 486 // base::GetTerminationStatus(), so there's not much we can do.
404 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) { 487 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) {
405 content::CrashTab(chrome::GetActiveWebContents(browser())); 488 content::CrashTab(chrome::GetActiveWebContents(browser()));
406 489
407 std::vector<linked_ptr<Event> > events = GetEvents(); 490 std::vector<linked_ptr<Event> > events = GetEvents();
408 491
409 ASSERT_EQ(1u, events.size()); 492 ASSERT_EQ(1u, events.size());
410 CheckEventType(EVENT_KILLED_BY_OS_CRASH, events[0]); 493 CheckEventType(EVENT_KILLED_BY_OS_CRASH, events[0]);
411 } 494 }
412 #endif // !defined(OS_WIN) 495 #endif // !defined(OS_WIN)
413 496
414 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, 497 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest,
415 DISABLED_RendererCrashEvent) { 498 DISABLED_RendererCrashEvent) {
416 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL)); 499 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL));
417 500
418 std::vector<linked_ptr<Event> > events = GetEvents(); 501 std::vector<linked_ptr<Event> > events = GetEvents();
419 ASSERT_EQ(1u, events.size()); 502 ASSERT_EQ(1u, events.size());
420 503
421 CheckEventType(EVENT_RENDERER_CRASH, events[0]); 504 CheckEventType(EVENT_RENDERER_CRASH, events[0]);
422 } 505 }
423 506
424 } // namespace performance_monitor 507 } // namespace performance_monitor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698