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

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

Issue 10827151: Revert 149766 - Performance monitor stats gathering. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/performance_monitor/performance_monitor.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/command_line.h" 7 #include "base/command_line.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/string_number_conversions.h" 11 #include "base/string_number_conversions.h"
12 #include "base/threading/sequenced_worker_pool.h" 12 #include "base/threading/sequenced_worker_pool.h"
13 #include "chrome/browser/browser_process.h" 13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/performance_monitor/constants.h" 14 #include "chrome/browser/performance_monitor/constants.h"
15 #include "chrome/browser/performance_monitor/database.h" 15 #include "chrome/browser/performance_monitor/database.h"
16 #include "chrome/browser/performance_monitor/performance_monitor.h" 16 #include "chrome/browser/performance_monitor/performance_monitor.h"
17 #include "chrome/browser/extensions/extension_browsertest.h" 17 #include "chrome/browser/extensions/extension_browsertest.h"
18 #include "chrome/browser/extensions/extension_service.h" 18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/unpacked_installer.h" 19 #include "chrome/browser/extensions/unpacked_installer.h"
20 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_manager.h" 21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/browser/ui/browser.h" 22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_tabstrip.h" 23 #include "chrome/browser/ui/browser_tabstrip.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h"
25 #include "chrome/common/chrome_constants.h" 24 #include "chrome/common/chrome_constants.h"
26 #include "chrome/common/chrome_notification_types.h" 25 #include "chrome/common/chrome_notification_types.h"
27 #include "chrome/common/chrome_paths.h" 26 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/chrome_switches.h" 27 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/chrome_version_info.h" 28 #include "chrome/common/chrome_version_info.h"
30 #include "chrome/common/extensions/extension.h" 29 #include "chrome/common/extensions/extension.h"
31 #include "chrome/common/url_constants.h" 30 #include "chrome/common/url_constants.h"
32 #include "chrome/test/base/ui_test_utils.h" 31 #include "chrome/test/base/ui_test_utils.h"
33 #include "content/public/browser/notification_registrar.h" 32 #include "content/public/browser/notification_registrar.h"
34 #include "content/public/browser/notification_service.h" 33 #include "content/public/browser/notification_service.h"
35 #include "content/public/test/browser_test_utils.h" 34 #include "content/public/test/browser_test_utils.h"
36 #include "content/public/test/test_utils.h" 35 #include "content/public/test/test_utils.h"
37 36
38 using extensions::Extension; 37 using extensions::Extension;
39 using performance_monitor::Event; 38 using performance_monitor::Event;
40 39
41 namespace { 40 namespace {
42
43 // Helper struct to store the information of an extension; this is needed if the 41 // Helper struct to store the information of an extension; this is needed if the
44 // pointer to the extension ever becomes invalid (e.g., if we uninstall the 42 // pointer to the extension ever becomes invalid (e.g., if we uninstall the
45 // extension). 43 // extension).
46 struct ExtensionBasicInfo { 44 struct ExtensionBasicInfo {
47 // Empty constructor for stl-container-happiness. 45 // Empty constructor for stl-container-happiness.
48 ExtensionBasicInfo() { 46 ExtensionBasicInfo() {
49 } 47 }
50 explicit ExtensionBasicInfo(const Extension* extension) 48 explicit ExtensionBasicInfo(const Extension* extension)
51 : description(extension->description()), 49 : description(extension->description()),
52 id(extension->id()), 50 id(extension->id()),
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 // to complete fully before proceeding with the test. 141 // to complete fully before proceeding with the test.
144 content::WindowedNotificationObserver windowed_observer( 142 content::WindowedNotificationObserver windowed_observer(
145 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, 143 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED,
146 content::NotificationService::AllSources()); 144 content::NotificationService::AllSources());
147 145
148 performance_monitor_->Start(); 146 performance_monitor_->Start();
149 147
150 windowed_observer.Wait(); 148 windowed_observer.Wait();
151 } 149 }
152 150
153 // A handle for gathering statistics from the database, which must be done on
154 // the background thread. Since we are testing, we can mock synchronicity with
155 // FlushForTesting().
156 void GatherStatistics() {
157 content::BrowserThread::PostBlockingPoolSequencedTask(
158 Database::kDatabaseSequenceToken,
159 FROM_HERE,
160 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread,
161 base::Unretained(performance_monitor())));
162
163 content::BrowserThread::GetBlockingPool()->FlushForTesting();
164 }
165
166 void GetEventsOnBackgroundThread(std::vector<linked_ptr<Event> >* events) { 151 void GetEventsOnBackgroundThread(std::vector<linked_ptr<Event> >* events) {
167 // base::Time is potentially flaky in that there is no guarantee that it 152 // base::Time is potentially flaky in that there is no guarantee that it
168 // won't actually decrease between successive calls. If we call GetEvents 153 // won't actually decrease between successive calls. If we call GetEvents
169 // and the Database uses base::Time::Now() and gets a lesser time, then it 154 // and the Database uses base::Time::Now() and gets a lesser time, then it
170 // will return 0 events. Thus, we use a time that is guaranteed to be in the 155 // will return 0 events. Thus, we use a time that is guaranteed to be in the
171 // future (for at least the next couple hundred thousand years). 156 // future (for at least the next couple hundred thousand years).
172 *events = performance_monitor_->database()->GetEvents( 157 *events = performance_monitor_->database()->GetEvents(
173 base::Time(), base::Time::FromInternalValue(kint64max)); 158 base::Time(), base::Time::FromInternalValue(kint64max));
174 } 159 }
175 160
(...skipping 11 matching lines...) Expand all
187 Database::kDatabaseSequenceToken, 172 Database::kDatabaseSequenceToken,
188 FROM_HERE, 173 FROM_HERE,
189 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, 174 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread,
190 base::Unretained(this), 175 base::Unretained(this),
191 &events)); 176 &events));
192 177
193 content::BrowserThread::GetBlockingPool()->FlushForTesting(); 178 content::BrowserThread::GetBlockingPool()->FlushForTesting();
194 return events; 179 return events;
195 } 180 }
196 181
197 void GetStatsOnBackgroundThread(Database::MetricInfoVector* metrics,
198 MetricType type) {
199 *metrics = performance_monitor_->database()->GetStatsForActivityAndMetric(
200 type, base::Time(), base::Time::FromInternalValue(kint64max));
201 }
202
203 // A handle for getting statistics from the database (see previous comments on
204 // GetEvents() and GetEventsOnBackgroundThread).
205 Database::MetricInfoVector GetStats(MetricType type) {
206 content::BrowserThread::GetBlockingPool()->FlushForTesting();
207 content::RunAllPendingInMessageLoop();
208
209 Database::MetricInfoVector metrics;
210 content::BrowserThread::PostBlockingPoolSequencedTask(
211 Database::kDatabaseSequenceToken,
212 FROM_HERE,
213 base::Bind(&PerformanceMonitorBrowserTest::GetStatsOnBackgroundThread,
214 base::Unretained(this),
215 &metrics,
216 type));
217
218 content::BrowserThread::GetBlockingPool()->FlushForTesting();
219 return metrics;
220 }
221
222 // A handle for inserting a state value into the database, which must be done 182 // A handle for inserting a state value into the database, which must be done
223 // on the background thread. This is useful for mocking up a scenario in which 183 // on the background thread. This is useful for mocking up a scenario in which
224 // the database has prior data stored. We mock synchronicity with 184 // the database has prior data stored. We mock synchronicity with
225 // FlushForTesting(). 185 // FlushForTesting().
226 void AddStateValue(const std::string& key, const std::string& value) { 186 void AddStateValue(const std::string& key, const std::string& value) {
227 content::BrowserThread::PostBlockingPoolSequencedTask( 187 content::BrowserThread::PostBlockingPoolSequencedTask(
228 Database::kDatabaseSequenceToken, 188 Database::kDatabaseSequenceToken,
229 FROM_HERE, 189 FROM_HERE,
230 base::Bind(base::IgnoreResult(&Database::AddStateValue), 190 base::Bind(base::IgnoreResult(&Database::AddStateValue),
231 base::Unretained(performance_monitor()->database()), 191 base::Unretained(performance_monitor()->database()),
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 455
496 std::string previous_version; 456 std::string previous_version;
497 std::string current_version; 457 std::string current_version;
498 458
499 ASSERT_TRUE(value->GetString("previousVersion", &previous_version)); 459 ASSERT_TRUE(value->GetString("previousVersion", &previous_version));
500 ASSERT_EQ(kOldVersion, previous_version); 460 ASSERT_EQ(kOldVersion, previous_version);
501 ASSERT_TRUE(value->GetString("currentVersion", &current_version)); 461 ASSERT_TRUE(value->GetString("currentVersion", &current_version));
502 ASSERT_EQ(version_string, current_version); 462 ASSERT_EQ(version_string, current_version);
503 } 463 }
504 464
505 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, GatherStatistics) {
506 GatherStatistics();
507
508 // Gather CPU usage. No stats should be recorded because this was the first
509 // call to GatherStatistics.
510 Database::MetricInfoVector stats = GetStats(METRIC_CPU_USAGE);
511 ASSERT_EQ(0u, stats.size());
512
513 // Gather private memory usage.
514 stats = GetStats(METRIC_PRIVATE_MEMORY_USAGE);
515 ASSERT_EQ(1u, stats.size());
516 EXPECT_GT(stats[0].value, 0);
517
518 // Gather shared memory usage.
519 stats = GetStats(METRIC_SHARED_MEMORY_USAGE);
520 ASSERT_EQ(1u, stats.size());
521 EXPECT_GT(stats[0].value, 0);
522
523 // Spin for a while, so CPU usage isn't 0.
524 const int kSpinCount = 1000000000;
525 int i = 0;
526 for (; i < kSpinCount; ++i) {
527 }
528 ASSERT_EQ(kSpinCount, i);
529
530 GatherStatistics();
531
532 // Gather CPU usage a second time and verify a stat was recorded.
533 stats = GetStats(METRIC_CPU_USAGE);
534 ASSERT_EQ(1u, stats.size());
535 EXPECT_GT(stats[0].value, 0);
mitchellwrosen 2012/08/07 17:26:32 http://build.chromium.org/p/chromium.chromiumos/bu
Yoyo Zhou 2012/08/07 19:09:50 Your assessment looks right: http://chromium-buil
536
537 // Gather private memory usage a second time and verify a second stat was
538 // recorded.
539 stats = GetStats(METRIC_PRIVATE_MEMORY_USAGE);
540 ASSERT_EQ(2u, stats.size());
541 EXPECT_GT(stats[1].value, 0);
542
543 // Gather shared memory usage a second time and verify a second stat was
544 // recorded.
545 stats = GetStats(METRIC_SHARED_MEMORY_USAGE);
546 ASSERT_EQ(2u, stats.size());
547 EXPECT_GT(stats[1].value, 0);
548 }
549
550 #if !defined(OS_WIN) 465 #if !defined(OS_WIN)
551 // Disabled on Windows due to a bug where Windows will return a normal exit 466 // Disabled on Windows due to a bug where Windows will return a normal exit
552 // code in the testing environment, even if the process died (this is not the 467 // code in the testing environment, even if the process died (this is not the
553 // case when hand-testing). This code can be traced to MSDN functions in 468 // case when hand-testing). This code can be traced to MSDN functions in
554 // base::GetTerminationStatus(), so there's not much we can do. 469 // base::GetTerminationStatus(), so there's not much we can do.
555 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) { 470 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) {
556 content::CrashTab(chrome::GetActiveWebContents(browser())); 471 content::CrashTab(chrome::GetActiveWebContents(browser()));
557 472
558 std::vector<linked_ptr<Event> > events = GetEvents(); 473 std::vector<linked_ptr<Event> > events = GetEvents();
559 474
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 544
630 std::string event_profile; 545 std::string event_profile;
631 ASSERT_TRUE(events[0]->data()->GetString("profileName", &event_profile)); 546 ASSERT_TRUE(events[0]->data()->GetString("profileName", &event_profile));
632 ASSERT_EQ(first_profile_name_, event_profile); 547 ASSERT_EQ(first_profile_name_, event_profile);
633 548
634 ASSERT_TRUE(events[1]->data()->GetString("profileName", &event_profile)); 549 ASSERT_TRUE(events[1]->data()->GetString("profileName", &event_profile));
635 ASSERT_EQ(second_profile_name_, event_profile); 550 ASSERT_EQ(second_profile_name_, event_profile);
636 } 551 }
637 552
638 } // namespace performance_monitor 553 } // namespace performance_monitor
OLDNEW
« no previous file with comments | « chrome/browser/performance_monitor/performance_monitor.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698