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 1073 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1084 // PrecacheFetcher::Fetcher constructor. | 1084 // PrecacheFetcher::Fetcher constructor. |
1085 // | 1085 // |
1086 // TODO(twifkak): Port these tests from FakeURLFetcherFactory to | 1086 // TODO(twifkak): Port these tests from FakeURLFetcherFactory to |
1087 // MockURLFetcherFactory or EmbeddedTestServer, and add a test that fetches are | 1087 // MockURLFetcherFactory or EmbeddedTestServer, and add a test that fetches are |
1088 // cancelled midstream. | 1088 // cancelled midstream. |
1089 | 1089 |
1090 TEST_F(PrecacheFetcherTest, MaxBytesTotal) { | 1090 TEST_F(PrecacheFetcherTest, MaxBytesTotal) { |
1091 SetDefaultFlags(); | 1091 SetDefaultFlags(); |
1092 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( | 1092 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( |
1093 new PrecacheUnfinishedWork()); | 1093 new PrecacheUnfinishedWork()); |
1094 unfinished_work->add_top_host()->set_hostname("good-manifest.com"); | 1094 auto* top_host = unfinished_work->add_top_host(); |
1095 top_host->set_hostname("good-manifest.com"); | |
1096 top_host->set_visits(1); | |
1095 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue()); | 1097 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue()); |
1096 | 1098 |
1097 // Should be greater than kMaxParallelFetches, so that we can observe | 1099 // Should be greater than kMaxParallelFetches, so that we can observe |
1098 // PrecacheFetcher not fetching the remaining resources after max bytes is | 1100 // PrecacheFetcher not fetching the remaining resources after max bytes is |
1099 // exceeded. | 1101 // exceeded. |
1100 const size_t kNumResources = kMaxParallelFetches + 5; | 1102 const size_t kNumResources = kMaxParallelFetches + 5; |
1101 // Should be smaller than kNumResources - kMaxParallelFetches, such that the | 1103 // Should be smaller than kNumResources - kMaxParallelFetches, such that the |
1102 // max bytes is guaranteed to be exceeded before all fetches have been | 1104 // max bytes is guaranteed to be exceeded before all fetches have been |
1103 // requested. In this case, after 3 fetches have been completed, 3 more are | 1105 // requested. In this case, after 3 fetches have been completed, 3 more are |
1104 // added to the fetcher pool, but 2 out of 5 still remain. | 1106 // added to the fetcher pool, but 2 out of 5 still remain. |
1105 const size_t kResourcesWithinMax = 3; | 1107 const size_t kResourcesWithinMax = 3; |
1106 // Should be big enough that the size of the config, manifest, and HTTP | 1108 // Should be big enough that the size of the config, manifest, and HTTP |
1107 // headers are negligible for max bytes computation. | 1109 // headers are negligible for max bytes computation. |
1108 const size_t kBytesPerResource = 500; | 1110 const size_t kBytesPerResource = 500; |
1109 const size_t kMaxBytesTotal = kResourcesWithinMax * kBytesPerResource; | 1111 const size_t kMaxBytesTotal = kResourcesWithinMax * kBytesPerResource; |
1110 | 1112 |
1111 PrecacheConfigurationSettings config; | 1113 PrecacheConfigurationSettings config; |
1112 config.set_max_bytes_total(kMaxBytesTotal); | 1114 config.set_max_bytes_total(kMaxBytesTotal); |
1115 config.set_global_ranking(true); | |
1113 | 1116 |
1114 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(), | 1117 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(), |
1115 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 1118 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
1116 | 1119 |
1117 PrecacheManifest good_manifest; | 1120 PrecacheManifest good_manifest; |
1118 for (size_t i = 0; i < kNumResources; ++i) { | 1121 for (size_t i = 0; i < kNumResources; ++i) { |
1119 const std::string url = "http://good-manifest.com/" + std::to_string(i); | 1122 const std::string url = "http://good-manifest.com/" + std::to_string(i); |
1120 good_manifest.add_resource()->set_url(url); | 1123 auto* resource = good_manifest.add_resource(); |
1124 resource->set_url(url); | |
1125 resource->set_weight_ratio(static_cast<double>(i) / kNumResources); | |
1121 factory_.SetFakeResponse(GURL(url), std::string(kBytesPerResource, '.'), | 1126 factory_.SetFakeResponse(GURL(url), std::string(kBytesPerResource, '.'), |
1122 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 1127 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
1123 } | 1128 } |
1124 | 1129 |
1125 factory_.SetFakeResponse(GURL(kGoodManifestURL), | 1130 factory_.SetFakeResponse(GURL(kGoodManifestURL), |
1126 good_manifest.SerializeAsString(), net::HTTP_OK, | 1131 good_manifest.SerializeAsString(), net::HTTP_OK, |
1127 net::URLRequestStatus::SUCCESS); | 1132 net::URLRequestStatus::SUCCESS); |
1128 | 1133 |
1129 base::HistogramTester histogram; | 1134 base::HistogramTester histogram; |
1130 | 1135 |
1131 { | 1136 { |
1132 PrecacheFetcher precache_fetcher( | 1137 PrecacheFetcher precache_fetcher( |
1133 request_context_.get(), GURL(), std::string(), | 1138 request_context_.get(), GURL(), std::string(), |
1134 std::move(unfinished_work), kExperimentID, | 1139 std::move(unfinished_work), kExperimentID, |
1135 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); | 1140 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); |
1136 precache_fetcher.Start(); | 1141 precache_fetcher.Start(); |
1137 | 1142 |
1138 base::RunLoop().RunUntilIdle(); | 1143 base::RunLoop().RunUntilIdle(); |
1139 } | 1144 } |
1140 | 1145 |
1141 // Fetcher should request config, manifest, and all but 3 resources. | 1146 // Fetcher should request config, manifest, and all but 3 resources. |
1142 // TODO(twifkak): I expected all but 2 resources; this result is surprising. | 1147 EXPECT_EQ(kNumResources - 2, url_callback_.requested_urls().size()); |
jamartin
2017/02/24 21:23:29
If it is config (1), manifest (1) and all but 3 re
twifkak
2017/02/25 01:13:39
Good point. Added a comment, and clarified the mat
| |
1143 // Figure it out and explain it here. | |
1144 EXPECT_EQ(kNumResources - 1, url_callback_.requested_urls().size()); | |
1145 | 1148 |
1146 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 1149 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
1147 | 1150 |
1148 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 1); | 1151 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 1); |
1149 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1); | 1152 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1); |
1153 | |
1154 const double expected_min_weight = | |
1155 good_manifest.resource(kNumResources - 3).weight_ratio(); | |
jamartin
2017/02/24 21:23:29
good_manifest.resource.weight_ratio is the weight
twifkak
2017/02/25 01:13:39
Per line 1096, top_host->visits() == 1, up until l
| |
1156 histogram.ExpectBucketCount("Precache.Fetch.MinWeight", | |
1157 1000.0 * expected_min_weight, 1); | |
1150 } | 1158 } |
1151 | 1159 |
1152 // Tests the parallel fetch behaviour when more precache resource and manifest | 1160 // Tests the parallel fetch behaviour when more precache resource and manifest |
1153 // requests are available than the maximum capacity of fetcher pool. | 1161 // requests are available than the maximum capacity of fetcher pool. |
1154 TEST_F(PrecacheFetcherTest, FetcherPoolMaxLimitReached) { | 1162 TEST_F(PrecacheFetcherTest, FetcherPoolMaxLimitReached) { |
1155 SetDefaultFlags(); | 1163 SetDefaultFlags(); |
1156 | 1164 |
1157 const size_t kNumTopHosts = 5; | 1165 const size_t kNumTopHosts = 5; |
1158 const size_t kNumResources = kMaxParallelFetches + 5; | 1166 const size_t kNumResources = kMaxParallelFetches + 5; |
1159 | 1167 |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1611 EXPECT_TRUE(cancelled_work->top_host().empty()); | 1619 EXPECT_TRUE(cancelled_work->top_host().empty()); |
1612 EXPECT_EQ(kNumTopHosts * kNumResources, | 1620 EXPECT_EQ(kNumTopHosts * kNumResources, |
1613 static_cast<size_t>(cancelled_work->resource().size())); | 1621 static_cast<size_t>(cancelled_work->resource().size())); |
1614 EXPECT_FALSE(precache_delegate_.was_on_done_called()); | 1622 EXPECT_FALSE(precache_delegate_.was_on_done_called()); |
1615 | 1623 |
1616 url_callback_.clear_requested_urls(); | 1624 url_callback_.clear_requested_urls(); |
1617 | 1625 |
1618 // Continuing with the precache should fetch all resources, as the previous | 1626 // Continuing with the precache should fetch all resources, as the previous |
1619 // run was cancelled before any finished. They should be fetched in global | 1627 // run was cancelled before any finished. They should be fetched in global |
1620 // ranking order. | 1628 // ranking order. |
1629 | |
1630 base::HistogramTester histogram; | |
1631 | |
1621 { | 1632 { |
1622 PrecacheFetcher precache_fetcher( | 1633 PrecacheFetcher precache_fetcher( |
1623 request_context_.get(), GURL(), std::string(), | 1634 request_context_.get(), GURL(), std::string(), |
1624 std::move(cancelled_work), kExperimentID, | 1635 std::move(cancelled_work), kExperimentID, |
1625 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); | 1636 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); |
1626 LOG(INFO) << "Resuming prefetch."; | 1637 LOG(INFO) << "Resuming prefetch."; |
1627 precache_fetcher.Start(); | 1638 precache_fetcher.Start(); |
1628 base::RunLoop().RunUntilIdle(); | 1639 base::RunLoop().RunUntilIdle(); |
1629 } | 1640 } |
1630 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 1641 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
1631 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 1642 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
1643 | |
1644 histogram.ExpectBucketCount("Precache.Fetch.MinWeight", | |
1645 1000.0 * resources.back().second, 1); | |
1632 } | 1646 } |
1633 | 1647 |
1634 TEST_F(PrecacheFetcherTest, MaxTotalResources) { | 1648 TEST_F(PrecacheFetcherTest, MaxTotalResources) { |
1635 SetDefaultFlags(); | 1649 SetDefaultFlags(); |
1636 | 1650 |
1637 const size_t kNumResources = 5; | 1651 const size_t kNumResources = 5; |
1638 | 1652 |
1639 std::vector<GURL> expected_requested_urls; | 1653 std::vector<GURL> expected_requested_urls; |
1640 | 1654 |
1641 PrecacheConfigurationSettings config; | 1655 PrecacheConfigurationSettings config; |
(...skipping 25 matching lines...) Expand all Loading... | |
1667 resource->set_weight_ratio(weight); | 1681 resource->set_weight_ratio(weight); |
1668 factory_.SetFakeResponse(GURL(resource_url), "good", net::HTTP_OK, | 1682 factory_.SetFakeResponse(GURL(resource_url), "good", net::HTTP_OK, |
1669 net::URLRequestStatus::SUCCESS); | 1683 net::URLRequestStatus::SUCCESS); |
1670 if (i < config.total_resources_count()) | 1684 if (i < config.total_resources_count()) |
1671 expected_requested_urls.emplace_back(resource_url); | 1685 expected_requested_urls.emplace_back(resource_url); |
1672 } | 1686 } |
1673 factory_.SetFakeResponse(GURL(kManifestURLPrefix + top_host->hostname()), | 1687 factory_.SetFakeResponse(GURL(kManifestURLPrefix + top_host->hostname()), |
1674 manifest.SerializeAsString(), net::HTTP_OK, | 1688 manifest.SerializeAsString(), net::HTTP_OK, |
1675 net::URLRequestStatus::SUCCESS); | 1689 net::URLRequestStatus::SUCCESS); |
1676 | 1690 |
1691 base::HistogramTester histogram; | |
1692 | |
1677 { | 1693 { |
1678 PrecacheFetcher precache_fetcher( | 1694 PrecacheFetcher precache_fetcher( |
1679 request_context_.get(), GURL(), std::string(), | 1695 request_context_.get(), GURL(), std::string(), |
1680 std::move(unfinished_work), kExperimentID, | 1696 std::move(unfinished_work), kExperimentID, |
1681 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); | 1697 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); |
1682 precache_fetcher.Start(); | 1698 precache_fetcher.Start(); |
1683 base::RunLoop().RunUntilIdle(); | 1699 base::RunLoop().RunUntilIdle(); |
1684 } | 1700 } |
1685 | 1701 |
1686 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 1702 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
1687 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 1703 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
1704 | |
1705 const float expected_min_weight = | |
1706 manifest.resource(config.total_resources_count() - 1).weight_ratio(); | |
1707 histogram.ExpectUniqueSample("Precache.Fetch.MinWeight", | |
1708 1000.0 * expected_min_weight, 1); | |
1688 } | 1709 } |
1689 | 1710 |
1690 TEST_F(PrecacheFetcherTest, MinWeight) { | 1711 TEST_F(PrecacheFetcherTest, MinWeight) { |
1691 SetDefaultFlags(); | 1712 SetDefaultFlags(); |
1692 | 1713 |
1693 const size_t kNumResources = 5; | 1714 const size_t kNumResources = 5; |
1694 | 1715 |
1695 std::vector<GURL> expected_requested_urls; | 1716 std::vector<GURL> expected_requested_urls; |
1696 | 1717 |
1697 PrecacheConfigurationSettings config; | 1718 PrecacheConfigurationSettings config; |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1937 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 1958 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
1938 | 1959 |
1939 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 1960 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
1940 | 1961 |
1941 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2); | 1962 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2); |
1942 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2); | 1963 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2); |
1943 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2); | 1964 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2); |
1944 } | 1965 } |
1945 | 1966 |
1946 } // namespace precache | 1967 } // namespace precache |
OLD | NEW |