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

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor.cc

Issue 2688633002: predictors: Add prefetching hit/miss histograms. (Closed)
Patch Set: Remove duplicated duplicated comment comment. Created 3 years, 10 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/browser/predictors/resource_prefetch_predictor.h" 5 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
6 6
7 #include <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 visit_count_ = url_row.visit_count(); 113 visit_count_ = url_row.visit_count();
114 return true; 114 return true;
115 } 115 }
116 116
117 void GetUrlVisitCountTask::DoneRunOnMainThread() { 117 void GetUrlVisitCountTask::DoneRunOnMainThread() {
118 callback_.Run(visit_count_, *summary_); 118 callback_.Run(visit_count_, *summary_);
119 } 119 }
120 120
121 GetUrlVisitCountTask::~GetUrlVisitCountTask() {} 121 GetUrlVisitCountTask::~GetUrlVisitCountTask() {}
122 122
123 void ReportPrefetchAccuracy(
124 const ResourcePrefetcher::PrefetcherStats& stats,
125 const std::vector<ResourcePrefetchPredictor::URLRequestSummary>&
126 summaries) {
127 if (stats.requests_stats.empty())
128 return;
129
130 std::set<GURL> urls;
131 for (const auto& summary : summaries)
132 urls.insert(summary.resource_url);
133
134 int cached_misses_count = 0;
135 int not_cached_misses_count = 0;
136 int cached_hits_count = 0;
137 int not_cached_hits_count = 0;
138 int64_t misses_bytes = 0;
139 int64_t hits_bytes = 0;
140
141 for (const auto& request_stats : stats.requests_stats) {
142 bool hit = urls.find(request_stats.resource_url) != urls.end();
143 bool cached = request_stats.was_cached;
144 size_t bytes = request_stats.total_received_bytes;
145
146 cached_hits_count += cached && hit;
147 cached_misses_count += cached && !hit;
148 not_cached_hits_count += !cached && hit;
149 not_cached_misses_count += !cached && !hit;
150 misses_bytes += !hit * bytes;
151 hits_bytes += hit * bytes;
152 }
153
154 UMA_HISTOGRAM_COUNTS_100(
155 internal::kResourcePrefetchPredictorPrefetchMissesCountCached,
156 cached_misses_count);
157 UMA_HISTOGRAM_COUNTS_100(
158 internal::kResourcePrefetchPredictorPrefetchMissesCountNotCached,
159 not_cached_misses_count);
160 UMA_HISTOGRAM_COUNTS_100(
161 internal::kResourcePrefetchPredictorPrefetchHitsCountCached,
162 cached_hits_count);
163 UMA_HISTOGRAM_COUNTS_100(
164 internal::kResourcePrefetchPredictorPrefetchHitsCountNotCached,
165 not_cached_hits_count);
166 UMA_HISTOGRAM_COUNTS_10000(
167 internal::kResourcePrefetchPredictorPrefetchHitsSize, hits_bytes / 1024);
168 UMA_HISTOGRAM_COUNTS_10000(
169 internal::kResourcePrefetchPredictorPrefetchMissesSize,
170 misses_bytes / 1024);
171 }
172
123 void ReportPredictionAccuracy( 173 void ReportPredictionAccuracy(
124 const std::vector<GURL>& predicted_urls, 174 const std::vector<GURL>& predicted_urls,
125 const ResourcePrefetchPredictor::PageRequestSummary& summary) { 175 const ResourcePrefetchPredictor::PageRequestSummary& summary) {
126 DCHECK(!predicted_urls.empty()); 176 DCHECK(!predicted_urls.empty());
127 177
128 if (predicted_urls.empty() || summary.subresource_requests.empty()) 178 if (predicted_urls.empty() || summary.subresource_requests.empty())
129 return; 179 return;
130 180
131 std::set<GURL> predicted_urls_set(predicted_urls.begin(), 181 std::set<GURL> predicted_urls_set(predicted_urls.begin(),
132 predicted_urls.end()); 182 predicted_urls.end());
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 if (!prefetch_manager_.get()) // Not enabled. 592 if (!prefetch_manager_.get()) // Not enabled.
543 return; 593 return;
544 594
545 BrowserThread::PostTask( 595 BrowserThread::PostTask(
546 BrowserThread::IO, FROM_HERE, 596 BrowserThread::IO, FROM_HERE,
547 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, 597 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch,
548 prefetch_manager_, url)); 598 prefetch_manager_, url));
549 } 599 }
550 600
551 void ResourcePrefetchPredictor::OnPrefetchingFinished( 601 void ResourcePrefetchPredictor::OnPrefetchingFinished(
552 const GURL& main_frame_url) { 602 const GURL& main_frame_url,
603 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats) {
553 if (observer_) 604 if (observer_)
554 observer_->OnPrefetchingFinished(main_frame_url); 605 observer_->OnPrefetchingFinished(main_frame_url);
606
607 prefetcher_stats_.insert(std::make_pair(main_frame_url, std::move(stats)));
555 } 608 }
556 609
557 bool ResourcePrefetchPredictor::IsUrlPrefetchable(const GURL& main_frame_url) { 610 bool ResourcePrefetchPredictor::IsUrlPrefetchable(const GURL& main_frame_url) {
558 DCHECK_CURRENTLY_ON(BrowserThread::UI); 611 DCHECK_CURRENTLY_ON(BrowserThread::UI);
559 if (initialization_state_ != INITIALIZED) 612 if (initialization_state_ != INITIALIZED)
560 return false; 613 return false;
561 614
562 return GetPrefetchData(main_frame_url, nullptr); 615 return GetPrefetchData(main_frame_url, nullptr);
563 } 616 }
564 617
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 703
651 NavigationMap::iterator nav_it = 704 NavigationMap::iterator nav_it =
652 inflight_navigations_.find(nav_id_without_timing_info); 705 inflight_navigations_.find(nav_id_without_timing_info);
653 if (nav_it == inflight_navigations_.end()) 706 if (nav_it == inflight_navigations_.end())
654 return; 707 return;
655 708
656 // Remove the navigation from the inflight navigations. 709 // Remove the navigation from the inflight navigations.
657 std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second); 710 std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second);
658 inflight_navigations_.erase(nav_it); 711 inflight_navigations_.erase(nav_it);
659 712
713 const GURL& main_frame_url = nav_id_without_timing_info.main_frame_url;
660 std::vector<GURL> predicted_urls; 714 std::vector<GURL> predicted_urls;
661 bool has_data = GetPrefetchData(nav_id_without_timing_info.main_frame_url, 715 bool has_data = GetPrefetchData(main_frame_url, &predicted_urls);
662 &predicted_urls);
663 if (has_data) 716 if (has_data)
664 ReportPredictionAccuracy(predicted_urls, *summary); 717 ReportPredictionAccuracy(predicted_urls, *summary);
665 718
719 auto it = prefetcher_stats_.find(main_frame_url);
720 if (it != prefetcher_stats_.end()) {
721 const std::vector<URLRequestSummary>& summaries =
722 summary->subresource_requests;
723 ReportPrefetchAccuracy(*it->second, summaries);
724 prefetcher_stats_.erase(it);
725 }
726
666 // Kick off history lookup to determine if we should record the URL. 727 // Kick off history lookup to determine if we should record the URL.
667 history::HistoryService* history_service = 728 history::HistoryService* history_service =
668 HistoryServiceFactory::GetForProfile(profile_, 729 HistoryServiceFactory::GetForProfile(profile_,
669 ServiceAccessType::EXPLICIT_ACCESS); 730 ServiceAccessType::EXPLICIT_ACCESS);
670 DCHECK(history_service); 731 DCHECK(history_service);
671 history_service->ScheduleDBTask( 732 history_service->ScheduleDBTask(
672 std::unique_ptr<history::HistoryDBTask>(new GetUrlVisitCountTask( 733 std::unique_ptr<history::HistoryDBTask>(new GetUrlVisitCountTask(
673 std::move(summary), 734 std::move(summary),
674 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup, 735 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup,
675 AsWeakPtr()))), 736 AsWeakPtr()))),
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 } 846 }
786 } 847 }
787 848
788 for (auto it = inflight_prefetches_.begin(); 849 for (auto it = inflight_prefetches_.begin();
789 it != inflight_prefetches_.end();) { 850 it != inflight_prefetches_.end();) {
790 if (time_now - it->second > max_navigation_age) 851 if (time_now - it->second > max_navigation_age)
791 it = inflight_prefetches_.erase(it); 852 it = inflight_prefetches_.erase(it);
792 else 853 else
793 ++it; 854 ++it;
794 } 855 }
856
857 // Remove old prefetches that haven't been claimed.
858 for (auto stats_it = prefetcher_stats_.begin();
859 stats_it != prefetcher_stats_.end();) {
860 if (time_now - stats_it->second->start_time > max_navigation_age) {
861 // No requests -> everything is a miss.
862 ReportPrefetchAccuracy(*stats_it->second,
863 std::vector<URLRequestSummary>());
864 stats_it = prefetcher_stats_.erase(stats_it);
865 } else {
866 ++stats_it;
867 }
868 }
795 } 869 }
796 870
797 void ResourcePrefetchPredictor::DeleteAllUrls() { 871 void ResourcePrefetchPredictor::DeleteAllUrls() {
798 inflight_navigations_.clear(); 872 inflight_navigations_.clear();
799 url_table_cache_->clear(); 873 url_table_cache_->clear();
800 host_table_cache_->clear(); 874 host_table_cache_->clear();
801 url_redirect_table_cache_->clear(); 875 url_redirect_table_cache_->clear();
802 host_redirect_table_cache_->clear(); 876 host_redirect_table_cache_->clear();
803 877
804 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 878 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after
1264 TestObserver::~TestObserver() { 1338 TestObserver::~TestObserver() {
1265 predictor_->SetObserverForTesting(nullptr); 1339 predictor_->SetObserverForTesting(nullptr);
1266 } 1340 }
1267 1341
1268 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) 1342 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor)
1269 : predictor_(predictor) { 1343 : predictor_(predictor) {
1270 predictor_->SetObserverForTesting(this); 1344 predictor_->SetObserverForTesting(this);
1271 } 1345 }
1272 1346
1273 } // namespace predictors 1347 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698