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 <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/metrics/sparse_histogram.h" | |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "chrome/browser/chrome_notification_types.h" | 18 #include "chrome/browser/chrome_notification_types.h" |
18 #include "chrome/browser/history/history_database.h" | 19 #include "chrome/browser/history/history_database.h" |
19 #include "chrome/browser/history/history_db_task.h" | 20 #include "chrome/browser/history/history_db_task.h" |
20 #include "chrome/browser/history/history_notifications.h" | 21 #include "chrome/browser/history/history_notifications.h" |
21 #include "chrome/browser/history/history_service.h" | 22 #include "chrome/browser/history/history_service.h" |
22 #include "chrome/browser/history/history_service_factory.h" | 23 #include "chrome/browser/history/history_service_factory.h" |
23 #include "chrome/browser/predictors/predictor_database.h" | 24 #include "chrome/browser/predictors/predictor_database.h" |
24 #include "chrome/browser/predictors/predictor_database_factory.h" | 25 #include "chrome/browser/predictors/predictor_database_factory.h" |
25 #include "chrome/browser/predictors/resource_prefetcher_manager.h" | 26 #include "chrome/browser/predictors/resource_prefetcher_manager.h" |
26 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
27 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
28 #include "chrome/common/url_constants.h" | 29 #include "chrome/common/url_constants.h" |
29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
30 #include "content/public/browser/navigation_controller.h" | 31 #include "content/public/browser/navigation_controller.h" |
31 #include "content/public/browser/notification_service.h" | 32 #include "content/public/browser/notification_service.h" |
32 #include "content/public/browser/notification_source.h" | 33 #include "content/public/browser/notification_source.h" |
33 #include "content/public/browser/notification_types.h" | 34 #include "content/public/browser/notification_types.h" |
34 #include "content/public/browser/resource_request_info.h" | 35 #include "content/public/browser/resource_request_info.h" |
35 #include "content/public/browser/web_contents.h" | 36 #include "content/public/browser/web_contents.h" |
36 #include "net/base/mime_util.h" | 37 #include "net/base/mime_util.h" |
38 #include "net/base/network_change_notifier.h" | |
37 #include "net/http/http_response_headers.h" | 39 #include "net/http/http_response_headers.h" |
38 #include "net/url_request/url_request.h" | 40 #include "net/url_request/url_request.h" |
39 #include "net/url_request/url_request_context_getter.h" | 41 #include "net/url_request/url_request_context_getter.h" |
40 | 42 |
41 using content::BrowserThread; | 43 using content::BrowserThread; |
42 | 44 |
43 namespace { | 45 namespace { |
44 | 46 |
45 // For reporting whether a subresource is handled or not, and for what reasons. | 47 // For reporting whether a subresource is handled or not, and for what reasons. |
46 enum ResourceStatus { | 48 enum ResourceStatus { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED = 1, | 85 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED = 1, |
84 REPORTING_EVENT_COUNT = 2 | 86 REPORTING_EVENT_COUNT = 2 |
85 }; | 87 }; |
86 | 88 |
87 void RecordNavigationEvent(NavigationEvent event) { | 89 void RecordNavigationEvent(NavigationEvent event) { |
88 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.NavigationEvent", | 90 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.NavigationEvent", |
89 event, | 91 event, |
90 NAVIGATION_EVENT_COUNT); | 92 NAVIGATION_EVENT_COUNT); |
91 } | 93 } |
92 | 94 |
95 enum AdditionalConnectionType { | |
96 CONNECTION_CELLULAR = -1, | |
97 CONNECTION_ALL = -2 | |
98 }; | |
99 | |
100 std::string GetNetTypeStr() { | |
101 switch (net::NetworkChangeNotifier::GetConnectionType()) { | |
102 case net::NetworkChangeNotifier::CONNECTION_ETHERNET: | |
103 return "Ethernet"; | |
104 case net::NetworkChangeNotifier::CONNECTION_WIFI: | |
105 return "WiFi"; | |
106 case net::NetworkChangeNotifier::CONNECTION_2G: | |
107 return "2G"; | |
108 case net::NetworkChangeNotifier::CONNECTION_3G: | |
109 return "3G"; | |
110 case net::NetworkChangeNotifier::CONNECTION_4G: | |
111 return "4G"; | |
112 case net::NetworkChangeNotifier::CONNECTION_NONE: | |
113 return "None"; | |
114 case net::NetworkChangeNotifier::CONNECTION_BLUETOOTH: | |
115 return "Bluetooth"; | |
116 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN: | |
117 default: | |
118 break; | |
119 } | |
120 return "Unknown"; | |
121 } | |
122 | |
123 void ReportPrefetchedNetworkType(int type) { | |
124 UMA_HISTOGRAM_SPARSE_SLOWLY( | |
125 "ResourcePrefetchPredictor.PrefetchedNetworkType", | |
126 type); | |
127 } | |
128 | |
129 void ReportNotPrefetchedNetworkType(int type) { | |
130 UMA_HISTOGRAM_SPARSE_SLOWLY( | |
131 "ResourcePrefetchPredictor.NotPrefetchedNetworkType", | |
132 type); | |
133 } | |
134 | |
93 } // namespace | 135 } // namespace |
94 | 136 |
95 namespace predictors { | 137 namespace predictors { |
96 | 138 |
97 //////////////////////////////////////////////////////////////////////////////// | 139 //////////////////////////////////////////////////////////////////////////////// |
98 // History lookup task. | 140 // History lookup task. |
99 | 141 |
100 // Used to fetch the visit count for a URL from the History database. | 142 // Used to fetch the visit count for a URL from the History database. |
101 class GetUrlVisitCountTask : public history::HistoryDBTask { | 143 class GetUrlVisitCountTask : public history::HistoryDBTask { |
102 public: | 144 public: |
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
518 | 560 |
519 NavigationMap::iterator nav_it = | 561 NavigationMap::iterator nav_it = |
520 inflight_navigations_.find(navigation_id); | 562 inflight_navigations_.find(navigation_id); |
521 if (nav_it == inflight_navigations_.end()) { | 563 if (nav_it == inflight_navigations_.end()) { |
522 RecordNavigationEvent(NAVIGATION_EVENT_ONLOAD_UNTRACKED_URL); | 564 RecordNavigationEvent(NAVIGATION_EVENT_ONLOAD_UNTRACKED_URL); |
523 return; | 565 return; |
524 } | 566 } |
525 RecordNavigationEvent(NAVIGATION_EVENT_ONLOAD_TRACKED_URL); | 567 RecordNavigationEvent(NAVIGATION_EVENT_ONLOAD_TRACKED_URL); |
526 | 568 |
527 // Report any stats. | 569 // Report any stats. |
570 base::TimeDelta plt = base::TimeTicks::Now() - navigation_id.creation_time; | |
571 ReportPageLoadTimeStats(plt); | |
528 if (prefetch_manager_.get()) { | 572 if (prefetch_manager_.get()) { |
529 ResultsMap::iterator results_it = results_map_.find(navigation_id); | 573 ResultsMap::iterator results_it = results_map_.find(navigation_id); |
530 bool have_prefetch_results = results_it != results_map_.end(); | 574 bool have_prefetch_results = results_it != results_map_.end(); |
531 UMA_HISTOGRAM_BOOLEAN("ResourcePrefetchPredictor.HavePrefetchResults", | 575 UMA_HISTOGRAM_BOOLEAN("ResourcePrefetchPredictor.HavePrefetchResults", |
532 have_prefetch_results); | 576 have_prefetch_results); |
533 if (have_prefetch_results) { | 577 if (have_prefetch_results) { |
534 ReportAccuracyStats(results_it->second->key_type, | 578 ReportAccuracyStats(results_it->second->key_type, |
535 *(nav_it->second), | 579 *(nav_it->second), |
536 results_it->second->requests.get()); | 580 results_it->second->requests.get()); |
581 ReportPageLoadTimePrefetchStats( | |
582 plt, | |
583 true, | |
584 base::Bind(&ReportPrefetchedNetworkType), | |
585 results_it->second->key_type); | |
586 } else { | |
587 ReportPageLoadTimePrefetchStats( | |
588 plt, | |
589 false, | |
590 base::Bind(&ReportNotPrefetchedNetworkType)); | |
537 } | 591 } |
538 } else { | 592 } else { |
539 scoped_ptr<ResourcePrefetcher::RequestVector> requests( | 593 scoped_ptr<ResourcePrefetcher::RequestVector> requests( |
540 new ResourcePrefetcher::RequestVector); | 594 new ResourcePrefetcher::RequestVector); |
541 PrefetchKeyType key_type; | 595 PrefetchKeyType key_type; |
542 if (GetPrefetchData(navigation_id, requests.get(), &key_type)) { | 596 if (GetPrefetchData(navigation_id, requests.get(), &key_type)) { |
543 RecordNavigationEvent(NAVIGATION_EVENT_HAVE_PREDICTIONS_FOR_URL); | 597 RecordNavigationEvent(NAVIGATION_EVENT_HAVE_PREDICTIONS_FOR_URL); |
544 ReportPredictedAccuracyStats(key_type, | 598 ReportPredictedAccuracyStats(key_type, |
545 *(nav_it->second), | 599 *(nav_it->second), |
546 *requests); | 600 *requests); |
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
996 BrowserThread::PostTask( | 1050 BrowserThread::PostTask( |
997 BrowserThread::DB, FROM_HERE, | 1051 BrowserThread::DB, FROM_HERE, |
998 base::Bind(&ResourcePrefetchPredictorTables::UpdateData, | 1052 base::Bind(&ResourcePrefetchPredictorTables::UpdateData, |
999 tables_, | 1053 tables_, |
1000 url_data, | 1054 url_data, |
1001 host_data)); | 1055 host_data)); |
1002 } | 1056 } |
1003 } | 1057 } |
1004 | 1058 |
1005 //////////////////////////////////////////////////////////////////////////////// | 1059 //////////////////////////////////////////////////////////////////////////////// |
1006 // Accuracy measurement. | 1060 // Page load time and accuracy measurement. |
1061 | |
1062 // This is essentially UMA_HISTOGRAM_MEDIUM_TIMES, but it avoids using the | |
1063 // STATIC_HISTOGRAM_POINTER_BLOCK in UMA_HISTOGRAM definitions. | |
1064 #define RPP_HISTOGRAM_MEDIUM_TIMES(name, page_load_time) \ | |
1065 do { \ | |
1066 base::HistogramBase* histogram = base::Histogram::FactoryTimeGet( \ | |
1067 name, \ | |
1068 base::TimeDelta::FromMilliseconds(10), \ | |
1069 base::TimeDelta::FromMinutes(3), \ | |
1070 50, \ | |
1071 base::HistogramBase::kUmaTargetedHistogramFlag); \ | |
1072 histogram->AddTime(page_load_time); \ | |
1073 } while (0) | |
1074 | |
1075 void ResourcePrefetchPredictor::ReportPageLoadTimeStats( | |
1076 base::TimeDelta plt) const { | |
1077 net::NetworkChangeNotifier::ConnectionType connection_type = | |
1078 net::NetworkChangeNotifier::GetConnectionType(); | |
1079 | |
1080 RPP_HISTOGRAM_MEDIUM_TIMES("ResourcePrefetchPredictor.PLT", plt); | |
1081 RPP_HISTOGRAM_MEDIUM_TIMES( | |
1082 "ResourcePrefetchPredictor.PLT_" + GetNetTypeStr(), plt); | |
1083 if (net::NetworkChangeNotifier::IsConnectionCellular(connection_type)) | |
1084 RPP_HISTOGRAM_MEDIUM_TIMES("ResourcePrefetchPredictor.PLT_Cellular", plt); | |
1085 } | |
1086 | |
1087 void ResourcePrefetchPredictor::ReportPageLoadTimePrefetchStats( | |
1088 base::TimeDelta plt, | |
1089 bool prefetched, | |
1090 base::Callback<void(int)> report_network_type_callback, | |
1091 PrefetchKeyType key_type) const { | |
1092 net::NetworkChangeNotifier::ConnectionType connection_type = | |
1093 net::NetworkChangeNotifier::GetConnectionType(); | |
1094 bool on_cellular = | |
1095 net::NetworkChangeNotifier::IsConnectionCellular(connection_type); | |
1096 | |
1097 report_network_type_callback.Run(CONNECTION_ALL); | |
1098 report_network_type_callback.Run(connection_type); | |
1099 if (on_cellular) | |
1100 report_network_type_callback.Run(CONNECTION_CELLULAR); | |
1101 | |
1102 std::string prefetched_str; | |
1103 if (prefetched) | |
1104 prefetched_str = "Prefetched"; | |
1105 else | |
1106 prefetched_str = "NotPrefetched"; | |
1107 | |
1108 RPP_HISTOGRAM_MEDIUM_TIMES( | |
1109 "ResourcePrefetchPredictor.PLT." + prefetched_str, plt); | |
1110 RPP_HISTOGRAM_MEDIUM_TIMES( | |
1111 "ResourcePrefetchPredictor.PLT." + prefetched_str + "_" + GetNetTypeStr(), | |
1112 plt); | |
1113 if (on_cellular) { | |
1114 RPP_HISTOGRAM_MEDIUM_TIMES( | |
1115 "ResourcePrefetchPredictor.PLT." + prefetched_str + "_Cellular", plt); | |
1116 } | |
1117 | |
1118 if (prefetched) { | |
Alexei Svitkine (slow)
2014/10/27 14:59:10
Nit: Make this an early return with inverse condit
Zhen Wang
2014/10/27 18:09:46
Done.
| |
1119 std::string type = | |
1120 key_type == PREFETCH_KEY_TYPE_HOST ? "Host" : "Url"; | |
1121 RPP_HISTOGRAM_MEDIUM_TIMES( | |
1122 "ResourcePrefetchPredictor.PLT.Prefetched." + type, plt); | |
1123 RPP_HISTOGRAM_MEDIUM_TIMES( | |
1124 "ResourcePrefetchPredictor.PLT.Prefetched." + type | |
1125 + "_" + GetNetTypeStr(), | |
Alexei Svitkine (slow)
2014/10/27 14:59:10
Nit: I think the + "_" fits on the previous line.
Zhen Wang
2014/10/27 18:09:46
Done.
| |
1126 plt); | |
1127 if (on_cellular) { | |
1128 RPP_HISTOGRAM_MEDIUM_TIMES( | |
1129 "ResourcePrefetchPredictor.PLT.Prefetched." + type + "_Cellular", | |
1130 plt); | |
1131 } | |
1132 } | |
1133 } | |
1007 | 1134 |
1008 void ResourcePrefetchPredictor::ReportAccuracyStats( | 1135 void ResourcePrefetchPredictor::ReportAccuracyStats( |
1009 PrefetchKeyType key_type, | 1136 PrefetchKeyType key_type, |
1010 const std::vector<URLRequestSummary>& actual, | 1137 const std::vector<URLRequestSummary>& actual, |
1011 ResourcePrefetcher::RequestVector* prefetched) const { | 1138 ResourcePrefetcher::RequestVector* prefetched) const { |
1012 // Annotate the results. | 1139 // Annotate the results. |
1013 std::map<GURL, bool> actual_resources; | 1140 std::map<GURL, bool> actual_resources; |
1014 for (std::vector<URLRequestSummary>::const_iterator it = actual.begin(); | 1141 for (std::vector<URLRequestSummary>::const_iterator it = actual.begin(); |
1015 it != actual.end(); ++it) { | 1142 it != actual.end(); ++it) { |
1016 actual_resources[it->resource_url] = it->was_cached; | 1143 actual_resources[it->resource_url] = it->was_cached; |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1210 prefetch_network * 100.0 / num_assumed_prefetched); | 1337 prefetch_network * 100.0 / num_assumed_prefetched); |
1211 | 1338 |
1212 // Measure the ratio of total number of resources prefetched from network vs | 1339 // Measure the ratio of total number of resources prefetched from network vs |
1213 // the total number of resources fetched by the page from the network. | 1340 // the total number of resources fetched by the page from the network. |
1214 if (total_resources_fetched_from_network > 0) { | 1341 if (total_resources_fetched_from_network > 0) { |
1215 RPP_PREDICTED_HISTOGRAM_PERCENTAGE( | 1342 RPP_PREDICTED_HISTOGRAM_PERCENTAGE( |
1216 "PrefetchFromNetworkPercentOfTotalFromNetwork", | 1343 "PrefetchFromNetworkPercentOfTotalFromNetwork", |
1217 prefetch_network * 100.0 / total_resources_fetched_from_network); | 1344 prefetch_network * 100.0 / total_resources_fetched_from_network); |
1218 } | 1345 } |
1219 | 1346 |
1347 #undef RPP_HISTOGRAM_MEDIUM_TIMES | |
1220 #undef RPP_PREDICTED_HISTOGRAM_PERCENTAGE | 1348 #undef RPP_PREDICTED_HISTOGRAM_PERCENTAGE |
1221 #undef RPP_PREDICTED_HISTOGRAM_COUNTS | 1349 #undef RPP_PREDICTED_HISTOGRAM_COUNTS |
1222 } | 1350 } |
1223 | 1351 |
1224 } // namespace predictors | 1352 } // namespace predictors |
OLD | NEW |