| 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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 profile_->BlockUntilHistoryProcessesPendingRequests(); | 227 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 228 } | 228 } |
| 229 | 229 |
| 230 void ResetPredictor() { | 230 void ResetPredictor() { |
| 231 ResourcePrefetchPredictorConfig config; | 231 ResourcePrefetchPredictorConfig config; |
| 232 config.max_urls_to_track = 3; | 232 config.max_urls_to_track = 3; |
| 233 config.max_hosts_to_track = 2; | 233 config.max_hosts_to_track = 2; |
| 234 config.min_url_visit_count = 2; | 234 config.min_url_visit_count = 2; |
| 235 config.max_resources_per_entry = 4; | 235 config.max_resources_per_entry = 4; |
| 236 config.max_consecutive_misses = 2; | 236 config.max_consecutive_misses = 2; |
| 237 config.max_redirect_consecutive_misses = 2; |
| 237 config.min_resource_confidence_to_trigger_prefetch = 0.5; | 238 config.min_resource_confidence_to_trigger_prefetch = 0.5; |
| 238 config.is_url_learning_enabled = true; | 239 config.is_url_learning_enabled = true; |
| 239 | 240 |
| 240 config.mode |= ResourcePrefetchPredictorConfig::LEARNING; | 241 config.mode |= ResourcePrefetchPredictorConfig::LEARNING; |
| 241 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get())); | 242 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get())); |
| 242 predictor_->set_mock_tables(mock_tables_); | 243 predictor_->set_mock_tables(mock_tables_); |
| 243 } | 244 } |
| 244 | 245 |
| 245 void InitializeSampleData(); | 246 void InitializeSampleData(); |
| 246 void TestRedirectStatusHistogram( | 247 void TestRedirectStatusHistogram( |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 InitializeResourceData( | 594 InitializeResourceData( |
| 594 host_data.add_resources(), "https://google.com/script1.js", | 595 host_data.add_resources(), "https://google.com/script1.js", |
| 595 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false); | 596 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false); |
| 596 InitializeResourceData( | 597 InitializeResourceData( |
| 597 host_data.add_resources(), "https://google.com/script2.js", | 598 host_data.add_resources(), "https://google.com/script2.js", |
| 598 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); | 599 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); |
| 599 EXPECT_CALL(*mock_tables_.get(), | 600 EXPECT_CALL(*mock_tables_.get(), |
| 600 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, | 601 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, |
| 601 empty_redirect_data_)); | 602 empty_redirect_data_)); |
| 602 | 603 |
| 604 RedirectData host_redirect_data = CreateRedirectData("www.google.com"); |
| 605 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), |
| 606 "www.google.com", 1, 0, 0); |
| 607 EXPECT_CALL(*mock_tables_.get(), |
| 608 UpdateData(empty_resource_data_, empty_resource_data_, |
| 609 empty_redirect_data_, host_redirect_data)); |
| 610 |
| 603 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); | 611 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
| 604 profile_->BlockUntilHistoryProcessesPendingRequests(); | 612 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 605 } | 613 } |
| 606 | 614 |
| 607 // Single navigation that will be recorded. Will check for duplicate | 615 // Single navigation that will be recorded. Will check for duplicate |
| 608 // resources and also for number of resources saved. | 616 // resources and also for number of resources saved. |
| 609 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { | 617 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { |
| 610 const int kVisitCount = 4; | 618 const int kVisitCount = 4; |
| 611 AddUrlToHistory("http://www.google.com", kVisitCount); | 619 AddUrlToHistory("http://www.google.com", kVisitCount); |
| 612 | 620 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 EXPECT_CALL(*mock_tables_.get(), | 679 EXPECT_CALL(*mock_tables_.get(), |
| 672 UpdateData(url_data, empty_resource_data_, empty_redirect_data_, | 680 UpdateData(url_data, empty_resource_data_, empty_redirect_data_, |
| 673 empty_redirect_data_)); | 681 empty_redirect_data_)); |
| 674 | 682 |
| 675 PrefetchData host_data = CreatePrefetchData("www.google.com"); | 683 PrefetchData host_data = CreatePrefetchData("www.google.com"); |
| 676 host_data.mutable_resources()->CopyFrom(url_data.resources()); | 684 host_data.mutable_resources()->CopyFrom(url_data.resources()); |
| 677 EXPECT_CALL(*mock_tables_.get(), | 685 EXPECT_CALL(*mock_tables_.get(), |
| 678 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, | 686 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, |
| 679 empty_redirect_data_)); | 687 empty_redirect_data_)); |
| 680 | 688 |
| 689 RedirectData url_redirect_data = CreateRedirectData("http://www.google.com/"); |
| 690 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), |
| 691 "http://www.google.com/", 1, 0, 0); |
| 692 EXPECT_CALL(*mock_tables_.get(), |
| 693 UpdateData(empty_resource_data_, empty_resource_data_, |
| 694 url_redirect_data, empty_redirect_data_)); |
| 695 |
| 696 RedirectData host_redirect_data = CreateRedirectData("www.google.com"); |
| 697 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), |
| 698 "www.google.com", 1, 0, 0); |
| 699 EXPECT_CALL(*mock_tables_.get(), |
| 700 UpdateData(empty_resource_data_, empty_resource_data_, |
| 701 empty_redirect_data_, host_redirect_data)); |
| 702 |
| 681 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); | 703 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
| 682 profile_->BlockUntilHistoryProcessesPendingRequests(); | 704 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 683 } | 705 } |
| 684 | 706 |
| 685 // Tests that navigation is recorded correctly for URL already present in | 707 // Tests that navigation is recorded correctly for URL already present in |
| 686 // the database cache. | 708 // the database cache. |
| 687 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { | 709 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { |
| 688 const int kVisitCount = 4; | 710 const int kVisitCount = 4; |
| 689 AddUrlToHistory("http://www.google.com", kVisitCount); | 711 AddUrlToHistory("http://www.google.com", kVisitCount); |
| 690 | 712 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 host_data.add_resources(), "http://google.com/script2.js", | 800 host_data.add_resources(), "http://google.com/script2.js", |
| 779 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); | 801 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); |
| 780 InitializeResourceData(host_data.add_resources(), | 802 InitializeResourceData(host_data.add_resources(), |
| 781 "http://google.com/style2.css", | 803 "http://google.com/style2.css", |
| 782 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, | 804 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, |
| 783 net::MEDIUM, false, false); | 805 net::MEDIUM, false, false); |
| 784 EXPECT_CALL(*mock_tables_.get(), | 806 EXPECT_CALL(*mock_tables_.get(), |
| 785 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, | 807 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, |
| 786 empty_redirect_data_)); | 808 empty_redirect_data_)); |
| 787 | 809 |
| 810 RedirectData url_redirect_data = CreateRedirectData("http://www.google.com/"); |
| 811 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), |
| 812 "http://www.google.com/", 1, 0, 0); |
| 813 EXPECT_CALL(*mock_tables_.get(), |
| 814 UpdateData(empty_resource_data_, empty_resource_data_, |
| 815 url_redirect_data, empty_redirect_data_)); |
| 816 |
| 817 RedirectData host_redirect_data = CreateRedirectData("www.google.com"); |
| 818 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), |
| 819 "www.google.com", 1, 0, 0); |
| 820 EXPECT_CALL(*mock_tables_.get(), |
| 821 UpdateData(empty_resource_data_, empty_resource_data_, |
| 822 empty_redirect_data_, host_redirect_data)); |
| 823 |
| 788 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); | 824 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
| 789 profile_->BlockUntilHistoryProcessesPendingRequests(); | 825 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 790 } | 826 } |
| 791 | 827 |
| 792 // Tests that a URL is deleted before another is added if the cache is full. | 828 // Tests that a URL is deleted before another is added if the cache is full. |
| 793 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) { | 829 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) { |
| 794 const int kVisitCount = 4; | 830 const int kVisitCount = 4; |
| 795 AddUrlToHistory("http://www.nike.com/", kVisitCount); | 831 AddUrlToHistory("http://www.nike.com/", kVisitCount); |
| 796 | 832 |
| 797 EXPECT_CALL(*mock_tables_.get(), | 833 EXPECT_CALL(*mock_tables_.get(), |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 EXPECT_CALL(*mock_tables_.get(), | 883 EXPECT_CALL(*mock_tables_.get(), |
| 848 UpdateData(url_data, empty_resource_data_, empty_redirect_data_, | 884 UpdateData(url_data, empty_resource_data_, empty_redirect_data_, |
| 849 empty_redirect_data_)); | 885 empty_redirect_data_)); |
| 850 | 886 |
| 851 PrefetchData host_data = CreatePrefetchData("www.nike.com"); | 887 PrefetchData host_data = CreatePrefetchData("www.nike.com"); |
| 852 host_data.mutable_resources()->CopyFrom(url_data.resources()); | 888 host_data.mutable_resources()->CopyFrom(url_data.resources()); |
| 853 EXPECT_CALL(*mock_tables_.get(), | 889 EXPECT_CALL(*mock_tables_.get(), |
| 854 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, | 890 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, |
| 855 empty_redirect_data_)); | 891 empty_redirect_data_)); |
| 856 | 892 |
| 893 RedirectData url_redirect_data = CreateRedirectData("http://www.nike.com/"); |
| 894 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), |
| 895 "http://www.nike.com/", 1, 0, 0); |
| 896 EXPECT_CALL(*mock_tables_.get(), |
| 897 UpdateData(empty_resource_data_, empty_resource_data_, |
| 898 url_redirect_data, empty_redirect_data_)); |
| 899 |
| 900 RedirectData host_redirect_data = CreateRedirectData("www.nike.com"); |
| 901 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), |
| 902 "www.nike.com", 1, 0, 0); |
| 903 EXPECT_CALL(*mock_tables_.get(), |
| 904 UpdateData(empty_resource_data_, empty_resource_data_, |
| 905 empty_redirect_data_, host_redirect_data)); |
| 906 |
| 857 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); | 907 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
| 858 profile_->BlockUntilHistoryProcessesPendingRequests(); | 908 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 859 } | 909 } |
| 860 | 910 |
| 861 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) { | 911 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) { |
| 862 const int kVisitCount = 4; | 912 const int kVisitCount = 4; |
| 863 AddUrlToHistory("https://facebook.com/google", kVisitCount); | 913 AddUrlToHistory("https://facebook.com/google", kVisitCount); |
| 864 | 914 |
| 865 URLRequestSummary fb1 = CreateURLRequestSummary(1, "http://fb.com/google"); | 915 URLRequestSummary fb1 = CreateURLRequestSummary(1, "http://fb.com/google"); |
| 866 predictor_->RecordURLRequest(fb1); | 916 predictor_->RecordURLRequest(fb1); |
| (...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1626 test_data, &urls)); | 1676 test_data, &urls)); |
| 1627 EXPECT_TRUE(urls.empty()); | 1677 EXPECT_TRUE(urls.empty()); |
| 1628 | 1678 |
| 1629 urls.clear(); | 1679 urls.clear(); |
| 1630 EXPECT_FALSE(predictor_->PopulatePrefetcherRequest("http://404.com", | 1680 EXPECT_FALSE(predictor_->PopulatePrefetcherRequest("http://404.com", |
| 1631 test_data, &urls)); | 1681 test_data, &urls)); |
| 1632 EXPECT_TRUE(urls.empty()); | 1682 EXPECT_TRUE(urls.empty()); |
| 1633 } | 1683 } |
| 1634 | 1684 |
| 1635 TEST_F(ResourcePrefetchPredictorTest, GetRedirectEndpoint) { | 1685 TEST_F(ResourcePrefetchPredictorTest, GetRedirectEndpoint) { |
| 1636 // The data to be requested for the most confident endpoint. | 1686 // The data to be requested for the confident endpoint. |
| 1637 RedirectData google = CreateRedirectData("http://google.com/", 1); | 1687 RedirectData nyt = CreateRedirectData("http://nyt.com", 1); |
| 1688 InitializeRedirectStat(nyt.add_redirect_endpoints(), |
| 1689 "https://mobile.nytimes.com", 10, 0, 0); |
| 1690 |
| 1691 // The data to be sure that other RedirectData won't be affected. |
| 1692 RedirectData gogle = CreateRedirectData("http://gogle.com", 2); |
| 1693 InitializeRedirectStat(gogle.add_redirect_endpoints(), "https://google.com", |
| 1694 100, 0, 0); |
| 1695 |
| 1696 // The data to check negative result due not enough confidence. |
| 1697 RedirectData facebook = CreateRedirectData("http://fb.com", 3); |
| 1698 InitializeRedirectStat(facebook.add_redirect_endpoints(), |
| 1699 "http://facebook.com", 5, 5, 0); |
| 1700 |
| 1701 // The data to check negative result due ambiguity. |
| 1702 RedirectData google = CreateRedirectData("http://google.com", 4); |
| 1638 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.com", | 1703 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.com", |
| 1639 10, 0, 0); | 1704 10, 0, 0); |
| 1640 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.fr", | 1705 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.fr", |
| 1641 10, 1, 0); | 1706 10, 1, 0); |
| 1642 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.ws", | 1707 InitializeRedirectStat(google.add_redirect_endpoints(), "https://google.ws", |
| 1643 20, 20, 0); | 1708 20, 20, 0); |
| 1644 | 1709 |
| 1645 // The data to be sure that other RedirectData won't be affected. | |
| 1646 RedirectData gogle = CreateRedirectData("http://gogle.com", 2); | |
| 1647 InitializeRedirectStat(gogle.add_redirect_endpoints(), "https://google.com", | |
| 1648 100, 0, 0); | |
| 1649 | |
| 1650 // The data to check negative result. | |
| 1651 RedirectData facebook = CreateRedirectData("http://fb.com/", 3); | |
| 1652 InitializeRedirectStat(facebook.add_redirect_endpoints(), | |
| 1653 "http://facebook.com", 5, 5, | |
| 1654 0); // not enough confidence | |
| 1655 | |
| 1656 RedirectDataMap data_map; | 1710 RedirectDataMap data_map; |
| 1657 data_map.insert(std::make_pair(google.primary_key(), google)); | 1711 data_map.insert(std::make_pair(nyt.primary_key(), nyt)); |
| 1658 data_map.insert(std::make_pair(gogle.primary_key(), gogle)); | 1712 data_map.insert(std::make_pair(gogle.primary_key(), gogle)); |
| 1659 data_map.insert(std::make_pair(facebook.primary_key(), facebook)); | 1713 data_map.insert(std::make_pair(facebook.primary_key(), facebook)); |
| 1714 data_map.insert(std::make_pair(google.primary_key(), google)); |
| 1660 | 1715 |
| 1661 std::string redirect_endpoint; | 1716 std::string redirect_endpoint; |
| 1662 EXPECT_TRUE(predictor_->GetRedirectEndpoint("http://google.com/", data_map, | 1717 EXPECT_TRUE(predictor_->GetRedirectEndpoint("http://nyt.com", data_map, |
| 1663 &redirect_endpoint)); | 1718 &redirect_endpoint)); |
| 1664 EXPECT_EQ(redirect_endpoint, "https://google.com"); | 1719 EXPECT_EQ(redirect_endpoint, "https://mobile.nytimes.com"); |
| 1720 |
| 1721 // Returns the initial url if data_map doesn't contain an entry for the url. |
| 1722 EXPECT_TRUE(predictor_->GetRedirectEndpoint("http://bbc.com", data_map, |
| 1723 &redirect_endpoint)); |
| 1724 EXPECT_EQ(redirect_endpoint, "http://bbc.com"); |
| 1665 | 1725 |
| 1666 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://fb.com", data_map, | 1726 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://fb.com", data_map, |
| 1667 &redirect_endpoint)); | 1727 &redirect_endpoint)); |
| 1668 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://404.com", data_map, | 1728 EXPECT_FALSE(predictor_->GetRedirectEndpoint("http://google.com", data_map, |
| 1669 &redirect_endpoint)); | 1729 &redirect_endpoint)); |
| 1670 } | 1730 } |
| 1671 | 1731 |
| 1672 TEST_F(ResourcePrefetchPredictorTest, GetPrefetchData) { | 1732 TEST_F(ResourcePrefetchPredictorTest, GetPrefetchData) { |
| 1673 const GURL main_frame_url("http://google.com/?query=cats"); | 1733 const GURL main_frame_url("http://google.com/?query=cats"); |
| 1674 ResourcePrefetchPredictor::Prediction prediction; | 1734 ResourcePrefetchPredictor::Prediction prediction; |
| 1675 std::vector<GURL>& urls = prediction.subresource_urls; | 1735 std::vector<GURL>& urls = prediction.subresource_urls; |
| 1676 // No prefetch data. | 1736 // No prefetch data. |
| 1677 EXPECT_FALSE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | 1737 EXPECT_FALSE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
| 1678 | 1738 |
| 1679 // Add a resource associated with the main frame host. | 1739 // Add a resource associated with the main frame host. |
| 1680 PrefetchData google_host = CreatePrefetchData("google.com", 1); | 1740 PrefetchData google_host = CreatePrefetchData("google.com", 1); |
| 1681 const std::string script_url = "https://cdn.google.com/script.js"; | 1741 const std::string script_url = "https://cdn.google.com/script.js"; |
| 1682 InitializeResourceData(google_host.add_resources(), script_url, | 1742 InitializeResourceData(google_host.add_resources(), script_url, |
| 1683 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, | 1743 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, |
| 1684 net::MEDIUM, false, false); | 1744 net::MEDIUM, false, false); |
| 1685 predictor_->host_table_cache_->insert( | 1745 predictor_->host_table_cache_->insert( |
| 1686 std::make_pair(google_host.primary_key(), google_host)); | 1746 std::make_pair(google_host.primary_key(), google_host)); |
| 1687 | 1747 |
| 1688 urls.clear(); | 1748 urls.clear(); |
| 1689 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | 1749 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
| 1690 EXPECT_THAT(urls, UnorderedElementsAre(GURL(script_url))); | 1750 EXPECT_THAT(urls, UnorderedElementsAre(GURL(script_url))); |
| 1691 | 1751 |
| 1692 // Add a resource associated with the main frame url. | |
| 1693 PrefetchData google_url = | |
| 1694 CreatePrefetchData("http://google.com/?query=cats", 2); | |
| 1695 const std::string image_url = "https://cdn.google.com/image.png"; | |
| 1696 InitializeResourceData(google_url.add_resources(), image_url, | |
| 1697 content::RESOURCE_TYPE_IMAGE, 10, 0, 1, 2.1, | |
| 1698 net::MEDIUM, false, false); | |
| 1699 predictor_->url_table_cache_->insert( | |
| 1700 std::make_pair(google_url.primary_key(), google_url)); | |
| 1701 | |
| 1702 urls.clear(); | |
| 1703 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | |
| 1704 EXPECT_THAT(urls, UnorderedElementsAre(GURL(image_url))); | |
| 1705 | |
| 1706 // Add host-based redirect. | 1752 // Add host-based redirect. |
| 1707 RedirectData host_redirect = CreateRedirectData("google.com", 3); | 1753 RedirectData host_redirect = CreateRedirectData("google.com", 3); |
| 1708 InitializeRedirectStat(host_redirect.add_redirect_endpoints(), | 1754 InitializeRedirectStat(host_redirect.add_redirect_endpoints(), |
| 1709 "www.google.com", 10, 0, 0); | 1755 "www.google.com", 10, 0, 0); |
| 1710 predictor_->host_redirect_table_cache_->insert( | 1756 predictor_->host_redirect_table_cache_->insert( |
| 1711 std::make_pair(host_redirect.primary_key(), host_redirect)); | 1757 std::make_pair(host_redirect.primary_key(), host_redirect)); |
| 1712 | 1758 |
| 1713 // Nothing changed: new redirect endpoint doesn't have any associated | 1759 // Prediction failed: no data associated with the host redirect endpoint. |
| 1714 // resources | |
| 1715 urls.clear(); | 1760 urls.clear(); |
| 1716 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | 1761 EXPECT_FALSE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
| 1717 EXPECT_THAT(urls, UnorderedElementsAre(GURL(image_url))); | |
| 1718 | 1762 |
| 1719 // Add a resource associated with host redirect endpoint. | 1763 // Add a resource associated with host redirect endpoint. |
| 1720 PrefetchData www_google_host = CreatePrefetchData("www.google.com", 4); | 1764 PrefetchData www_google_host = CreatePrefetchData("www.google.com", 4); |
| 1721 const std::string style_url = "https://cdn.google.com/style.css"; | 1765 const std::string style_url = "https://cdn.google.com/style.css"; |
| 1722 InitializeResourceData(www_google_host.add_resources(), style_url, | 1766 InitializeResourceData(www_google_host.add_resources(), style_url, |
| 1723 content::RESOURCE_TYPE_STYLESHEET, 10, 0, 1, 2.1, | 1767 content::RESOURCE_TYPE_STYLESHEET, 10, 0, 1, 2.1, |
| 1724 net::MEDIUM, false, false); | 1768 net::MEDIUM, false, false); |
| 1725 predictor_->host_table_cache_->insert( | 1769 predictor_->host_table_cache_->insert( |
| 1726 std::make_pair(www_google_host.primary_key(), www_google_host)); | 1770 std::make_pair(www_google_host.primary_key(), www_google_host)); |
| 1727 | 1771 |
| 1728 urls.clear(); | 1772 urls.clear(); |
| 1729 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | 1773 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
| 1730 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url))); | 1774 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url))); |
| 1731 | 1775 |
| 1776 // Add a resource associated with the main frame url. |
| 1777 PrefetchData google_url = |
| 1778 CreatePrefetchData("http://google.com/?query=cats", 2); |
| 1779 const std::string image_url = "https://cdn.google.com/image.png"; |
| 1780 InitializeResourceData(google_url.add_resources(), image_url, |
| 1781 content::RESOURCE_TYPE_IMAGE, 10, 0, 1, 2.1, |
| 1782 net::MEDIUM, false, false); |
| 1783 predictor_->url_table_cache_->insert( |
| 1784 std::make_pair(google_url.primary_key(), google_url)); |
| 1785 |
| 1786 urls.clear(); |
| 1787 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
| 1788 EXPECT_THAT(urls, UnorderedElementsAre(GURL(image_url))); |
| 1789 |
| 1732 // Add url-based redirect. | 1790 // Add url-based redirect. |
| 1733 RedirectData url_redirect = | 1791 RedirectData url_redirect = |
| 1734 CreateRedirectData("http://google.com/?query=cats", 5); | 1792 CreateRedirectData("http://google.com/?query=cats", 5); |
| 1735 InitializeRedirectStat(url_redirect.add_redirect_endpoints(), | 1793 InitializeRedirectStat(url_redirect.add_redirect_endpoints(), |
| 1736 "https://www.google.com/?query=cats", 10, 0, 0); | 1794 "https://www.google.com/?query=cats", 10, 0, 0); |
| 1737 predictor_->url_redirect_table_cache_->insert( | 1795 predictor_->url_redirect_table_cache_->insert( |
| 1738 std::make_pair(url_redirect.primary_key(), url_redirect)); | 1796 std::make_pair(url_redirect.primary_key(), url_redirect)); |
| 1739 | 1797 |
| 1740 // Url redirect endpoint doesn't have associated resources. | 1798 // Url redirect endpoint doesn't have associated resources so we get |
| 1799 // host-based data. |
| 1741 urls.clear(); | 1800 urls.clear(); |
| 1742 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | 1801 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
| 1743 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url))); | 1802 EXPECT_THAT(urls, UnorderedElementsAre(GURL(style_url))); |
| 1744 | 1803 |
| 1745 // Add a resource associated with url redirect endpoint. | 1804 // Add a resource associated with url redirect endpoint. |
| 1746 PrefetchData www_google_url = | 1805 PrefetchData www_google_url = |
| 1747 CreatePrefetchData("https://www.google.com/?query=cats", 4); | 1806 CreatePrefetchData("https://www.google.com/?query=cats", 4); |
| 1748 const std::string font_url = "https://cdn.google.com/comic-sans-ms.woff"; | 1807 const std::string font_url = "https://cdn.google.com/comic-sans-ms.woff"; |
| 1749 InitializeResourceData(www_google_url.add_resources(), font_url, | 1808 InitializeResourceData(www_google_url.add_resources(), font_url, |
| 1750 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1, | 1809 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1, |
| 1751 net::MEDIUM, false, false); | 1810 net::MEDIUM, false, false); |
| 1752 predictor_->url_table_cache_->insert( | 1811 predictor_->url_table_cache_->insert( |
| 1753 std::make_pair(www_google_url.primary_key(), www_google_url)); | 1812 std::make_pair(www_google_url.primary_key(), www_google_url)); |
| 1754 | 1813 |
| 1755 urls.clear(); | 1814 urls.clear(); |
| 1756 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); | 1815 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); |
| 1757 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url))); | 1816 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url))); |
| 1758 } | 1817 } |
| 1759 | 1818 |
| 1760 TEST_F(ResourcePrefetchPredictorTest, TestPrecisionRecallHistograms) { | 1819 TEST_F(ResourcePrefetchPredictorTest, TestPrecisionRecallHistograms) { |
| 1761 using testing::_; | 1820 using testing::_; |
| 1762 EXPECT_CALL(*mock_tables_.get(), UpdateData(_, _, _, _)); | 1821 EXPECT_CALL(*mock_tables_.get(), UpdateData(_, _, _, _)).Times(2); |
| 1763 | 1822 |
| 1764 // Fill the database with 3 resources: 1 useful, 2 useless. | 1823 // Fill the database with 3 resources: 1 useful, 2 useless. |
| 1765 const std::string main_frame_url = "http://google.com/?query=cats"; | 1824 const std::string main_frame_url = "http://google.com/?query=cats"; |
| 1766 PrefetchData google = CreatePrefetchData("google.com", 1); | 1825 PrefetchData google = CreatePrefetchData("google.com", 1); |
| 1767 | 1826 |
| 1768 const std::string script_url = "https://cdn.google.com/script.js"; | 1827 const std::string script_url = "https://cdn.google.com/script.js"; |
| 1769 InitializeResourceData(google.add_resources(), script_url, | 1828 InitializeResourceData(google.add_resources(), script_url, |
| 1770 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, | 1829 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, |
| 1771 net::MEDIUM, false, false); | 1830 net::MEDIUM, false, false); |
| 1772 InitializeResourceData(google.add_resources(), script_url + "foo", | 1831 InitializeResourceData(google.add_resources(), script_url + "foo", |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1859 predictor_->host_table_cache_->insert( | 1918 predictor_->host_table_cache_->insert( |
| 1860 std::make_pair(google.primary_key(), google)); | 1919 std::make_pair(google.primary_key(), google)); |
| 1861 | 1920 |
| 1862 predictor_->StartPrefetching(GURL(main_frame_url), PrefetchOrigin::EXTERNAL); | 1921 predictor_->StartPrefetching(GURL(main_frame_url), PrefetchOrigin::EXTERNAL); |
| 1863 predictor_->StopPrefetching(GURL(main_frame_url)); | 1922 predictor_->StopPrefetching(GURL(main_frame_url)); |
| 1864 histogram_tester_->ExpectTotalCount( | 1923 histogram_tester_->ExpectTotalCount( |
| 1865 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1); | 1924 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1); |
| 1866 } | 1925 } |
| 1867 | 1926 |
| 1868 } // namespace predictors | 1927 } // namespace predictors |
| OLD | NEW |