Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: components/precache/content/precache_manager_unittest.cc

Issue 2507753003: Do not precache when the cache size is small (Closed)
Patch Set: Sync'ed to HEAD Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/content/precache_manager.h" 5 #include "components/precache/content/precache_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <map> 9 #include <map>
10 #include <memory> 10 #include <memory>
(...skipping 10 matching lines...) Expand all
21 #include "base/run_loop.h" 21 #include "base/run_loop.h"
22 #include "base/single_thread_task_runner.h" 22 #include "base/single_thread_task_runner.h"
23 #include "base/test/histogram_tester.h" 23 #include "base/test/histogram_tester.h"
24 #include "base/threading/thread_task_runner_handle.h" 24 #include "base/threading/thread_task_runner_handle.h"
25 #include "components/history/core/browser/history_constants.h" 25 #include "components/history/core/browser/history_constants.h"
26 #include "components/history/core/browser/history_service.h" 26 #include "components/history/core/browser/history_service.h"
27 #include "components/history/core/browser/history_types.h" 27 #include "components/history/core/browser/history_types.h"
28 #include "components/precache/core/precache_database.h" 28 #include "components/precache/core/precache_database.h"
29 #include "components/precache/core/precache_switches.h" 29 #include "components/precache/core/precache_switches.h"
30 #include "components/precache/core/proto/unfinished_work.pb.h" 30 #include "components/precache/core/proto/unfinished_work.pb.h"
31 #include "components/variations/variations_params_manager.h"
31 #include "content/public/browser/browser_thread.h" 32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/storage_partition.h"
32 #include "content/public/test/test_browser_context.h" 34 #include "content/public/test/test_browser_context.h"
33 #include "content/public/test/test_browser_thread_bundle.h" 35 #include "content/public/test/test_browser_thread_bundle.h"
36 #include "net/base/test_completion_callback.h"
37 #include "net/disk_cache/simple/simple_backend_impl.h"
34 #include "net/http/http_response_headers.h" 38 #include "net/http/http_response_headers.h"
35 #include "net/http/http_response_info.h" 39 #include "net/http/http_response_info.h"
36 #include "net/http/http_status_code.h" 40 #include "net/http/http_status_code.h"
41 #include "net/test/gtest_util.h"
37 #include "net/url_request/test_url_fetcher_factory.h" 42 #include "net/url_request/test_url_fetcher_factory.h"
38 #include "net/url_request/url_request_status.h" 43 #include "net/url_request/url_request_status.h"
39 #include "net/url_request/url_request_test_util.h" 44 #include "net/url_request/url_request_test_util.h"
40 #include "testing/gmock/include/gmock/gmock.h" 45 #include "testing/gmock/include/gmock/gmock.h"
41 #include "testing/gtest/include/gtest/gtest.h" 46 #include "testing/gtest/include/gtest/gtest.h"
42 #include "url/gurl.h" 47 #include "url/gurl.h"
43 48
44 namespace precache { 49 namespace precache {
45 50
46 namespace { 51 namespace {
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 base::ScopedTempDir scoped_temp_dir_; 221 base::ScopedTempDir scoped_temp_dir_;
217 PrecacheDatabase* precache_database_; 222 PrecacheDatabase* precache_database_;
218 content::TestBrowserContext browser_context_; 223 content::TestBrowserContext browser_context_;
219 std::unique_ptr<PrecacheManagerUnderTest> precache_manager_; 224 std::unique_ptr<PrecacheManagerUnderTest> precache_manager_;
220 TestURLFetcherCallback url_callback_; 225 TestURLFetcherCallback url_callback_;
221 net::FakeURLFetcherFactory factory_; 226 net::FakeURLFetcherFactory factory_;
222 TestPrecacheCompletionCallback precache_callback_; 227 TestPrecacheCompletionCallback precache_callback_;
223 testing::NiceMock<MockHistoryService> history_service_; 228 testing::NiceMock<MockHistoryService> history_service_;
224 base::HistogramTester histograms_; 229 base::HistogramTester histograms_;
225 net::HttpResponseInfo info_; 230 net::HttpResponseInfo info_;
231 variations::testing::VariationParamsManager variation_params_;
226 }; 232 };
227 233
228 TEST_F(PrecacheManagerTest, StartAndFinishPrecaching) { 234 TEST_F(PrecacheManagerTest, StartAndFinishPrecaching) {
229 EXPECT_FALSE(precache_manager_->IsPrecaching()); 235 EXPECT_FALSE(precache_manager_->IsPrecaching());
230 236
231 MockHistoryService::TopHostsCallback top_hosts_callback; 237 MockHistoryService::TopHostsCallback top_hosts_callback;
232 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) 238 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _))
233 .WillOnce(SaveArg<1>(&top_hosts_callback)); 239 .WillOnce(SaveArg<1>(&top_hosts_callback));
234 240
235 factory_.SetFakeResponse(GURL(kConfigURL), "", net::HTTP_OK, 241 factory_.SetFakeResponse(GURL(kConfigURL), "", net::HTTP_OK,
(...skipping 10 matching lines...) Expand all
246 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher. 252 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher.
247 EXPECT_FALSE(precache_manager_->IsPrecaching()); 253 EXPECT_FALSE(precache_manager_->IsPrecaching());
248 EXPECT_TRUE(precache_callback_.was_on_done_called()); 254 EXPECT_TRUE(precache_callback_.was_on_done_called());
249 255
250 std::multiset<GURL> expected_requested_urls; 256 std::multiset<GURL> expected_requested_urls;
251 expected_requested_urls.insert(GURL(kConfigURL)); 257 expected_requested_urls.insert(GURL(kConfigURL));
252 expected_requested_urls.insert(GURL(kGoodManifestURL)); 258 expected_requested_urls.insert(GURL(kGoodManifestURL));
253 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); 259 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());
254 } 260 }
255 261
262 TEST_F(PrecacheManagerTest, StartPrecachingWithGoodSizedCache) {
263 variation_params_.SetVariationParams(kPrecacheFieldTrialName,
264 {{kMinCacheSizeParam, "1"}});
265
266 // Let's store something in the cache so we pass the min_cache_size threshold.
267 disk_cache::Backend* cache_backend;
268 {
269 // Get the CacheBackend.
270 net::TestCompletionCallback cb;
271 net::HttpCache* cache =
272 content::BrowserContext::GetDefaultStoragePartition(&browser_context_)
273 ->GetURLRequestContext()
274 ->GetURLRequestContext()
275 ->http_transaction_factory()
276 ->GetCache();
277 CHECK_NE(nullptr, cache);
278 int rv = cache->GetBackend(&cache_backend, cb.callback());
279 CHECK_EQ(net::OK, cb.GetResult(rv));
280 CHECK_NE(nullptr, cache_backend);
281 CHECK_EQ(cache_backend, cache->GetCurrentBackend());
282 }
283 disk_cache::Entry* entry = nullptr;
284 {
285 // Create a cache Entry.
286 net::TestCompletionCallback cb;
287 int rv = cache_backend->CreateEntry("key", &entry, cb.callback());
288 CHECK_EQ(net::OK, cb.GetResult(rv));
289 CHECK_NE(nullptr, entry);
290 }
291 {
292 // Store some data in the cache Entry.
293 const std::string data(1, 'a');
294 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data));
295 net::TestCompletionCallback cb;
296 int rv = entry->WriteData(0, 0, buffer.get(), buffer->size(), cb.callback(),
297 false);
298 entry->Close();
299 CHECK_EQ(buffer->size(), cb.GetResult(rv));
300 }
301 {
302 // Make sure everything went according to plan.
303 net::TestCompletionCallback cb;
304 int rv = cache_backend->CalculateSizeOfAllEntries(cb.callback());
305 CHECK_LE(1, cb.GetResult(rv));
306 }
307 EXPECT_FALSE(precache_manager_->IsPrecaching());
308
309 precache_manager_->StartPrecaching(precache_callback_.GetCallback());
310 base::RunLoop().RunUntilIdle();
311
312 EXPECT_TRUE(precache_manager_->IsPrecaching());
313 // Now it should be waiting for the top hosts.
314 }
315
316 TEST_F(PrecacheManagerTest, StartPrecachingStopsOnSmallCaches) {
317 // We don't have any entry in the cache, so the reported cache_size = 0 and
318 // thus it will fall below the threshold of 1.
319 variation_params_.SetVariationParams(kPrecacheFieldTrialName,
320 {{kMinCacheSizeParam, "1"}});
321 EXPECT_FALSE(precache_manager_->IsPrecaching());
322
323 precache_manager_->StartPrecaching(precache_callback_.GetCallback());
324 base::RunLoop().RunUntilIdle();
325
326 EXPECT_FALSE(precache_manager_->IsPrecaching());
327 EXPECT_TRUE(precache_callback_.was_on_done_called());
328 EXPECT_TRUE(url_callback_.requested_urls().empty());
329 }
330
256 TEST_F(PrecacheManagerTest, StartAndFinishPrecachingWithUnfinishedHosts) { 331 TEST_F(PrecacheManagerTest, StartAndFinishPrecachingWithUnfinishedHosts) {
257 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( 332 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work(
258 new PrecacheUnfinishedWork()); 333 new PrecacheUnfinishedWork());
259 unfinished_work->add_top_host()->set_hostname("evil-manifest.com"); 334 unfinished_work->add_top_host()->set_hostname("evil-manifest.com");
260 unfinished_work->set_start_time(base::Time::Now().ToInternalValue()); 335 unfinished_work->set_start_time(base::Time::Now().ToInternalValue());
261 precache_database_->SaveUnfinishedWork(std::move(unfinished_work)); 336 precache_database_->SaveUnfinishedWork(std::move(unfinished_work));
262 337
263 EXPECT_FALSE(precache_manager_->IsPrecaching()); 338 EXPECT_FALSE(precache_manager_->IsPrecaching());
264 339
265 factory_.SetFakeResponse(GURL(kConfigURL), "", net::HTTP_OK, 340 factory_.SetFakeResponse(GURL(kConfigURL), "", net::HTTP_OK,
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 good_manifest.add_resource()->set_url("http://good-resource.com"); 423 good_manifest.add_resource()->set_url("http://good-resource.com");
349 424
350 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) 425 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _))
351 .WillOnce(ReturnHosts( 426 .WillOnce(ReturnHosts(
352 history::TopHostsList(1, std::make_pair("starting-url.com", 1)))); 427 history::TopHostsList(1, std::make_pair("starting-url.com", 1))));
353 428
354 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); 429 precache_manager_->StartPrecaching(precache_callback_.GetCallback());
355 430
356 EXPECT_TRUE(precache_manager_->IsPrecaching()); 431 EXPECT_TRUE(precache_manager_->IsPrecaching());
357 // Run a task to get unfinished work, and to get hosts. 432 // Run a task to get unfinished work, and to get hosts.
358 for (int i = 0; i < 2; ++i) { 433 // We need to call run_loop.Run as many times as needed to go through the
434 // chain of callbacks :-(.
435 for (int i = 0; i < 4; ++i) {
359 base::RunLoop run_loop; 436 base::RunLoop run_loop;
360 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 437 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
361 run_loop.QuitClosure()); 438 run_loop.QuitClosure());
362 run_loop.Run(); 439 run_loop.Run();
363 } 440 }
364 // base::RunLoop().RunUntilIdle(); 441 // base::RunLoop().RunUntilIdle();
365 precache_manager_->CancelPrecaching(); 442 precache_manager_->CancelPrecaching();
366 EXPECT_FALSE(precache_manager_->IsPrecaching()); 443 EXPECT_FALSE(precache_manager_->IsPrecaching());
367 444
368 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher. 445 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 EXPECT_TRUE(precache_manager_->IsPrecaching()); 488 EXPECT_TRUE(precache_manager_->IsPrecaching());
412 RecordStatsForPrecacheFetch(GURL("http://url.com"), std::string(), 489 RecordStatsForPrecacheFetch(GURL("http://url.com"), std::string(),
413 base::TimeDelta(), base::Time(), info_, 1000); 490 base::TimeDelta(), base::Time(), info_, 1000);
414 base::RunLoop().RunUntilIdle(); 491 base::RunLoop().RunUntilIdle();
415 precache_manager_->CancelPrecaching(); 492 precache_manager_->CancelPrecaching();
416 493
417 // For PrecacheFetcher and RecordURLPrecached. 494 // For PrecacheFetcher and RecordURLPrecached.
418 base::RunLoop().RunUntilIdle(); 495 base::RunLoop().RunUntilIdle();
419 EXPECT_THAT( 496 EXPECT_THAT(
420 histograms_.GetTotalCountsForPrefix("Precache."), 497 histograms_.GetTotalCountsForPrefix("Precache."),
421 UnorderedElementsAre(Pair("Precache.DownloadedPrecacheMotivated", 1), 498 UnorderedElementsAre(Pair("Precache.CacheSize.AllEntries", 1),
499 Pair("Precache.DownloadedPrecacheMotivated", 1),
422 Pair("Precache.Fetch.PercentCompleted", 1), 500 Pair("Precache.Fetch.PercentCompleted", 1),
423 Pair("Precache.Fetch.ResponseBytes.Network", 1), 501 Pair("Precache.Fetch.ResponseBytes.Network", 1),
424 Pair("Precache.Fetch.ResponseBytes.Total", 1), 502 Pair("Precache.Fetch.ResponseBytes.Total", 1),
425 Pair("Precache.Fetch.TimeToComplete", 1), 503 Pair("Precache.Fetch.TimeToComplete", 1),
426 Pair("Precache.Latency.Prefetch", 1), 504 Pair("Precache.Latency.Prefetch", 1),
427 Pair("Precache.Freshness.Prefetch", 1))); 505 Pair("Precache.Freshness.Prefetch", 1)));
428 } 506 }
429 507
430 TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTP) { 508 TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTP) {
431 precache_manager_->RecordStatsForFetch(GURL("http://http-url.com"), GURL(), 509 precache_manager_->RecordStatsForFetch(GURL("http://http-url.com"), GURL(),
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 // Precache a bunch of URLs, with different fetch times. 569 // Precache a bunch of URLs, with different fetch times.
492 RecordStatsForPrecacheFetch( 570 RecordStatsForPrecacheFetch(
493 GURL("http://old-fetch.com"), std::string(), base::TimeDelta(), 571 GURL("http://old-fetch.com"), std::string(), base::TimeDelta(),
494 kCurrentTime - base::TimeDelta::FromDays(61), info_, 1000); 572 kCurrentTime - base::TimeDelta::FromDays(61), info_, 1000);
495 RecordStatsForPrecacheFetch( 573 RecordStatsForPrecacheFetch(
496 GURL("http://recent-fetch.com"), std::string(), base::TimeDelta(), 574 GURL("http://recent-fetch.com"), std::string(), base::TimeDelta(),
497 kCurrentTime - base::TimeDelta::FromDays(59), info_, 1000); 575 kCurrentTime - base::TimeDelta::FromDays(59), info_, 1000);
498 RecordStatsForPrecacheFetch( 576 RecordStatsForPrecacheFetch(
499 GURL("http://yesterday-fetch.com"), std::string(), base::TimeDelta(), 577 GURL("http://yesterday-fetch.com"), std::string(), base::TimeDelta(),
500 kCurrentTime - base::TimeDelta::FromDays(1), info_, 1000); 578 kCurrentTime - base::TimeDelta::FromDays(1), info_, 1000);
579 expected_histogram_count_map["Precache.CacheSize.AllEntries"]++;
501 expected_histogram_count_map["Precache.DownloadedPrecacheMotivated"] += 3; 580 expected_histogram_count_map["Precache.DownloadedPrecacheMotivated"] += 3;
502 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++; 581 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++;
503 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++; 582 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++;
504 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++; 583 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++;
505 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++; 584 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++;
506 expected_histogram_count_map["Precache.Latency.Prefetch"] += 3; 585 expected_histogram_count_map["Precache.Latency.Prefetch"] += 3;
507 expected_histogram_count_map["Precache.Freshness.Prefetch"] += 3; 586 expected_histogram_count_map["Precache.Freshness.Prefetch"] += 3;
508 base::RunLoop().RunUntilIdle(); 587 base::RunLoop().RunUntilIdle();
509 588
510 precache_manager_->CancelPrecaching(); 589 precache_manager_->CancelPrecaching();
511 base::RunLoop().RunUntilIdle(); 590 base::RunLoop().RunUntilIdle();
512 591
513 // Disable pause-resume. 592 // Disable pause-resume.
514 precache_database_->DeleteUnfinishedWork(); 593 precache_database_->DeleteUnfinishedWork();
515 base::RunLoop().RunUntilIdle(); 594 base::RunLoop().RunUntilIdle();
516 595
517 // For PrecacheFetcher and RecordURLPrecached. 596 // For PrecacheFetcher and RecordURLPrecached.
518 base::RunLoop().RunUntilIdle(); 597 base::RunLoop().RunUntilIdle();
519 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), 598 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."),
520 ContainerEq(expected_histogram_count_map)); 599 ContainerEq(expected_histogram_count_map));
521 600
522 // The expired precache will be deleted during precaching this time. 601 // The expired precache will be deleted during precaching this time.
523 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); 602 precache_manager_->StartPrecaching(precache_callback_.GetCallback());
524 EXPECT_TRUE(precache_manager_->IsPrecaching()); 603 EXPECT_TRUE(precache_manager_->IsPrecaching());
525 base::RunLoop().RunUntilIdle(); 604 base::RunLoop().RunUntilIdle();
526 605
527 // The precache fetcher runs until done, which records these histograms, 606 // The precache fetcher runs until done, which records these histograms,
528 // and then cancels precaching, which records these histograms again. 607 // and then cancels precaching, which records these histograms again.
529 // In practice 608 // In practice
609 expected_histogram_count_map["Precache.CacheSize.AllEntries"]++;
530 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++; 610 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++;
531 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++; 611 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++;
532 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++; 612 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++;
533 // For PrecacheFetcher and RecordURLPrecached. 613 // For PrecacheFetcher and RecordURLPrecached.
534 base::RunLoop().RunUntilIdle(); 614 base::RunLoop().RunUntilIdle();
535 EXPECT_FALSE(precache_manager_->IsPrecaching()); 615 EXPECT_FALSE(precache_manager_->IsPrecaching());
536 616
537 // A fetch for the same URL as the expired precache was served from the cache, 617 // A fetch for the same URL as the expired precache was served from the cache,
538 // but it isn't reported as saved bytes because it had expired in the precache 618 // but it isn't reported as saved bytes because it had expired in the precache
539 // history. 619 // history.
(...skipping 27 matching lines...) Expand all
567 expected_histogram_count_map["Precache.Saved"] += 2; 647 expected_histogram_count_map["Precache.Saved"] += 2;
568 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2; 648 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2;
569 expected_histogram_count_map["Precache.Saved.Freshness"] = 2; 649 expected_histogram_count_map["Precache.Saved.Freshness"] = 2;
570 650
571 base::RunLoop().RunUntilIdle(); 651 base::RunLoop().RunUntilIdle();
572 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), 652 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."),
573 ContainerEq(expected_histogram_count_map)); 653 ContainerEq(expected_histogram_count_map));
574 } 654 }
575 655
576 } // namespace precache 656 } // namespace precache
OLDNEW
« no previous file with comments | « components/precache/content/precache_manager.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698