OLD | NEW |
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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 | 154 |
155 // PerformanceMonitor's initialization process involves a significant | 155 // PerformanceMonitor's initialization process involves a significant |
156 // amount of thread-hopping between the UI thread and the background thread. | 156 // amount of thread-hopping between the UI thread and the background thread. |
157 // If we begin the tests prior to full initialization, we cannot predict | 157 // If we begin the tests prior to full initialization, we cannot predict |
158 // the behavior or mock synchronicity as we must. Wait for initialization | 158 // the behavior or mock synchronicity as we must. Wait for initialization |
159 // to complete fully before proceeding with the test. | 159 // to complete fully before proceeding with the test. |
160 content::WindowedNotificationObserver windowed_observer( | 160 content::WindowedNotificationObserver windowed_observer( |
161 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, | 161 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, |
162 content::NotificationService::AllSources()); | 162 content::NotificationService::AllSources()); |
163 | 163 |
| 164 // We stop the timer in charge of doing timed collections so that we can |
| 165 // enforce when, and how many times, we do these collections. |
| 166 performance_monitor_->disable_timer_autostart_for_testing_ = true; |
| 167 // Force metrics to be stored, regardless of switches used. |
| 168 performance_monitor_->database_logging_enabled_ = true; |
164 performance_monitor_->Start(); | 169 performance_monitor_->Start(); |
165 | 170 |
166 windowed_observer.Wait(); | 171 windowed_observer.Wait(); |
167 | |
168 // We stop the timer in charge of doing timed collections so that we can | |
169 // enforce when, and how many times, we do these collections. | |
170 performance_monitor_->timer_.Stop(); | |
171 } | 172 } |
172 | 173 |
173 // A handle for gathering statistics from the database, which must be done on | 174 // A handle for gathering statistics from the database, which must be done on |
174 // the background thread. Since we are testing, we can mock synchronicity with | 175 // the background thread. Since we are testing, we can mock synchronicity with |
175 // FlushForTesting(). | 176 // FlushForTesting(). |
176 void GatherStatistics() { | 177 void GatherStatistics() { |
177 content::BrowserThread::PostBlockingPoolSequencedTask( | 178 performance_monitor_->next_collection_time_ = base::Time::Now(); |
178 Database::kDatabaseSequenceToken, | 179 performance_monitor_->GatherMetricsMapOnUIThread(); |
179 FROM_HERE, | |
180 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread, | |
181 base::Unretained(performance_monitor()))); | |
182 | 180 |
| 181 RunAllPendingInMessageLoop(content::BrowserThread::IO); |
183 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 182 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
184 } | 183 } |
185 | 184 |
186 void GetEventsOnBackgroundThread(Database::EventVector* events) { | 185 void GetEventsOnBackgroundThread(Database::EventVector* events) { |
187 // base::Time is potentially flaky in that there is no guarantee that it | 186 // base::Time is potentially flaky in that there is no guarantee that it |
188 // won't actually decrease between successive calls. If we call GetEvents | 187 // won't actually decrease between successive calls. If we call GetEvents |
189 // and the Database uses base::Time::Now() and gets a lesser time, then it | 188 // and the Database uses base::Time::Now() and gets a lesser time, then it |
190 // will return 0 events. Thus, we use a time that is guaranteed to be in the | 189 // will return 0 events. Thus, we use a time that is guaranteed to be in the |
191 // future (for at least the next couple hundred thousand years). | 190 // future (for at least the next couple hundred thousand years). |
192 *events = performance_monitor_->database()->GetEvents( | 191 *events = performance_monitor_->database()->GetEvents( |
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
765 int64 page2_size = 0; | 764 int64 page2_size = 0; |
766 ASSERT_TRUE(file_util::GetFileSize(test_dir.AppendASCII("title2.html"), | 765 ASSERT_TRUE(file_util::GetFileSize(test_dir.AppendASCII("title2.html"), |
767 &page2_size)); | 766 &page2_size)); |
768 | 767 |
769 ASSERT_TRUE(test_server()->Start()); | 768 ASSERT_TRUE(test_server()->Start()); |
770 | 769 |
771 ui_test_utils::NavigateToURL( | 770 ui_test_utils::NavigateToURL( |
772 browser(), | 771 browser(), |
773 test_server()->GetURL(std::string("files/").append("title1.html"))); | 772 test_server()->GetURL(std::string("files/").append("title1.html"))); |
774 | 773 |
775 performance_monitor()->DoTimedCollections(); | 774 GatherStatistics(); |
776 | |
777 // Since network bytes are read and set on the IO thread, we must flush this | |
778 // additional thread to be sure that all messages are run. | |
779 RunAllPendingInMessageLoop(content::BrowserThread::IO); | |
780 | 775 |
781 Database::MetricVector metrics = GetStats(METRIC_NETWORK_BYTES_READ); | 776 Database::MetricVector metrics = GetStats(METRIC_NETWORK_BYTES_READ); |
782 ASSERT_EQ(1u, metrics.size()); | 777 ASSERT_EQ(1u, metrics.size()); |
783 // Since these pages are read over the "network" (actually the test_server), | 778 // Since these pages are read over the "network" (actually the test_server), |
784 // some extraneous information is carried along, and the best check we can do | 779 // some extraneous information is carried along, and the best check we can do |
785 // is for greater than or equal to. | 780 // is for greater than or equal to. |
786 EXPECT_GE(metrics[0].value, page1_size); | 781 EXPECT_GE(metrics[0].value, page1_size); |
787 | 782 |
788 ui_test_utils::NavigateToURL( | 783 ui_test_utils::NavigateToURL( |
789 browser(), | 784 browser(), |
790 test_server()->GetURL(std::string("files/").append("title2.html"))); | 785 test_server()->GetURL(std::string("files/").append("title2.html"))); |
791 | 786 |
792 performance_monitor()->DoTimedCollections(); | 787 GatherStatistics(); |
793 | 788 |
794 metrics = GetStats(METRIC_NETWORK_BYTES_READ); | 789 metrics = GetStats(METRIC_NETWORK_BYTES_READ); |
795 ASSERT_EQ(2u, metrics.size()); | 790 ASSERT_EQ(2u, metrics.size()); |
796 EXPECT_GE(metrics[1].value, page1_size + page2_size); | 791 EXPECT_GE(metrics[1].value, page1_size + page2_size); |
797 } | 792 } |
798 | 793 |
799 } // namespace performance_monitor | 794 } // namespace performance_monitor |
OLD | NEW |