| 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_prefetchings_.find(url) == inflight_prefetchings_.end() && |
| 513 IsUrlPrefetchable(url)) { |
| 514 inflight_prefetchings_.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 std::map<GURL, base::TimeTicks>::iterator prefetching_it = |
| 536 inflight_prefetchings_.find(url); |
| 537 if (prefetching_it != inflight_prefetchings_.end()) { |
| 538 UMA_HISTOGRAM_TIMES( |
| 539 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, |
| 540 base::TimeTicks::Now() - prefetching_it->second); |
| 541 inflight_prefetchings_.erase(prefetching_it); |
| 542 } |
| 527 if (!prefetch_manager_.get()) // Not enabled. | 543 if (!prefetch_manager_.get()) // Not enabled. |
| 528 return; | 544 return; |
| 529 | 545 |
| 530 BrowserThread::PostTask( | 546 BrowserThread::PostTask( |
| 531 BrowserThread::IO, FROM_HERE, | 547 BrowserThread::IO, FROM_HERE, |
| 532 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, | 548 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, |
| 533 prefetch_manager_, url)); | 549 prefetch_manager_, url)); |
| 534 } | 550 } |
| 535 | 551 |
| 536 void ResourcePrefetchPredictor::OnPrefetchingFinished( | 552 void ResourcePrefetchPredictor::OnPrefetchingFinished( |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 base::TimeTicks time_now = base::TimeTicks::Now(); | 778 base::TimeTicks time_now = base::TimeTicks::Now(); |
| 763 for (NavigationMap::iterator it = inflight_navigations_.begin(); | 779 for (NavigationMap::iterator it = inflight_navigations_.begin(); |
| 764 it != inflight_navigations_.end();) { | 780 it != inflight_navigations_.end();) { |
| 765 if ((it->first.tab_id == navigation_id.tab_id) || | 781 if ((it->first.tab_id == navigation_id.tab_id) || |
| 766 (time_now - it->first.creation_time > max_navigation_age)) { | 782 (time_now - it->first.creation_time > max_navigation_age)) { |
| 767 inflight_navigations_.erase(it++); | 783 inflight_navigations_.erase(it++); |
| 768 } else { | 784 } else { |
| 769 ++it; | 785 ++it; |
| 770 } | 786 } |
| 771 } | 787 } |
| 788 |
| 789 for (std::map<GURL, base::TimeTicks>::iterator it = |
| 790 inflight_prefetchings_.begin(); |
| 791 it != inflight_prefetchings_.end();) { |
| 792 if (time_now - it->second > max_navigation_age) |
| 793 inflight_prefetchings_.erase(it++); |
| 794 else |
| 795 ++it; |
| 796 } |
| 772 } | 797 } |
| 773 | 798 |
| 774 void ResourcePrefetchPredictor::DeleteAllUrls() { | 799 void ResourcePrefetchPredictor::DeleteAllUrls() { |
| 775 inflight_navigations_.clear(); | 800 inflight_navigations_.clear(); |
| 776 url_table_cache_->clear(); | 801 url_table_cache_->clear(); |
| 777 host_table_cache_->clear(); | 802 host_table_cache_->clear(); |
| 778 url_redirect_table_cache_->clear(); | 803 url_redirect_table_cache_->clear(); |
| 779 host_redirect_table_cache_->clear(); | 804 host_redirect_table_cache_->clear(); |
| 780 | 805 |
| 781 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 806 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1241 TestObserver::~TestObserver() { | 1266 TestObserver::~TestObserver() { |
| 1242 predictor_->SetObserverForTesting(nullptr); | 1267 predictor_->SetObserverForTesting(nullptr); |
| 1243 } | 1268 } |
| 1244 | 1269 |
| 1245 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1270 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
| 1246 : predictor_(predictor) { | 1271 : predictor_(predictor) { |
| 1247 predictor_->SetObserverForTesting(this); | 1272 predictor_->SetObserverForTesting(this); |
| 1248 } | 1273 } |
| 1249 | 1274 |
| 1250 } // namespace predictors | 1275 } // namespace predictors |
| OLD | NEW |