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

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, 5 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 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
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
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", &current_version)); 427 ASSERT_TRUE(value->GetString("currentVersion", &current_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 // CPU Usage -- no stats recorded the first time
Devlin 2012/07/23 21:10:33 Comments should be grammatically correct (need per
mitchellwrosen 2012/07/23 23:58:51 Done.
441 Database::MetricInfoVector stats = GetStatsForActivityAndMetric(
442 performance_monitor::kProcessChromeAggregate,
443 performance_monitor::kMetricCPUUsage);
444 ASSERT_EQ(0u, stats.size());
445
446 // Private memory usage
447 stats = GetStatsForActivityAndMetric(
448 performance_monitor::kProcessChromeAggregate,
449 performance_monitor::kMetricPrivateMemoryUsage);
450 ASSERT_EQ(1u, stats.size());
451 EXPECT_GT(stats[0].value, 0);
452
453 // Shared memory usage
454 stats = GetStatsForActivityAndMetric(
455 performance_monitor::kProcessChromeAggregate,
456 performance_monitor::kMetricSharedMemoryUsage);
457 ASSERT_EQ(1u, stats.size());
458 EXPECT_GT(stats[0].value, 0);
459
460 // TODO(mwrosen) something less barbaric?
461 // Spin for a while, so CPU usage isn't 0.
462 int i = 0;
463 for (; i < kSpinCount; ++i) {
464 }
465 ASSERT_EQ(kSpinCount, i);
466
467 content::BrowserThread::PostBlockingPoolSequencedTask(
468 Database::kDatabaseSequenceToken,
469 FROM_HERE,
470 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread,
471 base::Unretained(performance_monitor())));
472
473 content::BrowserThread::GetBlockingPool()->FlushForTesting();
474
475 stats = GetStatsForActivityAndMetric(
476 performance_monitor::kProcessChromeAggregate,
477 performance_monitor::kMetricCPUUsage);
478 ASSERT_EQ(1u, stats.size());
479 EXPECT_GT(stats[0].value, 0);
480
481 // Private memory usage #2
482 stats = GetStatsForActivityAndMetric(
483 performance_monitor::kProcessChromeAggregate,
484 performance_monitor::kMetricPrivateMemoryUsage);
485 ASSERT_EQ(2u, stats.size());
486 EXPECT_GT(stats[1].value, 0);
487
488 // Shared memory usage #2
489 stats = GetStatsForActivityAndMetric(
490 performance_monitor::kProcessChromeAggregate,
491 performance_monitor::kMetricSharedMemoryUsage);
492 ASSERT_EQ(2u, stats.size());
493 EXPECT_GT(stats[1].value, 0);
494 }
495
398 #if !defined(OS_WIN) 496 #if !defined(OS_WIN)
399 // Disabled on Windows due to a bug where Windows will return a normal exit 497 // 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 498 // 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 499 // case when hand-testing). This code can be traced to MSDN functions in
402 // base::GetTerminationStatus(), so there's not much we can do. 500 // base::GetTerminationStatus(), so there's not much we can do.
403 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) { 501 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) {
404 ui_test_utils::CrashTab(chrome::GetActiveWebContents(browser())); 502 ui_test_utils::CrashTab(chrome::GetActiveWebContents(browser()));
405 503
406 std::vector<linked_ptr<Event> > events = GetEvents(); 504 std::vector<linked_ptr<Event> > events = GetEvents();
407 505
408 ASSERT_EQ(1u, events.size()); 506 ASSERT_EQ(1u, events.size());
409 CheckEventType(EVENT_KILLED_BY_OS_CRASH, events[0]); 507 CheckEventType(EVENT_KILLED_BY_OS_CRASH, events[0]);
410 } 508 }
411 #endif // !defined(OS_WIN) 509 #endif // !defined(OS_WIN)
412 510
413 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, 511 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest,
414 DISABLED_RendererCrashEvent) { 512 DISABLED_RendererCrashEvent) {
415 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL)); 513 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL));
416 514
417 std::vector<linked_ptr<Event> > events = GetEvents(); 515 std::vector<linked_ptr<Event> > events = GetEvents();
418 ASSERT_EQ(1u, events.size()); 516 ASSERT_EQ(1u, events.size());
419 517
420 CheckEventType(EVENT_RENDERER_CRASH, events[0]); 518 CheckEventType(EVENT_RENDERER_CRASH, events[0]);
421 } 519 }
422 520
423 } // namespace performance_monitor 521 } // namespace performance_monitor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698