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

Side by Side Diff: net/sdch/sdch_owner_unittest.cc

Issue 1051353003: SDCH: add TimeWeightedMemoryUse (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change metric denominator Created 5 years, 8 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 "base/memory/memory_pressure_listener.h" 5 #include "base/memory/memory_pressure_listener.h"
6 #include "base/prefs/testing_pref_store.h" 6 #include "base/prefs/testing_pref_store.h"
7 #include "base/process/process_info.h"
7 #include "base/run_loop.h" 8 #include "base/run_loop.h"
8 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/test/histogram_tester.h"
9 #include "base/test/simple_test_clock.h" 11 #include "base/test/simple_test_clock.h"
10 #include "base/values.h" 12 #include "base/values.h"
11 #include "net/base/net_log.h" 13 #include "net/base/net_log.h"
12 #include "net/base/sdch_manager.h" 14 #include "net/base/sdch_manager.h"
13 #include "net/sdch/sdch_owner.h" 15 #include "net/sdch/sdch_owner.h"
14 #include "net/url_request/url_request.h" 16 #include "net/url_request/url_request.h"
15 #include "net/url_request/url_request_context.h" 17 #include "net/url_request/url_request_context.h"
16 #include "net/url_request/url_request_error_job.h" 18 #include "net/url_request/url_request_error_job.h"
17 #include "net/url_request/url_request_job.h" 19 #include "net/url_request/url_request_job.h"
18 #include "net/url_request/url_request_job_factory.h" 20 #include "net/url_request/url_request_job_factory.h"
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 // a particular initial state. 249 // a particular initial state.
248 class SdchOwnerTest : public testing::Test { 250 class SdchOwnerTest : public testing::Test {
249 public: 251 public:
250 static const size_t kMaxSizeForTesting = 1000 * 50; 252 static const size_t kMaxSizeForTesting = 1000 * 50;
251 static const size_t kMinFetchSpaceForTesting = 500; 253 static const size_t kMinFetchSpaceForTesting = 500;
252 254
253 SdchOwnerTest() 255 SdchOwnerTest()
254 : last_jobs_created_(error_jobs_created), 256 : last_jobs_created_(error_jobs_created),
255 dictionary_creation_index_(0), 257 dictionary_creation_index_(0),
256 pref_store_(new TestingPrefStore), 258 pref_store_(new TestingPrefStore),
257 sdch_owner_(&sdch_manager_, &url_request_context_) { 259 sdch_owner_(new SdchOwner(&sdch_manager_, &url_request_context_)) {
258 // Any jobs created on this context will immediately error, 260 // Any jobs created on this context will immediately error,
259 // which leaves the test in control of signals to SdchOwner. 261 // which leaves the test in control of signals to SdchOwner.
260 url_request_context_.set_job_factory(&job_factory_); 262 url_request_context_.set_job_factory(&job_factory_);
261 263
262 // Reduce sizes to reduce time for string operations. 264 // Reduce sizes to reduce time for string operations.
263 sdch_owner_.SetMaxTotalDictionarySize(kMaxSizeForTesting); 265 sdch_owner_->SetMaxTotalDictionarySize(kMaxSizeForTesting);
264 sdch_owner_.SetMinSpaceForDictionaryFetch(kMinFetchSpaceForTesting); 266 sdch_owner_->SetMinSpaceForDictionaryFetch(kMinFetchSpaceForTesting);
265 } 267 }
266 268
267 SdchManager& sdch_manager() { return sdch_manager_; } 269 SdchManager& sdch_manager() { return sdch_manager_; }
268 SdchOwner& sdch_owner() { return sdch_owner_; } 270 SdchOwner& sdch_owner() { return *(sdch_owner_.get()); }
269 BoundNetLog& bound_net_log() { return net_log_; } 271 BoundNetLog& bound_net_log() { return net_log_; }
270 TestingPrefStore& pref_store() { return *(pref_store_.get()); } 272 TestingPrefStore& pref_store() { return *(pref_store_.get()); }
271 273
272 int JobsRecentlyCreated() { 274 int JobsRecentlyCreated() {
273 int result = error_jobs_created - last_jobs_created_; 275 int result = error_jobs_created - last_jobs_created_;
274 last_jobs_created_ = error_jobs_created; 276 last_jobs_created_ = error_jobs_created;
275 return result; 277 return result;
276 } 278 }
277 279
278 bool DictionaryPresentInManager(const std::string& server_hash) { 280 bool DictionaryPresentInManager(const std::string& server_hash) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 320
319 if (DictionaryPresentInManager(server_hash)) 321 if (DictionaryPresentInManager(server_hash))
320 return false; 322 return false;
321 sdch_owner().OnDictionaryFetched(last_used_time, 0, dictionary_text, 323 sdch_owner().OnDictionaryFetched(last_used_time, 0, dictionary_text,
322 dictionary_url, net_log_); 324 dictionary_url, net_log_);
323 if (server_hash_p) 325 if (server_hash_p)
324 *server_hash_p = server_hash; 326 *server_hash_p = server_hash;
325 return DictionaryPresentInManager(server_hash); 327 return DictionaryPresentInManager(server_hash);
326 } 328 }
327 329
330 void ResetOwner() {
331 sdch_owner_.reset(new SdchOwner(&sdch_manager_, &url_request_context_));
332 }
333
328 private: 334 private:
329 int last_jobs_created_; 335 int last_jobs_created_;
330 BoundNetLog net_log_; 336 BoundNetLog net_log_;
331 int dictionary_creation_index_; 337 int dictionary_creation_index_;
332 338
333 // The dependencies of these objects (sdch_owner_ -> {sdch_manager_, 339 // The dependencies of these objects (sdch_owner_ -> {sdch_manager_,
334 // url_request_context_}, url_request_context_->job_factory_) require 340 // url_request_context_}, url_request_context_->job_factory_) require
335 // this order for correct destruction semantics. 341 // this order for correct destruction semantics.
336 MockURLRequestJobFactory job_factory_; 342 MockURLRequestJobFactory job_factory_;
337 URLRequestContext url_request_context_; 343 URLRequestContext url_request_context_;
338 SdchManager sdch_manager_; 344 SdchManager sdch_manager_;
339 scoped_refptr<TestingPrefStore> pref_store_; 345 scoped_refptr<TestingPrefStore> pref_store_;
340 SdchOwner sdch_owner_; 346 scoped_ptr<SdchOwner> sdch_owner_;
341 347
342 DISALLOW_COPY_AND_ASSIGN(SdchOwnerTest); 348 DISALLOW_COPY_AND_ASSIGN(SdchOwnerTest);
343 }; 349 };
344 350
345 // Does OnGetDictionary result in a fetch when there's enough space, and not 351 // Does OnGetDictionary result in a fetch when there's enough space, and not
346 // when there's not? 352 // when there's not?
347 TEST_F(SdchOwnerTest, OnGetDictionary_Fetching) { 353 TEST_F(SdchOwnerTest, OnGetDictionary_Fetching) {
348 GURL request_url(std::string(generic_url) + "/r1"); 354 GURL request_url(std::string(generic_url) + "/r1");
349 355
350 // Fetch generated when empty. 356 // Fetch generated when empty.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 dictionary_last_used_time)); 404 dictionary_last_used_time));
399 EXPECT_EQ(0, JobsRecentlyCreated()); 405 EXPECT_EQ(0, JobsRecentlyCreated());
400 } 406 }
401 407
402 // Confirm auto-eviction happens if space is needed. 408 // Confirm auto-eviction happens if space is needed.
403 TEST_F(SdchOwnerTest, ConfirmAutoEviction) { 409 TEST_F(SdchOwnerTest, ConfirmAutoEviction) {
404 std::string server_hash_d1; 410 std::string server_hash_d1;
405 std::string server_hash_d2; 411 std::string server_hash_d2;
406 std::string server_hash_d3; 412 std::string server_hash_d3;
407 413
414 base::SimpleTestClock* test_clock = new base::SimpleTestClock();
415 sdch_owner().SetClockForTesting(make_scoped_ptr(test_clock));
416 test_clock->SetNow(base::Time::Now());
417
408 // Add two dictionaries, one recent, one more than a day in the past. 418 // Add two dictionaries, one recent, one more than a day in the past.
409 base::Time fresh(base::Time::Now() - base::TimeDelta::FromHours(23)); 419 base::Time fresh(base::Time::Now() - base::TimeDelta::FromHours(23));
410 base::Time stale(base::Time::Now() - base::TimeDelta::FromHours(25)); 420 base::Time stale(base::Time::Now() - base::TimeDelta::FromHours(25));
411 421
412 EXPECT_TRUE( 422 EXPECT_TRUE(
413 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d1, fresh)); 423 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d1, fresh));
414 EXPECT_TRUE( 424 EXPECT_TRUE(
415 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d2, stale)); 425 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d2, stale));
416 426
417 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); 427 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1));
418 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); 428 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2));
419 429
430 base::HistogramTester tester;
431 const base::TimeDelta synthetic_delta = base::TimeDelta::FromSeconds(5);
432
433 test_clock->Advance(synthetic_delta);
434
420 EXPECT_TRUE( 435 EXPECT_TRUE(
421 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d3, fresh)); 436 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d3, fresh));
422 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); 437 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1));
423 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); 438 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2));
424 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); 439 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3));
440
441 base::TimeDelta expected_proc_lifetime = synthetic_delta * 3 +
442 base::Time::Now() - base::CurrentProcessInfo::CreationTime();
443 size_t expected_value_base = ((kMaxSizeForTesting / 2) *
444 synthetic_delta.InMilliseconds()) /
445 expected_proc_lifetime.InMilliseconds();
446
447 const char *kHistogram = "Sdch3.TimeWeightedMemoryUse";
448 tester.ExpectTotalCount(kHistogram, 0);
449
450 // Dictionary insertions and deletions:
451 // T = 0: insert d1 and d2
452 // T = 5: insert d3, which evicts d2
453 // T = 15: destroy SdchOwner, which evicts d1 and d3
454 // Therefore, d2's lifetime is synthetic_delta, d1's is synthetic_delta * 3,
455 // and d3's is synthetic_delta * 2. The expected_value_base variable is the
456 // base factor for d2's memory consumption, of which d1's and d3's are
457 // multiples.
458 test_clock->Advance(synthetic_delta * 2);
459 ResetOwner();
460
461 tester.ExpectTotalCount(kHistogram, 3);
462 tester.ExpectBucketCount(kHistogram, expected_value_base, 1);
463 tester.ExpectBucketCount(kHistogram, expected_value_base * 2, 1);
464 tester.ExpectBucketCount(kHistogram, expected_value_base * 3, 1);
425 } 465 }
426 466
427 // Confirm auto-eviction happens if space is needed, with a more complicated 467 // Confirm auto-eviction happens if space is needed, with a more complicated
428 // situation 468 // situation
429 TEST_F(SdchOwnerTest, ConfirmAutoEviction_2) { 469 TEST_F(SdchOwnerTest, ConfirmAutoEviction_2) {
430 std::string server_hash_d1; 470 std::string server_hash_d1;
431 std::string server_hash_d2; 471 std::string server_hash_d2;
432 std::string server_hash_d3; 472 std::string server_hash_d3;
433 473
434 // Add dictionaries, one recent, two more than a day in the past that 474 // Add dictionaries, one recent, two more than a day in the past that
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 887
848 ResetOwner(true); 888 ResetOwner(true);
849 InsertDictionaryForURL(url0, "0"); 889 InsertDictionaryForURL(url0, "0");
850 EXPECT_EQ(1, owner_->GetDictionaryCountForTesting()); 890 EXPECT_EQ(1, owner_->GetDictionaryCountForTesting());
851 owner_->EnablePersistentStorage(pref_store_.get()); 891 owner_->EnablePersistentStorage(pref_store_.get());
852 ASSERT_TRUE(CompleteLoadFromURL(url1, "1")); 892 ASSERT_TRUE(CompleteLoadFromURL(url1, "1"));
853 EXPECT_EQ(2, owner_->GetDictionaryCountForTesting()); 893 EXPECT_EQ(2, owner_->GetDictionaryCountForTesting());
854 } 894 }
855 895
856 } // namespace net 896 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698