| 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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 } | 156 } |
| 158 | 157 |
| 159 PageRequestSummary CreatePageRequestSummary( | 158 PageRequestSummary CreatePageRequestSummary( |
| 160 const std::string& main_frame_url, | 159 const std::string& main_frame_url, |
| 161 const std::string& initial_url, | 160 const std::string& initial_url, |
| 162 const std::vector<URLRequestSummary>& subresource_requests) { | 161 const std::vector<URLRequestSummary>& subresource_requests) { |
| 163 GURL main_frame_gurl(main_frame_url); | 162 GURL main_frame_gurl(main_frame_url); |
| 164 PageRequestSummary summary(main_frame_gurl); | 163 PageRequestSummary summary(main_frame_gurl); |
| 165 summary.initial_url = GURL(initial_url); | 164 summary.initial_url = GURL(initial_url); |
| 166 summary.subresource_requests = subresource_requests; | 165 summary.subresource_requests = subresource_requests; |
| 166 for (auto& request_summary : subresource_requests) |
| 167 summary.UpdateOrAddToOrigins(request_summary); |
| 167 return summary; | 168 return summary; |
| 168 } | 169 } |
| 169 | 170 |
| 170 URLRequestSummary CreateURLRequestSummary(SessionID::id_type tab_id, | 171 URLRequestSummary CreateURLRequestSummary(SessionID::id_type tab_id, |
| 171 const std::string& main_frame_url, | 172 const std::string& main_frame_url, |
| 172 const std::string& resource_url, | 173 const std::string& resource_url, |
| 173 content::ResourceType resource_type, | 174 content::ResourceType resource_type, |
| 174 net::RequestPriority priority, | 175 net::RequestPriority priority, |
| 175 const std::string& mime_type, | 176 const std::string& mime_type, |
| 176 bool was_cached, | 177 bool was_cached, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 189 summary.was_cached = was_cached; | 190 summary.was_cached = was_cached; |
| 190 if (!redirect_url.empty()) | 191 if (!redirect_url.empty()) |
| 191 summary.redirect_url = GURL(redirect_url); | 192 summary.redirect_url = GURL(redirect_url); |
| 192 summary.has_validators = has_validators; | 193 summary.has_validators = has_validators; |
| 193 summary.always_revalidate = always_revalidate; | 194 summary.always_revalidate = always_revalidate; |
| 194 summary.is_no_store = false; | 195 summary.is_no_store = false; |
| 195 summary.network_accessed = true; | 196 summary.network_accessed = true; |
| 196 return summary; | 197 return summary; |
| 197 } | 198 } |
| 198 | 199 |
| 200 URLRequestSummary CreateRedirectRequestSummary( |
| 201 SessionID::id_type session_id, |
| 202 const std::string& main_frame_url, |
| 203 const std::string& redirect_url) { |
| 204 URLRequestSummary summary = |
| 205 CreateURLRequestSummary(session_id, main_frame_url); |
| 206 summary.redirect_url = GURL(redirect_url); |
| 207 return summary; |
| 208 } |
| 209 |
| 199 ResourcePrefetchPredictor::Prediction CreatePrediction( | 210 ResourcePrefetchPredictor::Prediction CreatePrediction( |
| 200 const std::string& main_frame_key, | 211 const std::string& main_frame_key, |
| 201 std::vector<GURL> subresource_urls) { | 212 std::vector<GURL> subresource_urls) { |
| 202 Prediction prediction; | 213 Prediction prediction; |
| 203 prediction.main_frame_key = main_frame_key; | 214 prediction.main_frame_key = main_frame_key; |
| 204 prediction.subresource_urls = subresource_urls; | 215 prediction.subresource_urls = subresource_urls; |
| 205 prediction.is_host = true; | 216 prediction.is_host = true; |
| 206 prediction.is_redirected = false; | 217 prediction.is_redirected = false; |
| 207 return prediction; | 218 return prediction; |
| 208 } | 219 } |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 } | 368 } |
| 358 | 369 |
| 359 std::ostream& operator<<(std::ostream& os, const OriginStat& origin) { | 370 std::ostream& operator<<(std::ostream& os, const OriginStat& origin) { |
| 360 return os << "[" << origin.origin() << "," << origin.number_of_hits() << "," | 371 return os << "[" << origin.origin() << "," << origin.number_of_hits() << "," |
| 361 << origin.number_of_misses() << "," << origin.consecutive_misses() | 372 << origin.number_of_misses() << "," << origin.consecutive_misses() |
| 362 << "," << origin.average_position() << "," | 373 << "," << origin.average_position() << "," |
| 363 << origin.always_access_network() << "," | 374 << origin.always_access_network() << "," |
| 364 << origin.accessed_network() << "]"; | 375 << origin.accessed_network() << "]"; |
| 365 } | 376 } |
| 366 | 377 |
| 378 std::ostream& operator<<(std::ostream& os, |
| 379 const OriginRequestSummary& summary) { |
| 380 return os << "[" << summary.origin << "," << summary.always_access_network |
| 381 << "," << summary.accessed_network << "," |
| 382 << summary.first_occurrence << "]"; |
| 383 } |
| 384 |
| 367 std::ostream& operator<<(std::ostream& os, const PageRequestSummary& summary) { | 385 std::ostream& operator<<(std::ostream& os, const PageRequestSummary& summary) { |
| 368 os << "[" << summary.main_frame_url << "," << summary.initial_url << "]" | 386 os << "[" << summary.main_frame_url << "," << summary.initial_url << "]" |
| 369 << std::endl; | 387 << std::endl; |
| 370 for (const auto& request : summary.subresource_requests) | 388 for (const auto& request : summary.subresource_requests) |
| 371 os << "\t\t" << request << std::endl; | 389 os << "\t\t" << request << std::endl; |
| 390 for (const auto& pair : summary.origins) |
| 391 os << "\t\t" << pair.first << ":" << pair.second << std::endl; |
| 372 return os; | 392 return os; |
| 373 } | 393 } |
| 374 | 394 |
| 375 std::ostream& operator<<(std::ostream& os, const URLRequestSummary& summary) { | 395 std::ostream& operator<<(std::ostream& os, const URLRequestSummary& summary) { |
| 376 return os << "[" << summary.navigation_id << "," << summary.resource_url | 396 return os << "[" << summary.navigation_id << "," << summary.resource_url |
| 377 << "," << summary.resource_type << "," << summary.priority << "," | 397 << "," << summary.resource_type << "," << summary.priority << "," |
| 378 << summary.before_first_contentful_paint << "," << summary.mime_type | 398 << summary.before_first_contentful_paint << "," << summary.mime_type |
| 379 << "," << summary.was_cached << "," << summary.redirect_url << "," | 399 << "," << summary.was_cached << "," << summary.redirect_url << "," |
| 380 << summary.has_validators << "," << summary.always_revalidate | 400 << summary.has_validators << "," << summary.always_revalidate |
| 381 << "]"; | 401 << "]"; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 bool operator==(const RedirectStat& lhs, const RedirectStat& rhs) { | 463 bool operator==(const RedirectStat& lhs, const RedirectStat& rhs) { |
| 444 return lhs.url() == rhs.url() && | 464 return lhs.url() == rhs.url() && |
| 445 lhs.number_of_hits() == rhs.number_of_hits() && | 465 lhs.number_of_hits() == rhs.number_of_hits() && |
| 446 lhs.number_of_misses() == rhs.number_of_misses() && | 466 lhs.number_of_misses() == rhs.number_of_misses() && |
| 447 lhs.consecutive_misses() == rhs.consecutive_misses(); | 467 lhs.consecutive_misses() == rhs.consecutive_misses(); |
| 448 } | 468 } |
| 449 | 469 |
| 450 bool operator==(const PageRequestSummary& lhs, const PageRequestSummary& rhs) { | 470 bool operator==(const PageRequestSummary& lhs, const PageRequestSummary& rhs) { |
| 451 return lhs.main_frame_url == rhs.main_frame_url && | 471 return lhs.main_frame_url == rhs.main_frame_url && |
| 452 lhs.initial_url == rhs.initial_url && | 472 lhs.initial_url == rhs.initial_url && |
| 453 lhs.subresource_requests == rhs.subresource_requests; | 473 lhs.subresource_requests == rhs.subresource_requests && |
| 474 lhs.origins == rhs.origins; |
| 454 } | 475 } |
| 455 | 476 |
| 456 bool operator==(const URLRequestSummary& lhs, const URLRequestSummary& rhs) { | 477 bool operator==(const URLRequestSummary& lhs, const URLRequestSummary& rhs) { |
| 457 return lhs.navigation_id == rhs.navigation_id && | 478 return lhs.navigation_id == rhs.navigation_id && |
| 458 lhs.resource_url == rhs.resource_url && | 479 lhs.resource_url == rhs.resource_url && |
| 459 lhs.resource_type == rhs.resource_type && | 480 lhs.resource_type == rhs.resource_type && |
| 460 lhs.priority == rhs.priority && lhs.mime_type == rhs.mime_type && | 481 lhs.priority == rhs.priority && lhs.mime_type == rhs.mime_type && |
| 461 lhs.before_first_contentful_paint == | 482 lhs.before_first_contentful_paint == |
| 462 rhs.before_first_contentful_paint && | 483 rhs.before_first_contentful_paint && |
| 463 lhs.was_cached == rhs.was_cached && | 484 lhs.was_cached == rhs.was_cached && |
| 464 lhs.redirect_url == rhs.redirect_url && | 485 lhs.redirect_url == rhs.redirect_url && |
| 465 lhs.has_validators == rhs.has_validators && | 486 lhs.has_validators == rhs.has_validators && |
| 466 lhs.always_revalidate == rhs.always_revalidate; | 487 lhs.always_revalidate == rhs.always_revalidate; |
| 467 } | 488 } |
| 468 | 489 |
| 490 bool operator==(const OriginRequestSummary& lhs, |
| 491 const OriginRequestSummary& rhs) { |
| 492 return lhs.origin == rhs.origin && |
| 493 lhs.always_access_network == rhs.always_access_network && |
| 494 lhs.accessed_network == rhs.accessed_network && |
| 495 lhs.first_occurrence == rhs.first_occurrence; |
| 496 } |
| 497 |
| 469 bool operator==(const OriginData& lhs, const OriginData& rhs) { | 498 bool operator==(const OriginData& lhs, const OriginData& rhs) { |
| 470 bool equal = | 499 bool equal = |
| 471 lhs.host() == rhs.host() && lhs.origins_size() == rhs.origins_size(); | 500 lhs.host() == rhs.host() && lhs.origins_size() == rhs.origins_size(); |
| 472 if (!equal) | 501 if (!equal) |
| 473 return false; | 502 return false; |
| 474 | 503 |
| 475 for (int i = 0; i < lhs.origins_size(); ++i) | 504 for (int i = 0; i < lhs.origins_size(); ++i) |
| 476 equal = equal && lhs.origins(i) == rhs.origins(i); | 505 equal = equal && lhs.origins(i) == rhs.origins(i); |
| 477 | 506 |
| 478 return equal; | 507 return equal; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 | 552 |
| 524 return equal; | 553 return equal; |
| 525 } | 554 } |
| 526 | 555 |
| 527 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { | 556 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { |
| 528 return lhs.url() == rhs.url() && | 557 return lhs.url() == rhs.url() && |
| 529 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio()); | 558 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio()); |
| 530 } | 559 } |
| 531 | 560 |
| 532 } // namespace precache | 561 } // namespace precache |
| OLD | NEW |