Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/net/sqlite_persistent_cookie_store.h" | 5 #include "net/extras/sqlite/sqlite_persistent_cookie_store.h" |
| 6 | |
| 7 #include <vector> | |
| 6 | 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 11 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 12 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/test/perf_time_logger.h" | 16 #include "base/test/perf_time_logger.h" |
| 14 #include "base/test/sequenced_worker_pool_owner.h" | 17 #include "base/test/sequenced_worker_pool_owner.h" |
| 15 #include "base/threading/sequenced_worker_pool.h" | 18 #include "base/threading/sequenced_worker_pool.h" |
| 16 #include "net/cookies/canonical_cookie.h" | 19 #include "net/cookies/canonical_cookie.h" |
| 17 #include "net/cookies/cookie_constants.h" | 20 #include "net/cookies/cookie_constants.h" |
| 18 #include "net/extras/sqlite/cookie_crypto_delegate.h" | 21 #include "net/extras/sqlite/cookie_crypto_delegate.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 21 | 24 |
| 22 namespace content { | 25 namespace net { |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 const base::FilePath::CharType cookie_filename[] = FILE_PATH_LITERAL("Cookies"); | 29 const base::FilePath::CharType cookie_filename[] = FILE_PATH_LITERAL("Cookies"); |
| 27 | 30 |
| 28 } // namespace | 31 } // namespace |
| 29 | 32 |
| 30 class SQLitePersistentCookieStorePerfTest : public testing::Test { | 33 class SQLitePersistentCookieStorePerfTest : public testing::Test { |
| 31 public: | 34 public: |
| 32 SQLitePersistentCookieStorePerfTest() | 35 SQLitePersistentCookieStorePerfTest() |
| 33 : pool_owner_(new base::SequencedWorkerPoolOwner(1, "Background Pool")), | 36 : pool_owner_(new base::SequencedWorkerPoolOwner(1, "Background Pool")), |
| 34 loaded_event_(false, false), | 37 loaded_event_(false, false), |
| 35 key_loaded_event_(false, false) { | 38 key_loaded_event_(false, false) { |
| 36 } | 39 } |
| 37 | 40 |
| 38 void OnLoaded(const std::vector<net::CanonicalCookie*>& cookies) { | 41 void OnLoaded(const std::vector<CanonicalCookie*>& cookies) { |
| 39 cookies_ = cookies; | 42 cookies_ = cookies; |
| 40 loaded_event_.Signal(); | 43 loaded_event_.Signal(); |
| 41 } | 44 } |
| 42 | 45 |
| 43 void OnKeyLoaded(const std::vector<net::CanonicalCookie*>& cookies) { | 46 void OnKeyLoaded(const std::vector<CanonicalCookie*>& cookies) { |
| 44 cookies_ = cookies; | 47 cookies_ = cookies; |
| 45 key_loaded_event_.Signal(); | 48 key_loaded_event_.Signal(); |
| 46 } | 49 } |
| 47 | 50 |
| 48 void Load() { | 51 void Load() { |
| 49 store_->Load(base::Bind(&SQLitePersistentCookieStorePerfTest::OnLoaded, | 52 store_->Load(base::Bind(&SQLitePersistentCookieStorePerfTest::OnLoaded, |
| 50 base::Unretained(this))); | 53 base::Unretained(this))); |
| 51 loaded_event_.Wait(); | 54 loaded_event_.Wait(); |
| 52 } | 55 } |
| 53 | 56 |
| 54 scoped_refptr<base::SequencedTaskRunner> background_task_runner() { | 57 scoped_refptr<base::SequencedTaskRunner> background_task_runner() { |
| 55 return pool_owner_->pool()->GetSequencedTaskRunner( | 58 if (!background_task_runner_.get()) { |
| 56 pool_owner_->pool()->GetNamedSequenceToken("background")); | 59 background_task_runner_ = pool_owner_->pool()->GetSequencedTaskRunner( |
| 60 pool_owner_->pool()->GetNamedSequenceToken("background")); | |
| 61 } | |
| 62 return background_task_runner_; | |
| 57 } | 63 } |
| 58 | 64 |
| 59 scoped_refptr<base::SequencedTaskRunner> client_task_runner() { | 65 scoped_refptr<base::SequencedTaskRunner> client_task_runner() { |
| 60 return pool_owner_->pool()->GetSequencedTaskRunner( | 66 if (!client_task_runner_.get()) { |
| 61 pool_owner_->pool()->GetNamedSequenceToken("client")); | 67 client_task_runner_ = pool_owner_->pool()->GetSequencedTaskRunner( |
| 68 pool_owner_->pool()->GetNamedSequenceToken("client")); | |
| 69 } | |
| 70 return client_task_runner_; | |
|
Ryan Sleevi
2015/05/14 01:19:35
Same comments about "oof, sorry" :)
| |
| 62 } | 71 } |
| 63 | 72 |
| 64 void SetUp() override { | 73 void SetUp() override { |
| 65 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 74 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 66 store_ = new SQLitePersistentCookieStore( | 75 store_ = new SQLitePersistentCookieStore( |
| 67 temp_dir_.path().Append(cookie_filename), | 76 temp_dir_.path().Append(cookie_filename), |
| 68 client_task_runner(), | 77 client_task_runner(), |
| 69 background_task_runner(), | 78 background_task_runner(), |
| 70 false, NULL, NULL); | 79 false, NULL); |
| 71 std::vector<net::CanonicalCookie*> cookies; | 80 std::vector<CanonicalCookie*> cookies; |
| 72 Load(); | 81 Load(); |
| 73 ASSERT_EQ(0u, cookies_.size()); | 82 ASSERT_EQ(0u, cookies_.size()); |
| 74 // Creates 15000 cookies from 300 eTLD+1s. | 83 // Creates 15000 cookies from 300 eTLD+1s. |
| 75 base::Time t = base::Time::Now(); | 84 base::Time t = base::Time::Now(); |
| 76 for (int domain_num = 0; domain_num < 300; domain_num++) { | 85 for (int domain_num = 0; domain_num < 300; domain_num++) { |
| 77 std::string domain_name(base::StringPrintf(".domain_%d.com", domain_num)); | 86 std::string domain_name(base::StringPrintf(".domain_%d.com", domain_num)); |
| 78 GURL gurl("www" + domain_name); | 87 GURL gurl("www" + domain_name); |
| 79 for (int cookie_num = 0; cookie_num < 50; ++cookie_num) { | 88 for (int cookie_num = 0; cookie_num < 50; ++cookie_num) { |
| 80 t += base::TimeDelta::FromInternalValue(10); | 89 t += base::TimeDelta::FromInternalValue(10); |
| 81 store_->AddCookie(net::CanonicalCookie( | 90 store_->AddCookie(CanonicalCookie( |
| 82 gurl, base::StringPrintf("Cookie_%d", cookie_num), "1", domain_name, | 91 gurl, base::StringPrintf("Cookie_%d", cookie_num), "1", domain_name, |
| 83 "/", t, t, t, false, false, false, net::COOKIE_PRIORITY_DEFAULT)); | 92 "/", t, t, t, false, false, false, COOKIE_PRIORITY_DEFAULT)); |
| 84 } | 93 } |
| 85 } | 94 } |
| 86 // Replace the store effectively destroying the current one and forcing it | 95 // Replace the store effectively destroying the current one and forcing it |
| 87 // to write its data to disk. | 96 // to write its data to disk. |
| 88 store_ = NULL; | 97 store_ = NULL; |
| 89 | 98 |
| 90 // Shut down the pool, causing deferred (no-op) commits to be discarded. | 99 // Shut down the pool, causing deferred (no-op) commits to be discarded. |
| 91 pool_owner_->pool()->Shutdown(); | 100 pool_owner_->pool()->Shutdown(); |
| 92 // ~SequencedWorkerPoolOwner blocks on pool shutdown. | 101 // ~SequencedWorkerPoolOwner blocks on pool shutdown. |
| 93 pool_owner_.reset(new base::SequencedWorkerPoolOwner(1, "pool")); | 102 pool_owner_.reset(new base::SequencedWorkerPoolOwner(1, "pool")); |
| 94 | 103 |
| 95 store_ = new SQLitePersistentCookieStore( | 104 store_ = new SQLitePersistentCookieStore( |
| 96 temp_dir_.path().Append(cookie_filename), | 105 temp_dir_.path().Append(cookie_filename), |
| 97 client_task_runner(), | 106 client_task_runner(), |
| 98 background_task_runner(), | 107 background_task_runner(), |
| 99 false, NULL, NULL); | 108 false, NULL); |
| 100 } | 109 } |
| 101 | 110 |
| 102 void TearDown() override { | 111 void TearDown() override { |
| 103 store_ = NULL; | 112 store_ = NULL; |
| 113 background_task_runner_ = nullptr; | |
| 114 client_task_runner_ = nullptr; | |
| 104 pool_owner_->pool()->Shutdown(); | 115 pool_owner_->pool()->Shutdown(); |
| 105 } | 116 } |
| 106 | 117 |
| 107 protected: | 118 protected: |
| 119 base::MessageLoop main_loop_; | |
| 108 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; | 120 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; |
| 121 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | |
| 122 scoped_refptr<base::SequencedTaskRunner> client_task_runner_; | |
| 109 base::WaitableEvent loaded_event_; | 123 base::WaitableEvent loaded_event_; |
| 110 base::WaitableEvent key_loaded_event_; | 124 base::WaitableEvent key_loaded_event_; |
| 111 std::vector<net::CanonicalCookie*> cookies_; | 125 std::vector<CanonicalCookie*> cookies_; |
| 112 base::ScopedTempDir temp_dir_; | 126 base::ScopedTempDir temp_dir_; |
| 113 scoped_refptr<SQLitePersistentCookieStore> store_; | 127 scoped_refptr<SQLitePersistentCookieStore> store_; |
| 114 }; | 128 }; |
| 115 | 129 |
| 116 // Test the performance of priority load of cookies for a specfic domain key | 130 // Test the performance of priority load of cookies for a specfic domain key |
| 117 TEST_F(SQLitePersistentCookieStorePerfTest, TestLoadForKeyPerformance) { | 131 TEST_F(SQLitePersistentCookieStorePerfTest, TestLoadForKeyPerformance) { |
| 118 for (int domain_num = 0; domain_num < 3; ++domain_num) { | 132 for (int domain_num = 0; domain_num < 3; ++domain_num) { |
| 119 std::string domain_name(base::StringPrintf("domain_%d.com", domain_num)); | 133 std::string domain_name(base::StringPrintf("domain_%d.com", domain_num)); |
| 120 base::PerfTimeLogger timer( | 134 base::PerfTimeLogger timer( |
| 121 ("Load cookies for the eTLD+1 " + domain_name).c_str()); | 135 ("Load cookies for the eTLD+1 " + domain_name).c_str()); |
| 122 store_->LoadCookiesForKey(domain_name, | 136 store_->LoadCookiesForKey(domain_name, |
| 123 base::Bind(&SQLitePersistentCookieStorePerfTest::OnKeyLoaded, | 137 base::Bind(&SQLitePersistentCookieStorePerfTest::OnKeyLoaded, |
| 124 base::Unretained(this))); | 138 base::Unretained(this))); |
| 125 key_loaded_event_.Wait(); | 139 key_loaded_event_.Wait(); |
| 126 timer.Done(); | 140 timer.Done(); |
| 127 | 141 |
| 128 ASSERT_EQ(50U, cookies_.size()); | 142 ASSERT_EQ(50U, cookies_.size()); |
| 129 } | 143 } |
| 130 } | 144 } |
| 131 | 145 |
| 132 // Test the performance of load | 146 // Test the performance of load |
| 133 TEST_F(SQLitePersistentCookieStorePerfTest, TestLoadPerformance) { | 147 TEST_F(SQLitePersistentCookieStorePerfTest, TestLoadPerformance) { |
| 134 base::PerfTimeLogger timer("Load all cookies"); | 148 base::PerfTimeLogger timer("Load all cookies"); |
| 135 Load(); | 149 Load(); |
| 136 timer.Done(); | 150 timer.Done(); |
| 137 | 151 |
| 138 ASSERT_EQ(15000U, cookies_.size()); | 152 ASSERT_EQ(15000U, cookies_.size()); |
| 139 } | 153 } |
| 140 | 154 |
| 141 } // namespace content | 155 } // namespace net |
| OLD | NEW |