| 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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 size_t precision_percentage = | 143 size_t precision_percentage = |
| 144 (100 * correctly_predicted_count) / predicted_urls_set.size(); | 144 (100 * correctly_predicted_count) / predicted_urls_set.size(); |
| 145 size_t recall_percentage = | 145 size_t recall_percentage = |
| 146 (100 * correctly_predicted_count) / actual_urls_set.size(); | 146 (100 * correctly_predicted_count) / actual_urls_set.size(); |
| 147 | 147 |
| 148 UMA_HISTOGRAM_PERCENTAGE( | 148 UMA_HISTOGRAM_PERCENTAGE( |
| 149 internal::kResourcePrefetchPredictorPrecisionHistogram, | 149 internal::kResourcePrefetchPredictorPrecisionHistogram, |
| 150 precision_percentage); | 150 precision_percentage); |
| 151 UMA_HISTOGRAM_PERCENTAGE(internal::kResourcePrefetchPredictorRecallHistogram, | 151 UMA_HISTOGRAM_PERCENTAGE(internal::kResourcePrefetchPredictorRecallHistogram, |
| 152 recall_percentage); | 152 recall_percentage); |
| 153 UMA_HISTOGRAM_COUNTS_100(internal::kResourcePrefetchPredictorCountHistogram, |
| 154 predicted_urls.size()); |
| 153 } | 155 } |
| 154 | 156 |
| 155 } // namespace | 157 } // namespace |
| 156 | 158 |
| 157 //////////////////////////////////////////////////////////////////////////////// | 159 //////////////////////////////////////////////////////////////////////////////// |
| 158 // ResourcePrefetchPredictor static functions. | 160 // ResourcePrefetchPredictor static functions. |
| 159 | 161 |
| 160 // static | 162 // static |
| 161 bool ResourcePrefetchPredictor::ShouldRecordRequest( | 163 bool ResourcePrefetchPredictor::ShouldRecordRequest( |
| 162 net::URLRequest* request, | 164 net::URLRequest* request, |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 default: | 501 default: |
| 500 NOTREACHED() << "Unexpected initialization_state_: " | 502 NOTREACHED() << "Unexpected initialization_state_: " |
| 501 << initialization_state_; | 503 << initialization_state_; |
| 502 } | 504 } |
| 503 } | 505 } |
| 504 | 506 |
| 505 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url, | 507 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url, |
| 506 PrefetchOrigin origin) { | 508 PrefetchOrigin origin) { |
| 507 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StartPrefetching", "url", | 509 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StartPrefetching", "url", |
| 508 url.spec()); | 510 url.spec()); |
| 511 // Save prefetch start time to report prefetching duration. |
| 512 if (inflight_prefetches_.find(url) == inflight_prefetches_.end() && |
| 513 IsUrlPrefetchable(url)) { |
| 514 inflight_prefetches_.insert(std::make_pair(url, base::TimeTicks::Now())); |
| 515 } |
| 516 |
| 509 if (!prefetch_manager_.get()) // Prefetching not enabled. | 517 if (!prefetch_manager_.get()) // Prefetching not enabled. |
| 510 return; | 518 return; |
| 511 if (!config_.IsPrefetchingEnabledForOrigin(profile_, origin)) | 519 if (!config_.IsPrefetchingEnabledForOrigin(profile_, origin)) |
| 512 return; | 520 return; |
| 513 | 521 |
| 514 std::vector<GURL> subresource_urls; | 522 std::vector<GURL> subresource_urls; |
| 515 if (!GetPrefetchData(url, &subresource_urls)) | 523 if (!GetPrefetchData(url, &subresource_urls)) |
| 516 return; | 524 return; |
| 517 | 525 |
| 518 BrowserThread::PostTask( | 526 BrowserThread::PostTask( |
| 519 BrowserThread::IO, FROM_HERE, | 527 BrowserThread::IO, FROM_HERE, |
| 520 base::Bind(&ResourcePrefetcherManager::MaybeAddPrefetch, | 528 base::Bind(&ResourcePrefetcherManager::MaybeAddPrefetch, |
| 521 prefetch_manager_, url, subresource_urls)); | 529 prefetch_manager_, url, subresource_urls)); |
| 522 } | 530 } |
| 523 | 531 |
| 524 void ResourcePrefetchPredictor::StopPrefetching(const GURL& url) { | 532 void ResourcePrefetchPredictor::StopPrefetching(const GURL& url) { |
| 525 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StopPrefetching", "url", | 533 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StopPrefetching", "url", |
| 526 url.spec()); | 534 url.spec()); |
| 535 auto it = inflight_prefetches_.find(url); |
| 536 if (it != inflight_prefetches_.end()) { |
| 537 UMA_HISTOGRAM_TIMES( |
| 538 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, |
| 539 base::TimeTicks::Now() - it->second); |
| 540 inflight_prefetches_.erase(it); |
| 541 } |
| 527 if (!prefetch_manager_.get()) // Not enabled. | 542 if (!prefetch_manager_.get()) // Not enabled. |
| 528 return; | 543 return; |
| 529 | 544 |
| 530 BrowserThread::PostTask( | 545 BrowserThread::PostTask( |
| 531 BrowserThread::IO, FROM_HERE, | 546 BrowserThread::IO, FROM_HERE, |
| 532 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, | 547 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, |
| 533 prefetch_manager_, url)); | 548 prefetch_manager_, url)); |
| 534 } | 549 } |
| 535 | 550 |
| 536 void ResourcePrefetchPredictor::OnPrefetchingFinished( | 551 void ResourcePrefetchPredictor::OnPrefetchingFinished( |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 base::TimeTicks time_now = base::TimeTicks::Now(); | 777 base::TimeTicks time_now = base::TimeTicks::Now(); |
| 763 for (NavigationMap::iterator it = inflight_navigations_.begin(); | 778 for (NavigationMap::iterator it = inflight_navigations_.begin(); |
| 764 it != inflight_navigations_.end();) { | 779 it != inflight_navigations_.end();) { |
| 765 if ((it->first.tab_id == navigation_id.tab_id) || | 780 if ((it->first.tab_id == navigation_id.tab_id) || |
| 766 (time_now - it->first.creation_time > max_navigation_age)) { | 781 (time_now - it->first.creation_time > max_navigation_age)) { |
| 767 inflight_navigations_.erase(it++); | 782 inflight_navigations_.erase(it++); |
| 768 } else { | 783 } else { |
| 769 ++it; | 784 ++it; |
| 770 } | 785 } |
| 771 } | 786 } |
| 787 |
| 788 for (auto it = inflight_prefetches_.begin(); |
| 789 it != inflight_prefetches_.end();) { |
| 790 if (time_now - it->second > max_navigation_age) |
| 791 it = inflight_prefetches_.erase(it); |
| 792 else |
| 793 ++it; |
| 794 } |
| 772 } | 795 } |
| 773 | 796 |
| 774 void ResourcePrefetchPredictor::DeleteAllUrls() { | 797 void ResourcePrefetchPredictor::DeleteAllUrls() { |
| 775 inflight_navigations_.clear(); | 798 inflight_navigations_.clear(); |
| 776 url_table_cache_->clear(); | 799 url_table_cache_->clear(); |
| 777 host_table_cache_->clear(); | 800 host_table_cache_->clear(); |
| 778 url_redirect_table_cache_->clear(); | 801 url_redirect_table_cache_->clear(); |
| 779 host_redirect_table_cache_->clear(); | 802 host_redirect_table_cache_->clear(); |
| 780 | 803 |
| 781 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 804 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1241 TestObserver::~TestObserver() { | 1264 TestObserver::~TestObserver() { |
| 1242 predictor_->SetObserverForTesting(nullptr); | 1265 predictor_->SetObserverForTesting(nullptr); |
| 1243 } | 1266 } |
| 1244 | 1267 |
| 1245 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1268 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
| 1246 : predictor_(predictor) { | 1269 : predictor_(predictor) { |
| 1247 predictor_->SetObserverForTesting(this); | 1270 predictor_->SetObserverForTesting(this); |
| 1248 } | 1271 } |
| 1249 | 1272 |
| 1250 } // namespace predictors | 1273 } // namespace predictors |
| OLD | NEW |