Chromium Code Reviews| 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 22 matching lines...) Expand all Loading... | |
| 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); |
| 113 summary.resource_type = resource_type; | 145 summary.resource_type = resource_type; |
| 114 summary.priority = priority; | 146 summary.priority = priority; |
| 115 summary.mime_type = mime_type; | 147 summary.mime_type = mime_type; |
| 116 summary.was_cached = was_cached; | 148 summary.was_cached = was_cached; |
| 117 if (!redirect_url.empty()) | 149 if (!redirect_url.empty()) |
| 118 summary.redirect_url = GURL(redirect_url); | 150 summary.redirect_url = GURL(redirect_url); |
| 119 summary.has_validators = has_validators; | 151 summary.has_validators = has_validators; |
| 120 summary.always_revalidate = always_revalidate; | 152 summary.always_revalidate = always_revalidate; |
| 153 summary.is_no_store = false; | |
| 154 summary.network_accessed = true; | |
| 121 return summary; | 155 return summary; |
| 122 } | 156 } |
| 123 | 157 |
| 124 std::ostream& operator<<(std::ostream& os, const PrefetchData& data) { | 158 std::ostream& operator<<(std::ostream& os, const PrefetchData& data) { |
| 125 os << "[" << data.primary_key() << "," << data.last_visit_time() << "]" | 159 os << "[" << data.primary_key() << "," << data.last_visit_time() << "]" |
| 126 << std::endl; | 160 << std::endl; |
| 127 for (const ResourceData& resource : data.resources()) | 161 for (const ResourceData& resource : data.resources()) |
| 128 os << "\t\t" << resource << std::endl; | 162 os << "\t\t" << resource << std::endl; |
| 129 return os; | 163 return os; |
| 130 } | 164 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 146 os << "\t\t" << redirect << std::endl; | 180 os << "\t\t" << redirect << std::endl; |
| 147 return os; | 181 return os; |
| 148 } | 182 } |
| 149 | 183 |
| 150 std::ostream& operator<<(std::ostream& os, const RedirectStat& redirect) { | 184 std::ostream& operator<<(std::ostream& os, const RedirectStat& redirect) { |
| 151 return os << "[" << redirect.url() << "," << redirect.number_of_hits() << "," | 185 return os << "[" << redirect.url() << "," << redirect.number_of_hits() << "," |
| 152 << redirect.number_of_misses() << "," | 186 << redirect.number_of_misses() << "," |
| 153 << redirect.consecutive_misses() << "]"; | 187 << redirect.consecutive_misses() << "]"; |
| 154 } | 188 } |
| 155 | 189 |
| 190 std::ostream& operator<<(std::ostream& os, const OriginData& data) { | |
| 191 os << "[" << data.host() << "," << data.last_visit_time() << "]" << std::endl; | |
| 192 for (const OriginStat& origin : data.origins()) | |
| 193 os << "\t\t" << origin << std::endl; | |
| 194 return os; | |
| 195 } | |
| 196 | |
| 197 std::ostream& operator<<(std::ostream& os, const OriginStat& origin) { | |
| 198 return os << "[" << origin.origin() << "," << origin.number_of_hits() << "," | |
| 199 << origin.number_of_misses() << "," << origin.consecutive_misses() | |
| 200 << "," << origin.average_position() << "," | |
| 201 << origin.always_access_network() << "," | |
| 202 << origin.accessed_network() << "]"; | |
| 203 } | |
| 204 | |
| 156 std::ostream& operator<<(std::ostream& os, const PageRequestSummary& summary) { | 205 std::ostream& operator<<(std::ostream& os, const PageRequestSummary& summary) { |
| 157 os << "[" << summary.main_frame_url << "," << summary.initial_url << "]" | 206 os << "[" << summary.main_frame_url << "," << summary.initial_url << "]" |
| 158 << std::endl; | 207 << std::endl; |
| 159 for (const auto& request : summary.subresource_requests) | 208 for (const auto& request : summary.subresource_requests) |
| 160 os << "\t\t" << request << std::endl; | 209 os << "\t\t" << request << std::endl; |
| 161 return os; | 210 return os; |
| 162 } | 211 } |
| 163 | 212 |
| 164 std::ostream& operator<<(std::ostream& os, const URLRequestSummary& summary) { | 213 std::ostream& operator<<(std::ostream& os, const URLRequestSummary& summary) { |
| 165 return os << "[" << summary.navigation_id << "," << summary.resource_url | 214 return os << "[" << summary.navigation_id << "," << summary.resource_url |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 185 | 234 |
| 186 return equal; | 235 return equal; |
| 187 } | 236 } |
| 188 | 237 |
| 189 bool operator==(const ResourceData& lhs, const ResourceData& rhs) { | 238 bool operator==(const ResourceData& lhs, const ResourceData& rhs) { |
| 190 return lhs.resource_url() == rhs.resource_url() && | 239 return lhs.resource_url() == rhs.resource_url() && |
| 191 lhs.resource_type() == rhs.resource_type() && | 240 lhs.resource_type() == rhs.resource_type() && |
| 192 lhs.number_of_hits() == rhs.number_of_hits() && | 241 lhs.number_of_hits() == rhs.number_of_hits() && |
| 193 lhs.number_of_misses() == rhs.number_of_misses() && | 242 lhs.number_of_misses() == rhs.number_of_misses() && |
| 194 lhs.consecutive_misses() == rhs.consecutive_misses() && | 243 lhs.consecutive_misses() == rhs.consecutive_misses() && |
| 195 lhs.average_position() == rhs.average_position() && | 244 AlmostEqual(lhs.average_position(), rhs.average_position()) && |
| 196 lhs.priority() == rhs.priority() && | 245 lhs.priority() == rhs.priority() && |
| 197 lhs.has_validators() == rhs.has_validators() && | 246 lhs.has_validators() == rhs.has_validators() && |
| 198 lhs.always_revalidate() == rhs.always_revalidate(); | 247 lhs.always_revalidate() == rhs.always_revalidate(); |
| 199 } | 248 } |
| 200 | 249 |
| 201 bool operator==(const RedirectData& lhs, const RedirectData& rhs) { | 250 bool operator==(const RedirectData& lhs, const RedirectData& rhs) { |
| 202 bool equal = lhs.primary_key() == rhs.primary_key() && | 251 bool equal = lhs.primary_key() == rhs.primary_key() && |
| 203 lhs.redirect_endpoints_size() == rhs.redirect_endpoints_size(); | 252 lhs.redirect_endpoints_size() == rhs.redirect_endpoints_size(); |
| 204 | 253 |
| 205 if (!equal) | 254 if (!equal) |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 228 return lhs.navigation_id == rhs.navigation_id && | 277 return lhs.navigation_id == rhs.navigation_id && |
| 229 lhs.resource_url == rhs.resource_url && | 278 lhs.resource_url == rhs.resource_url && |
| 230 lhs.resource_type == rhs.resource_type && | 279 lhs.resource_type == rhs.resource_type && |
| 231 lhs.priority == rhs.priority && lhs.mime_type == rhs.mime_type && | 280 lhs.priority == rhs.priority && lhs.mime_type == rhs.mime_type && |
| 232 lhs.was_cached == rhs.was_cached && | 281 lhs.was_cached == rhs.was_cached && |
| 233 lhs.redirect_url == rhs.redirect_url && | 282 lhs.redirect_url == rhs.redirect_url && |
| 234 lhs.has_validators == rhs.has_validators && | 283 lhs.has_validators == rhs.has_validators && |
| 235 lhs.always_revalidate == rhs.always_revalidate; | 284 lhs.always_revalidate == rhs.always_revalidate; |
| 236 } | 285 } |
| 237 | 286 |
| 287 bool operator==(const OriginData& lhs, const OriginData& rhs) { | |
| 288 bool equal = | |
| 289 lhs.host() == rhs.host() && lhs.origins_size() == rhs.origins_size(); | |
| 290 if (!equal) | |
| 291 return false; | |
| 292 | |
| 293 for (int i = 0; i < lhs.origins_size(); ++i) | |
| 294 equal = equal && lhs.origins(i) == rhs.origins(i); | |
| 295 | |
| 296 return equal; | |
| 297 } | |
| 298 | |
| 299 bool operator==(const OriginStat& lhs, const OriginStat& rhs) { | |
| 300 return lhs.origin() == rhs.origin() && | |
| 301 lhs.number_of_hits() == rhs.number_of_hits() && | |
| 302 lhs.number_of_misses() == rhs.number_of_misses() && | |
| 303 lhs.consecutive_misses() == rhs.consecutive_misses() && | |
| 304 AlmostEqual(lhs.average_position(), rhs.average_position()) && | |
| 305 lhs.always_access_network() == rhs.always_access_network() && | |
| 306 lhs.accessed_network() == rhs.accessed_network(); | |
| 307 } | |
| 308 | |
| 238 } // namespace predictors | 309 } // namespace predictors |
| 239 | 310 |
| 240 namespace precache { | 311 namespace precache { |
| 241 | 312 |
| 242 std::ostream& operator<<(std::ostream& os, const PrecacheManifest& manifest) { | 313 std::ostream& operator<<(std::ostream& os, const PrecacheManifest& manifest) { |
| 243 os << "[" << manifest.id().id() << "]" << std::endl; | 314 os << "[" << manifest.id().id() << "]" << std::endl; |
| 244 for (const PrecacheResource& resource : manifest.resource()) | 315 for (const PrecacheResource& resource : manifest.resource()) |
| 245 os << "\t\t" << resource << std::endl; | 316 os << "\t\t" << resource << std::endl; |
| 246 return os; | 317 return os; |
| 247 } | 318 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 259 return false; | 330 return false; |
| 260 | 331 |
| 261 for (int i = 0; i < lhs.resource_size(); ++i) | 332 for (int i = 0; i < lhs.resource_size(); ++i) |
| 262 equal = equal && lhs.resource(i) == rhs.resource(i); | 333 equal = equal && lhs.resource(i) == rhs.resource(i); |
| 263 | 334 |
| 264 return equal; | 335 return equal; |
| 265 } | 336 } |
| 266 | 337 |
| 267 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { | 338 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { |
| 268 return lhs.url() == rhs.url() && | 339 return lhs.url() == rhs.url() && |
| 269 std::fabs(lhs.weight_ratio() - rhs.weight_ratio()) < | 340 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio()); |
|
alexilin
2017/04/10 14:58:28
Thanks for fixing this!
| |
| 270 std::numeric_limits<double>::epsilon(); | |
| 271 } | 341 } |
| 272 | 342 |
| 273 } // namespace precache | 343 } // namespace precache |
| OLD | NEW |