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

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor.cc

Issue 2755093002: predictors: Mark before_first_contentful_paint for resources fetched before fcp. (Closed)
Patch Set: Just check db. Created 3 years, 7 months 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 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 <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 first_occurrence(0) {} 462 first_occurrence(0) {}
463 463
464 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary( 464 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary(
465 const OriginRequestSummary& other) = default; 465 const OriginRequestSummary& other) = default;
466 466
467 ResourcePrefetchPredictor::OriginRequestSummary::~OriginRequestSummary() {} 467 ResourcePrefetchPredictor::OriginRequestSummary::~OriginRequestSummary() {}
468 468
469 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() 469 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary()
470 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), 470 : resource_type(content::RESOURCE_TYPE_LAST_TYPE),
471 priority(net::IDLE), 471 priority(net::IDLE),
472 response_time(base::TimeTicks()),
alexilin 2017/04/28 11:22:04 nit: Unnecessary call of the copy constructor. Ple
trevordixon 2017/04/28 12:39:54 Deleted.
473 before_first_contentful_paint(false),
472 was_cached(false), 474 was_cached(false),
473 has_validators(false), 475 has_validators(false),
474 always_revalidate(false), 476 always_revalidate(false),
475 is_no_store(false), 477 is_no_store(false),
476 network_accessed(false) {} 478 network_accessed(false) {}
477 479
478 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary( 480 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary(
479 const URLRequestSummary& other) = default; 481 const URLRequestSummary& other) = default;
480 482
481 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() { 483 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() {
482 } 484 }
483 485
484 // static 486 // static
485 bool ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( 487 bool ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse(
486 const net::URLRequest& request, 488 const net::URLRequest& request,
487 URLRequestSummary* summary) { 489 URLRequestSummary* summary) {
488 const content::ResourceRequestInfo* request_info = 490 const content::ResourceRequestInfo* request_info =
489 content::ResourceRequestInfo::ForRequest(&request); 491 content::ResourceRequestInfo::ForRequest(&request);
490 if (!request_info) 492 if (!request_info)
491 return false; 493 return false;
492 494
495 // This method is called when the response is started, so this field reflects
496 // the time at which the response began, not when it finished, as would
497 // arguably be ideal. This means if firstContentfulPaint happens after the
498 // response has started, but before it's finished, we will erroneously mark
499 // the resource as having been loaded before firstContentfulPaint. This is
500 // a rare and insignificant enough occurrence that we opt to record the time
501 // here for the sake of simplicity.
502 summary->response_time = base::TimeTicks::Now();
493 summary->resource_url = request.original_url(); 503 summary->resource_url = request.original_url();
494 summary->request_url = request.url(); 504 summary->request_url = request.url();
495 content::ResourceType resource_type_from_request = 505 content::ResourceType resource_type_from_request =
496 request_info->GetResourceType(); 506 request_info->GetResourceType();
497 summary->priority = request.priority(); 507 summary->priority = request.priority();
498 request.GetMimeType(&summary->mime_type); 508 request.GetMimeType(&summary->mime_type);
499 summary->was_cached = request.was_cached(); 509 summary->was_cached = request.was_cached();
500 summary->resource_type = 510 summary->resource_type =
501 GetResourceType(resource_type_from_request, summary->mime_type); 511 GetResourceType(resource_type_from_request, summary->mime_type);
502 512
503 scoped_refptr<net::HttpResponseHeaders> headers = 513 scoped_refptr<net::HttpResponseHeaders> headers =
504 request.response_info().headers; 514 request.response_info().headers;
505 if (headers.get()) { 515 if (headers.get()) {
506 summary->has_validators = headers->HasValidators(); 516 summary->has_validators = headers->HasValidators();
507 // RFC 2616, section 14.9. 517 // RFC 2616, section 14.9.
508 summary->always_revalidate = 518 summary->always_revalidate =
509 headers->HasHeaderValue("cache-control", "no-cache") || 519 headers->HasHeaderValue("cache-control", "no-cache") ||
510 headers->HasHeaderValue("pragma", "no-cache") || 520 headers->HasHeaderValue("pragma", "no-cache") ||
511 headers->HasHeaderValue("vary", "*"); 521 headers->HasHeaderValue("vary", "*");
512 summary->is_no_store = IsNoStore(request); 522 summary->is_no_store = IsNoStore(request);
513 } 523 }
514 summary->network_accessed = request.response_info().network_accessed; 524 summary->network_accessed = request.response_info().network_accessed;
515 return true; 525 return true;
516 } 526 }
517 527
518 ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary( 528 ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary(
519 const GURL& i_main_frame_url) 529 const GURL& i_main_frame_url)
520 : main_frame_url(i_main_frame_url), initial_url(i_main_frame_url) {} 530 : main_frame_url(i_main_frame_url),
531 initial_url(i_main_frame_url),
532 first_contentful_paint(base::TimeTicks::Max()) {}
521 533
522 ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary( 534 ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary(
523 const PageRequestSummary& other) = default; 535 const PageRequestSummary& other) = default;
524 536
525 ResourcePrefetchPredictor::PageRequestSummary::~PageRequestSummary() {} 537 ResourcePrefetchPredictor::PageRequestSummary::~PageRequestSummary() {}
526 538
527 ResourcePrefetchPredictor::Prediction::Prediction() = default; 539 ResourcePrefetchPredictor::Prediction::Prediction() = default;
528 540
529 ResourcePrefetchPredictor::Prediction::Prediction( 541 ResourcePrefetchPredictor::Prediction::Prediction(
530 const ResourcePrefetchPredictor::Prediction& other) = default; 542 const ResourcePrefetchPredictor::Prediction& other) = default;
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 // corresponding to the navigation has not been created yet. 650 // corresponding to the navigation has not been created yet.
639 if (!navigation_id.main_frame_url.is_empty()) 651 if (!navigation_id.main_frame_url.is_empty())
640 OnNavigationComplete(navigation_id); 652 OnNavigationComplete(navigation_id);
641 break; 653 break;
642 default: 654 default:
643 NOTREACHED() << "Unexpected initialization_state_: " 655 NOTREACHED() << "Unexpected initialization_state_: "
644 << initialization_state_; 656 << initialization_state_;
645 } 657 }
646 } 658 }
647 659
660 void ResourcePrefetchPredictor::RecordFirstContentfulPaint(
661 const NavigationID& navigation_id,
662 const base::TimeTicks& first_contentful_paint) {
663 DCHECK_CURRENTLY_ON(BrowserThread::UI);
664 if (initialization_state_ != INITIALIZED)
665 return;
666
667 NavigationMap::iterator nav_it = inflight_navigations_.find(navigation_id);
668 if (nav_it != inflight_navigations_.end())
669 nav_it->second->first_contentful_paint = first_contentful_paint;
670 }
671
648 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url, 672 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url,
649 PrefetchOrigin origin) { 673 PrefetchOrigin origin) {
650 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StartPrefetching", "url", 674 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StartPrefetching", "url",
651 url.spec()); 675 url.spec());
652 // Save prefetch start time to report prefetching duration. 676 // Save prefetch start time to report prefetching duration.
653 if (inflight_prefetches_.find(url) == inflight_prefetches_.end() && 677 if (inflight_prefetches_.find(url) == inflight_prefetches_.end() &&
654 IsUrlPrefetchable(url)) { 678 IsUrlPrefetchable(url)) {
655 inflight_prefetches_.insert(std::make_pair(url, base::TimeTicks::Now())); 679 inflight_prefetches_.insert(std::make_pair(url, base::TimeTicks::Now()));
656 } 680 }
657 681
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 861
838 NavigationMap::iterator nav_it = 862 NavigationMap::iterator nav_it =
839 inflight_navigations_.find(nav_id_without_timing_info); 863 inflight_navigations_.find(nav_id_without_timing_info);
840 if (nav_it == inflight_navigations_.end()) 864 if (nav_it == inflight_navigations_.end())
841 return; 865 return;
842 866
843 // Remove the navigation from the inflight navigations. 867 // Remove the navigation from the inflight navigations.
844 std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second); 868 std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second);
845 inflight_navigations_.erase(nav_it); 869 inflight_navigations_.erase(nav_it);
846 870
871 // Set before_first_contentful paint for each resource.
872 for (auto& request_summary : summary->subresource_requests) {
873 request_summary.before_first_contentful_paint =
874 request_summary.response_time < summary->first_contentful_paint;
875 }
876
847 const GURL& initial_url = summary->initial_url; 877 const GURL& initial_url = summary->initial_url;
848 ResourcePrefetchPredictor::Prediction prediction; 878 ResourcePrefetchPredictor::Prediction prediction;
849 bool has_data = GetPrefetchData(initial_url, &prediction); 879 bool has_data = GetPrefetchData(initial_url, &prediction);
850 if (has_data) 880 if (has_data)
851 ReportPredictionAccuracy(prediction, *summary); 881 ReportPredictionAccuracy(prediction, *summary);
852 882
853 auto it = prefetcher_stats_.find(initial_url); 883 auto it = prefetcher_stats_.find(initial_url);
854 if (it != prefetcher_stats_.end()) { 884 if (it != prefetcher_stats_.end()) {
855 const std::vector<URLRequestSummary>& summaries = 885 const std::vector<URLRequestSummary>& summaries =
856 summary->subresource_requests; 886 summary->subresource_requests;
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after
1336 continue; 1366 continue;
1337 1367
1338 ResourceData* resource_to_add = data.add_resources(); 1368 ResourceData* resource_to_add = data.add_resources();
1339 resource_to_add->set_resource_url(summary.resource_url.spec()); 1369 resource_to_add->set_resource_url(summary.resource_url.spec());
1340 resource_to_add->set_resource_type( 1370 resource_to_add->set_resource_type(
1341 static_cast<ResourceData::ResourceType>(summary.resource_type)); 1371 static_cast<ResourceData::ResourceType>(summary.resource_type));
1342 resource_to_add->set_number_of_hits(1); 1372 resource_to_add->set_number_of_hits(1);
1343 resource_to_add->set_average_position(i + 1); 1373 resource_to_add->set_average_position(i + 1);
1344 resource_to_add->set_priority( 1374 resource_to_add->set_priority(
1345 static_cast<ResourceData::Priority>(summary.priority)); 1375 static_cast<ResourceData::Priority>(summary.priority));
1376 resource_to_add->set_before_first_contentful_paint(
1377 summary.before_first_contentful_paint);
1346 resource_to_add->set_has_validators(summary.has_validators); 1378 resource_to_add->set_has_validators(summary.has_validators);
1347 resource_to_add->set_always_revalidate(summary.always_revalidate); 1379 resource_to_add->set_always_revalidate(summary.always_revalidate);
1348 1380
1349 resources_seen.insert(summary.resource_url); 1381 resources_seen.insert(summary.resource_url);
1350 } 1382 }
1351 } else { 1383 } else {
1352 PrefetchData& data = cache_entry->second; 1384 PrefetchData& data = cache_entry->second;
1353 data.set_last_visit_time(base::Time::Now().ToInternalValue()); 1385 data.set_last_visit_time(base::Time::Now().ToInternalValue());
1354 1386
1355 // Build indices over the data. 1387 // Build indices over the data.
(...skipping 29 matching lines...) Expand all
1385 1417
1386 // Update the resource type since it could have changed. 1418 // Update the resource type since it could have changed.
1387 if (new_summary.resource_type != content::RESOURCE_TYPE_LAST_TYPE) { 1419 if (new_summary.resource_type != content::RESOURCE_TYPE_LAST_TYPE) {
1388 old_resource->set_resource_type( 1420 old_resource->set_resource_type(
1389 static_cast<ResourceData::ResourceType>( 1421 static_cast<ResourceData::ResourceType>(
1390 new_summary.resource_type)); 1422 new_summary.resource_type));
1391 } 1423 }
1392 1424
1393 old_resource->set_priority( 1425 old_resource->set_priority(
1394 static_cast<ResourceData::Priority>(new_summary.priority)); 1426 static_cast<ResourceData::Priority>(new_summary.priority));
1427 old_resource->set_before_first_contentful_paint(
1428 new_summary.before_first_contentful_paint);
1395 1429
1396 int position = new_index[resource_url] + 1; 1430 int position = new_index[resource_url] + 1;
1397 int total = 1431 int total =
1398 old_resource->number_of_hits() + old_resource->number_of_misses(); 1432 old_resource->number_of_hits() + old_resource->number_of_misses();
1399 old_resource->set_average_position( 1433 old_resource->set_average_position(
1400 ((old_resource->average_position() * total) + position) / 1434 ((old_resource->average_position() * total) + position) /
1401 (total + 1)); 1435 (total + 1));
1402 old_resource->set_number_of_hits(old_resource->number_of_hits() + 1); 1436 old_resource->set_number_of_hits(old_resource->number_of_hits() + 1);
1403 old_resource->set_consecutive_misses(0); 1437 old_resource->set_consecutive_misses(0);
1404 } 1438 }
1405 } 1439 }
1406 1440
1407 // Add the new ones that we have not seen before. 1441 // Add the new ones that we have not seen before.
1408 for (int i = 0; i < new_resources_size; ++i) { 1442 for (int i = 0; i < new_resources_size; ++i) {
1409 const URLRequestSummary& summary = new_resources[i]; 1443 const URLRequestSummary& summary = new_resources[i];
1410 if (old_index.find(summary.resource_url) != old_index.end()) 1444 if (old_index.find(summary.resource_url) != old_index.end())
1411 continue; 1445 continue;
1412 1446
1413 // Only need to add new stuff. 1447 // Only need to add new stuff.
1414 ResourceData* resource_to_add = data.add_resources(); 1448 ResourceData* resource_to_add = data.add_resources();
1415 resource_to_add->set_resource_url(summary.resource_url.spec()); 1449 resource_to_add->set_resource_url(summary.resource_url.spec());
1416 resource_to_add->set_resource_type( 1450 resource_to_add->set_resource_type(
1417 static_cast<ResourceData::ResourceType>(summary.resource_type)); 1451 static_cast<ResourceData::ResourceType>(summary.resource_type));
1418 resource_to_add->set_number_of_hits(1); 1452 resource_to_add->set_number_of_hits(1);
1419 resource_to_add->set_average_position(i + 1); 1453 resource_to_add->set_average_position(i + 1);
1420 resource_to_add->set_priority( 1454 resource_to_add->set_priority(
1421 static_cast<ResourceData::Priority>(summary.priority)); 1455 static_cast<ResourceData::Priority>(summary.priority));
1456 resource_to_add->set_before_first_contentful_paint(
1457 summary.before_first_contentful_paint);
1422 resource_to_add->set_has_validators(new_resources[i].has_validators); 1458 resource_to_add->set_has_validators(new_resources[i].has_validators);
1423 resource_to_add->set_always_revalidate( 1459 resource_to_add->set_always_revalidate(
1424 new_resources[i].always_revalidate); 1460 new_resources[i].always_revalidate);
1425 1461
1426 // To ensure we dont add the same url twice. 1462 // To ensure we dont add the same url twice.
1427 old_index[summary.resource_url] = 0; 1463 old_index[summary.resource_url] = 0;
1428 } 1464 }
1429 } 1465 }
1430 1466
1431 PrefetchData& data = cache_entry->second; 1467 PrefetchData& data = cache_entry->second;
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
1800 TestObserver::~TestObserver() { 1836 TestObserver::~TestObserver() {
1801 predictor_->SetObserverForTesting(nullptr); 1837 predictor_->SetObserverForTesting(nullptr);
1802 } 1838 }
1803 1839
1804 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) 1840 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor)
1805 : predictor_(predictor) { 1841 : predictor_(predictor) {
1806 predictor_->SetObserverForTesting(this); 1842 predictor_->SetObserverForTesting(this);
1807 } 1843 }
1808 1844
1809 } // namespace predictors 1845 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698