Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor_unittest.cc

Issue 2719533002: predictors: Add RedirectStatus histogram + fix redirects related bug. (Closed)
Patch Set: Nit. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <iostream> 7 #include <iostream>
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/test/histogram_tester.h" 14 #include "base/test/histogram_tester.h"
15 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "chrome/browser/history/history_service_factory.h" 16 #include "chrome/browser/history/history_service_factory.h"
17 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" 17 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
18 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" 18 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h"
19 #include "chrome/test/base/testing_profile.h" 19 #include "chrome/test/base/testing_profile.h"
20 #include "components/history/core/browser/history_service.h" 20 #include "components/history/core/browser/history_service.h"
21 #include "components/history/core/browser/history_types.h" 21 #include "components/history/core/browser/history_types.h"
22 #include "components/sessions/core/session_id.h" 22 #include "components/sessions/core/session_id.h"
23 #include "content/public/browser/resource_request_info.h" 23 #include "content/public/browser/resource_request_info.h"
24 #include "content/public/common/previews_state.h"
25 #include "content/public/test/test_browser_thread_bundle.h" 24 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "net/http/http_response_headers.h" 25 #include "net/http/http_response_headers.h"
27 #include "net/url_request/url_request_context.h" 26 #include "net/url_request/url_request_context.h"
28 #include "net/url_request/url_request_job.h" 27 #include "net/url_request/url_request_job.h"
29 #include "net/url_request/url_request_test_util.h" 28 #include "net/url_request/url_request_test_util.h"
30 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
32 31
33 using testing::ContainerEq; 32 using testing::ContainerEq;
34 using testing::Pointee; 33 using testing::Pointee;
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 return request; 216 return request;
218 } 217 }
219 218
220 void InitializePredictor() { 219 void InitializePredictor() {
221 predictor_->StartInitialization(); 220 predictor_->StartInitialization();
222 base::RunLoop loop; 221 base::RunLoop loop;
223 loop.RunUntilIdle(); // Runs the DB lookup. 222 loop.RunUntilIdle(); // Runs the DB lookup.
224 profile_->BlockUntilHistoryProcessesPendingRequests(); 223 profile_->BlockUntilHistoryProcessesPendingRequests();
225 } 224 }
226 225
227 bool URLRequestSummaryAreEqual(const URLRequestSummary& lhs,
228 const URLRequestSummary& rhs) {
229 return lhs.navigation_id == rhs.navigation_id &&
230 lhs.resource_url == rhs.resource_url &&
231 lhs.resource_type == rhs.resource_type &&
232 lhs.mime_type == rhs.mime_type &&
233 lhs.was_cached == rhs.was_cached;
234 }
235
236 void ResetPredictor() { 226 void ResetPredictor() {
237 ResourcePrefetchPredictorConfig config; 227 ResourcePrefetchPredictorConfig config;
238 config.max_urls_to_track = 3; 228 config.max_urls_to_track = 3;
239 config.max_hosts_to_track = 2; 229 config.max_hosts_to_track = 2;
240 config.min_url_visit_count = 2; 230 config.min_url_visit_count = 2;
241 config.max_resources_per_entry = 4; 231 config.max_resources_per_entry = 4;
242 config.max_consecutive_misses = 2; 232 config.max_consecutive_misses = 2;
243 config.min_resource_confidence_to_trigger_prefetch = 0.5; 233 config.min_resource_confidence_to_trigger_prefetch = 0.5;
244 234
245 // TODO(shishir): Enable the prefetching mode in the tests. 235 // TODO(shishir): Enable the prefetching mode in the tests.
246 config.mode |= ResourcePrefetchPredictorConfig::LEARNING; 236 config.mode |= ResourcePrefetchPredictorConfig::LEARNING;
247 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get())); 237 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get()));
248 predictor_->set_mock_tables(mock_tables_); 238 predictor_->set_mock_tables(mock_tables_);
249 } 239 }
250 240
251 void InitializeSampleData(); 241 void InitializeSampleData();
242 void TestRedirectStatusHistogram(
243 const std::string& predictor_initial_key,
244 const std::string& predictor_key,
245 const std::string& navigation_initial_url,
246 const std::string& navigation_url,
247 ResourcePrefetchPredictor::RedirectStatus expected_status);
252 248
253 content::TestBrowserThreadBundle thread_bundle_; 249 content::TestBrowserThreadBundle thread_bundle_;
254 std::unique_ptr<TestingProfile> profile_; 250 std::unique_ptr<TestingProfile> profile_;
255 net::TestURLRequestContext url_request_context_; 251 net::TestURLRequestContext url_request_context_;
256 252
257 std::unique_ptr<ResourcePrefetchPredictor> predictor_; 253 std::unique_ptr<ResourcePrefetchPredictor> predictor_;
258 scoped_refptr<StrictMock<MockResourcePrefetchPredictorTables> > mock_tables_; 254 scoped_refptr<StrictMock<MockResourcePrefetchPredictorTables>> mock_tables_;
259 255
260 PrefetchDataMap test_url_data_; 256 PrefetchDataMap test_url_data_;
261 PrefetchDataMap test_host_data_; 257 PrefetchDataMap test_host_data_;
262 RedirectDataMap test_url_redirect_data_; 258 RedirectDataMap test_url_redirect_data_;
263 RedirectDataMap test_host_redirect_data_; 259 RedirectDataMap test_host_redirect_data_;
264 PrefetchData empty_resource_data_; 260 PrefetchData empty_resource_data_;
265 RedirectData empty_redirect_data_; 261 RedirectData empty_redirect_data_;
266 262
267 MockURLRequestJobFactory url_request_job_factory_; 263 MockURLRequestJobFactory url_request_job_factory_;
268 EmptyURLRequestDelegate url_request_delegate_; 264 EmptyURLRequestDelegate url_request_delegate_;
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 InitializeRedirectStat(microsoft.add_redirect_endpoints(), 415 InitializeRedirectStat(microsoft.add_redirect_endpoints(),
420 "www.microsoft.com", 10, 0, 0); 416 "www.microsoft.com", 10, 0, 0);
421 417
422 test_host_redirect_data_.clear(); 418 test_host_redirect_data_.clear();
423 test_host_redirect_data_.insert(std::make_pair(bbc.primary_key(), bbc)); 419 test_host_redirect_data_.insert(std::make_pair(bbc.primary_key(), bbc));
424 test_host_redirect_data_.insert( 420 test_host_redirect_data_.insert(
425 std::make_pair(microsoft.primary_key(), microsoft)); 421 std::make_pair(microsoft.primary_key(), microsoft));
426 } 422 }
427 } 423 }
428 424
425 void ResourcePrefetchPredictorTest::TestRedirectStatusHistogram(
426 const std::string& predictor_initial_key,
427 const std::string& predictor_key,
428 const std::string& navigation_initial_url,
429 const std::string& navigation_url,
430 ResourcePrefetchPredictor::RedirectStatus expected_status) {
431 // Database initialization.
432 const std::string& script_url = "https://cdn.google.com/script.js";
433 PrefetchData google = CreatePrefetchData(predictor_key, 1);
434 // We need at least one resource for prediction.
435 InitializeResourceData(google.add_resources(), script_url,
436 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1,
437 net::MEDIUM, false, false);
438 predictor_->host_table_cache_->insert(
439 std::make_pair(google.primary_key(), google));
440
441 if (predictor_initial_key != predictor_key) {
442 RedirectData redirect = CreateRedirectData(predictor_initial_key, 1);
443 InitializeRedirectStat(redirect.add_redirect_endpoints(), predictor_key, 10,
444 0, 0);
445 predictor_->host_redirect_table_cache_->insert(
446 std::make_pair(redirect.primary_key(), redirect));
447 }
448
449 // Navigation simulation.
450 using testing::_;
451 EXPECT_CALL(*mock_tables_.get(), UpdateData(_, _, _, _))
452 .Times(testing::AtLeast(1));
453 URLRequestSummary initial =
454 CreateURLRequestSummary(1, navigation_initial_url);
455 predictor_->RecordURLRequest(initial);
456
457 if (navigation_initial_url != navigation_url) {
458 URLRequestSummary redirect =
459 CreateRedirectRequestSummary(1, navigation_initial_url, navigation_url);
460 predictor_->RecordURLRedirect(redirect);
461 }
462 NavigationID navigation_id = CreateNavigationID(1, navigation_url);
463
464 URLRequestSummary script = CreateURLRequestSummary(
465 1, navigation_url, script_url, content::RESOURCE_TYPE_SCRIPT);
466 predictor_->RecordURLResponse(script);
467
468 predictor_->RecordMainFrameLoadComplete(navigation_id);
469 profile_->BlockUntilHistoryProcessesPendingRequests();
470
471 // Histogram check.
472 histogram_tester_->ExpectBucketCount(
473 internal::kResourcePrefetchPredictorRedirectStatusHistogram,
474 static_cast<int>(expected_status), 1);
475 }
476
429 // Confirm that there's been no shift in the 477 // Confirm that there's been no shift in the
430 // ResourceData_Priority/net::RequestPriority equivalence. 478 // ResourceData_Priority/net::RequestPriority equivalence.
431 static_assert(static_cast<int>(net::MINIMUM_PRIORITY) == 479 static_assert(static_cast<int>(net::MINIMUM_PRIORITY) ==
432 static_cast<int>( 480 static_cast<int>(
433 ResourceData_Priority_REQUEST_PRIORITY_THROTTLED), 481 ResourceData_Priority_REQUEST_PRIORITY_THROTTLED),
434 "Database/Net priority mismatch: Minimum"); 482 "Database/Net priority mismatch: Minimum");
435 static_assert(static_cast<int>(net::MAXIMUM_PRIORITY) == 483 static_assert(static_cast<int>(net::MAXIMUM_PRIORITY) ==
436 static_cast<int>(ResourceData_Priority_REQUEST_PRIORITY_HIGHEST), 484 static_cast<int>(ResourceData_Priority_REQUEST_PRIORITY_HIGHEST),
437 "Database/Net priority mismatch: Maximum"); 485 "Database/Net priority mismatch: Maximum");
438 486
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after
1129 URLRequestSummary resource3 = CreateURLRequestSummary( 1177 URLRequestSummary resource3 = CreateURLRequestSummary(
1130 1, "http://www.google.com", "http://google.com/script2.js", 1178 1, "http://www.google.com", "http://google.com/script2.js",
1131 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 1179 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1132 predictor_->OnSubresourceResponse(resource1); 1180 predictor_->OnSubresourceResponse(resource1);
1133 predictor_->OnSubresourceResponse(resource2); 1181 predictor_->OnSubresourceResponse(resource2);
1134 predictor_->OnSubresourceResponse(resource3); 1182 predictor_->OnSubresourceResponse(resource3);
1135 1183
1136 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1184 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1137 EXPECT_EQ(3U, predictor_->inflight_navigations_[main_frame1.navigation_id] 1185 EXPECT_EQ(3U, predictor_->inflight_navigations_[main_frame1.navigation_id]
1138 ->subresource_requests.size()); 1186 ->subresource_requests.size());
1139 EXPECT_TRUE(URLRequestSummaryAreEqual( 1187 EXPECT_EQ(resource1,
1140 resource1, predictor_->inflight_navigations_[main_frame1.navigation_id] 1188 predictor_->inflight_navigations_[main_frame1.navigation_id]
1141 ->subresource_requests[0])); 1189 ->subresource_requests[0]);
1142 EXPECT_TRUE(URLRequestSummaryAreEqual( 1190 EXPECT_EQ(resource2,
1143 resource2, predictor_->inflight_navigations_[main_frame1.navigation_id] 1191 predictor_->inflight_navigations_[main_frame1.navigation_id]
1144 ->subresource_requests[1])); 1192 ->subresource_requests[1]);
1145 EXPECT_TRUE(URLRequestSummaryAreEqual( 1193 EXPECT_EQ(resource3,
1146 resource3, predictor_->inflight_navigations_[main_frame1.navigation_id] 1194 predictor_->inflight_navigations_[main_frame1.navigation_id]
1147 ->subresource_requests[2])); 1195 ->subresource_requests[2]);
1148 } 1196 }
1149 1197
1150 TEST_F(ResourcePrefetchPredictorTest, HandledResourceTypes) { 1198 TEST_F(ResourcePrefetchPredictorTest, HandledResourceTypes) {
1151 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( 1199 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
1152 content::RESOURCE_TYPE_STYLESHEET, "bogus/mime-type")); 1200 content::RESOURCE_TYPE_STYLESHEET, "bogus/mime-type"));
1153 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( 1201 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
1154 content::RESOURCE_TYPE_STYLESHEET, "")); 1202 content::RESOURCE_TYPE_STYLESHEET, ""));
1155 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( 1203 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
1156 content::RESOURCE_TYPE_WORKER, "text/css")); 1204 content::RESOURCE_TYPE_WORKER, "text/css"));
1157 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( 1205 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
1466 EXPECT_EQ(redirect_endpoint, "https://google.com"); 1514 EXPECT_EQ(redirect_endpoint, "https://google.com");
1467 1515
1468 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://fb.com", data_map, 1516 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://fb.com", data_map,
1469 &redirect_endpoint)); 1517 &redirect_endpoint));
1470 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://404.com", data_map, 1518 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://404.com", data_map,
1471 &redirect_endpoint)); 1519 &redirect_endpoint));
1472 } 1520 }
1473 1521
1474 TEST_F(ResourcePrefetchPredictorTest, GetPrefetchData) { 1522 TEST_F(ResourcePrefetchPredictorTest, GetPrefetchData) {
1475 const GURL main_frame_url("http://google.com/?query=cats"); 1523 const GURL main_frame_url("http://google.com/?query=cats");
1476 std::vector<GURL> urls; 1524 ResourcePrefetchPredictor::Prediction prediction;
1525 std::vector<GURL>& urls = prediction.subresource_urls;
1477 // No prefetch data. 1526 // No prefetch data.
1478 EXPECT_FALSE(predictor_->GetPrefetchData(main_frame_url, &urls)); 1527 EXPECT_FALSE(predictor_->GetPrefetchData(main_frame_url, &prediction));
1479 1528
1480 // Add a resource associated with the main frame host. 1529 // Add a resource associated with the main frame host.
1481 PrefetchData google_host = CreatePrefetchData("google.com", 1); 1530 PrefetchData google_host = CreatePrefetchData("google.com", 1);
1482 const std::string script_url = "https://cdn.google.com/script.js"; 1531 const std::string script_url = "https://cdn.google.com/script.js";
1483 InitializeResourceData(google_host.add_resources(), script_url, 1532 InitializeResourceData(google_host.add_resources(), script_url,
1484 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, 1533 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1,
1485 net::MEDIUM, false, false); 1534 net::MEDIUM, false, false);
1486 predictor_->host_table_cache_->insert( 1535 predictor_->host_table_cache_->insert(
1487 std::make_pair(google_host.primary_key(), google_host)); 1536 std::make_pair(google_host.primary_key(), google_host));
1488 1537
1489 urls.clear(); 1538 urls.clear();
1490 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); 1539 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction));
1491 EXPECT_THAT(urls, UnorderedElementsAre(GURL(script_url))); 1540 EXPECT_THAT(urls, UnorderedElementsAre(GURL(script_url)));
1492 1541
1493 // Add a resource associated with the main frame url. 1542 // Add a resource associated with the main frame url.
1494 PrefetchData google_url = 1543 PrefetchData google_url =
1495 CreatePrefetchData("http://google.com/?query=cats", 2); 1544 CreatePrefetchData("http://google.com/?query=cats", 2);
1496 const std::string image_url = "https://cdn.google.com/image.png"; 1545 const std::string image_url = "https://cdn.google.com/image.png";
1497 InitializeResourceData(google_url.add_resources(), image_url, 1546 InitializeResourceData(google_url.add_resources(), image_url,
1498 content::RESOURCE_TYPE_IMAGE, 10, 0, 1, 2.1, 1547 content::RESOURCE_TYPE_IMAGE, 10, 0, 1, 2.1,
1499 net::MEDIUM, false, false); 1548 net::MEDIUM, false, false);
1500 predictor_->url_table_cache_->insert( 1549 predictor_->url_table_cache_->insert(
1501 std::make_pair(google_url.primary_key(), google_url)); 1550 std::make_pair(google_url.primary_key(), google_url));
1502 1551
1503 urls.clear(); 1552 urls.clear();
1504 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); 1553 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction));
1505 EXPECT_THAT(urls, UnorderedElementsAre(GURL(image_url))); 1554 EXPECT_THAT(urls, UnorderedElementsAre(GURL(image_url)));
1506 1555
1507 // Add host-based redirect. 1556 // Add host-based redirect.
1508 RedirectData host_redirect = CreateRedirectData("google.com", 3); 1557 RedirectData host_redirect = CreateRedirectData("google.com", 3);
1509 InitializeRedirectStat(host_redirect.add_redirect_endpoints(), 1558 InitializeRedirectStat(host_redirect.add_redirect_endpoints(),
1510 "www.google.com", 10, 0, 0); 1559 "www.google.com", 10, 0, 0);
1511 predictor_->host_redirect_table_cache_->insert( 1560 predictor_->host_redirect_table_cache_->insert(
1512 std::make_pair(host_redirect.primary_key(), host_redirect)); 1561 std::make_pair(host_redirect.primary_key(), host_redirect));
1513 1562
1514 // Nothing changed: new redirect endpoint doesn't have any associated 1563 // Nothing changed: new redirect endpoint doesn't have any associated
1515 // resources 1564 // resources
1516 urls.clear(); 1565 urls.clear();
1517 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); 1566 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction));
1518 EXPECT_THAT(urls, UnorderedElementsAre(GURL(image_url))); 1567 EXPECT_THAT(urls, UnorderedElementsAre(GURL(image_url)));
1519 1568
1520 // Add a resource associated with host redirect endpoint. 1569 // Add a resource associated with host redirect endpoint.
1521 PrefetchData www_google_host = CreatePrefetchData("www.google.com", 4); 1570 PrefetchData www_google_host = CreatePrefetchData("www.google.com", 4);
1522 const std::string style_url = "https://cdn.google.com/style.css"; 1571 const std::string style_url = "https://cdn.google.com/style.css";
1523 InitializeResourceData(www_google_host.add_resources(), style_url, 1572 InitializeResourceData(www_google_host.add_resources(), style_url,
1524 content::RESOURCE_TYPE_STYLESHEET, 10, 0, 1, 2.1, 1573 content::RESOURCE_TYPE_STYLESHEET, 10, 0, 1, 2.1,
1525 net::MEDIUM, false, false); 1574 net::MEDIUM, false, false);
1526 predictor_->host_table_cache_->insert( 1575 predictor_->host_table_cache_->insert(
1527 std::make_pair(www_google_host.primary_key(), www_google_host)); 1576 std::make_pair(www_google_host.primary_key(), www_google_host));
1528 1577
1529 urls.clear(); 1578 urls.clear();
1530 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); 1579 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction));
1531 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url))); 1580 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url)));
1532 1581
1533 // Add url-based redirect. 1582 // Add url-based redirect.
1534 RedirectData url_redirect = 1583 RedirectData url_redirect =
1535 CreateRedirectData("http://google.com/?query=cats", 5); 1584 CreateRedirectData("http://google.com/?query=cats", 5);
1536 InitializeRedirectStat(url_redirect.add_redirect_endpoints(), 1585 InitializeRedirectStat(url_redirect.add_redirect_endpoints(),
1537 "https://www.google.com/?query=cats", 10, 0, 0); 1586 "https://www.google.com/?query=cats", 10, 0, 0);
1538 predictor_->url_redirect_table_cache_->insert( 1587 predictor_->url_redirect_table_cache_->insert(
1539 std::make_pair(url_redirect.primary_key(), url_redirect)); 1588 std::make_pair(url_redirect.primary_key(), url_redirect));
1540 1589
1541 // Url redirect endpoint doesn't have associated resources. 1590 // Url redirect endpoint doesn't have associated resources.
1542 urls.clear(); 1591 urls.clear();
1543 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); 1592 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction));
1544 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url))); 1593 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url)));
1545 1594
1546 // Add a resource associated with url redirect endpoint. 1595 // Add a resource associated with url redirect endpoint.
1547 PrefetchData www_google_url = 1596 PrefetchData www_google_url =
1548 CreatePrefetchData("https://www.google.com/?query=cats", 4); 1597 CreatePrefetchData("https://www.google.com/?query=cats", 4);
1549 const std::string font_url = "https://cdn.google.com/comic-sans-ms.woff"; 1598 const std::string font_url = "https://cdn.google.com/comic-sans-ms.woff";
1550 InitializeResourceData(www_google_url.add_resources(), font_url, 1599 InitializeResourceData(www_google_url.add_resources(), font_url,
1551 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1, 1600 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1,
1552 net::MEDIUM, false, false); 1601 net::MEDIUM, false, false);
1553 predictor_->url_table_cache_->insert( 1602 predictor_->url_table_cache_->insert(
1554 std::make_pair(www_google_url.primary_key(), www_google_url)); 1603 std::make_pair(www_google_url.primary_key(), www_google_url));
1555 1604
1556 urls.clear(); 1605 urls.clear();
1557 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); 1606 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction));
1558 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url))); 1607 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url)));
1559 } 1608 }
1560 1609
1561 TEST_F(ResourcePrefetchPredictorTest, TestPrecisionRecallHistograms) { 1610 TEST_F(ResourcePrefetchPredictorTest, TestPrecisionRecallHistograms) {
1562 using testing::_; 1611 using testing::_;
1563 EXPECT_CALL(*mock_tables_.get(), UpdateData(_, _, _, _)); 1612 EXPECT_CALL(*mock_tables_.get(), UpdateData(_, _, _, _));
1564 1613
1565 // Fill the database with 3 resources: 1 useful, 2 useless. 1614 // Fill the database with 3 resources: 1 useful, 2 useless.
1566 const std::string main_frame_url = "http://google.com/?query=cats"; 1615 const std::string main_frame_url = "http://google.com/?query=cats";
1567 PrefetchData google = CreatePrefetchData("google.com", 1); 1616 PrefetchData google = CreatePrefetchData("google.com", 1);
1568 1617
1569 const std::string script_url = "https://cdn.google.com/script.js"; 1618 const std::string script_url = "https://cdn.google.com/script.js";
1570 InitializeResourceData(google.add_resources(), script_url, 1619 InitializeResourceData(google.add_resources(), script_url,
1571 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, 1620 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1,
1572 net::MEDIUM, false, false); 1621 net::MEDIUM, false, false);
1573 InitializeResourceData(google.add_resources(), script_url + "foo", 1622 InitializeResourceData(google.add_resources(), script_url + "foo",
1574 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, 1623 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1,
1575 net::MEDIUM, false, false); 1624 net::MEDIUM, false, false);
1576 InitializeResourceData(google.add_resources(), script_url + "bar", 1625 InitializeResourceData(google.add_resources(), script_url + "bar",
1577 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, 1626 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1,
1578 net::MEDIUM, false, false); 1627 net::MEDIUM, false, false);
1579 predictor_->host_table_cache_->insert( 1628 predictor_->host_table_cache_->insert(
1580 std::make_pair(google.primary_key(), google)); 1629 std::make_pair(google.primary_key(), google));
1581 1630
1582 std::vector<GURL> urls; 1631 ResourcePrefetchPredictor::Prediction prediction;
1583 EXPECT_TRUE(predictor_->GetPrefetchData(GURL(main_frame_url), &urls)); 1632 EXPECT_TRUE(predictor_->GetPrefetchData(GURL(main_frame_url), &prediction));
1584 1633
1585 // Simulate a navigation with 2 resources, one we know, one we don't. 1634 // Simulate a navigation with 2 resources, one we know, one we don't.
1586 URLRequestSummary main_frame = CreateURLRequestSummary(1, main_frame_url); 1635 URLRequestSummary main_frame = CreateURLRequestSummary(1, main_frame_url);
1587 predictor_->RecordURLRequest(main_frame); 1636 predictor_->RecordURLRequest(main_frame);
1588 1637
1589 URLRequestSummary script = CreateURLRequestSummary( 1638 URLRequestSummary script = CreateURLRequestSummary(
1590 1, main_frame_url, script_url, content::RESOURCE_TYPE_SCRIPT); 1639 1, main_frame_url, script_url, content::RESOURCE_TYPE_SCRIPT);
1591 predictor_->RecordURLResponse(script); 1640 predictor_->RecordURLResponse(script);
1592 1641
1593 URLRequestSummary new_script = CreateURLRequestSummary( 1642 URLRequestSummary new_script = CreateURLRequestSummary(
1594 1, main_frame_url, script_url + "2", content::RESOURCE_TYPE_SCRIPT); 1643 1, main_frame_url, script_url + "2", content::RESOURCE_TYPE_SCRIPT);
1595 predictor_->RecordURLResponse(new_script); 1644 predictor_->RecordURLResponse(new_script);
1596 1645
1597 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 1646 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
1598 profile_->BlockUntilHistoryProcessesPendingRequests(); 1647 profile_->BlockUntilHistoryProcessesPendingRequests();
1599 1648
1600 histogram_tester_->ExpectBucketCount( 1649 histogram_tester_->ExpectBucketCount(
1601 internal::kResourcePrefetchPredictorRecallHistogram, 50, 1); 1650 internal::kResourcePrefetchPredictorRecallHistogram, 50, 1);
1602 histogram_tester_->ExpectBucketCount( 1651 histogram_tester_->ExpectBucketCount(
1603 internal::kResourcePrefetchPredictorPrecisionHistogram, 33, 1); 1652 internal::kResourcePrefetchPredictorPrecisionHistogram, 33, 1);
1604 histogram_tester_->ExpectBucketCount( 1653 histogram_tester_->ExpectBucketCount(
1605 internal::kResourcePrefetchPredictorCountHistogram, 3, 1); 1654 internal::kResourcePrefetchPredictorCountHistogram, 3, 1);
1606 } 1655 }
1607 1656
1657 TEST_F(ResourcePrefetchPredictorTest, TestRedirectStatusNoRedirect) {
1658 TestRedirectStatusHistogram(
1659 "google.com", "google.com", "http://google.com?query=cats",
1660 "http://google.com?query=cats",
1661 ResourcePrefetchPredictor::RedirectStatus::NO_REDIRECT);
1662 }
1663
1664 TEST_F(ResourcePrefetchPredictorTest,
1665 TestRedirectStatusNoRedirectButPredicted) {
1666 TestRedirectStatusHistogram(
1667 "google.com", "www.google.com", "http://google.com?query=cats",
1668 "http://google.com?query=cats",
1669 ResourcePrefetchPredictor::RedirectStatus::NO_REDIRECT_BUT_PREDICTED);
1670 }
1671
1672 TEST_F(ResourcePrefetchPredictorTest, TestRedirectStatusRedirectNotPredicted) {
1673 TestRedirectStatusHistogram(
1674 "google.com", "google.com", "http://google.com?query=cats",
1675 "http://www.google.com?query=cats",
1676 ResourcePrefetchPredictor::RedirectStatus::REDIRECT_NOT_PREDICTED);
1677 }
1678
1679 TEST_F(ResourcePrefetchPredictorTest,
1680 TestRedirectStatusRedirectWrongPredicted) {
1681 TestRedirectStatusHistogram(
1682 "google.com", "google.fr", "http://google.com?query=cats",
1683 "http://www.google.com?query=cats",
1684 ResourcePrefetchPredictor::RedirectStatus::REDIRECT_WRONG_PREDICTED);
1685 }
1686
1687 TEST_F(ResourcePrefetchPredictorTest,
1688 TestRedirectStatusRedirectCorrectlyPredicted) {
1689 TestRedirectStatusHistogram(
1690 "google.com", "www.google.com", "http://google.com?query=cats",
1691 "http://www.google.com?query=cats",
1692 ResourcePrefetchPredictor::RedirectStatus::REDIRECT_CORRECTLY_PREDICTED);
1693 }
1694
1608 TEST_F(ResourcePrefetchPredictorTest, TestPrefetchingDurationHistogram) { 1695 TEST_F(ResourcePrefetchPredictorTest, TestPrefetchingDurationHistogram) {
1609 // Prefetching duration for an url without resources in the database 1696 // Prefetching duration for an url without resources in the database
1610 // shouldn't be recorded. 1697 // shouldn't be recorded.
1611 const std::string main_frame_url = "http://google.com/?query=cats"; 1698 const std::string main_frame_url = "http://google.com/?query=cats";
1612 predictor_->StartPrefetching(GURL(main_frame_url), PrefetchOrigin::EXTERNAL); 1699 predictor_->StartPrefetching(GURL(main_frame_url), PrefetchOrigin::EXTERNAL);
1613 predictor_->StopPrefetching(GURL(main_frame_url)); 1700 predictor_->StopPrefetching(GURL(main_frame_url));
1614 histogram_tester_->ExpectTotalCount( 1701 histogram_tester_->ExpectTotalCount(
1615 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 0); 1702 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 0);
1616 1703
1617 // Fill the database to record a duration. 1704 // Fill the database to record a duration.
1618 PrefetchData google = CreatePrefetchData("google.com", 1); 1705 PrefetchData google = CreatePrefetchData("google.com", 1);
1619 InitializeResourceData( 1706 InitializeResourceData(
1620 google.add_resources(), "https://cdn.google.com/script.js", 1707 google.add_resources(), "https://cdn.google.com/script.js",
1621 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, net::MEDIUM, false, false); 1708 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, net::MEDIUM, false, false);
1622 predictor_->host_table_cache_->insert( 1709 predictor_->host_table_cache_->insert(
1623 std::make_pair(google.primary_key(), google)); 1710 std::make_pair(google.primary_key(), google));
1624 1711
1625 predictor_->StartPrefetching(GURL(main_frame_url), PrefetchOrigin::EXTERNAL); 1712 predictor_->StartPrefetching(GURL(main_frame_url), PrefetchOrigin::EXTERNAL);
1626 predictor_->StopPrefetching(GURL(main_frame_url)); 1713 predictor_->StopPrefetching(GURL(main_frame_url));
1627 histogram_tester_->ExpectTotalCount( 1714 histogram_tester_->ExpectTotalCount(
1628 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1); 1715 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1);
1629 } 1716 }
1630 1717
1631 } // namespace predictors 1718 } // namespace predictors
OLDNEW
« no previous file with comments | « chrome/browser/predictors/resource_prefetch_predictor.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698