| OLD | NEW |
| 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 Loading... |
| 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 ReportPredictionAccuracy( |
| 124 const std::vector<GURL>& predicted_urls, |
| 125 const ResourcePrefetchPredictor::PageRequestSummary& summary) { |
| 126 DCHECK(!predicted_urls.empty()); |
| 127 |
| 128 if (predicted_urls.empty() || summary.subresource_requests.empty()) |
| 129 return; |
| 130 |
| 131 std::set<GURL> predicted_urls_set(predicted_urls.begin(), |
| 132 predicted_urls.end()); |
| 133 std::set<GURL> actual_urls_set; |
| 134 for (const auto& request_summary : summary.subresource_requests) |
| 135 actual_urls_set.insert(request_summary.resource_url); |
| 136 |
| 137 size_t correctly_predicted_count = 0; |
| 138 for (const GURL& predicted_url : predicted_urls_set) { |
| 139 if (actual_urls_set.find(predicted_url) != actual_urls_set.end()) |
| 140 correctly_predicted_count++; |
| 141 } |
| 142 |
| 143 size_t precision_percentage = |
| 144 (100 * correctly_predicted_count) / predicted_urls_set.size(); |
| 145 size_t recall_percentage = |
| 146 (100 * correctly_predicted_count) / actual_urls_set.size(); |
| 147 |
| 148 UMA_HISTOGRAM_PERCENTAGE( |
| 149 internal::kResourcePrefetchPredictorPrecisionHistogram, |
| 150 precision_percentage); |
| 151 UMA_HISTOGRAM_PERCENTAGE(internal::kResourcePrefetchPredictorRecallHistogram, |
| 152 recall_percentage); |
| 153 } |
| 154 |
| 123 } // namespace | 155 } // namespace |
| 124 | 156 |
| 125 //////////////////////////////////////////////////////////////////////////////// | 157 //////////////////////////////////////////////////////////////////////////////// |
| 126 // ResourcePrefetchPredictor static functions. | 158 // ResourcePrefetchPredictor static functions. |
| 127 | 159 |
| 128 // static | 160 // static |
| 129 bool ResourcePrefetchPredictor::ShouldRecordRequest( | 161 bool ResourcePrefetchPredictor::ShouldRecordRequest( |
| 130 net::URLRequest* request, | 162 net::URLRequest* request, |
| 131 content::ResourceType resource_type) { | 163 content::ResourceType resource_type) { |
| 132 const content::ResourceRequestInfo* request_info = | 164 const content::ResourceRequestInfo* request_info = |
| (...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 595 | 627 |
| 596 NavigationMap::iterator nav_it = | 628 NavigationMap::iterator nav_it = |
| 597 inflight_navigations_.find(nav_id_without_timing_info); | 629 inflight_navigations_.find(nav_id_without_timing_info); |
| 598 if (nav_it == inflight_navigations_.end()) | 630 if (nav_it == inflight_navigations_.end()) |
| 599 return; | 631 return; |
| 600 | 632 |
| 601 // Remove the navigation from the inflight navigations. | 633 // Remove the navigation from the inflight navigations. |
| 602 std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second); | 634 std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second); |
| 603 inflight_navigations_.erase(nav_it); | 635 inflight_navigations_.erase(nav_it); |
| 604 | 636 |
| 637 std::vector<GURL> predicted_urls; |
| 638 bool has_data = GetPrefetchData(nav_id_without_timing_info.main_frame_url, |
| 639 &predicted_urls); |
| 640 if (has_data) |
| 641 ReportPredictionAccuracy(predicted_urls, *summary); |
| 642 |
| 605 // Kick off history lookup to determine if we should record the URL. | 643 // Kick off history lookup to determine if we should record the URL. |
| 606 history::HistoryService* history_service = | 644 history::HistoryService* history_service = |
| 607 HistoryServiceFactory::GetForProfile(profile_, | 645 HistoryServiceFactory::GetForProfile(profile_, |
| 608 ServiceAccessType::EXPLICIT_ACCESS); | 646 ServiceAccessType::EXPLICIT_ACCESS); |
| 609 DCHECK(history_service); | 647 DCHECK(history_service); |
| 610 history_service->ScheduleDBTask( | 648 history_service->ScheduleDBTask( |
| 611 std::unique_ptr<history::HistoryDBTask>(new GetUrlVisitCountTask( | 649 std::unique_ptr<history::HistoryDBTask>(new GetUrlVisitCountTask( |
| 612 std::move(summary), | 650 std::move(summary), |
| 613 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup, | 651 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup, |
| 614 AsWeakPtr()))), | 652 AsWeakPtr()))), |
| (...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1212 TestObserver::~TestObserver() { | 1250 TestObserver::~TestObserver() { |
| 1213 predictor_->SetObserverForTesting(nullptr); | 1251 predictor_->SetObserverForTesting(nullptr); |
| 1214 } | 1252 } |
| 1215 | 1253 |
| 1216 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1254 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
| 1217 : predictor_(predictor) { | 1255 : predictor_(predictor) { |
| 1218 predictor_->SetObserverForTesting(this); | 1256 predictor_->SetObserverForTesting(this); |
| 1219 } | 1257 } |
| 1220 | 1258 |
| 1221 } // namespace predictors | 1259 } // namespace predictors |
| OLD | NEW |