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