| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/loading_test_util.h" | 5 #include "chrome/browser/predictors/loading_test_util.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> |
| 9 | 10 |
| 10 #include "content/public/browser/resource_request_info.h" | 11 #include "content/public/browser/resource_request_info.h" |
| 11 #include "net/http/http_response_headers.h" | 12 #include "net/http/http_response_headers.h" |
| 12 #include "net/url_request/url_request_test_util.h" | 13 #include "net/url_request/url_request_test_util.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { | 17 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { |
| 17 void OnResponseStarted(net::URLRequest* request, int net_error) override {} | 18 void OnResponseStarted(net::URLRequest* request, int net_error) override {} |
| 18 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} | 19 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} |
| 19 }; | 20 }; |
| 20 | 21 |
| 21 EmptyURLRequestDelegate g_empty_url_request_delegate; | 22 EmptyURLRequestDelegate g_empty_url_request_delegate; |
| 22 | 23 |
| 23 bool AlmostEqual(const double x, const double y) { | 24 bool AlmostEqual(const double x, const double y) { |
| 24 return std::fabs(x - y) <= 1e-6; // Arbitrary but close enough. | 25 return std::fabs(x - y) <= 1e-6; // Arbitrary but close enough. |
| 25 } | 26 } |
| 26 | 27 |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 namespace predictors { | 30 namespace predictors { |
| 30 | 31 |
| 31 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | |
| 32 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | |
| 33 using Prediction = ResourcePrefetchPredictor::Prediction; | 32 using Prediction = ResourcePrefetchPredictor::Prediction; |
| 34 | 33 |
| 35 MockResourcePrefetchPredictor::MockResourcePrefetchPredictor( | 34 MockResourcePrefetchPredictor::MockResourcePrefetchPredictor( |
| 36 const LoadingPredictorConfig& config, | 35 const LoadingPredictorConfig& config, |
| 37 Profile* profile) | 36 Profile* profile) |
| 38 : ResourcePrefetchPredictor(config, profile) {} | 37 : ResourcePrefetchPredictor(config, profile) {} |
| 39 | 38 |
| 40 MockResourcePrefetchPredictor::~MockResourcePrefetchPredictor() = default; | 39 MockResourcePrefetchPredictor::~MockResourcePrefetchPredictor() = default; |
| 41 | 40 |
| 42 void InitializeResourceData(ResourceData* resource, | 41 void InitializeResourceData(ResourceData* resource, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 } | 122 } |
| 124 | 123 |
| 125 PageRequestSummary CreatePageRequestSummary( | 124 PageRequestSummary CreatePageRequestSummary( |
| 126 const std::string& main_frame_url, | 125 const std::string& main_frame_url, |
| 127 const std::string& initial_url, | 126 const std::string& initial_url, |
| 128 const std::vector<URLRequestSummary>& subresource_requests) { | 127 const std::vector<URLRequestSummary>& subresource_requests) { |
| 129 GURL main_frame_gurl(main_frame_url); | 128 GURL main_frame_gurl(main_frame_url); |
| 130 PageRequestSummary summary(main_frame_gurl); | 129 PageRequestSummary summary(main_frame_gurl); |
| 131 summary.initial_url = GURL(initial_url); | 130 summary.initial_url = GURL(initial_url); |
| 132 summary.subresource_requests = subresource_requests; | 131 summary.subresource_requests = subresource_requests; |
| 132 for (auto& request_summary : subresource_requests) |
| 133 summary.UpdateOrAddToOrigins(request_summary); |
| 133 return summary; | 134 return summary; |
| 134 } | 135 } |
| 135 | 136 |
| 136 URLRequestSummary CreateURLRequestSummary(SessionID::id_type tab_id, | 137 URLRequestSummary CreateURLRequestSummary(SessionID::id_type tab_id, |
| 137 const std::string& main_frame_url, | 138 const std::string& main_frame_url, |
| 138 const std::string& resource_url, | 139 const std::string& resource_url, |
| 139 content::ResourceType resource_type, | 140 content::ResourceType resource_type, |
| 140 net::RequestPriority priority, | 141 net::RequestPriority priority, |
| 141 const std::string& mime_type, | 142 const std::string& mime_type, |
| 142 bool was_cached, | 143 bool was_cached, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 155 summary.was_cached = was_cached; | 156 summary.was_cached = was_cached; |
| 156 if (!redirect_url.empty()) | 157 if (!redirect_url.empty()) |
| 157 summary.redirect_url = GURL(redirect_url); | 158 summary.redirect_url = GURL(redirect_url); |
| 158 summary.has_validators = has_validators; | 159 summary.has_validators = has_validators; |
| 159 summary.always_revalidate = always_revalidate; | 160 summary.always_revalidate = always_revalidate; |
| 160 summary.is_no_store = false; | 161 summary.is_no_store = false; |
| 161 summary.network_accessed = true; | 162 summary.network_accessed = true; |
| 162 return summary; | 163 return summary; |
| 163 } | 164 } |
| 164 | 165 |
| 166 URLRequestSummary CreateRedirectRequestSummary( |
| 167 SessionID::id_type session_id, |
| 168 const std::string& main_frame_url, |
| 169 const std::string& redirect_url) { |
| 170 URLRequestSummary summary = |
| 171 CreateURLRequestSummary(session_id, main_frame_url); |
| 172 summary.redirect_url = GURL(redirect_url); |
| 173 return summary; |
| 174 } |
| 175 |
| 165 ResourcePrefetchPredictor::Prediction CreatePrediction( | 176 ResourcePrefetchPredictor::Prediction CreatePrediction( |
| 166 const std::string& main_frame_key, | 177 const std::string& main_frame_key, |
| 167 std::vector<GURL> subresource_urls) { | 178 std::vector<GURL> subresource_urls) { |
| 168 Prediction prediction; | 179 Prediction prediction; |
| 169 prediction.main_frame_key = main_frame_key; | 180 prediction.main_frame_key = main_frame_key; |
| 170 prediction.subresource_urls = subresource_urls; | 181 prediction.subresource_urls = subresource_urls; |
| 171 prediction.is_host = true; | 182 prediction.is_host = true; |
| 172 prediction.is_redirected = false; | 183 prediction.is_redirected = false; |
| 173 return prediction; | 184 return prediction; |
| 174 } | 185 } |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 } | 334 } |
| 324 | 335 |
| 325 std::ostream& operator<<(std::ostream& os, const OriginStat& origin) { | 336 std::ostream& operator<<(std::ostream& os, const OriginStat& origin) { |
| 326 return os << "[" << origin.origin() << "," << origin.number_of_hits() << "," | 337 return os << "[" << origin.origin() << "," << origin.number_of_hits() << "," |
| 327 << origin.number_of_misses() << "," << origin.consecutive_misses() | 338 << origin.number_of_misses() << "," << origin.consecutive_misses() |
| 328 << "," << origin.average_position() << "," | 339 << "," << origin.average_position() << "," |
| 329 << origin.always_access_network() << "," | 340 << origin.always_access_network() << "," |
| 330 << origin.accessed_network() << "]"; | 341 << origin.accessed_network() << "]"; |
| 331 } | 342 } |
| 332 | 343 |
| 344 std::ostream& operator<<(std::ostream& os, |
| 345 const OriginRequestSummary& summary) { |
| 346 return os << "[" << summary.origin << "," << summary.always_access_network |
| 347 << "," << summary.accessed_network << "," |
| 348 << summary.first_occurrence << "]"; |
| 349 } |
| 350 |
| 333 std::ostream& operator<<(std::ostream& os, const PageRequestSummary& summary) { | 351 std::ostream& operator<<(std::ostream& os, const PageRequestSummary& summary) { |
| 334 os << "[" << summary.main_frame_url << "," << summary.initial_url << "]" | 352 os << "[" << summary.main_frame_url << "," << summary.initial_url << "]" |
| 335 << std::endl; | 353 << std::endl; |
| 336 for (const auto& request : summary.subresource_requests) | 354 for (const auto& request : summary.subresource_requests) |
| 337 os << "\t\t" << request << std::endl; | 355 os << "\t\t" << request << std::endl; |
| 356 for (const auto& pair : summary.origins) |
| 357 os << "\t\t" << pair.first << ":" << pair.second << std::endl; |
| 338 return os; | 358 return os; |
| 339 } | 359 } |
| 340 | 360 |
| 341 std::ostream& operator<<(std::ostream& os, const URLRequestSummary& summary) { | 361 std::ostream& operator<<(std::ostream& os, const URLRequestSummary& summary) { |
| 342 return os << "[" << summary.navigation_id << "," << summary.resource_url | 362 return os << "[" << summary.navigation_id << "," << summary.resource_url |
| 343 << "," << summary.resource_type << "," << summary.priority << "," | 363 << "," << summary.resource_type << "," << summary.priority << "," |
| 344 << summary.before_first_contentful_paint << "," << summary.mime_type | 364 << summary.before_first_contentful_paint << "," << summary.mime_type |
| 345 << "," << summary.was_cached << "," << summary.redirect_url << "," | 365 << "," << summary.was_cached << "," << summary.redirect_url << "," |
| 346 << summary.has_validators << "," << summary.always_revalidate | 366 << summary.has_validators << "," << summary.always_revalidate |
| 347 << "]"; | 367 << "]"; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 bool operator==(const RedirectStat& lhs, const RedirectStat& rhs) { | 429 bool operator==(const RedirectStat& lhs, const RedirectStat& rhs) { |
| 410 return lhs.url() == rhs.url() && | 430 return lhs.url() == rhs.url() && |
| 411 lhs.number_of_hits() == rhs.number_of_hits() && | 431 lhs.number_of_hits() == rhs.number_of_hits() && |
| 412 lhs.number_of_misses() == rhs.number_of_misses() && | 432 lhs.number_of_misses() == rhs.number_of_misses() && |
| 413 lhs.consecutive_misses() == rhs.consecutive_misses(); | 433 lhs.consecutive_misses() == rhs.consecutive_misses(); |
| 414 } | 434 } |
| 415 | 435 |
| 416 bool operator==(const PageRequestSummary& lhs, const PageRequestSummary& rhs) { | 436 bool operator==(const PageRequestSummary& lhs, const PageRequestSummary& rhs) { |
| 417 return lhs.main_frame_url == rhs.main_frame_url && | 437 return lhs.main_frame_url == rhs.main_frame_url && |
| 418 lhs.initial_url == rhs.initial_url && | 438 lhs.initial_url == rhs.initial_url && |
| 419 lhs.subresource_requests == rhs.subresource_requests; | 439 lhs.subresource_requests == rhs.subresource_requests && |
| 440 lhs.origins == rhs.origins; |
| 420 } | 441 } |
| 421 | 442 |
| 422 bool operator==(const URLRequestSummary& lhs, const URLRequestSummary& rhs) { | 443 bool operator==(const URLRequestSummary& lhs, const URLRequestSummary& rhs) { |
| 423 return lhs.navigation_id == rhs.navigation_id && | 444 return lhs.navigation_id == rhs.navigation_id && |
| 424 lhs.resource_url == rhs.resource_url && | 445 lhs.resource_url == rhs.resource_url && |
| 425 lhs.resource_type == rhs.resource_type && | 446 lhs.resource_type == rhs.resource_type && |
| 426 lhs.priority == rhs.priority && lhs.mime_type == rhs.mime_type && | 447 lhs.priority == rhs.priority && lhs.mime_type == rhs.mime_type && |
| 427 lhs.before_first_contentful_paint == | 448 lhs.before_first_contentful_paint == |
| 428 rhs.before_first_contentful_paint && | 449 rhs.before_first_contentful_paint && |
| 429 lhs.was_cached == rhs.was_cached && | 450 lhs.was_cached == rhs.was_cached && |
| 430 lhs.redirect_url == rhs.redirect_url && | 451 lhs.redirect_url == rhs.redirect_url && |
| 431 lhs.has_validators == rhs.has_validators && | 452 lhs.has_validators == rhs.has_validators && |
| 432 lhs.always_revalidate == rhs.always_revalidate; | 453 lhs.always_revalidate == rhs.always_revalidate; |
| 433 } | 454 } |
| 434 | 455 |
| 456 bool operator==(const OriginRequestSummary& lhs, |
| 457 const OriginRequestSummary& rhs) { |
| 458 return lhs.origin == rhs.origin && |
| 459 lhs.always_access_network == rhs.always_access_network && |
| 460 lhs.accessed_network == rhs.accessed_network && |
| 461 lhs.first_occurrence == rhs.first_occurrence; |
| 462 } |
| 463 |
| 435 bool operator==(const OriginData& lhs, const OriginData& rhs) { | 464 bool operator==(const OriginData& lhs, const OriginData& rhs) { |
| 436 bool equal = | 465 bool equal = |
| 437 lhs.host() == rhs.host() && lhs.origins_size() == rhs.origins_size(); | 466 lhs.host() == rhs.host() && lhs.origins_size() == rhs.origins_size(); |
| 438 if (!equal) | 467 if (!equal) |
| 439 return false; | 468 return false; |
| 440 | 469 |
| 441 for (int i = 0; i < lhs.origins_size(); ++i) | 470 for (int i = 0; i < lhs.origins_size(); ++i) |
| 442 equal = equal && lhs.origins(i) == rhs.origins(i); | 471 equal = equal && lhs.origins(i) == rhs.origins(i); |
| 443 | 472 |
| 444 return equal; | 473 return equal; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 455 } | 484 } |
| 456 | 485 |
| 457 bool operator==(const PreconnectPrediction& lhs, | 486 bool operator==(const PreconnectPrediction& lhs, |
| 458 const PreconnectPrediction& rhs) { | 487 const PreconnectPrediction& rhs) { |
| 459 return lhs.is_redirected == rhs.is_redirected && lhs.host == rhs.host && | 488 return lhs.is_redirected == rhs.is_redirected && lhs.host == rhs.host && |
| 460 lhs.preconnect_origins == rhs.preconnect_origins && | 489 lhs.preconnect_origins == rhs.preconnect_origins && |
| 461 lhs.preresolve_hosts == rhs.preresolve_hosts; | 490 lhs.preresolve_hosts == rhs.preresolve_hosts; |
| 462 } | 491 } |
| 463 | 492 |
| 464 } // namespace predictors | 493 } // namespace predictors |
| OLD | NEW |