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

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

Issue 2260573002: predictors: Track whether resources have validators, and require validation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make Visual Studio happy. Created 4 years, 4 months 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 <set> 5 #include <set>
6 #include <utility> 6 #include <utility>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 // Useful for debugging test. 60 // Useful for debugging test.
61 void PrintPrefetchData(const PrefetchData& data) const { 61 void PrintPrefetchData(const PrefetchData& data) const {
62 LOG(ERROR) << "[" << data.key_type << "," << data.primary_key 62 LOG(ERROR) << "[" << data.key_type << "," << data.primary_key
63 << "," << data.last_visit.ToInternalValue() << "]"; 63 << "," << data.last_visit.ToInternalValue() << "]";
64 for (ResourceRows::const_iterator it = data.resources.begin(); 64 for (ResourceRows::const_iterator it = data.resources.begin();
65 it != data.resources.end(); ++it) { 65 it != data.resources.end(); ++it) {
66 LOG(ERROR) << "\t\t" << it->resource_url << "\t" << it->resource_type 66 LOG(ERROR) << "\t\t" << it->resource_url << "\t" << it->resource_type
67 << "\t" << it->number_of_hits << "\t" << it->number_of_misses 67 << "\t" << it->number_of_hits << "\t" << it->number_of_misses
68 << "\t" << it->consecutive_misses << "\t" 68 << "\t" << it->consecutive_misses << "\t"
69 << it->average_position << "\t" << it->priority << "\t" 69 << it->average_position << "\t" << it->priority << "\t"
70 << it->has_validators << "\t" << it->always_revalidate << "\t"
70 << it->score; 71 << it->score;
71 } 72 }
72 } 73 }
73 74
74 PrefetchDataMap test_url_data_; 75 PrefetchDataMap test_url_data_;
75 PrefetchDataMap test_host_data_; 76 PrefetchDataMap test_host_data_;
76 }; 77 };
77 78
78 class ResourcePrefetchPredictorTablesReopenTest 79 class ResourcePrefetchPredictorTablesReopenTest
79 : public ResourcePrefetchPredictorTablesTest { 80 : public ResourcePrefetchPredictorTablesTest {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 PrefetchDataMap expected_host_data; 163 PrefetchDataMap expected_host_data;
163 AddKey(&expected_host_data, "www.yahoo.com"); 164 AddKey(&expected_host_data, "www.yahoo.com");
164 165
165 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); 166 TestPrefetchDataAreEqual(expected_url_data, actual_url_data);
166 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); 167 TestPrefetchDataAreEqual(expected_host_data, actual_host_data);
167 } 168 }
168 169
169 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { 170 void ResourcePrefetchPredictorTablesTest::TestUpdateData() {
170 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); 171 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com");
171 google.last_visit = base::Time::FromInternalValue(10); 172 google.last_visit = base::Time::FromInternalValue(10);
173 google.resources.push_back(ResourceRow(std::string(),
174 "http://www.google.com/style.css",
175 content::RESOURCE_TYPE_STYLESHEET, 6,
176 2, 0, 1.0, net::MEDIUM, true, false));
172 google.resources.push_back(ResourceRow( 177 google.resources.push_back(ResourceRow(
173 std::string(), "http://www.google.com/style.css", 178 std::string(), "http://www.google.com/image.png",
174 content::RESOURCE_TYPE_STYLESHEET, 6, 2, 0, 1.0, net::MEDIUM)); 179 content::RESOURCE_TYPE_IMAGE, 6, 4, 1, 4.2, net::MEDIUM, false, false));
175 google.resources.push_back( 180 google.resources.push_back(ResourceRow(std::string(),
176 ResourceRow(std::string(), "http://www.google.com/image.png", 181 "http://www.google.com/a.xml",
177 content::RESOURCE_TYPE_IMAGE, 6, 4, 1, 4.2, net::MEDIUM)); 182 content::RESOURCE_TYPE_LAST_TYPE, 1, 0,
178 google.resources.push_back( 183 0, 6.1, net::MEDIUM, false, false));
179 ResourceRow(std::string(), "http://www.google.com/a.xml", 184 google.resources.push_back(ResourceRow(
180 content::RESOURCE_TYPE_LAST_TYPE, 1, 0, 0, 6.1, net::MEDIUM)); 185 std::string(), "http://www.resources.google.com/script.js",
181 google.resources.push_back( 186 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM, true, true));
182 ResourceRow(std::string(), "http://www.resources.google.com/script.js",
183 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM));
184 187
185 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); 188 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com");
186 yahoo.last_visit = base::Time::FromInternalValue(7); 189 yahoo.last_visit = base::Time::FromInternalValue(7);
187 yahoo.resources.push_back( 190 yahoo.resources.push_back(ResourceRow(
188 ResourceRow(std::string(), "http://www.yahoo.com/image.png", 191 std::string(), "http://www.yahoo.com/image.png",
189 content::RESOURCE_TYPE_IMAGE, 120, 1, 1, 10.0, net::MEDIUM)); 192 content::RESOURCE_TYPE_IMAGE, 120, 1, 1, 10.0, net::MEDIUM, true, false));
190 193
191 tables_->UpdateData(google, yahoo); 194 tables_->UpdateData(google, yahoo);
192 195
193 PrefetchDataMap actual_url_data, actual_host_data; 196 PrefetchDataMap actual_url_data, actual_host_data;
194 tables_->GetAllData(&actual_url_data, &actual_host_data); 197 tables_->GetAllData(&actual_url_data, &actual_host_data);
195 198
196 PrefetchDataMap expected_url_data, expected_host_data; 199 PrefetchDataMap expected_url_data, expected_host_data;
197 AddKey(&expected_url_data, "http://www.reddit.com"); 200 AddKey(&expected_url_data, "http://www.reddit.com");
198 AddKey(&expected_url_data, "http://www.yahoo.com"); 201 AddKey(&expected_url_data, "http://www.yahoo.com");
199 expected_url_data.insert(std::make_pair("http://www.google.com", google)); 202 expected_url_data.insert(std::make_pair("http://www.google.com", google));
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 } 264 }
262 it = test_host_data_.find(key); 265 it = test_host_data_.find(key);
263 ASSERT_TRUE(it != test_host_data_.end()); 266 ASSERT_TRUE(it != test_host_data_.end());
264 m->insert(std::make_pair(it->first, it->second)); 267 m->insert(std::make_pair(it->first, it->second));
265 } 268 }
266 269
267 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { 270 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() {
268 { // Url data. 271 { // Url data.
269 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); 272 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com");
270 google.last_visit = base::Time::FromInternalValue(1); 273 google.last_visit = base::Time::FromInternalValue(1);
274 google.resources.push_back(
275 ResourceRow(std::string(), "http://www.google.com/style.css",
276 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1,
277 net::MEDIUM, false, false));
278 google.resources.push_back(ResourceRow(std::string(),
279 "http://www.google.com/script.js",
280 content::RESOURCE_TYPE_SCRIPT, 4, 0,
281 1, 2.1, net::MEDIUM, false, false));
271 google.resources.push_back(ResourceRow( 282 google.resources.push_back(ResourceRow(
272 std::string(), "http://www.google.com/style.css", 283 std::string(), "http://www.google.com/image.png",
273 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, net::MEDIUM)); 284 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM, false, false));
274 google.resources.push_back( 285 google.resources.push_back(
275 ResourceRow(std::string(), "http://www.google.com/script.js", 286 ResourceRow(std::string(), "http://www.google.com/a.font",
276 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 2.1, net::MEDIUM)); 287 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM,
277 google.resources.push_back( 288 false, false));
278 ResourceRow(std::string(), "http://www.google.com/image.png",
279 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM));
280 google.resources.push_back(ResourceRow(
281 std::string(), "http://www.google.com/a.font",
282 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM));
283 google.resources.push_back( 289 google.resources.push_back(
284 ResourceRow(std::string(), "http://www.resources.google.com/script.js", 290 ResourceRow(std::string(), "http://www.resources.google.com/script.js",
285 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM)); 291 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM,
292 false, false));
286 293
287 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com"); 294 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com");
288 reddit.last_visit = base::Time::FromInternalValue(2); 295 reddit.last_visit = base::Time::FromInternalValue(2);
289 reddit.resources.push_back( 296 reddit.resources.push_back(
290 ResourceRow(std::string(), "http://reddit-resource.com/script1.js", 297 ResourceRow(std::string(), "http://reddit-resource.com/script1.js",
291 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 1.0, net::MEDIUM)); 298 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 1.0, net::MEDIUM,
299 false, false));
292 reddit.resources.push_back( 300 reddit.resources.push_back(
293 ResourceRow(std::string(), "http://reddit-resource.com/script2.js", 301 ResourceRow(std::string(), "http://reddit-resource.com/script2.js",
294 content::RESOURCE_TYPE_SCRIPT, 2, 0, 0, 2.1, net::MEDIUM)); 302 content::RESOURCE_TYPE_SCRIPT, 2, 0, 0, 2.1, net::MEDIUM,
303 false, false));
295 304
296 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com"); 305 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com");
297 yahoo.last_visit = base::Time::FromInternalValue(3); 306 yahoo.last_visit = base::Time::FromInternalValue(3);
298 yahoo.resources.push_back( 307 yahoo.resources.push_back(ResourceRow(std::string(),
299 ResourceRow(std::string(), "http://www.google.com/image.png", 308 "http://www.google.com/image.png",
300 content::RESOURCE_TYPE_IMAGE, 20, 1, 0, 10.0, net::MEDIUM)); 309 content::RESOURCE_TYPE_IMAGE, 20, 1,
310 0, 10.0, net::MEDIUM, false, false));
301 311
302 test_url_data_.clear(); 312 test_url_data_.clear();
303 test_url_data_.insert(std::make_pair("http://www.google.com", google)); 313 test_url_data_.insert(std::make_pair("http://www.google.com", google));
304 test_url_data_.insert(std::make_pair("http://www.reddit.com", reddit)); 314 test_url_data_.insert(std::make_pair("http://www.reddit.com", reddit));
305 test_url_data_.insert(std::make_pair("http://www.yahoo.com", yahoo)); 315 test_url_data_.insert(std::make_pair("http://www.yahoo.com", yahoo));
306 316
307 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string()); 317 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string());
308 tables_->UpdateData(google, empty_host_data); 318 tables_->UpdateData(google, empty_host_data);
309 tables_->UpdateData(reddit, empty_host_data); 319 tables_->UpdateData(reddit, empty_host_data);
310 tables_->UpdateData(yahoo, empty_host_data); 320 tables_->UpdateData(yahoo, empty_host_data);
311 } 321 }
312 322
313 { // Host data. 323 { // Host data.
314 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); 324 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com");
315 facebook.last_visit = base::Time::FromInternalValue(4); 325 facebook.last_visit = base::Time::FromInternalValue(4);
316 facebook.resources.push_back(ResourceRow( 326 facebook.resources.push_back(
317 std::string(), "http://www.facebook.com/style.css", 327 ResourceRow(std::string(), "http://www.facebook.com/style.css",
318 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, net::MEDIUM)); 328 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1,
329 net::MEDIUM, false, false));
319 facebook.resources.push_back( 330 facebook.resources.push_back(
320 ResourceRow(std::string(), "http://www.facebook.com/script.js", 331 ResourceRow(std::string(), "http://www.facebook.com/script.js",
321 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 2.1, net::MEDIUM)); 332 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 2.1, net::MEDIUM,
333 false, false));
334 facebook.resources.push_back(ResourceRow(
335 std::string(), "http://www.facebook.com/image.png",
336 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM, false, false));
322 facebook.resources.push_back( 337 facebook.resources.push_back(
323 ResourceRow(std::string(), "http://www.facebook.com/image.png", 338 ResourceRow(std::string(), "http://www.facebook.com/a.font",
324 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM)); 339 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM,
325 facebook.resources.push_back(ResourceRow( 340 false, false));
326 std::string(), "http://www.facebook.com/a.font",
327 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM));
328 facebook.resources.push_back(ResourceRow( 341 facebook.resources.push_back(ResourceRow(
329 std::string(), "http://www.resources.facebook.com/script.js", 342 std::string(), "http://www.resources.facebook.com/script.js",
330 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM)); 343 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM, false,
344 false));
331 345
332 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); 346 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com");
333 yahoo.last_visit = base::Time::FromInternalValue(5); 347 yahoo.last_visit = base::Time::FromInternalValue(5);
334 yahoo.resources.push_back( 348 yahoo.resources.push_back(ResourceRow(std::string(),
335 ResourceRow(std::string(), "http://www.google.com/image.png", 349 "http://www.google.com/image.png",
336 content::RESOURCE_TYPE_IMAGE, 20, 1, 0, 10.0, net::MEDIUM)); 350 content::RESOURCE_TYPE_IMAGE, 20, 1,
351 0, 10.0, net::MEDIUM, false, false));
337 352
338 test_host_data_.clear(); 353 test_host_data_.clear();
339 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); 354 test_host_data_.insert(std::make_pair("www.facebook.com", facebook));
340 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); 355 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo));
341 356
342 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); 357 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string());
343 tables_->UpdateData(empty_url_data, facebook); 358 tables_->UpdateData(empty_url_data, facebook);
344 tables_->UpdateData(empty_url_data, yahoo); 359 tables_->UpdateData(empty_url_data, yahoo);
345 } 360 }
346 } 361 }
347 362
348 // Test cases. 363 // Test cases.
349 364
350 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeScore) { 365 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeScore) {
351 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow; 366 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow;
352 ResourceRow js_resource( 367 ResourceRow js_resource(
353 std::string(), "http://www.resources.google.com/script.js", 368 std::string(), "http://www.resources.google.com/script.js",
354 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., net::MEDIUM); 369 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., net::MEDIUM, false, false);
355 ResourceRow image_resource( 370 ResourceRow image_resource(
356 std::string(), "http://www.resources.google.com/image.jpg", 371 std::string(), "http://www.resources.google.com/image.jpg",
357 content::RESOURCE_TYPE_IMAGE, 11, 0, 0, 1., net::MEDIUM); 372 content::RESOURCE_TYPE_IMAGE, 11, 0, 0, 1., net::MEDIUM, false, false);
358 ResourceRow css_resource( 373 ResourceRow css_resource(std::string(),
359 std::string(), "http://www.resources.google.com/stylesheet.css", 374 "http://www.resources.google.com/stylesheet.css",
360 content::RESOURCE_TYPE_STYLESHEET, 11, 0, 0, 1., net::MEDIUM); 375 content::RESOURCE_TYPE_STYLESHEET, 11, 0, 0, 1.,
361 ResourceRow font_resource( 376 net::MEDIUM, false, false);
362 std::string(), "http://www.resources.google.com/font.woff", 377 ResourceRow font_resource(std::string(),
363 content::RESOURCE_TYPE_FONT_RESOURCE, 11, 0, 0, 1., net::MEDIUM); 378 "http://www.resources.google.com/font.woff",
379 content::RESOURCE_TYPE_FONT_RESOURCE, 11, 0, 0, 1.,
380 net::MEDIUM, false, false);
364 EXPECT_TRUE(js_resource.score == css_resource.score); 381 EXPECT_TRUE(js_resource.score == css_resource.score);
365 EXPECT_TRUE(js_resource.score == font_resource.score); 382 EXPECT_TRUE(js_resource.score == font_resource.score);
366 EXPECT_NEAR(199., js_resource.score, 1e-4); 383 EXPECT_NEAR(199., js_resource.score, 1e-4);
367 EXPECT_NEAR(99., image_resource.score, 1e-4); 384 EXPECT_NEAR(99., image_resource.score, 1e-4);
368 } 385 }
369 386
370 TEST_F(ResourcePrefetchPredictorTablesTest, GetAllData) { 387 TEST_F(ResourcePrefetchPredictorTablesTest, GetAllData) {
371 TestGetAllData(); 388 TestGetAllData();
372 } 389 }
373 390
(...skipping 27 matching lines...) Expand all
401 418
402 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { 419 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) {
403 TestDeleteSingleDataPoint(); 420 TestDeleteSingleDataPoint();
404 } 421 }
405 422
406 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { 423 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) {
407 TestDeleteAllData(); 424 TestDeleteAllData();
408 } 425 }
409 426
410 } // namespace predictors 427 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698