| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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_test_util.h" | 5 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
| 6 | 6 |
| 7 #include <limits> | 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 |
| 8 | 16 |
| 9 namespace predictors { | 17 namespace predictors { |
| 10 | 18 |
| 11 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 19 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
| 12 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | 20 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
| 13 | 21 |
| 14 void InitializeResourceData(ResourceData* resource, | 22 void InitializeResourceData(ResourceData* resource, |
| 15 const std::string& resource_url, | 23 const std::string& resource_url, |
| 16 content::ResourceType resource_type, | 24 content::ResourceType resource_type, |
| 17 int number_of_hits, | 25 int number_of_hits, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 44 redirect->set_consecutive_misses(consecutive_misses); | 52 redirect->set_consecutive_misses(consecutive_misses); |
| 45 } | 53 } |
| 46 | 54 |
| 47 void InitializePrecacheResource(precache::PrecacheResource* resource, | 55 void InitializePrecacheResource(precache::PrecacheResource* resource, |
| 48 const std::string& url, | 56 const std::string& url, |
| 49 double weight_ratio) { | 57 double weight_ratio) { |
| 50 resource->set_url(url); | 58 resource->set_url(url); |
| 51 resource->set_weight_ratio(weight_ratio); | 59 resource->set_weight_ratio(weight_ratio); |
| 52 } | 60 } |
| 53 | 61 |
| 62 void InitializeOriginStat(OriginStat* origin_stat, |
| 63 const std::string& origin, |
| 64 int number_of_hits, |
| 65 int number_of_misses, |
| 66 int consecutive_misses, |
| 67 double average_position, |
| 68 bool always_access_network, |
| 69 bool accessed_network) { |
| 70 origin_stat->set_origin(origin); |
| 71 origin_stat->set_number_of_hits(number_of_hits); |
| 72 origin_stat->set_number_of_misses(number_of_misses); |
| 73 origin_stat->set_consecutive_misses(consecutive_misses); |
| 74 origin_stat->set_average_position(average_position); |
| 75 origin_stat->set_always_access_network(always_access_network); |
| 76 origin_stat->set_accessed_network(accessed_network); |
| 77 } |
| 78 |
| 54 PrefetchData CreatePrefetchData(const std::string& primary_key, | 79 PrefetchData CreatePrefetchData(const std::string& primary_key, |
| 55 uint64_t last_visit_time) { | 80 uint64_t last_visit_time) { |
| 56 PrefetchData data; | 81 PrefetchData data; |
| 57 data.set_primary_key(primary_key); | 82 data.set_primary_key(primary_key); |
| 58 data.set_last_visit_time(last_visit_time); | 83 data.set_last_visit_time(last_visit_time); |
| 59 return data; | 84 return data; |
| 60 } | 85 } |
| 61 | 86 |
| 62 RedirectData CreateRedirectData(const std::string& primary_key, | 87 RedirectData CreateRedirectData(const std::string& primary_key, |
| 63 uint64_t last_visit_time) { | 88 uint64_t last_visit_time) { |
| 64 RedirectData data; | 89 RedirectData data; |
| 65 data.set_primary_key(primary_key); | 90 data.set_primary_key(primary_key); |
| 66 data.set_last_visit_time(last_visit_time); | 91 data.set_last_visit_time(last_visit_time); |
| 67 return data; | 92 return data; |
| 68 } | 93 } |
| 69 | 94 |
| 70 precache::PrecacheManifest CreateManifestData(uint64_t id) { | 95 precache::PrecacheManifest CreateManifestData(uint64_t id) { |
| 71 precache::PrecacheManifestId* manifest_id = | 96 precache::PrecacheManifestId* manifest_id = |
| 72 new precache::PrecacheManifestId(); | 97 new precache::PrecacheManifestId(); |
| 73 manifest_id->set_id(id); | 98 manifest_id->set_id(id); |
| 74 precache::PrecacheManifest manifest; | 99 precache::PrecacheManifest manifest; |
| 75 manifest.set_allocated_id(manifest_id); | 100 manifest.set_allocated_id(manifest_id); |
| 76 return manifest; | 101 return manifest; |
| 77 } | 102 } |
| 78 | 103 |
| 104 OriginData CreateOriginData(const std::string& host, uint64_t last_visit_time) { |
| 105 OriginData data; |
| 106 data.set_host(host); |
| 107 data.set_last_visit_time(last_visit_time); |
| 108 return data; |
| 109 } |
| 110 |
| 79 NavigationID CreateNavigationID(SessionID::id_type tab_id, | 111 NavigationID CreateNavigationID(SessionID::id_type tab_id, |
| 80 const std::string& main_frame_url) { | 112 const std::string& main_frame_url) { |
| 81 NavigationID navigation_id; | 113 NavigationID navigation_id; |
| 82 navigation_id.tab_id = tab_id; | 114 navigation_id.tab_id = tab_id; |
| 83 navigation_id.main_frame_url = GURL(main_frame_url); | 115 navigation_id.main_frame_url = GURL(main_frame_url); |
| 84 navigation_id.creation_time = base::TimeTicks::Now(); | 116 navigation_id.creation_time = base::TimeTicks::Now(); |
| 85 return navigation_id; | 117 return navigation_id; |
| 86 } | 118 } |
| 87 | 119 |
| 88 PageRequestSummary CreatePageRequestSummary( | 120 PageRequestSummary CreatePageRequestSummary( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 103 net::RequestPriority priority, | 135 net::RequestPriority priority, |
| 104 const std::string& mime_type, | 136 const std::string& mime_type, |
| 105 bool was_cached, | 137 bool was_cached, |
| 106 const std::string& redirect_url, | 138 const std::string& redirect_url, |
| 107 bool has_validators, | 139 bool has_validators, |
| 108 bool always_revalidate) { | 140 bool always_revalidate) { |
| 109 URLRequestSummary summary; | 141 URLRequestSummary summary; |
| 110 summary.navigation_id = CreateNavigationID(tab_id, main_frame_url); | 142 summary.navigation_id = CreateNavigationID(tab_id, main_frame_url); |
| 111 summary.resource_url = | 143 summary.resource_url = |
| 112 resource_url.empty() ? GURL(main_frame_url) : GURL(resource_url); | 144 resource_url.empty() ? GURL(main_frame_url) : GURL(resource_url); |
| 145 summary.request_url = summary.resource_url; |
| 113 summary.resource_type = resource_type; | 146 summary.resource_type = resource_type; |
| 114 summary.priority = priority; | 147 summary.priority = priority; |
| 115 summary.mime_type = mime_type; | 148 summary.mime_type = mime_type; |
| 116 summary.was_cached = was_cached; | 149 summary.was_cached = was_cached; |
| 117 if (!redirect_url.empty()) | 150 if (!redirect_url.empty()) |
| 118 summary.redirect_url = GURL(redirect_url); | 151 summary.redirect_url = GURL(redirect_url); |
| 119 summary.has_validators = has_validators; | 152 summary.has_validators = has_validators; |
| 120 summary.always_revalidate = always_revalidate; | 153 summary.always_revalidate = always_revalidate; |
| 154 summary.is_no_store = false; |
| 155 summary.network_accessed = true; |
| 121 return summary; | 156 return summary; |
| 122 } | 157 } |
| 123 | 158 |
| 124 std::ostream& operator<<(std::ostream& os, const PrefetchData& data) { | 159 std::ostream& operator<<(std::ostream& os, const PrefetchData& data) { |
| 125 os << "[" << data.primary_key() << "," << data.last_visit_time() << "]" | 160 os << "[" << data.primary_key() << "," << data.last_visit_time() << "]" |
| 126 << std::endl; | 161 << std::endl; |
| 127 for (const ResourceData& resource : data.resources()) | 162 for (const ResourceData& resource : data.resources()) |
| 128 os << "\t\t" << resource << std::endl; | 163 os << "\t\t" << resource << std::endl; |
| 129 return os; | 164 return os; |
| 130 } | 165 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 146 os << "\t\t" << redirect << std::endl; | 181 os << "\t\t" << redirect << std::endl; |
| 147 return os; | 182 return os; |
| 148 } | 183 } |
| 149 | 184 |
| 150 std::ostream& operator<<(std::ostream& os, const RedirectStat& redirect) { | 185 std::ostream& operator<<(std::ostream& os, const RedirectStat& redirect) { |
| 151 return os << "[" << redirect.url() << "," << redirect.number_of_hits() << "," | 186 return os << "[" << redirect.url() << "," << redirect.number_of_hits() << "," |
| 152 << redirect.number_of_misses() << "," | 187 << redirect.number_of_misses() << "," |
| 153 << redirect.consecutive_misses() << "]"; | 188 << redirect.consecutive_misses() << "]"; |
| 154 } | 189 } |
| 155 | 190 |
| 191 std::ostream& operator<<(std::ostream& os, const OriginData& data) { |
| 192 os << "[" << data.host() << "," << data.last_visit_time() << "]" << std::endl; |
| 193 for (const OriginStat& origin : data.origins()) |
| 194 os << "\t\t" << origin << std::endl; |
| 195 return os; |
| 196 } |
| 197 |
| 198 std::ostream& operator<<(std::ostream& os, const OriginStat& origin) { |
| 199 return os << "[" << origin.origin() << "," << origin.number_of_hits() << "," |
| 200 << origin.number_of_misses() << "," << origin.consecutive_misses() |
| 201 << "," << origin.average_position() << "," |
| 202 << origin.always_access_network() << "," |
| 203 << origin.accessed_network() << "]"; |
| 204 } |
| 205 |
| 156 std::ostream& operator<<(std::ostream& os, const PageRequestSummary& summary) { | 206 std::ostream& operator<<(std::ostream& os, const PageRequestSummary& summary) { |
| 157 os << "[" << summary.main_frame_url << "," << summary.initial_url << "]" | 207 os << "[" << summary.main_frame_url << "," << summary.initial_url << "]" |
| 158 << std::endl; | 208 << std::endl; |
| 159 for (const auto& request : summary.subresource_requests) | 209 for (const auto& request : summary.subresource_requests) |
| 160 os << "\t\t" << request << std::endl; | 210 os << "\t\t" << request << std::endl; |
| 161 return os; | 211 return os; |
| 162 } | 212 } |
| 163 | 213 |
| 164 std::ostream& operator<<(std::ostream& os, const URLRequestSummary& summary) { | 214 std::ostream& operator<<(std::ostream& os, const URLRequestSummary& summary) { |
| 165 return os << "[" << summary.navigation_id << "," << summary.resource_url | 215 return os << "[" << summary.navigation_id << "," << summary.resource_url |
| (...skipping 19 matching lines...) Expand all Loading... |
| 185 | 235 |
| 186 return equal; | 236 return equal; |
| 187 } | 237 } |
| 188 | 238 |
| 189 bool operator==(const ResourceData& lhs, const ResourceData& rhs) { | 239 bool operator==(const ResourceData& lhs, const ResourceData& rhs) { |
| 190 return lhs.resource_url() == rhs.resource_url() && | 240 return lhs.resource_url() == rhs.resource_url() && |
| 191 lhs.resource_type() == rhs.resource_type() && | 241 lhs.resource_type() == rhs.resource_type() && |
| 192 lhs.number_of_hits() == rhs.number_of_hits() && | 242 lhs.number_of_hits() == rhs.number_of_hits() && |
| 193 lhs.number_of_misses() == rhs.number_of_misses() && | 243 lhs.number_of_misses() == rhs.number_of_misses() && |
| 194 lhs.consecutive_misses() == rhs.consecutive_misses() && | 244 lhs.consecutive_misses() == rhs.consecutive_misses() && |
| 195 lhs.average_position() == rhs.average_position() && | 245 AlmostEqual(lhs.average_position(), rhs.average_position()) && |
| 196 lhs.priority() == rhs.priority() && | 246 lhs.priority() == rhs.priority() && |
| 197 lhs.has_validators() == rhs.has_validators() && | 247 lhs.has_validators() == rhs.has_validators() && |
| 198 lhs.always_revalidate() == rhs.always_revalidate(); | 248 lhs.always_revalidate() == rhs.always_revalidate(); |
| 199 } | 249 } |
| 200 | 250 |
| 201 bool operator==(const RedirectData& lhs, const RedirectData& rhs) { | 251 bool operator==(const RedirectData& lhs, const RedirectData& rhs) { |
| 202 bool equal = lhs.primary_key() == rhs.primary_key() && | 252 bool equal = lhs.primary_key() == rhs.primary_key() && |
| 203 lhs.redirect_endpoints_size() == rhs.redirect_endpoints_size(); | 253 lhs.redirect_endpoints_size() == rhs.redirect_endpoints_size(); |
| 204 | 254 |
| 205 if (!equal) | 255 if (!equal) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 228 return lhs.navigation_id == rhs.navigation_id && | 278 return lhs.navigation_id == rhs.navigation_id && |
| 229 lhs.resource_url == rhs.resource_url && | 279 lhs.resource_url == rhs.resource_url && |
| 230 lhs.resource_type == rhs.resource_type && | 280 lhs.resource_type == rhs.resource_type && |
| 231 lhs.priority == rhs.priority && lhs.mime_type == rhs.mime_type && | 281 lhs.priority == rhs.priority && lhs.mime_type == rhs.mime_type && |
| 232 lhs.was_cached == rhs.was_cached && | 282 lhs.was_cached == rhs.was_cached && |
| 233 lhs.redirect_url == rhs.redirect_url && | 283 lhs.redirect_url == rhs.redirect_url && |
| 234 lhs.has_validators == rhs.has_validators && | 284 lhs.has_validators == rhs.has_validators && |
| 235 lhs.always_revalidate == rhs.always_revalidate; | 285 lhs.always_revalidate == rhs.always_revalidate; |
| 236 } | 286 } |
| 237 | 287 |
| 288 bool operator==(const OriginData& lhs, const OriginData& rhs) { |
| 289 bool equal = |
| 290 lhs.host() == rhs.host() && lhs.origins_size() == rhs.origins_size(); |
| 291 if (!equal) |
| 292 return false; |
| 293 |
| 294 for (int i = 0; i < lhs.origins_size(); ++i) |
| 295 equal = equal && lhs.origins(i) == rhs.origins(i); |
| 296 |
| 297 return equal; |
| 298 } |
| 299 |
| 300 bool operator==(const OriginStat& lhs, const OriginStat& rhs) { |
| 301 return lhs.origin() == rhs.origin() && |
| 302 lhs.number_of_hits() == rhs.number_of_hits() && |
| 303 lhs.number_of_misses() == rhs.number_of_misses() && |
| 304 lhs.consecutive_misses() == rhs.consecutive_misses() && |
| 305 AlmostEqual(lhs.average_position(), rhs.average_position()) && |
| 306 lhs.always_access_network() == rhs.always_access_network() && |
| 307 lhs.accessed_network() == rhs.accessed_network(); |
| 308 } |
| 309 |
| 238 } // namespace predictors | 310 } // namespace predictors |
| 239 | 311 |
| 240 namespace precache { | 312 namespace precache { |
| 241 | 313 |
| 242 std::ostream& operator<<(std::ostream& os, const PrecacheManifest& manifest) { | 314 std::ostream& operator<<(std::ostream& os, const PrecacheManifest& manifest) { |
| 243 os << "[" << manifest.id().id() << "]" << std::endl; | 315 os << "[" << manifest.id().id() << "]" << std::endl; |
| 244 for (const PrecacheResource& resource : manifest.resource()) | 316 for (const PrecacheResource& resource : manifest.resource()) |
| 245 os << "\t\t" << resource << std::endl; | 317 os << "\t\t" << resource << std::endl; |
| 246 return os; | 318 return os; |
| 247 } | 319 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 259 return false; | 331 return false; |
| 260 | 332 |
| 261 for (int i = 0; i < lhs.resource_size(); ++i) | 333 for (int i = 0; i < lhs.resource_size(); ++i) |
| 262 equal = equal && lhs.resource(i) == rhs.resource(i); | 334 equal = equal && lhs.resource(i) == rhs.resource(i); |
| 263 | 335 |
| 264 return equal; | 336 return equal; |
| 265 } | 337 } |
| 266 | 338 |
| 267 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { | 339 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { |
| 268 return lhs.url() == rhs.url() && | 340 return lhs.url() == rhs.url() && |
| 269 std::fabs(lhs.weight_ratio() - rhs.weight_ratio()) < | 341 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio()); |
| 270 std::numeric_limits<double>::epsilon(); | |
| 271 } | 342 } |
| 272 | 343 |
| 273 } // namespace precache | 344 } // namespace precache |
| OLD | NEW |