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

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

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

Powered by Google App Engine
This is Rietveld 408576698