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

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: Don't reset clock Created 5 years, 7 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
« no previous file with comments | « net/sdch/sdch_owner.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/run_loop.h" 7 #include "base/run_loop.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "base/test/histogram_tester.h"
9 #include "base/test/simple_test_clock.h" 10 #include "base/test/simple_test_clock.h"
10 #include "base/values.h" 11 #include "base/values.h"
11 #include "net/base/net_log.h" 12 #include "net/base/net_log.h"
12 #include "net/base/sdch_manager.h" 13 #include "net/base/sdch_manager.h"
13 #include "net/sdch/sdch_owner.h" 14 #include "net/sdch/sdch_owner.h"
14 #include "net/url_request/url_request.h" 15 #include "net/url_request/url_request.h"
15 #include "net/url_request/url_request_context.h" 16 #include "net/url_request/url_request_context.h"
16 #include "net/url_request/url_request_error_job.h" 17 #include "net/url_request/url_request_error_job.h"
17 #include "net/url_request/url_request_job.h" 18 #include "net/url_request/url_request_job.h"
18 #include "net/url_request/url_request_job_factory.h" 19 #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. 248 // a particular initial state.
248 class SdchOwnerTest : public testing::Test { 249 class SdchOwnerTest : public testing::Test {
249 public: 250 public:
250 static const size_t kMaxSizeForTesting = 1000 * 50; 251 static const size_t kMaxSizeForTesting = 1000 * 50;
251 static const size_t kMinFetchSpaceForTesting = 500; 252 static const size_t kMinFetchSpaceForTesting = 500;
252 253
253 SdchOwnerTest() 254 SdchOwnerTest()
254 : last_jobs_created_(error_jobs_created), 255 : last_jobs_created_(error_jobs_created),
255 dictionary_creation_index_(0), 256 dictionary_creation_index_(0),
256 pref_store_(new TestingPrefStore), 257 pref_store_(new TestingPrefStore),
257 sdch_owner_(&sdch_manager_, &url_request_context_) { 258 sdch_owner_(new SdchOwner(&sdch_manager_, &url_request_context_)) {
258 // Any jobs created on this context will immediately error, 259 // Any jobs created on this context will immediately error,
259 // which leaves the test in control of signals to SdchOwner. 260 // which leaves the test in control of signals to SdchOwner.
260 url_request_context_.set_job_factory(&job_factory_); 261 url_request_context_.set_job_factory(&job_factory_);
261 262
262 // Reduce sizes to reduce time for string operations. 263 // Reduce sizes to reduce time for string operations.
263 sdch_owner_.SetMaxTotalDictionarySize(kMaxSizeForTesting); 264 sdch_owner_->SetMaxTotalDictionarySize(kMaxSizeForTesting);
264 sdch_owner_.SetMinSpaceForDictionaryFetch(kMinFetchSpaceForTesting); 265 sdch_owner_->SetMinSpaceForDictionaryFetch(kMinFetchSpaceForTesting);
265 } 266 }
266 267
267 SdchManager& sdch_manager() { return sdch_manager_; } 268 SdchManager& sdch_manager() { return sdch_manager_; }
268 SdchOwner& sdch_owner() { return sdch_owner_; } 269 SdchOwner& sdch_owner() { return *(sdch_owner_.get()); }
269 BoundNetLog& bound_net_log() { return net_log_; } 270 BoundNetLog& bound_net_log() { return net_log_; }
270 TestingPrefStore& pref_store() { return *(pref_store_.get()); } 271 TestingPrefStore& pref_store() { return *(pref_store_.get()); }
271 272
272 int JobsRecentlyCreated() { 273 int JobsRecentlyCreated() {
273 int result = error_jobs_created - last_jobs_created_; 274 int result = error_jobs_created - last_jobs_created_;
274 last_jobs_created_ = error_jobs_created; 275 last_jobs_created_ = error_jobs_created;
275 return result; 276 return result;
276 } 277 }
277 278
278 bool DictionaryPresentInManager(const std::string& server_hash) { 279 bool DictionaryPresentInManager(const std::string& server_hash) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 319
319 if (DictionaryPresentInManager(server_hash)) 320 if (DictionaryPresentInManager(server_hash))
320 return false; 321 return false;
321 sdch_owner().OnDictionaryFetched(last_used_time, 0, dictionary_text, 322 sdch_owner().OnDictionaryFetched(last_used_time, 0, dictionary_text,
322 dictionary_url, net_log_); 323 dictionary_url, net_log_);
323 if (server_hash_p) 324 if (server_hash_p)
324 *server_hash_p = server_hash; 325 *server_hash_p = server_hash;
325 return DictionaryPresentInManager(server_hash); 326 return DictionaryPresentInManager(server_hash);
326 } 327 }
327 328
329 void ResetOwner() {
330 sdch_owner_.reset(new SdchOwner(&sdch_manager_, &url_request_context_));
331 }
332
328 private: 333 private:
329 int last_jobs_created_; 334 int last_jobs_created_;
330 BoundNetLog net_log_; 335 BoundNetLog net_log_;
331 int dictionary_creation_index_; 336 int dictionary_creation_index_;
332 337
333 // The dependencies of these objects (sdch_owner_ -> {sdch_manager_, 338 // The dependencies of these objects (sdch_owner_ -> {sdch_manager_,
334 // url_request_context_}, url_request_context_->job_factory_) require 339 // url_request_context_}, url_request_context_->job_factory_) require
335 // this order for correct destruction semantics. 340 // this order for correct destruction semantics.
336 MockURLRequestJobFactory job_factory_; 341 MockURLRequestJobFactory job_factory_;
337 URLRequestContext url_request_context_; 342 URLRequestContext url_request_context_;
338 SdchManager sdch_manager_; 343 SdchManager sdch_manager_;
339 scoped_refptr<TestingPrefStore> pref_store_; 344 scoped_refptr<TestingPrefStore> pref_store_;
340 SdchOwner sdch_owner_; 345 scoped_ptr<SdchOwner> sdch_owner_;
341 346
342 DISALLOW_COPY_AND_ASSIGN(SdchOwnerTest); 347 DISALLOW_COPY_AND_ASSIGN(SdchOwnerTest);
343 }; 348 };
344 349
345 // Does OnGetDictionary result in a fetch when there's enough space, and not 350 // Does OnGetDictionary result in a fetch when there's enough space, and not
346 // when there's not? 351 // when there's not?
347 TEST_F(SdchOwnerTest, OnGetDictionary_Fetching) { 352 TEST_F(SdchOwnerTest, OnGetDictionary_Fetching) {
348 GURL request_url(std::string(generic_url) + "/r1"); 353 GURL request_url(std::string(generic_url) + "/r1");
349 354
350 // Fetch generated when empty. 355 // Fetch generated when empty.
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 EXPECT_EQ(0, JobsRecentlyCreated()); 399 EXPECT_EQ(0, JobsRecentlyCreated());
395 400
396 // Add unsuccessful when full. 401 // Add unsuccessful when full.
397 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr, 402 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr,
398 dictionary_last_used_time)); 403 dictionary_last_used_time));
399 EXPECT_EQ(0, JobsRecentlyCreated()); 404 EXPECT_EQ(0, JobsRecentlyCreated());
400 } 405 }
401 406
402 // Confirm auto-eviction happens if space is needed. 407 // Confirm auto-eviction happens if space is needed.
403 TEST_F(SdchOwnerTest, ConfirmAutoEviction) { 408 TEST_F(SdchOwnerTest, ConfirmAutoEviction) {
409 base::Time start_time = base::Time::Now();
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() - start_time;
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
« no previous file with comments | « net/sdch/sdch_owner.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698