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 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 profile_->BlockUntilHistoryProcessesPendingRequests(); | 231 profile_->BlockUntilHistoryProcessesPendingRequests(); |
232 } | 232 } |
233 | 233 |
234 void ResetPredictor() { | 234 void ResetPredictor() { |
235 ResourcePrefetchPredictorConfig config; | 235 ResourcePrefetchPredictorConfig config; |
236 config.max_urls_to_track = 3; | 236 config.max_urls_to_track = 3; |
237 config.max_hosts_to_track = 2; | 237 config.max_hosts_to_track = 2; |
238 config.min_url_visit_count = 2; | 238 config.min_url_visit_count = 2; |
239 config.max_resources_per_entry = 4; | 239 config.max_resources_per_entry = 4; |
240 config.max_consecutive_misses = 2; | 240 config.max_consecutive_misses = 2; |
| 241 config.max_redirect_consecutive_misses = 2; |
241 config.min_resource_confidence_to_trigger_prefetch = 0.5; | 242 config.min_resource_confidence_to_trigger_prefetch = 0.5; |
242 config.is_url_learning_enabled = true; | 243 config.is_url_learning_enabled = true; |
243 config.is_manifests_enabled = true; | 244 config.is_manifests_enabled = true; |
244 config.is_origin_prediction_enabled = true; | 245 config.is_origin_prediction_enabled = true; |
245 | 246 |
246 config.mode |= ResourcePrefetchPredictorConfig::LEARNING; | 247 config.mode |= ResourcePrefetchPredictorConfig::LEARNING; |
247 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get())); | 248 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get())); |
248 predictor_->set_mock_tables(mock_tables_); | 249 predictor_->set_mock_tables(mock_tables_); |
249 } | 250 } |
250 | 251 |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 host_data.add_resources(), "https://google.com/script2.js", | 628 host_data.add_resources(), "https://google.com/script2.js", |
628 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); | 629 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); |
629 EXPECT_CALL(*mock_tables_.get(), | 630 EXPECT_CALL(*mock_tables_.get(), |
630 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, | 631 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, |
631 empty_redirect_data_)); | 632 empty_redirect_data_)); |
632 OriginData origin_data = CreateOriginData("www.google.com"); | 633 OriginData origin_data = CreateOriginData("www.google.com"); |
633 InitializeOriginStat(origin_data.add_origins(), "https://google.com/", 1, 0, | 634 InitializeOriginStat(origin_data.add_origins(), "https://google.com/", 1, 0, |
634 0, 1., false, true); | 635 0, 1., false, true); |
635 EXPECT_CALL(*mock_tables_.get(), UpdateOriginData(origin_data)); | 636 EXPECT_CALL(*mock_tables_.get(), UpdateOriginData(origin_data)); |
636 | 637 |
| 638 RedirectData host_redirect_data = CreateRedirectData("www.google.com"); |
| 639 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), |
| 640 "www.google.com", 1, 0, 0); |
| 641 EXPECT_CALL(*mock_tables_.get(), |
| 642 UpdateData(empty_resource_data_, empty_resource_data_, |
| 643 empty_redirect_data_, host_redirect_data)); |
| 644 |
637 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); | 645 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
638 profile_->BlockUntilHistoryProcessesPendingRequests(); | 646 profile_->BlockUntilHistoryProcessesPendingRequests(); |
639 } | 647 } |
640 | 648 |
641 // Single navigation that will be recorded. Will check for duplicate | 649 // Single navigation that will be recorded. Will check for duplicate |
642 // resources and also for number of resources saved. | 650 // resources and also for number of resources saved. |
643 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { | 651 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { |
644 const int kVisitCount = 4; | 652 const int kVisitCount = 4; |
645 AddUrlToHistory("http://www.google.com", kVisitCount); | 653 AddUrlToHistory("http://www.google.com", kVisitCount); |
646 | 654 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 InitializeOriginStat(origin_data.add_origins(), "http://reader.google.com/", | 743 InitializeOriginStat(origin_data.add_origins(), "http://reader.google.com/", |
736 1, 0, 0, 3., false, true); | 744 1, 0, 0, 3., false, true); |
737 EXPECT_CALL(*mock_tables_.get(), UpdateOriginData(origin_data)); | 745 EXPECT_CALL(*mock_tables_.get(), UpdateOriginData(origin_data)); |
738 | 746 |
739 PrefetchData host_data = CreatePrefetchData("www.google.com"); | 747 PrefetchData host_data = CreatePrefetchData("www.google.com"); |
740 host_data.mutable_resources()->CopyFrom(url_data.resources()); | 748 host_data.mutable_resources()->CopyFrom(url_data.resources()); |
741 EXPECT_CALL(*mock_tables_.get(), | 749 EXPECT_CALL(*mock_tables_.get(), |
742 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, | 750 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, |
743 empty_redirect_data_)); | 751 empty_redirect_data_)); |
744 | 752 |
| 753 RedirectData url_redirect_data = CreateRedirectData("http://www.google.com/"); |
| 754 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), |
| 755 "http://www.google.com/", 1, 0, 0); |
| 756 EXPECT_CALL(*mock_tables_.get(), |
| 757 UpdateData(empty_resource_data_, empty_resource_data_, |
| 758 url_redirect_data, empty_redirect_data_)); |
| 759 |
| 760 RedirectData host_redirect_data = CreateRedirectData("www.google.com"); |
| 761 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), |
| 762 "www.google.com", 1, 0, 0); |
| 763 EXPECT_CALL(*mock_tables_.get(), |
| 764 UpdateData(empty_resource_data_, empty_resource_data_, |
| 765 empty_redirect_data_, host_redirect_data)); |
| 766 |
745 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); | 767 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
746 profile_->BlockUntilHistoryProcessesPendingRequests(); | 768 profile_->BlockUntilHistoryProcessesPendingRequests(); |
747 } | 769 } |
748 | 770 |
749 // Tests that navigation is recorded correctly for URL already present in | 771 // Tests that navigation is recorded correctly for URL already present in |
750 // the database cache. | 772 // the database cache. |
751 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { | 773 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { |
752 const int kVisitCount = 4; | 774 const int kVisitCount = 4; |
753 AddUrlToHistory("http://www.google.com", kVisitCount); | 775 AddUrlToHistory("http://www.google.com", kVisitCount); |
754 | 776 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
849 host_data.add_resources(), "http://google.com/script2.js", | 871 host_data.add_resources(), "http://google.com/script2.js", |
850 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); | 872 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); |
851 InitializeResourceData(host_data.add_resources(), | 873 InitializeResourceData(host_data.add_resources(), |
852 "http://google.com/style2.css", | 874 "http://google.com/style2.css", |
853 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, | 875 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, |
854 net::MEDIUM, false, false); | 876 net::MEDIUM, false, false); |
855 EXPECT_CALL(*mock_tables_.get(), | 877 EXPECT_CALL(*mock_tables_.get(), |
856 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, | 878 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, |
857 empty_redirect_data_)); | 879 empty_redirect_data_)); |
858 | 880 |
| 881 RedirectData url_redirect_data = CreateRedirectData("http://www.google.com/"); |
| 882 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), |
| 883 "http://www.google.com/", 1, 0, 0); |
| 884 EXPECT_CALL(*mock_tables_.get(), |
| 885 UpdateData(empty_resource_data_, empty_resource_data_, |
| 886 url_redirect_data, empty_redirect_data_)); |
| 887 |
| 888 RedirectData host_redirect_data = CreateRedirectData("www.google.com"); |
| 889 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), |
| 890 "www.google.com", 1, 0, 0); |
| 891 EXPECT_CALL(*mock_tables_.get(), |
| 892 UpdateData(empty_resource_data_, empty_resource_data_, |
| 893 empty_redirect_data_, host_redirect_data)); |
| 894 |
859 OriginData origin_data = CreateOriginData("www.google.com"); | 895 OriginData origin_data = CreateOriginData("www.google.com"); |
860 InitializeOriginStat(origin_data.add_origins(), "http://static.google.com/", | 896 InitializeOriginStat(origin_data.add_origins(), "http://static.google.com/", |
861 1, 0, 0, 2., true, true); | 897 1, 0, 0, 2., true, true); |
862 InitializeOriginStat(origin_data.add_origins(), "http://google.com/", 1, 0, 0, | 898 InitializeOriginStat(origin_data.add_origins(), "http://google.com/", 1, 0, 0, |
863 1., false, true); | 899 1., false, true); |
864 EXPECT_CALL(*mock_tables_.get(), UpdateOriginData(origin_data)); | 900 EXPECT_CALL(*mock_tables_.get(), UpdateOriginData(origin_data)); |
865 | 901 |
866 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); | 902 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
867 profile_->BlockUntilHistoryProcessesPendingRequests(); | 903 profile_->BlockUntilHistoryProcessesPendingRequests(); |
868 } | 904 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
930 EXPECT_CALL(*mock_tables_.get(), | 966 EXPECT_CALL(*mock_tables_.get(), |
931 UpdateData(url_data, empty_resource_data_, empty_redirect_data_, | 967 UpdateData(url_data, empty_resource_data_, empty_redirect_data_, |
932 empty_redirect_data_)); | 968 empty_redirect_data_)); |
933 | 969 |
934 PrefetchData host_data = CreatePrefetchData("www.nike.com"); | 970 PrefetchData host_data = CreatePrefetchData("www.nike.com"); |
935 host_data.mutable_resources()->CopyFrom(url_data.resources()); | 971 host_data.mutable_resources()->CopyFrom(url_data.resources()); |
936 EXPECT_CALL(*mock_tables_.get(), | 972 EXPECT_CALL(*mock_tables_.get(), |
937 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, | 973 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, |
938 empty_redirect_data_)); | 974 empty_redirect_data_)); |
939 | 975 |
| 976 RedirectData url_redirect_data = CreateRedirectData("http://www.nike.com/"); |
| 977 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), |
| 978 "http://www.nike.com/", 1, 0, 0); |
| 979 EXPECT_CALL(*mock_tables_.get(), |
| 980 UpdateData(empty_resource_data_, empty_resource_data_, |
| 981 url_redirect_data, empty_redirect_data_)); |
| 982 |
| 983 RedirectData host_redirect_data = CreateRedirectData("www.nike.com"); |
| 984 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), |
| 985 "www.nike.com", 1, 0, 0); |
| 986 EXPECT_CALL(*mock_tables_.get(), |
| 987 UpdateData(empty_resource_data_, empty_resource_data_, |
| 988 empty_redirect_data_, host_redirect_data)); |
| 989 |
940 EXPECT_CALL(*mock_tables_.get(), UpdateOriginData(testing::_)); | 990 EXPECT_CALL(*mock_tables_.get(), UpdateOriginData(testing::_)); |
941 | 991 |
942 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); | 992 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
943 profile_->BlockUntilHistoryProcessesPendingRequests(); | 993 profile_->BlockUntilHistoryProcessesPendingRequests(); |
944 } | 994 } |
945 | 995 |
946 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) { | 996 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) { |
947 const int kVisitCount = 4; | 997 const int kVisitCount = 4; |
948 AddUrlToHistory("https://facebook.com/google", kVisitCount); | 998 AddUrlToHistory("https://facebook.com/google", kVisitCount); |
949 | 999 |
(...skipping 764 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1714 test_data, &urls)); | 1764 test_data, &urls)); |
1715 EXPECT_TRUE(urls.empty()); | 1765 EXPECT_TRUE(urls.empty()); |
1716 | 1766 |
1717 urls.clear(); | 1767 urls.clear(); |
1718 EXPECT_FALSE(predictor_->PopulatePrefetcherRequest("http://404.com", | 1768 EXPECT_FALSE(predictor_->PopulatePrefetcherRequest("http://404.com", |
1719 test_data, &urls)); | 1769 test_data, &urls)); |
1720 EXPECT_TRUE(urls.empty()); | 1770 EXPECT_TRUE(urls.empty()); |
1721 } | 1771 } |
1722 | 1772 |
1723 TEST_F(ResourcePrefetchPredictorTest, GetRedirectEndpoint) { | 1773 TEST_F(ResourcePrefetchPredictorTest, GetRedirectEndpoint) { |
1724 // The data to be requested for the most confident endpoint. | 1774 // The data to be requested for the confident endpoint. |
1725 RedirectData google = CreateRedirectData("http://google.com/", 1); | 1775 RedirectData nyt = CreateRedirectData("http://nyt.com", 1); |
| 1776 InitializeRedirectStat(nyt.add_redirect_endpoints(), |
| 1777 "https://mobile.nytimes.com", 10, 0, 0); |
| 1778 |
| 1779 // The data to be sure that other RedirectData won't be affected. |
| 1780 RedirectData gogle = CreateRedirectData("http://gogle.com", 2); |
| 1781 InitializeRedirectStat(gogle.add_redirect_endpoints(), "https://google.com", |
| 1782 100, 0, 0); |
| 1783 |
| 1784 // The data to check negative result due not enough confidence. |
| 1785 RedirectData facebook = CreateRedirectData("http://fb.com", 3); |
| 1786 InitializeRedirectStat(facebook.add_redirect_endpoints(), |
| 1787 "http://facebook.com", 5, 5, 0); |
| 1788 |
| 1789 // The data to check negative result due ambiguity. |
| 1790 RedirectData google = CreateRedirectData("http://google.com", 4); |
1726 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.com", | 1791 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.com", |
1727 10, 0, 0); | 1792 10, 0, 0); |
1728 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.fr", | 1793 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.fr", |
1729 10, 1, 0); | 1794 10, 1, 0); |
1730 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.ws", | 1795 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.ws", |
1731 20, 20, 0); | 1796 20, 20, 0); |
1732 | 1797 |
1733 // The data to be sure that other RedirectData won't be affected. | |
1734 RedirectData gogle = CreateRedirectData("http://gogle.com", 2); | |
1735 InitializeRedirectStat(gogle.add_redirect_endpoints(), "https://google.com", | |
1736 100, 0, 0); | |
1737 | |
1738 // The data to check negative result. | |
1739 RedirectData facebook = CreateRedirectData("http://fb.com/", 3); | |
1740 InitializeRedirectStat(facebook.add_redirect_endpoints(), | |
1741 "http://facebook.com", 5, 5, | |
1742 0); // not enough confidence | |
1743 | |
1744 RedirectDataMap data_map; | 1798 RedirectDataMap data_map; |
1745 data_map.insert(std::make_pair(google.primary_key(), google)); | 1799 data_map.insert(std::make_pair(nyt.primary_key(), nyt)); |
1746 data_map.insert(std::make_pair(gogle.primary_key(), gogle)); | 1800 data_map.insert(std::make_pair(gogle.primary_key(), gogle)); |
1747 data_map.insert(std::make_pair(facebook.primary_key(), facebook)); | 1801 data_map.insert(std::make_pair(facebook.primary_key(), facebook)); |
| 1802 data_map.insert(std::make_pair(google.primary_key(), google)); |
1748 | 1803 |
1749 std::string redirect_endpoint; | 1804 std::string redirect_endpoint; |
1750 EXPECT_TRUE(predictor_->GetRedirectEndpoint("http://google.com/", data_map, | 1805 EXPECT_TRUE(predictor_->GetRedirectEndpoint("http://nyt.com", data_map, |
1751 &redirect_endpoint)); | 1806 &redirect_endpoint)); |
1752 EXPECT_EQ(redirect_endpoint, "https://google.com"); | 1807 EXPECT_EQ(redirect_endpoint, "https://mobile.nytimes.com"); |
| 1808 |
| 1809 // Returns the initial url if data_map doesn't contain an entry for the url. |
| 1810 EXPECT_TRUE(predictor_->GetRedirectEndpoint("http://bbc.com", data_map, |
| 1811 &redirect_endpoint)); |
| 1812 EXPECT_EQ(redirect_endpoint, "http://bbc.com"); |
1753 | 1813 |
1754 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://fb.com", data_map, | 1814 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://fb.com", data_map, |
1755 &redirect_endpoint)); | 1815 &redirect_endpoint)); |
1756 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://404.com", data_map, | 1816 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://google.com", data_map, |
1757 &redirect_endpoint)); | 1817 &redirect_endpoint)); |
1758 } | 1818 } |
1759 | 1819 |
1760 TEST_F(ResourcePrefetchPredictorTest, GetPrefetchData) { | 1820 TEST_F(ResourcePrefetchPredictorTest, GetPrefetchData) { |
1761 const GURL main_frame_url("http://google.com/?query=cats"); | 1821 const GURL main_frame_url("http://google.com/?query=cats"); |
1762 ResourcePrefetchPredictor::Prediction prediction; | 1822 ResourcePrefetchPredictor::Prediction prediction; |
1763 std::vector<GURL>& urls = prediction.subresource_urls; | 1823 std::vector<GURL>& urls = prediction.subresource_urls; |
1764 // No prefetch data. | 1824 // No prefetch data. |
1765 EXPECT_FALSE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | 1825 EXPECT_FALSE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
1766 | 1826 |
1767 // Add a resource associated with the main frame host. | 1827 // Add a resource associated with the main frame host. |
1768 PrefetchData google_host = CreatePrefetchData("google.com", 1); | 1828 PrefetchData google_host = CreatePrefetchData("google.com", 1); |
1769 const std::string script_url = "https://cdn.google.com/script.js"; | 1829 const std::string script_url = "https://cdn.google.com/script.js"; |
1770 InitializeResourceData(google_host.add_resources(), script_url, | 1830 InitializeResourceData(google_host.add_resources(), script_url, |
1771 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, | 1831 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, |
1772 net::MEDIUM, false, false); | 1832 net::MEDIUM, false, false); |
1773 predictor_->host_table_cache_->insert( | 1833 predictor_->host_table_cache_->insert( |
1774 std::make_pair(google_host.primary_key(), google_host)); | 1834 std::make_pair(google_host.primary_key(), google_host)); |
1775 | 1835 |
1776 urls.clear(); | 1836 urls.clear(); |
1777 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | 1837 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
1778 EXPECT_THAT(urls, UnorderedElementsAre(GURL(script_url))); | 1838 EXPECT_THAT(urls, UnorderedElementsAre(GURL(script_url))); |
1779 | 1839 |
1780 // Add a resource associated with the main frame url. | |
1781 PrefetchData google_url = | |
1782 CreatePrefetchData("http://google.com/?query=cats", 2); | |
1783 const std::string image_url = "https://cdn.google.com/image.png"; | |
1784 InitializeResourceData(google_url.add_resources(), image_url, | |
1785 content::RESOURCE_TYPE_IMAGE, 10, 0, 1, 2.1, | |
1786 net::MEDIUM, false, false); | |
1787 predictor_->url_table_cache_->insert( | |
1788 std::make_pair(google_url.primary_key(), google_url)); | |
1789 | |
1790 urls.clear(); | |
1791 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | |
1792 EXPECT_THAT(urls, UnorderedElementsAre(GURL(image_url))); | |
1793 | |
1794 // Add host-based redirect. | 1840 // Add host-based redirect. |
1795 RedirectData host_redirect = CreateRedirectData("google.com", 3); | 1841 RedirectData host_redirect = CreateRedirectData("google.com", 3); |
1796 InitializeRedirectStat(host_redirect.add_redirect_endpoints(), | 1842 InitializeRedirectStat(host_redirect.add_redirect_endpoints(), |
1797 "www.google.com", 10, 0, 0); | 1843 "www.google.com", 10, 0, 0); |
1798 predictor_->host_redirect_table_cache_->insert( | 1844 predictor_->host_redirect_table_cache_->insert( |
1799 std::make_pair(host_redirect.primary_key(), host_redirect)); | 1845 std::make_pair(host_redirect.primary_key(), host_redirect)); |
1800 | 1846 |
1801 // Nothing changed: new redirect endpoint doesn't have any associated | 1847 // Prediction failed: no data associated with the host redirect endpoint. |
1802 // resources | |
1803 urls.clear(); | 1848 urls.clear(); |
1804 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | 1849 EXPECT_FALSE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
1805 EXPECT_THAT(urls, UnorderedElementsAre(GURL(image_url))); | |
1806 | 1850 |
1807 // Add a resource associated with host redirect endpoint. | 1851 // Add a resource associated with host redirect endpoint. |
1808 PrefetchData www_google_host = CreatePrefetchData("www.google.com", 4); | 1852 PrefetchData www_google_host = CreatePrefetchData("www.google.com", 4); |
1809 const std::string style_url = "https://cdn.google.com/style.css"; | 1853 const std::string style_url = "https://cdn.google.com/style.css"; |
1810 InitializeResourceData(www_google_host.add_resources(), style_url, | 1854 InitializeResourceData(www_google_host.add_resources(), style_url, |
1811 content::RESOURCE_TYPE_STYLESHEET, 10, 0, 1, 2.1, | 1855 content::RESOURCE_TYPE_STYLESHEET, 10, 0, 1, 2.1, |
1812 net::MEDIUM, false, false); | 1856 net::MEDIUM, false, false); |
1813 predictor_->host_table_cache_->insert( | 1857 predictor_->host_table_cache_->insert( |
1814 std::make_pair(www_google_host.primary_key(), www_google_host)); | 1858 std::make_pair(www_google_host.primary_key(), www_google_host)); |
1815 | 1859 |
1816 urls.clear(); | 1860 urls.clear(); |
1817 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | 1861 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
1818 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url))); | 1862 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url))); |
1819 | 1863 |
| 1864 // Add a resource associated with the main frame url. |
| 1865 PrefetchData google_url = |
| 1866 CreatePrefetchData("http://google.com/?query=cats", 2); |
| 1867 const std::string image_url = "https://cdn.google.com/image.png"; |
| 1868 InitializeResourceData(google_url.add_resources(), image_url, |
| 1869 content::RESOURCE_TYPE_IMAGE, 10, 0, 1, 2.1, |
| 1870 net::MEDIUM, false, false); |
| 1871 predictor_->url_table_cache_->insert( |
| 1872 std::make_pair(google_url.primary_key(), google_url)); |
| 1873 |
| 1874 urls.clear(); |
| 1875 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
| 1876 EXPECT_THAT(urls, UnorderedElementsAre(GURL(image_url))); |
| 1877 |
1820 // Add url-based redirect. | 1878 // Add url-based redirect. |
1821 RedirectData url_redirect = | 1879 RedirectData url_redirect = |
1822 CreateRedirectData("http://google.com/?query=cats", 5); | 1880 CreateRedirectData("http://google.com/?query=cats", 5); |
1823 InitializeRedirectStat(url_redirect.add_redirect_endpoints(), | 1881 InitializeRedirectStat(url_redirect.add_redirect_endpoints(), |
1824 "https://www.google.com/?query=cats", 10, 0, 0); | 1882 "https://www.google.com/?query=cats", 10, 0, 0); |
1825 predictor_->url_redirect_table_cache_->insert( | 1883 predictor_->url_redirect_table_cache_->insert( |
1826 std::make_pair(url_redirect.primary_key(), url_redirect)); | 1884 std::make_pair(url_redirect.primary_key(), url_redirect)); |
1827 | 1885 |
1828 // Url redirect endpoint doesn't have associated resources. | 1886 // Url redirect endpoint doesn't have associated resources so we get |
| 1887 // host-based data. |
1829 urls.clear(); | 1888 urls.clear(); |
1830 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | 1889 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
1831 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url))); | 1890 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url))); |
1832 | 1891 |
1833 // Add a resource associated with url redirect endpoint. | 1892 // Add a resource associated with url redirect endpoint. |
1834 PrefetchData www_google_url = | 1893 PrefetchData www_google_url = |
1835 CreatePrefetchData("https://www.google.com/?query=cats", 4); | 1894 CreatePrefetchData("https://www.google.com/?query=cats", 4); |
1836 const std::string font_url = "https://cdn.google.com/comic-sans-ms.woff"; | 1895 const std::string font_url = "https://cdn.google.com/comic-sans-ms.woff"; |
1837 InitializeResourceData(www_google_url.add_resources(), font_url, | 1896 InitializeResourceData(www_google_url.add_resources(), font_url, |
1838 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1, | 1897 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1, |
1839 net::MEDIUM, false, false); | 1898 net::MEDIUM, false, false); |
1840 predictor_->url_table_cache_->insert( | 1899 predictor_->url_table_cache_->insert( |
1841 std::make_pair(www_google_url.primary_key(), www_google_url)); | 1900 std::make_pair(www_google_url.primary_key(), www_google_url)); |
1842 | 1901 |
1843 urls.clear(); | 1902 urls.clear(); |
1844 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | 1903 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
1845 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url))); | 1904 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url))); |
1846 } | 1905 } |
1847 | 1906 |
1848 TEST_F(ResourcePrefetchPredictorTest, TestPrecisionRecallHistograms) { | 1907 TEST_F(ResourcePrefetchPredictorTest, TestPrecisionRecallHistograms) { |
1849 using testing::_; | 1908 using testing::_; |
1850 EXPECT_CALL(*mock_tables_.get(), UpdateData(_, _, _, _)) | 1909 EXPECT_CALL(*mock_tables_.get(), UpdateData(_, _, _, _)).Times(2); |
1851 .Times(testing::AtLeast(1)); | |
1852 EXPECT_CALL(*mock_tables_.get(), UpdateOriginData(_)); | 1910 EXPECT_CALL(*mock_tables_.get(), UpdateOriginData(_)); |
1853 | 1911 |
1854 // Fill the database with 3 resources: 1 useful, 2 useless. | 1912 // Fill the database with 3 resources: 1 useful, 2 useless. |
1855 const std::string main_frame_url = "http://google.com/?query=cats"; | 1913 const std::string main_frame_url = "http://google.com/?query=cats"; |
1856 PrefetchData google = CreatePrefetchData("google.com", 1); | 1914 PrefetchData google = CreatePrefetchData("google.com", 1); |
1857 | 1915 |
1858 const std::string script_url = "https://cdn.google.com/script.js"; | 1916 const std::string script_url = "https://cdn.google.com/script.js"; |
1859 InitializeResourceData(google.add_resources(), script_url, | 1917 InitializeResourceData(google.add_resources(), script_url, |
1860 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, | 1918 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, |
1861 net::MEDIUM, false, false); | 1919 net::MEDIUM, false, false); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1949 predictor_->host_table_cache_->insert( | 2007 predictor_->host_table_cache_->insert( |
1950 std::make_pair(google.primary_key(), google)); | 2008 std::make_pair(google.primary_key(), google)); |
1951 | 2009 |
1952 predictor_->StartPrefetching(GURL(main_frame_url), PrefetchOrigin::EXTERNAL); | 2010 predictor_->StartPrefetching(GURL(main_frame_url), PrefetchOrigin::EXTERNAL); |
1953 predictor_->StopPrefetching(GURL(main_frame_url)); | 2011 predictor_->StopPrefetching(GURL(main_frame_url)); |
1954 histogram_tester_->ExpectTotalCount( | 2012 histogram_tester_->ExpectTotalCount( |
1955 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1); | 2013 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1); |
1956 } | 2014 } |
1957 | 2015 |
1958 } // namespace predictors | 2016 } // namespace predictors |
OLD | NEW |