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

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: Fixed Windows bug, duplicate CPU usage gather, couple nits 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"
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
36 // Used in PerformanceMonitorBrowserTest.GatherStatistics to consume CPU cycles.
Devlin 2012/07/27 22:09:47 Since it's only used in the one test, it can go at
mitchellwrosen 2012/07/30 21:50:40 Done.
37 int kSpinCount = 1000000000;
38
34 // Helper struct to store the information of an extension; this is needed if the 39 // 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 40 // pointer to the extension ever becomes invalid (e.g., if we uninstall the
36 // extension). 41 // extension).
37 struct ExtensionBasicInfo { 42 struct ExtensionBasicInfo {
38 // Empty constructor for stl-container-happiness. 43 // Empty constructor for stl-container-happiness.
39 ExtensionBasicInfo() { 44 ExtensionBasicInfo() {
40 } 45 }
41 explicit ExtensionBasicInfo(const Extension* extension) 46 explicit ExtensionBasicInfo(const Extension* extension)
42 : description(extension->description()), 47 : description(extension->description()),
43 id(extension->id()), 48 id(extension->id()),
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 Database::kDatabaseSequenceToken, 170 Database::kDatabaseSequenceToken,
166 FROM_HERE, 171 FROM_HERE,
167 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, 172 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread,
168 base::Unretained(this), 173 base::Unretained(this),
169 &events)); 174 &events));
170 175
171 content::BrowserThread::GetBlockingPool()->FlushForTesting(); 176 content::BrowserThread::GetBlockingPool()->FlushForTesting();
172 return events; 177 return events;
173 } 178 }
174 179
180 void GetStatsOnBackgroundThread(Database::MetricInfoVector* metrics,
Devlin 2012/07/27 22:09:47 Not sure if you noticed, but while doing the merge
mitchellwrosen 2012/07/30 21:50:40 Looks good. Thanks.
181 MetricType type) {
182 *metrics = performance_monitor_->database()->GetStatsForActivityAndMetric(
183 type, base::Time(), base::Time::FromInternalValue(kint64max));
184 }
185
186 // A handle for getting statistics from the database (see previous comments on
187 // GetEvents() and GetEventsOnBackgroundThread).
188 Database::MetricInfoVector GetStats(MetricType type) {
189 content::BrowserThread::GetBlockingPool()->FlushForTesting();
190 ui_test_utils::RunAllPendingInMessageLoop();
191
192 Database::MetricInfoVector metrics;
193 content::BrowserThread::PostBlockingPoolSequencedTask(
194 Database::kDatabaseSequenceToken,
195 FROM_HERE,
196 base::Bind(&PerformanceMonitorBrowserTest::GetStatsOnBackgroundThread,
197 base::Unretained(this),
198 &metrics,
199 type));
200
201 content::BrowserThread::GetBlockingPool()->FlushForTesting();
202 return metrics;
203 }
204
175 // A handle for inserting a state value into the database, which must be done 205 // 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 206 // 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 207 // the database has prior data stored. We mock synchronicity with
178 // FlushForTesting(). 208 // FlushForTesting().
179 void AddStateValue(const std::string& key, const std::string& value) { 209 void AddStateValue(const std::string& key, const std::string& value) {
180 content::BrowserThread::PostBlockingPoolSequencedTask( 210 content::BrowserThread::PostBlockingPoolSequencedTask(
181 Database::kDatabaseSequenceToken, 211 Database::kDatabaseSequenceToken,
182 FROM_HERE, 212 FROM_HERE,
183 base::Bind(base::IgnoreResult(&Database::AddStateValue), 213 base::Bind(base::IgnoreResult(&Database::AddStateValue),
184 base::Unretained(performance_monitor()->database()), 214 base::Unretained(performance_monitor()->database()),
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 419
390 std::string previous_version; 420 std::string previous_version;
391 std::string current_version; 421 std::string current_version;
392 422
393 ASSERT_TRUE(value->GetString("previousVersion", &previous_version)); 423 ASSERT_TRUE(value->GetString("previousVersion", &previous_version));
394 ASSERT_EQ(kOldVersion, previous_version); 424 ASSERT_EQ(kOldVersion, previous_version);
395 ASSERT_TRUE(value->GetString("currentVersion", &current_version)); 425 ASSERT_TRUE(value->GetString("currentVersion", &current_version));
396 ASSERT_EQ(version_string, current_version); 426 ASSERT_EQ(version_string, current_version);
397 } 427 }
398 428
429 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, GatherStatistics) {
430 content::BrowserThread::PostBlockingPoolSequencedTask(
431 Database::kDatabaseSequenceToken,
432 FROM_HERE,
433 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread,
434 base::Unretained(performance_monitor())));
435
436 content::BrowserThread::GetBlockingPool()->FlushForTesting();
437
438 // Gather CPU usage. No stats should be recorded because this was the first
439 // call to GatherStatistics.
440 Database::MetricInfoVector stats = GetStats(METRIC_CPU_USAGE);
441 ASSERT_EQ(0u, stats.size());
442
443 // Gather private memory usage.
444 stats = GetStats(METRIC_PRIVATE_MEMORY_USAGE);
445 ASSERT_EQ(1u, stats.size());
446 EXPECT_GT(stats[0].value, 0);
447
448 // Gather shared memory usage.
449 stats = GetStats(METRIC_SHARED_MEMORY_USAGE);
450 ASSERT_EQ(1u, stats.size());
451 EXPECT_GT(stats[0].value, 0);
452
453 // TODO(mwrosen) something less barbaric?
454 // Spin for a while, so CPU usage isn't 0.
455 int i = 0;
456 for (; i < kSpinCount; ++i) {
457 }
458 ASSERT_EQ(kSpinCount, i);
459
460 content::BrowserThread::PostBlockingPoolSequencedTask(
461 Database::kDatabaseSequenceToken,
462 FROM_HERE,
463 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread,
464 base::Unretained(performance_monitor())));
465
466 content::BrowserThread::GetBlockingPool()->FlushForTesting();
467
468 // Gather CPU usage a second time and verify a stat was recorded.
469 stats = GetStats(METRIC_CPU_USAGE);
470 ASSERT_EQ(1u, stats.size());
471 EXPECT_GT(stats[0].value, 0);
472
473 // Gather private memory usage a second time and verify a second stat was
474 // recorded.
475 stats = GetStats(METRIC_PRIVATE_MEMORY_USAGE);
476 ASSERT_EQ(2u, stats.size());
477 EXPECT_GT(stats[1].value, 0);
478
479 // Gather shared memory usage a second time and verify a second stat was
480 // recorded.
481 stats = GetStats(METRIC_SHARED_MEMORY_USAGE);
482 ASSERT_EQ(2u, stats.size());
483 EXPECT_GT(stats[1].value, 0);
484 }
485
399 #if !defined(OS_WIN) 486 #if !defined(OS_WIN)
400 // Disabled on Windows due to a bug where Windows will return a normal exit 487 // 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 488 // 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 489 // case when hand-testing). This code can be traced to MSDN functions in
403 // base::GetTerminationStatus(), so there's not much we can do. 490 // base::GetTerminationStatus(), so there's not much we can do.
404 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) { 491 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) {
405 content::CrashTab(chrome::GetActiveWebContents(browser())); 492 content::CrashTab(chrome::GetActiveWebContents(browser()));
406 493
407 std::vector<linked_ptr<Event> > events = GetEvents(); 494 std::vector<linked_ptr<Event> > events = GetEvents();
408 495
409 ASSERT_EQ(1u, events.size()); 496 ASSERT_EQ(1u, events.size());
410 CheckEventType(EVENT_KILLED_BY_OS_CRASH, events[0]); 497 CheckEventType(EVENT_KILLED_BY_OS_CRASH, events[0]);
411 } 498 }
412 #endif // !defined(OS_WIN) 499 #endif // !defined(OS_WIN)
413 500
414 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, 501 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest,
415 DISABLED_RendererCrashEvent) { 502 DISABLED_RendererCrashEvent) {
416 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL)); 503 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL));
417 504
418 std::vector<linked_ptr<Event> > events = GetEvents(); 505 std::vector<linked_ptr<Event> > events = GetEvents();
419 ASSERT_EQ(1u, events.size()); 506 ASSERT_EQ(1u, events.size());
420 507
421 CheckEventType(EVENT_RENDERER_CRASH, events[0]); 508 CheckEventType(EVENT_RENDERER_CRASH, events[0]);
422 } 509 }
423 510
424 } // namespace performance_monitor 511 } // namespace performance_monitor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698