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 |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/test/histogram_tester.h" |
14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
15 #include "chrome/browser/history/history_service_factory.h" | 16 #include "chrome/browser/history/history_service_factory.h" |
16 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 17 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
17 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | 18 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
18 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
19 #include "components/history/core/browser/history_service.h" | 20 #include "components/history/core/browser/history_service.h" |
20 #include "components/history/core/browser/history_types.h" | 21 #include "components/history/core/browser/history_types.h" |
21 #include "components/sessions/core/session_id.h" | 22 #include "components/sessions/core/session_id.h" |
22 #include "content/public/browser/resource_request_info.h" | 23 #include "content/public/browser/resource_request_info.h" |
23 #include "content/public/common/previews_state.h" | 24 #include "content/public/common/previews_state.h" |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 | 259 |
259 PrefetchDataMap test_url_data_; | 260 PrefetchDataMap test_url_data_; |
260 PrefetchDataMap test_host_data_; | 261 PrefetchDataMap test_host_data_; |
261 RedirectDataMap test_url_redirect_data_; | 262 RedirectDataMap test_url_redirect_data_; |
262 RedirectDataMap test_host_redirect_data_; | 263 RedirectDataMap test_host_redirect_data_; |
263 PrefetchData empty_resource_data_; | 264 PrefetchData empty_resource_data_; |
264 RedirectData empty_redirect_data_; | 265 RedirectData empty_redirect_data_; |
265 | 266 |
266 MockURLRequestJobFactory url_request_job_factory_; | 267 MockURLRequestJobFactory url_request_job_factory_; |
267 EmptyURLRequestDelegate url_request_delegate_; | 268 EmptyURLRequestDelegate url_request_delegate_; |
| 269 |
| 270 std::unique_ptr<base::HistogramTester> histogram_tester_; |
268 }; | 271 }; |
269 | 272 |
270 ResourcePrefetchPredictorTest::ResourcePrefetchPredictorTest() | 273 ResourcePrefetchPredictorTest::ResourcePrefetchPredictorTest() |
271 : thread_bundle_(), | 274 : thread_bundle_(), |
272 profile_(new TestingProfile()), | 275 profile_(new TestingProfile()), |
273 mock_tables_(new StrictMock<MockResourcePrefetchPredictorTables>()), | 276 mock_tables_(new StrictMock<MockResourcePrefetchPredictorTables>()), |
274 empty_resource_data_(), | 277 empty_resource_data_(), |
275 empty_redirect_data_() {} | 278 empty_redirect_data_() {} |
276 | 279 |
277 ResourcePrefetchPredictorTest::~ResourcePrefetchPredictorTest() { | 280 ResourcePrefetchPredictorTest::~ResourcePrefetchPredictorTest() { |
(...skipping 16 matching lines...) Expand all Loading... |
294 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), | 297 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), |
295 Pointee(ContainerEq(PrefetchDataMap())), | 298 Pointee(ContainerEq(PrefetchDataMap())), |
296 Pointee(ContainerEq(RedirectDataMap())), | 299 Pointee(ContainerEq(RedirectDataMap())), |
297 Pointee(ContainerEq(RedirectDataMap())))); | 300 Pointee(ContainerEq(RedirectDataMap())))); |
298 InitializePredictor(); | 301 InitializePredictor(); |
299 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); | 302 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); |
300 EXPECT_EQ(predictor_->initialization_state_, | 303 EXPECT_EQ(predictor_->initialization_state_, |
301 ResourcePrefetchPredictor::INITIALIZED); | 304 ResourcePrefetchPredictor::INITIALIZED); |
302 | 305 |
303 url_request_context_.set_job_factory(&url_request_job_factory_); | 306 url_request_context_.set_job_factory(&url_request_job_factory_); |
| 307 |
| 308 histogram_tester_.reset(new base::HistogramTester()); |
304 } | 309 } |
305 | 310 |
306 void ResourcePrefetchPredictorTest::TearDown() { | 311 void ResourcePrefetchPredictorTest::TearDown() { |
307 predictor_.reset(NULL); | 312 predictor_.reset(NULL); |
308 profile_->DestroyHistoryService(); | 313 profile_->DestroyHistoryService(); |
309 } | 314 } |
310 | 315 |
311 void ResourcePrefetchPredictorTest::InitializeSampleData() { | 316 void ResourcePrefetchPredictorTest::InitializeSampleData() { |
312 { // Url data. | 317 { // Url data. |
313 PrefetchData google = CreatePrefetchData("http://www.google.com/", 1); | 318 PrefetchData google = CreatePrefetchData("http://www.google.com/", 1); |
(...skipping 1232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1546 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1, | 1551 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1, |
1547 net::MEDIUM, false, false); | 1552 net::MEDIUM, false, false); |
1548 predictor_->url_table_cache_->insert( | 1553 predictor_->url_table_cache_->insert( |
1549 std::make_pair(www_google_url.primary_key(), www_google_url)); | 1554 std::make_pair(www_google_url.primary_key(), www_google_url)); |
1550 | 1555 |
1551 urls.clear(); | 1556 urls.clear(); |
1552 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); | 1557 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); |
1553 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url))); | 1558 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url))); |
1554 } | 1559 } |
1555 | 1560 |
| 1561 TEST_F(ResourcePrefetchPredictorTest, TestPrecisionRecallHistograms) { |
| 1562 using testing::_; |
| 1563 EXPECT_CALL(*mock_tables_.get(), UpdateData(_, _, _, _)); |
| 1564 |
| 1565 // Fill the database with 3 resources: 1 useful, 2 useless. |
| 1566 const std::string main_frame_url = "http://google.com/?query=cats"; |
| 1567 PrefetchData google = CreatePrefetchData("google.com", 1); |
| 1568 |
| 1569 const std::string script_url = "https://cdn.google.com/script.js"; |
| 1570 InitializeResourceData(google.add_resources(), script_url, |
| 1571 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, |
| 1572 net::MEDIUM, false, false); |
| 1573 InitializeResourceData(google.add_resources(), script_url + "foo", |
| 1574 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, |
| 1575 net::MEDIUM, false, false); |
| 1576 InitializeResourceData(google.add_resources(), script_url + "bar", |
| 1577 content::RESOURCE_TYPE_SCRIPT, 10, 0, 1, 2.1, |
| 1578 net::MEDIUM, false, false); |
| 1579 predictor_->host_table_cache_->insert( |
| 1580 std::make_pair(google.primary_key(), google)); |
| 1581 |
| 1582 std::vector<GURL> urls; |
| 1583 EXPECT_TRUE(predictor_->GetPrefetchData(GURL(main_frame_url), &urls)); |
| 1584 |
| 1585 // Simulate a navigation with 2 resources, one we know, one we don't. |
| 1586 URLRequestSummary main_frame = CreateURLRequestSummary(1, main_frame_url); |
| 1587 predictor_->RecordURLRequest(main_frame); |
| 1588 |
| 1589 URLRequestSummary script = CreateURLRequestSummary( |
| 1590 1, main_frame_url, script_url, content::RESOURCE_TYPE_SCRIPT); |
| 1591 predictor_->RecordURLResponse(script); |
| 1592 |
| 1593 URLRequestSummary new_script = CreateURLRequestSummary( |
| 1594 1, main_frame_url, script_url + "2", content::RESOURCE_TYPE_SCRIPT); |
| 1595 predictor_->RecordURLResponse(new_script); |
| 1596 |
| 1597 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
| 1598 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 1599 |
| 1600 histogram_tester_->ExpectBucketCount( |
| 1601 internal::kResourcePrefetchPredictorRecallHistogram, 50, 1); |
| 1602 histogram_tester_->ExpectBucketCount( |
| 1603 internal::kResourcePrefetchPredictorPrecisionHistogram, 33, 1); |
| 1604 } |
| 1605 |
1556 } // namespace predictors | 1606 } // namespace predictors |
OLD | NEW |