| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | |
| 6 | |
| 7 #include <cmath> | |
| 8 | |
| 9 namespace { | |
| 10 | |
| 11 bool AlmostEqual(const double x, const double y) { | |
| 12 return std::fabs(x - y) <= 1e-6; // Arbitrary but close enough. | |
| 13 } | |
| 14 | |
| 15 } // namespace | |
| 16 | |
| 17 namespace predictors { | |
| 18 | |
| 19 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | |
| 20 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | |
| 21 | |
| 22 void InitializeResourceData(ResourceData* resource, | |
| 23 const std::string& resource_url, | |
| 24 content::ResourceType resource_type, | |
| 25 int number_of_hits, | |
| 26 int number_of_misses, | |
| 27 int consecutive_misses, | |
| 28 double average_position, | |
| 29 net::RequestPriority priority, | |
| 30 bool has_validators, | |
| 31 bool always_revalidate) { | |
| 32 resource->set_resource_url(resource_url); | |
| 33 resource->set_resource_type( | |
| 34 static_cast<ResourceData::ResourceType>(resource_type)); | |
| 35 resource->set_number_of_hits(number_of_hits); | |
| 36 resource->set_number_of_misses(number_of_misses); | |
| 37 resource->set_consecutive_misses(consecutive_misses); | |
| 38 resource->set_average_position(average_position); | |
| 39 resource->set_priority(static_cast<ResourceData::Priority>(priority)); | |
| 40 resource->set_before_first_contentful_paint(true); | |
| 41 resource->set_has_validators(has_validators); | |
| 42 resource->set_always_revalidate(always_revalidate); | |
| 43 } | |
| 44 | |
| 45 void InitializeRedirectStat(RedirectStat* redirect, | |
| 46 const std::string& url, | |
| 47 int number_of_hits, | |
| 48 int number_of_misses, | |
| 49 int consecutive_misses) { | |
| 50 redirect->set_url(url); | |
| 51 redirect->set_number_of_hits(number_of_hits); | |
| 52 redirect->set_number_of_misses(number_of_misses); | |
| 53 redirect->set_consecutive_misses(consecutive_misses); | |
| 54 } | |
| 55 | |
| 56 void InitializePrecacheResource(precache::PrecacheResource* resource, | |
| 57 const std::string& url, | |
| 58 double weight_ratio, | |
| 59 precache::PrecacheResource::Type type) { | |
| 60 resource->set_url(url); | |
| 61 resource->set_weight_ratio(weight_ratio); | |
| 62 resource->set_type(type); | |
| 63 } | |
| 64 | |
| 65 void InitializeOriginStat(OriginStat* origin_stat, | |
| 66 const std::string& origin, | |
| 67 int number_of_hits, | |
| 68 int number_of_misses, | |
| 69 int consecutive_misses, | |
| 70 double average_position, | |
| 71 bool always_access_network, | |
| 72 bool accessed_network) { | |
| 73 origin_stat->set_origin(origin); | |
| 74 origin_stat->set_number_of_hits(number_of_hits); | |
| 75 origin_stat->set_number_of_misses(number_of_misses); | |
| 76 origin_stat->set_consecutive_misses(consecutive_misses); | |
| 77 origin_stat->set_average_position(average_position); | |
| 78 origin_stat->set_always_access_network(always_access_network); | |
| 79 origin_stat->set_accessed_network(accessed_network); | |
| 80 } | |
| 81 | |
| 82 void InitializeExperiment(precache::PrecacheManifest* manifest, | |
| 83 uint32_t experiment_id, | |
| 84 const std::vector<bool>& bitset) { | |
| 85 std::string binary_bitset; | |
| 86 for (size_t i = 0; i < (bitset.size() + 7) / 8; ++i) { | |
| 87 char c = 0; | |
| 88 for (size_t j = 0; j < 8; ++j) { | |
| 89 if (i * 8 + j < bitset.size() && bitset[i * 8 + j]) | |
| 90 c |= (1 << j); | |
| 91 } | |
| 92 binary_bitset.push_back(c); | |
| 93 } | |
| 94 | |
| 95 precache::PrecacheResourceSelection prs; | |
| 96 prs.set_bitset(binary_bitset); | |
| 97 (*manifest->mutable_experiments() | |
| 98 ->mutable_resources_by_experiment_group())[experiment_id] = prs; | |
| 99 } | |
| 100 | |
| 101 PrefetchData CreatePrefetchData(const std::string& primary_key, | |
| 102 uint64_t last_visit_time) { | |
| 103 PrefetchData data; | |
| 104 data.set_primary_key(primary_key); | |
| 105 data.set_last_visit_time(last_visit_time); | |
| 106 return data; | |
| 107 } | |
| 108 | |
| 109 RedirectData CreateRedirectData(const std::string& primary_key, | |
| 110 uint64_t last_visit_time) { | |
| 111 RedirectData data; | |
| 112 data.set_primary_key(primary_key); | |
| 113 data.set_last_visit_time(last_visit_time); | |
| 114 return data; | |
| 115 } | |
| 116 | |
| 117 precache::PrecacheManifest CreateManifestData(int64_t id) { | |
| 118 precache::PrecacheManifest manifest; | |
| 119 manifest.mutable_id()->set_id(id); | |
| 120 return manifest; | |
| 121 } | |
| 122 | |
| 123 OriginData CreateOriginData(const std::string& host, uint64_t last_visit_time) { | |
| 124 OriginData data; | |
| 125 data.set_host(host); | |
| 126 data.set_last_visit_time(last_visit_time); | |
| 127 return data; | |
| 128 } | |
| 129 | |
| 130 NavigationID CreateNavigationID(SessionID::id_type tab_id, | |
| 131 const std::string& main_frame_url) { | |
| 132 NavigationID navigation_id; | |
| 133 navigation_id.tab_id = tab_id; | |
| 134 navigation_id.main_frame_url = GURL(main_frame_url); | |
| 135 navigation_id.creation_time = base::TimeTicks::Now(); | |
| 136 return navigation_id; | |
| 137 } | |
| 138 | |
| 139 PageRequestSummary CreatePageRequestSummary( | |
| 140 const std::string& main_frame_url, | |
| 141 const std::string& initial_url, | |
| 142 const std::vector<URLRequestSummary>& subresource_requests) { | |
| 143 GURL main_frame_gurl(main_frame_url); | |
| 144 PageRequestSummary summary(main_frame_gurl); | |
| 145 summary.initial_url = GURL(initial_url); | |
| 146 summary.subresource_requests = subresource_requests; | |
| 147 return summary; | |
| 148 } | |
| 149 | |
| 150 URLRequestSummary CreateURLRequestSummary(SessionID::id_type tab_id, | |
| 151 const std::string& main_frame_url, | |
| 152 const std::string& resource_url, | |
| 153 content::ResourceType resource_type, | |
| 154 net::RequestPriority priority, | |
| 155 const std::string& mime_type, | |
| 156 bool was_cached, | |
| 157 const std::string& redirect_url, | |
| 158 bool has_validators, | |
| 159 bool always_revalidate) { | |
| 160 URLRequestSummary summary; | |
| 161 summary.navigation_id = CreateNavigationID(tab_id, main_frame_url); | |
| 162 summary.resource_url = | |
| 163 resource_url.empty() ? GURL(main_frame_url) : GURL(resource_url); | |
| 164 summary.request_url = summary.resource_url; | |
| 165 summary.resource_type = resource_type; | |
| 166 summary.priority = priority; | |
| 167 summary.before_first_contentful_paint = true; | |
| 168 summary.mime_type = mime_type; | |
| 169 summary.was_cached = was_cached; | |
| 170 if (!redirect_url.empty()) | |
| 171 summary.redirect_url = GURL(redirect_url); | |
| 172 summary.has_validators = has_validators; | |
| 173 summary.always_revalidate = always_revalidate; | |
| 174 summary.is_no_store = false; | |
| 175 summary.network_accessed = true; | |
| 176 return summary; | |
| 177 } | |
| 178 | |
| 179 void PopulateTestConfig(LoadingPredictorConfig* config, bool small_db) { | |
| 180 if (small_db) { | |
| 181 config->max_urls_to_track = 3; | |
| 182 config->max_hosts_to_track = 2; | |
| 183 config->min_url_visit_count = 2; | |
| 184 config->max_resources_per_entry = 4; | |
| 185 config->max_consecutive_misses = 2; | |
| 186 config->max_redirect_consecutive_misses = 2; | |
| 187 config->min_resource_confidence_to_trigger_prefetch = 0.5; | |
| 188 } | |
| 189 config->is_url_learning_enabled = true; | |
| 190 config->is_manifests_enabled = true; | |
| 191 config->is_origin_learning_enabled = true; | |
| 192 config->mode = LoadingPredictorConfig::LEARNING; | |
| 193 } | |
| 194 | |
| 195 std::ostream& operator<<(std::ostream& os, const PrefetchData& data) { | |
| 196 os << "[" << data.primary_key() << "," << data.last_visit_time() << "]" | |
| 197 << std::endl; | |
| 198 for (const ResourceData& resource : data.resources()) | |
| 199 os << "\t\t" << resource << std::endl; | |
| 200 return os; | |
| 201 } | |
| 202 | |
| 203 std::ostream& operator<<(std::ostream& os, const ResourceData& resource) { | |
| 204 return os << "[" << resource.resource_url() << "," << resource.resource_type() | |
| 205 << "," << resource.number_of_hits() << "," | |
| 206 << resource.number_of_misses() << "," | |
| 207 << resource.consecutive_misses() << "," | |
| 208 << resource.average_position() << "," << resource.priority() << "," | |
| 209 << resource.before_first_contentful_paint() << "," | |
| 210 << resource.has_validators() << "," << resource.always_revalidate() | |
| 211 << "]"; | |
| 212 } | |
| 213 | |
| 214 std::ostream& operator<<(std::ostream& os, const RedirectData& data) { | |
| 215 os << "[" << data.primary_key() << "," << data.last_visit_time() << "]" | |
| 216 << std::endl; | |
| 217 for (const RedirectStat& redirect : data.redirect_endpoints()) | |
| 218 os << "\t\t" << redirect << std::endl; | |
| 219 return os; | |
| 220 } | |
| 221 | |
| 222 std::ostream& operator<<(std::ostream& os, const RedirectStat& redirect) { | |
| 223 return os << "[" << redirect.url() << "," << redirect.number_of_hits() << "," | |
| 224 << redirect.number_of_misses() << "," | |
| 225 << redirect.consecutive_misses() << "]"; | |
| 226 } | |
| 227 | |
| 228 std::ostream& operator<<(std::ostream& os, const OriginData& data) { | |
| 229 os << "[" << data.host() << "," << data.last_visit_time() << "]" << std::endl; | |
| 230 for (const OriginStat& origin : data.origins()) | |
| 231 os << "\t\t" << origin << std::endl; | |
| 232 return os; | |
| 233 } | |
| 234 | |
| 235 std::ostream& operator<<(std::ostream& os, const OriginStat& origin) { | |
| 236 return os << "[" << origin.origin() << "," << origin.number_of_hits() << "," | |
| 237 << origin.number_of_misses() << "," << origin.consecutive_misses() | |
| 238 << "," << origin.average_position() << "," | |
| 239 << origin.always_access_network() << "," | |
| 240 << origin.accessed_network() << "]"; | |
| 241 } | |
| 242 | |
| 243 std::ostream& operator<<(std::ostream& os, const PageRequestSummary& summary) { | |
| 244 os << "[" << summary.main_frame_url << "," << summary.initial_url << "]" | |
| 245 << std::endl; | |
| 246 for (const auto& request : summary.subresource_requests) | |
| 247 os << "\t\t" << request << std::endl; | |
| 248 return os; | |
| 249 } | |
| 250 | |
| 251 std::ostream& operator<<(std::ostream& os, const URLRequestSummary& summary) { | |
| 252 return os << "[" << summary.navigation_id << "," << summary.resource_url | |
| 253 << "," << summary.resource_type << "," << summary.priority << "," | |
| 254 << summary.before_first_contentful_paint << "," << summary.mime_type | |
| 255 << "," << summary.was_cached << "," << summary.redirect_url << "," | |
| 256 << summary.has_validators << "," << summary.always_revalidate | |
| 257 << "]"; | |
| 258 } | |
| 259 | |
| 260 std::ostream& operator<<(std::ostream& os, const NavigationID& navigation_id) { | |
| 261 return os << navigation_id.tab_id << "," << navigation_id.main_frame_url; | |
| 262 } | |
| 263 | |
| 264 bool operator==(const PrefetchData& lhs, const PrefetchData& rhs) { | |
| 265 bool equal = lhs.primary_key() == rhs.primary_key() && | |
| 266 lhs.resources_size() == rhs.resources_size(); | |
| 267 | |
| 268 if (!equal) | |
| 269 return false; | |
| 270 | |
| 271 for (int i = 0; i < lhs.resources_size(); ++i) | |
| 272 equal = equal && lhs.resources(i) == rhs.resources(i); | |
| 273 | |
| 274 return equal; | |
| 275 } | |
| 276 | |
| 277 bool operator==(const ResourceData& lhs, const ResourceData& rhs) { | |
| 278 return lhs.resource_url() == rhs.resource_url() && | |
| 279 lhs.resource_type() == rhs.resource_type() && | |
| 280 lhs.number_of_hits() == rhs.number_of_hits() && | |
| 281 lhs.number_of_misses() == rhs.number_of_misses() && | |
| 282 lhs.consecutive_misses() == rhs.consecutive_misses() && | |
| 283 AlmostEqual(lhs.average_position(), rhs.average_position()) && | |
| 284 lhs.priority() == rhs.priority() && | |
| 285 lhs.before_first_contentful_paint() == | |
| 286 rhs.before_first_contentful_paint() && | |
| 287 lhs.has_validators() == rhs.has_validators() && | |
| 288 lhs.always_revalidate() == rhs.always_revalidate(); | |
| 289 } | |
| 290 | |
| 291 bool operator==(const RedirectData& lhs, const RedirectData& rhs) { | |
| 292 bool equal = lhs.primary_key() == rhs.primary_key() && | |
| 293 lhs.redirect_endpoints_size() == rhs.redirect_endpoints_size(); | |
| 294 | |
| 295 if (!equal) | |
| 296 return false; | |
| 297 | |
| 298 for (int i = 0; i < lhs.redirect_endpoints_size(); ++i) | |
| 299 equal = equal && lhs.redirect_endpoints(i) == rhs.redirect_endpoints(i); | |
| 300 | |
| 301 return equal; | |
| 302 } | |
| 303 | |
| 304 bool operator==(const RedirectStat& lhs, const RedirectStat& rhs) { | |
| 305 return lhs.url() == rhs.url() && | |
| 306 lhs.number_of_hits() == rhs.number_of_hits() && | |
| 307 lhs.number_of_misses() == rhs.number_of_misses() && | |
| 308 lhs.consecutive_misses() == rhs.consecutive_misses(); | |
| 309 } | |
| 310 | |
| 311 bool operator==(const PageRequestSummary& lhs, const PageRequestSummary& rhs) { | |
| 312 return lhs.main_frame_url == rhs.main_frame_url && | |
| 313 lhs.initial_url == rhs.initial_url && | |
| 314 lhs.subresource_requests == rhs.subresource_requests; | |
| 315 } | |
| 316 | |
| 317 bool operator==(const URLRequestSummary& lhs, const URLRequestSummary& rhs) { | |
| 318 return lhs.navigation_id == rhs.navigation_id && | |
| 319 lhs.resource_url == rhs.resource_url && | |
| 320 lhs.resource_type == rhs.resource_type && | |
| 321 lhs.priority == rhs.priority && lhs.mime_type == rhs.mime_type && | |
| 322 lhs.before_first_contentful_paint == | |
| 323 rhs.before_first_contentful_paint && | |
| 324 lhs.was_cached == rhs.was_cached && | |
| 325 lhs.redirect_url == rhs.redirect_url && | |
| 326 lhs.has_validators == rhs.has_validators && | |
| 327 lhs.always_revalidate == rhs.always_revalidate; | |
| 328 } | |
| 329 | |
| 330 bool operator==(const OriginData& lhs, const OriginData& rhs) { | |
| 331 bool equal = | |
| 332 lhs.host() == rhs.host() && lhs.origins_size() == rhs.origins_size(); | |
| 333 if (!equal) | |
| 334 return false; | |
| 335 | |
| 336 for (int i = 0; i < lhs.origins_size(); ++i) | |
| 337 equal = equal && lhs.origins(i) == rhs.origins(i); | |
| 338 | |
| 339 return equal; | |
| 340 } | |
| 341 | |
| 342 bool operator==(const OriginStat& lhs, const OriginStat& rhs) { | |
| 343 return lhs.origin() == rhs.origin() && | |
| 344 lhs.number_of_hits() == rhs.number_of_hits() && | |
| 345 lhs.number_of_misses() == rhs.number_of_misses() && | |
| 346 lhs.consecutive_misses() == rhs.consecutive_misses() && | |
| 347 AlmostEqual(lhs.average_position(), rhs.average_position()) && | |
| 348 lhs.always_access_network() == rhs.always_access_network() && | |
| 349 lhs.accessed_network() == rhs.accessed_network(); | |
| 350 } | |
| 351 | |
| 352 } // namespace predictors | |
| 353 | |
| 354 namespace precache { | |
| 355 | |
| 356 std::ostream& operator<<(std::ostream& os, const PrecacheManifest& manifest) { | |
| 357 os << "[" << manifest.id().id() << "]" << std::endl; | |
| 358 for (const PrecacheResource& resource : manifest.resource()) | |
| 359 os << "\t\t" << resource << std::endl; | |
| 360 return os; | |
| 361 } | |
| 362 | |
| 363 std::ostream& operator<<(std::ostream& os, const PrecacheResource& resource) { | |
| 364 return os << "[" << resource.url() << "," << resource.top_host_name() << "," | |
| 365 << resource.weight_ratio() << "," << resource.weight() << "]"; | |
| 366 } | |
| 367 | |
| 368 bool operator==(const PrecacheManifest& lhs, const PrecacheManifest& rhs) { | |
| 369 bool equal = lhs.id().id() == rhs.id().id() && | |
| 370 lhs.resource_size() == rhs.resource_size(); | |
| 371 | |
| 372 if (!equal) | |
| 373 return false; | |
| 374 | |
| 375 for (int i = 0; i < lhs.resource_size(); ++i) | |
| 376 equal = equal && lhs.resource(i) == rhs.resource(i); | |
| 377 | |
| 378 return equal; | |
| 379 } | |
| 380 | |
| 381 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { | |
| 382 return lhs.url() == rhs.url() && | |
| 383 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio()); | |
| 384 } | |
| 385 | |
| 386 } // namespace precache | |
| OLD | NEW |