Chromium Code Reviews| 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 <iostream> | 7 #include <iostream> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
| 26 #include "net/url_request/url_request_job.h" | 26 #include "net/url_request/url_request_job.h" |
| 27 #include "net/url_request/url_request_test_util.h" | 27 #include "net/url_request/url_request_test_util.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" | 28 #include "testing/gmock/include/gmock/gmock.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 30 |
| 31 using testing::ContainerEq; | 31 using testing::ContainerEq; |
| 32 using testing::Pointee; | 32 using testing::Pointee; |
| 33 using testing::SetArgPointee; | 33 using testing::SetArgPointee; |
| 34 using testing::StrictMock; | 34 using testing::StrictMock; |
| 35 using testing::UnorderedElementsAre; | |
| 35 | 36 |
| 36 namespace predictors { | 37 namespace predictors { |
| 37 | 38 |
| 38 typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary; | 39 typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary; |
| 39 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; | 40 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; |
| 40 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; | 41 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; |
| 41 | 42 |
| 42 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( | 43 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( |
| 43 const char* headers) { | 44 const char* headers) { |
| 44 return make_scoped_refptr(new net::HttpResponseHeaders( | 45 return make_scoped_refptr(new net::HttpResponseHeaders( |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 lhs.was_cached == rhs.was_cached; | 251 lhs.was_cached == rhs.was_cached; |
| 251 } | 252 } |
| 252 | 253 |
| 253 void ResetPredictor() { | 254 void ResetPredictor() { |
| 254 ResourcePrefetchPredictorConfig config; | 255 ResourcePrefetchPredictorConfig config; |
| 255 config.max_urls_to_track = 3; | 256 config.max_urls_to_track = 3; |
| 256 config.max_hosts_to_track = 2; | 257 config.max_hosts_to_track = 2; |
| 257 config.min_url_visit_count = 2; | 258 config.min_url_visit_count = 2; |
| 258 config.max_resources_per_entry = 4; | 259 config.max_resources_per_entry = 4; |
| 259 config.max_consecutive_misses = 2; | 260 config.max_consecutive_misses = 2; |
| 261 config.min_resource_confidence_to_trigger_prefetch = 0.5; | |
| 260 | 262 |
| 261 // TODO(shishir): Enable the prefetching mode in the tests. | 263 // TODO(shishir): Enable the prefetching mode in the tests. |
| 262 config.mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; | 264 config.mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; |
| 263 config.mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING; | 265 config.mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING; |
| 264 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get())); | 266 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get())); |
| 265 predictor_->set_mock_tables(mock_tables_); | 267 predictor_->set_mock_tables(mock_tables_); |
| 266 } | 268 } |
| 267 | 269 |
| 268 void InitializeSampleData(); | 270 void InitializeSampleData(); |
| 269 | 271 |
| (...skipping 1049 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1319 "Cache-Control: no-cache\n"); | 1321 "Cache-Control: no-cache\n"); |
| 1320 url_request_job_factory_.set_response_info(response_info); | 1322 url_request_job_factory_.set_response_info(response_info); |
| 1321 std::unique_ptr<net::URLRequest> request_etag = | 1323 std::unique_ptr<net::URLRequest> request_etag = |
| 1322 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | 1324 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, |
| 1323 content::RESOURCE_TYPE_PREFETCH, 1, 1, true); | 1325 content::RESOURCE_TYPE_PREFETCH, 1, 1, true); |
| 1324 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary)); | 1326 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary)); |
| 1325 EXPECT_TRUE(summary.has_validators); | 1327 EXPECT_TRUE(summary.has_validators); |
| 1326 EXPECT_TRUE(summary.always_revalidate); | 1328 EXPECT_TRUE(summary.always_revalidate); |
| 1327 } | 1329 } |
| 1328 | 1330 |
| 1331 TEST_F(ResourcePrefetchPredictorTest, PopulatePrefetcherRequest) { | |
| 1332 // The data that will be used in populating. | |
| 1333 PrefetchData google = CreatePrefetchData("http://www.google.com/", 1); | |
| 1334 InitializeResourceData(google.add_resources(), "http://google.com/image1.png", | |
| 1335 content::RESOURCE_TYPE_IMAGE, 10, 0, 0, 2.2, | |
| 1336 net::MEDIUM, false, false); // good | |
| 1337 InitializeResourceData(google.add_resources(), "http://google.com/style1.css", | |
| 1338 content::RESOURCE_TYPE_STYLESHEET, 2, 2, 1, 1.0, | |
| 1339 net::MEDIUM, false, false); // still good | |
| 1340 InitializeResourceData(google.add_resources(), "http://google.com/script3.js", | |
| 1341 content::RESOURCE_TYPE_SCRIPT, 1, 0, 1, 2.1, | |
| 1342 net::MEDIUM, false, false); // bad - not enough hits | |
| 1343 InitializeResourceData( | |
| 1344 google.add_resources(), "http://google.com/script4.js", | |
| 1345 content::RESOURCE_TYPE_SCRIPT, 4, 5, 0, 2.1, net::MEDIUM, false, | |
| 1346 false); // bad - more misses than hits (min_confidence = 0.5) | |
| 1347 | |
| 1348 // The data to be sure that other PrefetchData won't affect. | |
|
Benoit L
2016/10/11 13:37:11
nit: won't be affected.
| |
| 1349 PrefetchData twitter = CreatePrefetchData("http://twitter.com", 2); | |
| 1350 InitializeResourceData( | |
| 1351 twitter.add_resources(), "http://twitter.com/image.jpg", | |
| 1352 content::RESOURCE_TYPE_IMAGE, 10, 0, 0, 1.0, net::MEDIUM, false, false); | |
| 1353 | |
| 1354 // The data to check negative result. | |
| 1355 PrefetchData nyt = CreatePrefetchData("http://nyt.com", 3); | |
| 1356 InitializeResourceData(nyt.add_resources(), "http://nyt.com/old_script.js", | |
| 1357 content::RESOURCE_TYPE_SCRIPT, 5, 7, 7, 1.0, | |
| 1358 net::MEDIUM, false, false); | |
| 1359 | |
| 1360 PrefetchDataMap test_data; | |
| 1361 test_data.insert(std::make_pair(google.primary_key(), google)); | |
| 1362 test_data.insert(std::make_pair(twitter.primary_key(), twitter)); | |
| 1363 test_data.insert(std::make_pair(nyt.primary_key(), nyt)); | |
| 1364 | |
| 1365 std::vector<GURL> urls; | |
| 1366 EXPECT_TRUE(predictor_->PopulatePrefetcherRequest(google.primary_key(), | |
| 1367 test_data, &urls)); | |
| 1368 EXPECT_THAT(urls, UnorderedElementsAre(GURL("http://google.com/image1.png"), | |
| 1369 GURL("http://google.com/style1.css"))); | |
| 1370 | |
| 1371 urls.clear(); | |
| 1372 EXPECT_FALSE(predictor_->PopulatePrefetcherRequest(nyt.primary_key(), | |
| 1373 test_data, &urls)); | |
| 1374 EXPECT_TRUE(urls.empty()); | |
| 1375 | |
| 1376 urls.clear(); | |
| 1377 EXPECT_FALSE(predictor_->PopulatePrefetcherRequest("http://404.com", | |
| 1378 test_data, &urls)); | |
| 1379 EXPECT_TRUE(urls.empty()); | |
| 1380 } | |
| 1381 | |
| 1382 TEST_F(ResourcePrefetchPredictorTest, GetRedirectEndpoint) { | |
| 1383 // The data to be requested for the most confident endpoint. | |
| 1384 RedirectData google = CreateRedirectData("http://google.com/", 1); | |
| 1385 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.com", | |
| 1386 10, 0, 0); | |
| 1387 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.fr", | |
| 1388 10, 1, 0); | |
| 1389 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.ws", | |
| 1390 20, 20, 0); | |
| 1391 | |
| 1392 // The data to be ignored. | |
| 1393 RedirectData gogle = CreateRedirectData("http://gogle.com", 2); | |
| 1394 InitializeRedirectStat(gogle.add_redirect_endpoints(), "https://google.com", | |
| 1395 100, 0, 0); | |
| 1396 | |
| 1397 // The data to check negative result. | |
| 1398 RedirectData facebook = CreateRedirectData("http://fb.com/", 3); | |
| 1399 InitializeRedirectStat(facebook.add_redirect_endpoints(), | |
| 1400 "http://facebook.com", 5, 5, | |
| 1401 0); // not enough confidence | |
| 1402 | |
| 1403 RedirectDataMap data_map; | |
| 1404 data_map.insert(std::make_pair(google.primary_key(), google)); | |
| 1405 data_map.insert(std::make_pair(gogle.primary_key(), gogle)); | |
| 1406 data_map.insert(std::make_pair(facebook.primary_key(), facebook)); | |
| 1407 | |
| 1408 std::string redirect_endpoint; | |
| 1409 EXPECT_TRUE(predictor_->GetRedirectEndpoint("http://google.com/", data_map, | |
| 1410 &redirect_endpoint)); | |
| 1411 EXPECT_EQ(redirect_endpoint, "https://google.com"); | |
| 1412 | |
| 1413 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://fb.com", data_map, | |
| 1414 &redirect_endpoint)); | |
| 1415 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://404.com", data_map, | |
| 1416 &redirect_endpoint)); | |
| 1417 } | |
| 1418 | |
| 1419 TEST_F(ResourcePrefetchPredictorTest, GetPrefetchData) { | |
| 1420 const GURL main_frame_url("http://google.com/?query=cats"); | |
| 1421 std::vector<GURL> urls; | |
| 1422 // No prefetch data. | |
| 1423 EXPECT_FALSE(predictor_->GetPrefetchData(main_frame_url, &urls)); | |
| 1424 | |
| 1425 // Add a resource associated with the main frame host. | |
| 1426 PrefetchData google_host = CreatePrefetchData("google.com", 1); | |
| 1427 const std::string script_url = "https://cdn.google.com/script.js"; | |
| 1428 InitializeResourceData(google_host.add_resources(), script_url, | |
| 1429 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, | |
| 1430 net::MEDIUM, false, false); | |
| 1431 predictor_->host_table_cache_->insert( | |
| 1432 std::make_pair(google_host.primary_key(), google_host)); | |
| 1433 | |
| 1434 urls.clear(); | |
| 1435 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); | |
| 1436 EXPECT_THAT(urls, UnorderedElementsAre(GURL(script_url))); | |
| 1437 | |
| 1438 // Add a resource associated with the main frame url. | |
| 1439 PrefetchData google_url = | |
| 1440 CreatePrefetchData("http://google.com/?query=cats", 2); | |
| 1441 const std::string image_url = "https://cdn.google.com/image.png"; | |
| 1442 InitializeResourceData(google_url.add_resources(), image_url, | |
| 1443 content::RESOURCE_TYPE_IMAGE, 10, 0, 1, 2.1, | |
| 1444 net::MEDIUM, false, false); | |
| 1445 predictor_->url_table_cache_->insert( | |
| 1446 std::make_pair(google_url.primary_key(), google_url)); | |
| 1447 | |
| 1448 urls.clear(); | |
| 1449 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); | |
| 1450 EXPECT_THAT(urls, UnorderedElementsAre(GURL(image_url))); | |
| 1451 | |
| 1452 // Add host-based redirect. | |
| 1453 RedirectData host_redirect = CreateRedirectData("google.com", 3); | |
| 1454 InitializeRedirectStat(host_redirect.add_redirect_endpoints(), | |
| 1455 "www.google.com", 10, 0, 0); | |
| 1456 predictor_->host_redirect_table_cache_->insert( | |
| 1457 std::make_pair(host_redirect.primary_key(), host_redirect)); | |
| 1458 | |
| 1459 // Nothing changed: new redirect endpoint doesn't have any associated | |
| 1460 // resources | |
| 1461 urls.clear(); | |
| 1462 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); | |
| 1463 EXPECT_THAT(urls, UnorderedElementsAre(GURL(image_url))); | |
| 1464 | |
| 1465 // Add a resource associated with host redirect endpoint. | |
| 1466 PrefetchData www_google_host = CreatePrefetchData("www.google.com", 4); | |
| 1467 const std::string style_url = "https://cdn.google.com/style.css"; | |
| 1468 InitializeResourceData(www_google_host.add_resources(), style_url, | |
| 1469 content::RESOURCE_TYPE_STYLESHEET, 10, 0, 1, 2.1, | |
| 1470 net::MEDIUM, false, false); | |
| 1471 predictor_->host_table_cache_->insert( | |
| 1472 std::make_pair(www_google_host.primary_key(), www_google_host)); | |
| 1473 | |
| 1474 urls.clear(); | |
| 1475 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); | |
| 1476 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url))); | |
| 1477 | |
| 1478 // Add url-based redirect. | |
| 1479 RedirectData url_redirect = | |
| 1480 CreateRedirectData("http://google.com/?query=cats", 5); | |
| 1481 InitializeRedirectStat(url_redirect.add_redirect_endpoints(), | |
| 1482 "https://www.google.com/?query=cats", 10, 0, 0); | |
| 1483 predictor_->url_redirect_table_cache_->insert( | |
| 1484 std::make_pair(url_redirect.primary_key(), url_redirect)); | |
| 1485 | |
| 1486 // Url redirect endpoint doesn't have associated resources. | |
| 1487 urls.clear(); | |
| 1488 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); | |
| 1489 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url))); | |
| 1490 | |
| 1491 // Add a resource associated with url redirect endpoint. | |
| 1492 PrefetchData www_google_url = | |
| 1493 CreatePrefetchData("https://www.google.com/?query=cats", 4); | |
| 1494 const std::string font_url = "https://cdn.google.com/comic-sans-ms.woff"; | |
| 1495 InitializeResourceData(www_google_url.add_resources(), font_url, | |
| 1496 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1, | |
| 1497 net::MEDIUM, false, false); | |
| 1498 predictor_->url_table_cache_->insert( | |
| 1499 std::make_pair(www_google_url.primary_key(), www_google_url)); | |
| 1500 | |
| 1501 urls.clear(); | |
| 1502 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); | |
| 1503 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url))); | |
| 1504 } | |
| 1505 | |
| 1329 } // namespace predictors | 1506 } // namespace predictors |
| OLD | NEW |