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

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

Issue 2508933002: tools: Local tests for the speculative prefetch predictor. (Closed)
Patch Set: . Created 4 years, 1 month 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_common.h" 5 #include "chrome/browser/predictors/resource_prefetch_common.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 #include <tuple> 8 #include <tuple>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/metrics/field_trial.h" 11 #include "base/metrics/field_trial.h"
12 #include "base/strings/string_split.h" 12 #include "base/strings/string_split.h"
13 #include "chrome/browser/net/prediction_options.h" 13 #include "chrome/browser/net/prediction_options.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/chrome_switches.h" 15 #include "chrome/common/chrome_switches.h"
16 #include "components/prefs/pref_service.h" 16 #include "components/prefs/pref_service.h"
17 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/render_frame_host.h" 18 #include "content/public/browser/render_frame_host.h"
19 #include "content/public/browser/render_process_host.h" 19 #include "content/public/browser/render_process_host.h"
20 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
21 21
22 using base::FieldTrialList; 22 using base::FieldTrialList;
23 using std::string; 23 using std::string;
24 using std::vector; 24 using std::vector;
25 25
26 namespace predictors { 26 namespace predictors {
27 27
28 namespace {
29 const int kUrlHostLearningMode = ResourcePrefetchPredictorConfig::URL_LEARNING |
pasko 2016/11/17 15:59:04 constexpr has less risks creating entries in .data
Benoit L 2016/11/21 13:40:32 Done.
30 ResourcePrefetchPredictorConfig::HOST_LEARNING;
31 const int kUrlHostPrefetchingMode =
32 ResourcePrefetchPredictorConfig::URL_PREFETCHING |
33 ResourcePrefetchPredictorConfig::HOST_PREFETCHING;
34
28 const char kSpeculativePrefetchingTrialName[] = 35 const char kSpeculativePrefetchingTrialName[] =
29 "SpeculativeResourcePrefetching"; 36 "SpeculativeResourcePrefetching";
37 } // namespace
30 38
31 /* 39 /*
32 * SpeculativeResourcePrefetching is a field trial, and its value must have the 40 * SpeculativeResourcePrefetching is a field trial, and its value must have the
33 * following format: key1=value1:key2=value2:key3=value3 41 * following format: key1=value1:key2=value2:key3=value3
34 * e.g. "Prefetching=Enabled:Predictor=Url:Confidence=High" 42 * e.g. "Prefetching=Enabled:Predictor=Url:Confidence=High"
35 * The function below extracts the value corresponding to a key provided from 43 * The function below extracts the value corresponding to a key provided from
36 * the SpeculativeResourcePrefetching field trial. 44 * the SpeculativeResourcePrefetching field trial.
37 */ 45 */
38 std::string GetFieldTrialSpecValue(string key) { 46 std::string GetFieldTrialSpecValue(string key) {
39 std::string trial_name = 47 std::string trial_name =
(...skipping 21 matching lines...) Expand all
61 // set. The command line with just enable them with the default params. 69 // set. The command line with just enable them with the default params.
62 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 70 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
63 switches::kSpeculativeResourcePrefetching)) { 71 switches::kSpeculativeResourcePrefetching)) {
64 const std::string value = 72 const std::string value =
65 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 73 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
66 switches::kSpeculativeResourcePrefetching); 74 switches::kSpeculativeResourcePrefetching);
67 75
68 if (value == switches::kSpeculativeResourcePrefetchingDisabled) { 76 if (value == switches::kSpeculativeResourcePrefetchingDisabled) {
69 return false; 77 return false;
70 } else if (value == switches::kSpeculativeResourcePrefetchingLearning) { 78 } else if (value == switches::kSpeculativeResourcePrefetchingLearning) {
71 config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; 79 config->mode |= kUrlHostLearningMode;
72 config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
73 return true; 80 return true;
81 } else if (value ==
82 switches::kSpeculativeResourceExternalPrefetchingEnabled) {
83 config->mode |= kUrlHostLearningMode | kUrlHostPrefetchingMode;
84 config->mode |= ResourcePrefetchPredictorConfig::EXTERNAL_ONLY;
74 } else if (value == switches::kSpeculativeResourcePrefetchingEnabled) { 85 } else if (value == switches::kSpeculativeResourcePrefetchingEnabled) {
75 config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; 86 config->mode |= kUrlHostLearningMode;
76 config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
77 config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING;
78 config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING;
79 return true; 87 return true;
80 } 88 }
81 } 89 }
82 90
83 // Disable if no field trial is specified. 91 // Disable if no field trial is specified.
84 std::string trial = base::FieldTrialList::FindFullName( 92 std::string trial = base::FieldTrialList::FindFullName(
85 kSpeculativePrefetchingTrialName); 93 kSpeculativePrefetchingTrialName);
86 if (trial.empty()) 94 if (trial.empty())
87 return false; 95 return false;
88 96
89 // Enabled by field trial. 97 // Enabled by field trial.
90 std::string spec_prefetching = GetFieldTrialSpecValue("Prefetching"); 98 std::string spec_prefetching = GetFieldTrialSpecValue("Prefetching");
91 std::string spec_predictor = GetFieldTrialSpecValue("Predictor"); 99 std::string spec_predictor = GetFieldTrialSpecValue("Predictor");
92 std::string spec_confidence = GetFieldTrialSpecValue("Confidence"); 100 std::string spec_confidence = GetFieldTrialSpecValue("Confidence");
93 std::string spec_more_resources = GetFieldTrialSpecValue("MoreResources"); 101 std::string spec_more_resources = GetFieldTrialSpecValue("MoreResources");
94 std::string spec_small_db = GetFieldTrialSpecValue("SmallDB"); 102 std::string spec_small_db = GetFieldTrialSpecValue("SmallDB");
95 103
96 if (spec_prefetching == "Learning") { 104 if (spec_prefetching == "Learning") {
97 if (spec_predictor == "Url") { 105 if (spec_predictor == "Url") {
98 config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; 106 config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
99 } else if (spec_predictor == "Host") { 107 } else if (spec_predictor == "Host") {
100 config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING; 108 config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
101 } else { 109 } else {
102 // Default: both Url and Host 110 // Default: both Url and Host
103 config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; 111 config->mode |= kUrlHostLearningMode;
104 config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
105 } 112 }
106 } else if (spec_prefetching == "Enabled") { 113 } else if (spec_prefetching == "Enabled") {
107 if (spec_predictor == "Url") { 114 if (spec_predictor == "Url") {
108 config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; 115 config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
109 config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING; 116 config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING;
110 } else if (spec_predictor == "Host") { 117 } else if (spec_predictor == "Host") {
111 config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING; 118 config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
112 config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING; 119 config->mode |= ResourcePrefetchPredictorConfig::HOST_PREFETCHING;
113 } else { 120 } else {
114 // Default: both Url and Host 121 // Default: both Url and Host
115 config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; 122 config->mode |= kUrlHostLearningMode | kUrlHostPrefetchingMode;
116 config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
117 config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING;
118 config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING;
119 } 123 }
120 } else { 124 } else {
121 // Default: spec_prefetching == "Disabled" 125 // Default: spec_prefetching == "Disabled"
122 return false; 126 return false;
123 } 127 }
124 128
125 if (spec_confidence == "Low") { 129 if (spec_confidence == "Low") {
126 config->min_url_visit_count = 1; 130 config->min_url_visit_count = 1;
127 config->min_resource_confidence_to_trigger_prefetch = 0.5f; 131 config->min_resource_confidence_to_trigger_prefetch = 0.5f;
128 config->min_resource_hits_to_trigger_prefetch = 1; 132 config->min_resource_hits_to_trigger_prefetch = 1;
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 ResourcePrefetchPredictorConfig::ResourcePrefetchPredictorConfig( 217 ResourcePrefetchPredictorConfig::ResourcePrefetchPredictorConfig(
214 const ResourcePrefetchPredictorConfig& other) = default; 218 const ResourcePrefetchPredictorConfig& other) = default;
215 219
216 ResourcePrefetchPredictorConfig::~ResourcePrefetchPredictorConfig() { 220 ResourcePrefetchPredictorConfig::~ResourcePrefetchPredictorConfig() {
217 } 221 }
218 222
219 bool ResourcePrefetchPredictorConfig::IsLearningEnabled() const { 223 bool ResourcePrefetchPredictorConfig::IsLearningEnabled() const {
220 return IsURLLearningEnabled() || IsHostLearningEnabled(); 224 return IsURLLearningEnabled() || IsHostLearningEnabled();
221 } 225 }
222 226
227 bool ResourcePrefetchPredictorConfig::ExternalPrefetchingOnly() const {
228 return (mode & EXTERNAL_ONLY) > 0;
229 }
230
223 bool ResourcePrefetchPredictorConfig::IsPrefetchingEnabled( 231 bool ResourcePrefetchPredictorConfig::IsPrefetchingEnabled(
224 Profile* profile) const { 232 Profile* profile) const {
225 return IsURLPrefetchingEnabled(profile) || IsHostPrefetchingEnabled(profile); 233 return IsURLPrefetchingEnabled(profile) || IsHostPrefetchingEnabled(profile);
226 } 234 }
227 235
228 bool ResourcePrefetchPredictorConfig::IsURLLearningEnabled() const { 236 bool ResourcePrefetchPredictorConfig::IsURLLearningEnabled() const {
229 return (mode & URL_LEARNING) > 0; 237 return (mode & URL_LEARNING) > 0;
230 } 238 }
231 239
232 bool ResourcePrefetchPredictorConfig::IsHostLearningEnabled() const { 240 bool ResourcePrefetchPredictorConfig::IsHostLearningEnabled() const {
(...skipping 12 matching lines...) Expand all
245 } 253 }
246 254
247 bool ResourcePrefetchPredictorConfig::IsHostPrefetchingEnabled( 255 bool ResourcePrefetchPredictorConfig::IsHostPrefetchingEnabled(
248 Profile* profile) const { 256 Profile* profile) const {
249 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 257 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
250 if (!profile || !profile->GetPrefs() || 258 if (!profile || !profile->GetPrefs() ||
251 chrome_browser_net::CanPrefetchAndPrerenderUI(profile->GetPrefs()) != 259 chrome_browser_net::CanPrefetchAndPrerenderUI(profile->GetPrefs()) !=
252 chrome_browser_net::NetworkPredictionStatus::ENABLED) { 260 chrome_browser_net::NetworkPredictionStatus::ENABLED) {
253 return false; 261 return false;
254 } 262 }
255 return (mode & HOST_PRFETCHING) > 0; 263 return (mode & HOST_PREFETCHING) > 0;
256 } 264 }
257 265
258 bool ResourcePrefetchPredictorConfig::IsLowConfidenceForTest() const { 266 bool ResourcePrefetchPredictorConfig::IsLowConfidenceForTest() const {
259 return min_url_visit_count == 1 && 267 return min_url_visit_count == 1 &&
260 std::abs(min_resource_confidence_to_trigger_prefetch - 0.5f) < 1e-6 && 268 std::abs(min_resource_confidence_to_trigger_prefetch - 0.5f) < 1e-6 &&
261 min_resource_hits_to_trigger_prefetch == 1; 269 min_resource_hits_to_trigger_prefetch == 1;
262 } 270 }
263 271
264 bool ResourcePrefetchPredictorConfig::IsHighConfidenceForTest() const { 272 bool ResourcePrefetchPredictorConfig::IsHighConfidenceForTest() const {
265 return min_url_visit_count == 3 && 273 return min_url_visit_count == 3 &&
266 std::abs(min_resource_confidence_to_trigger_prefetch - 0.9f) < 1e-6 && 274 std::abs(min_resource_confidence_to_trigger_prefetch - 0.9f) < 1e-6 &&
267 min_resource_hits_to_trigger_prefetch == 3; 275 min_resource_hits_to_trigger_prefetch == 3;
268 } 276 }
269 277
270 bool ResourcePrefetchPredictorConfig::IsMoreResourcesEnabledForTest() const { 278 bool ResourcePrefetchPredictorConfig::IsMoreResourcesEnabledForTest() const {
271 return max_resources_per_entry == 100; 279 return max_resources_per_entry == 100;
272 } 280 }
273 281
274 bool ResourcePrefetchPredictorConfig::IsSmallDBEnabledForTest() const { 282 bool ResourcePrefetchPredictorConfig::IsSmallDBEnabledForTest() const {
275 return max_urls_to_track == 200 && max_hosts_to_track == 100; 283 return max_urls_to_track == 200 && max_hosts_to_track == 100;
276 } 284 }
277 285
278 } // namespace predictors 286 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698