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 1335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1346 PrecacheFetcher precache_fetcher( | 1346 PrecacheFetcher precache_fetcher( |
1347 request_context_.get(), GURL(), std::string(), std::move(more_work), | 1347 request_context_.get(), GURL(), std::string(), std::move(more_work), |
1348 kExperimentID, precache_database_.GetWeakPtr(), task_runner(), | 1348 kExperimentID, precache_database_.GetWeakPtr(), task_runner(), |
1349 &precache_delegate_); | 1349 &precache_delegate_); |
1350 precache_fetcher.Start(); | 1350 precache_fetcher.Start(); |
1351 | 1351 |
1352 base::RunLoop().RunUntilIdle(); | 1352 base::RunLoop().RunUntilIdle(); |
1353 } | 1353 } |
1354 } | 1354 } |
1355 | 1355 |
1356 TEST_F(PrecacheFetcherTest, GloballyRankResources) { | 1356 TEST(PrecacheFetcherResourceWeightTest, Naive) { |
| 1357 ASSERT_EQ( |
| 1358 0, ResourceWeight(PrecacheConfigurationSettings::FUNCTION_NAIVE, 0, 100)); |
| 1359 ASSERT_EQ( |
| 1360 4, ResourceWeight(PrecacheConfigurationSettings::FUNCTION_NAIVE, 1, 4)); |
| 1361 ASSERT_EQ(8, ResourceWeight(PrecacheConfigurationSettings::FUNCTION_NAIVE, |
| 1362 0.5, 16)); |
| 1363 } |
| 1364 |
| 1365 TEST(PrecacheFetcherResourceWeightTest, Geometric) { |
| 1366 ASSERT_EQ(0, ResourceWeight(PrecacheConfigurationSettings::FUNCTION_GEOMETRIC, |
| 1367 0, 100)); |
| 1368 ASSERT_EQ(1, ResourceWeight(PrecacheConfigurationSettings::FUNCTION_GEOMETRIC, |
| 1369 1, 4)); |
| 1370 ASSERT_NEAR(0.9999847, |
| 1371 ResourceWeight(PrecacheConfigurationSettings::FUNCTION_GEOMETRIC, |
| 1372 0.5, 16), |
| 1373 0.0000001); |
| 1374 } |
| 1375 |
| 1376 class PrecacheFetcherGlobalRankingTest |
| 1377 : public PrecacheFetcherTest, |
| 1378 public testing::WithParamInterface< |
| 1379 PrecacheConfigurationSettings::ResourceWeightFunction> {}; |
| 1380 |
| 1381 TEST_P(PrecacheFetcherGlobalRankingTest, GloballyRankResources) { |
1357 SetDefaultFlags(); | 1382 SetDefaultFlags(); |
1358 | 1383 |
1359 const size_t kNumTopHosts = 5; | 1384 const size_t kNumTopHosts = 5; |
1360 const size_t kNumResources = 5; | 1385 const size_t kNumResources = 5; |
1361 | 1386 |
1362 std::vector<GURL> expected_requested_urls; | 1387 std::vector<GURL> expected_requested_urls; |
1363 | 1388 |
1364 PrecacheConfigurationSettings config; | 1389 PrecacheConfigurationSettings config; |
1365 config.set_top_sites_count(kNumTopHosts); | 1390 config.set_top_sites_count(kNumTopHosts); |
1366 config.set_global_ranking(true); | 1391 config.set_global_ranking(true); |
| 1392 config.set_resource_weight_function(GetParam()); |
1367 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(), | 1393 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(), |
1368 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 1394 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
1369 expected_requested_urls.emplace_back(kConfigURL); | 1395 expected_requested_urls.emplace_back(kConfigURL); |
1370 | 1396 |
1371 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( | 1397 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( |
1372 new PrecacheUnfinishedWork()); | 1398 new PrecacheUnfinishedWork()); |
1373 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue()); | 1399 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue()); |
1374 | 1400 |
1375 for (size_t i = 0; i < kNumTopHosts; ++i) { | 1401 for (size_t i = 0; i < kNumTopHosts; ++i) { |
1376 const std::string top_host_url = base::StringPrintf("top-host-%zu.com", i); | 1402 const std::string top_host_url = base::StringPrintf("top-host-%zu.com", i); |
(...skipping 13 matching lines...) Expand all Loading... |
1390 for (size_t j = 0; j < kNumResources; ++j) { | 1416 for (size_t j = 0; j < kNumResources; ++j) { |
1391 const float weight = 1 - static_cast<float>(j) / kNumResources; | 1417 const float weight = 1 - static_cast<float>(j) / kNumResources; |
1392 const std::string resource_url = | 1418 const std::string resource_url = |
1393 base::StringPrintf("http://top-host-%zu.com/resource-%zu-weight-%.1f", | 1419 base::StringPrintf("http://top-host-%zu.com/resource-%zu-weight-%.1f", |
1394 i, j, top_host->visits() * weight); | 1420 i, j, top_host->visits() * weight); |
1395 PrecacheResource* resource = manifest.add_resource(); | 1421 PrecacheResource* resource = manifest.add_resource(); |
1396 resource->set_url(resource_url); | 1422 resource->set_url(resource_url); |
1397 resource->set_weight_ratio(weight); | 1423 resource->set_weight_ratio(weight); |
1398 factory_.SetFakeResponse(GURL(resource_url), "good", net::HTTP_OK, | 1424 factory_.SetFakeResponse(GURL(resource_url), "good", net::HTTP_OK, |
1399 net::URLRequestStatus::SUCCESS); | 1425 net::URLRequestStatus::SUCCESS); |
1400 resources.emplace_back(resource_url, | 1426 resources.emplace_back( |
1401 top_host->visits() * resource->weight_ratio()); | 1427 resource_url, ResourceWeight(GetParam(), weight, top_host->visits())); |
1402 } | 1428 } |
1403 factory_.SetFakeResponse(GURL(kManifestURLPrefix + top_host_url), | 1429 factory_.SetFakeResponse(GURL(kManifestURLPrefix + top_host_url), |
1404 manifest.SerializeAsString(), net::HTTP_OK, | 1430 manifest.SerializeAsString(), net::HTTP_OK, |
1405 net::URLRequestStatus::SUCCESS); | 1431 net::URLRequestStatus::SUCCESS); |
1406 } | 1432 } |
1407 // Sort by descending weight. | 1433 // Sort by descending weight. |
1408 std::stable_sort(resources.begin(), resources.end(), | 1434 std::stable_sort(resources.begin(), resources.end(), |
1409 [](const std::pair<std::string, float>& a, | 1435 [](const std::pair<std::string, float>& a, |
1410 const std::pair<std::string, float>& b) { | 1436 const std::pair<std::string, float>& b) { |
1411 return a.second > b.second; | 1437 return a.second > b.second; |
1412 }); | 1438 }); |
1413 for (const auto& resource : resources) | 1439 for (const auto& resource : resources) |
1414 expected_requested_urls.emplace_back(resource.first); | 1440 expected_requested_urls.emplace_back(resource.first); |
1415 | 1441 |
1416 { | 1442 { |
1417 PrecacheFetcher precache_fetcher( | 1443 PrecacheFetcher precache_fetcher( |
1418 request_context_.get(), GURL(), std::string(), | 1444 request_context_.get(), GURL(), std::string(), |
1419 std::move(unfinished_work), kExperimentID, | 1445 std::move(unfinished_work), kExperimentID, |
1420 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); | 1446 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); |
1421 precache_fetcher.Start(); | 1447 precache_fetcher.Start(); |
1422 base::RunLoop().RunUntilIdle(); | 1448 base::RunLoop().RunUntilIdle(); |
1423 } | 1449 } |
1424 | 1450 |
1425 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 1451 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
1426 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 1452 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
1427 } | 1453 } |
1428 | 1454 |
| 1455 INSTANTIATE_TEST_CASE_P( |
| 1456 PrecacheFetcherGlobalRankingTest, |
| 1457 PrecacheFetcherGlobalRankingTest, |
| 1458 testing::Values(PrecacheConfigurationSettings::FUNCTION_NAIVE, |
| 1459 PrecacheConfigurationSettings::FUNCTION_GEOMETRIC)); |
| 1460 |
1429 TEST_F(PrecacheFetcherTest, GloballyRankResourcesAfterPauseResume) { | 1461 TEST_F(PrecacheFetcherTest, GloballyRankResourcesAfterPauseResume) { |
1430 SetDefaultFlags(); | 1462 SetDefaultFlags(); |
1431 | 1463 |
1432 const size_t kNumTopHosts = 5; | 1464 const size_t kNumTopHosts = 5; |
1433 const size_t kNumResources = 5; | 1465 const size_t kNumResources = 5; |
1434 | 1466 |
1435 std::vector<GURL> expected_requested_urls; | 1467 std::vector<GURL> expected_requested_urls; |
1436 | 1468 |
1437 PrecacheConfigurationSettings config; | 1469 PrecacheConfigurationSettings config; |
1438 config.set_top_sites_count(kNumTopHosts); | 1470 config.set_top_sites_count(kNumTopHosts); |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1837 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 1869 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
1838 | 1870 |
1839 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 1871 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
1840 | 1872 |
1841 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2); | 1873 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2); |
1842 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2); | 1874 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2); |
1843 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2); | 1875 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2); |
1844 } | 1876 } |
1845 | 1877 |
1846 } // namespace precache | 1878 } // namespace precache |
OLD | NEW |