OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/precache/core/precache_fetcher.h" | 5 #include "components/precache/core/precache_fetcher.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <cstring> | 9 #include <cstring> |
10 #include <memory> | 10 #include <memory> |
(...skipping 1403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1414 PrecacheFetcher precache_fetcher( | 1414 PrecacheFetcher precache_fetcher( |
1415 request_context_.get(), GURL(), std::string(), std::move(more_work), | 1415 request_context_.get(), GURL(), std::string(), std::move(more_work), |
1416 kExperimentID, precache_database_.GetWeakPtr(), task_runner(), | 1416 kExperimentID, precache_database_.GetWeakPtr(), task_runner(), |
1417 &precache_delegate_); | 1417 &precache_delegate_); |
1418 precache_fetcher.Start(); | 1418 precache_fetcher.Start(); |
1419 | 1419 |
1420 base::RunLoop().RunUntilIdle(); | 1420 base::RunLoop().RunUntilIdle(); |
1421 } | 1421 } |
1422 } | 1422 } |
1423 | 1423 |
1424 TEST_F(PrecacheFetcherTest, GloballyRankResources) { | 1424 TEST(PrecacheFetcherResourceWeightTest, Naive) { |
| 1425 ASSERT_EQ( |
| 1426 0, ResourceWeight(PrecacheConfigurationSettings::FUNCTION_NAIVE, 0, 100)); |
| 1427 ASSERT_EQ( |
| 1428 4, ResourceWeight(PrecacheConfigurationSettings::FUNCTION_NAIVE, 1, 4)); |
| 1429 ASSERT_EQ(8, ResourceWeight(PrecacheConfigurationSettings::FUNCTION_NAIVE, |
| 1430 0.5, 16)); |
| 1431 } |
| 1432 |
| 1433 TEST(PrecacheFetcherResourceWeightTest, Geometric) { |
| 1434 ASSERT_EQ(0, ResourceWeight(PrecacheConfigurationSettings::FUNCTION_GEOMETRIC, |
| 1435 0, 100)); |
| 1436 ASSERT_EQ(1, ResourceWeight(PrecacheConfigurationSettings::FUNCTION_GEOMETRIC, |
| 1437 1, 4)); |
| 1438 ASSERT_NEAR(0.9999847, |
| 1439 ResourceWeight(PrecacheConfigurationSettings::FUNCTION_GEOMETRIC, |
| 1440 0.5, 16), |
| 1441 0.0000001); |
| 1442 } |
| 1443 |
| 1444 class PrecacheFetcherGlobalRankingTest |
| 1445 : public PrecacheFetcherTest, |
| 1446 public testing::WithParamInterface< |
| 1447 PrecacheConfigurationSettings::ResourceWeightFunction> {}; |
| 1448 |
| 1449 TEST_P(PrecacheFetcherGlobalRankingTest, GloballyRankResources) { |
1425 SetDefaultFlags(); | 1450 SetDefaultFlags(); |
1426 | 1451 |
1427 const size_t kNumTopHosts = 5; | 1452 const size_t kNumTopHosts = 5; |
1428 const size_t kNumResources = 5; | 1453 const size_t kNumResources = 5; |
1429 | 1454 |
1430 std::vector<GURL> expected_requested_urls; | 1455 std::vector<GURL> expected_requested_urls; |
1431 | 1456 |
1432 PrecacheConfigurationSettings config; | 1457 PrecacheConfigurationSettings config; |
1433 config.set_top_sites_count(kNumTopHosts); | 1458 config.set_top_sites_count(kNumTopHosts); |
1434 config.set_global_ranking(true); | 1459 config.set_global_ranking(true); |
| 1460 config.set_resource_weight_function(GetParam()); |
1435 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(), | 1461 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(), |
1436 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 1462 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
1437 expected_requested_urls.emplace_back(kConfigURL); | 1463 expected_requested_urls.emplace_back(kConfigURL); |
1438 | 1464 |
1439 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( | 1465 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( |
1440 new PrecacheUnfinishedWork()); | 1466 new PrecacheUnfinishedWork()); |
1441 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue()); | 1467 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue()); |
1442 | 1468 |
1443 for (size_t i = 0; i < kNumTopHosts; ++i) { | 1469 for (size_t i = 0; i < kNumTopHosts; ++i) { |
1444 const std::string top_host_url = base::StringPrintf("top-host-%zu.com", i); | 1470 const std::string top_host_url = base::StringPrintf("top-host-%zu.com", i); |
(...skipping 13 matching lines...) Expand all Loading... |
1458 for (size_t j = 0; j < kNumResources; ++j) { | 1484 for (size_t j = 0; j < kNumResources; ++j) { |
1459 const float weight = 1 - static_cast<float>(j) / kNumResources; | 1485 const float weight = 1 - static_cast<float>(j) / kNumResources; |
1460 const std::string resource_url = | 1486 const std::string resource_url = |
1461 base::StringPrintf("http://top-host-%zu.com/resource-%zu-weight-%.1f", | 1487 base::StringPrintf("http://top-host-%zu.com/resource-%zu-weight-%.1f", |
1462 i, j, top_host->visits() * weight); | 1488 i, j, top_host->visits() * weight); |
1463 PrecacheResource* resource = manifest.add_resource(); | 1489 PrecacheResource* resource = manifest.add_resource(); |
1464 resource->set_url(resource_url); | 1490 resource->set_url(resource_url); |
1465 resource->set_weight_ratio(weight); | 1491 resource->set_weight_ratio(weight); |
1466 factory_.SetFakeResponse(GURL(resource_url), "good", net::HTTP_OK, | 1492 factory_.SetFakeResponse(GURL(resource_url), "good", net::HTTP_OK, |
1467 net::URLRequestStatus::SUCCESS); | 1493 net::URLRequestStatus::SUCCESS); |
1468 resources.emplace_back(resource_url, | 1494 resources.emplace_back( |
1469 top_host->visits() * resource->weight_ratio()); | 1495 resource_url, ResourceWeight(GetParam(), weight, top_host->visits())); |
1470 } | 1496 } |
1471 factory_.SetFakeResponse(GURL(kManifestURLPrefix + top_host_url), | 1497 factory_.SetFakeResponse(GURL(kManifestURLPrefix + top_host_url), |
1472 manifest.SerializeAsString(), net::HTTP_OK, | 1498 manifest.SerializeAsString(), net::HTTP_OK, |
1473 net::URLRequestStatus::SUCCESS); | 1499 net::URLRequestStatus::SUCCESS); |
1474 } | 1500 } |
1475 // Sort by descending weight. | 1501 // Sort by descending weight. |
1476 std::stable_sort(resources.begin(), resources.end(), | 1502 std::stable_sort(resources.begin(), resources.end(), |
1477 [](const std::pair<std::string, float>& a, | 1503 [](const std::pair<std::string, float>& a, |
1478 const std::pair<std::string, float>& b) { | 1504 const std::pair<std::string, float>& b) { |
1479 return a.second > b.second; | 1505 return a.second > b.second; |
1480 }); | 1506 }); |
1481 for (const auto& resource : resources) | 1507 for (const auto& resource : resources) |
1482 expected_requested_urls.emplace_back(resource.first); | 1508 expected_requested_urls.emplace_back(resource.first); |
1483 | 1509 |
1484 { | 1510 { |
1485 PrecacheFetcher precache_fetcher( | 1511 PrecacheFetcher precache_fetcher( |
1486 request_context_.get(), GURL(), std::string(), | 1512 request_context_.get(), GURL(), std::string(), |
1487 std::move(unfinished_work), kExperimentID, | 1513 std::move(unfinished_work), kExperimentID, |
1488 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); | 1514 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); |
1489 precache_fetcher.Start(); | 1515 precache_fetcher.Start(); |
1490 base::RunLoop().RunUntilIdle(); | 1516 base::RunLoop().RunUntilIdle(); |
1491 } | 1517 } |
1492 | 1518 |
1493 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 1519 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
1494 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 1520 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
1495 } | 1521 } |
1496 | 1522 |
| 1523 INSTANTIATE_TEST_CASE_P( |
| 1524 PrecacheFetcherGlobalRankingTest, |
| 1525 PrecacheFetcherGlobalRankingTest, |
| 1526 testing::Values(PrecacheConfigurationSettings::FUNCTION_NAIVE, |
| 1527 PrecacheConfigurationSettings::FUNCTION_GEOMETRIC)); |
| 1528 |
1497 TEST_F(PrecacheFetcherTest, GloballyRankResourcesAfterPauseResume) { | 1529 TEST_F(PrecacheFetcherTest, GloballyRankResourcesAfterPauseResume) { |
1498 SetDefaultFlags(); | 1530 SetDefaultFlags(); |
1499 | 1531 |
1500 const size_t kNumTopHosts = 5; | 1532 const size_t kNumTopHosts = 5; |
1501 const size_t kNumResources = 5; | 1533 const size_t kNumResources = 5; |
1502 | 1534 |
1503 std::vector<GURL> expected_requested_urls; | 1535 std::vector<GURL> expected_requested_urls; |
1504 | 1536 |
1505 PrecacheConfigurationSettings config; | 1537 PrecacheConfigurationSettings config; |
1506 config.set_top_sites_count(kNumTopHosts); | 1538 config.set_top_sites_count(kNumTopHosts); |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1905 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 1937 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
1906 | 1938 |
1907 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 1939 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
1908 | 1940 |
1909 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2); | 1941 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2); |
1910 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2); | 1942 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2); |
1911 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2); | 1943 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2); |
1912 } | 1944 } |
1913 | 1945 |
1914 } // namespace precache | 1946 } // namespace precache |
OLD | NEW |