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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/compiler_specific.h" | |
7 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/message_loop.h" |
8 #include "base/perftimer.h" | 8 #include "base/perftimer.h" |
9 #include "base/sequenced_task_runner.h" | |
10 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
11 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
12 #include "base/test/sequenced_worker_pool_owner.h" | 11 #include "base/test/thread_test_helper.h" |
13 #include "base/threading/sequenced_worker_pool.h" | |
14 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" | 12 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
15 #include "chrome/common/chrome_constants.h" | 13 #include "chrome/common/chrome_constants.h" |
| 14 #include "content/public/test/test_browser_thread.h" |
16 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
17 #include "net/cookies/canonical_cookie.h" | 16 #include "net/cookies/canonical_cookie.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
19 | 18 |
| 19 using content::BrowserThread; |
| 20 |
20 class SQLitePersistentCookieStorePerfTest : public testing::Test { | 21 class SQLitePersistentCookieStorePerfTest : public testing::Test { |
21 public: | 22 public: |
22 SQLitePersistentCookieStorePerfTest() | 23 SQLitePersistentCookieStorePerfTest() |
23 : pool_owner_(new base::SequencedWorkerPoolOwner(1, "Background Pool")), | 24 : db_thread_(BrowserThread::DB), |
| 25 io_thread_(BrowserThread::IO), |
24 loaded_event_(false, false), | 26 loaded_event_(false, false), |
25 key_loaded_event_(false, false) { | 27 key_loaded_event_(false, false) { |
26 } | 28 } |
27 | 29 |
28 void OnLoaded(const std::vector<net::CanonicalCookie*>& cookies) { | 30 void OnLoaded(const std::vector<net::CanonicalCookie*>& cookies) { |
29 cookies_ = cookies; | 31 cookies_ = cookies; |
30 loaded_event_.Signal(); | 32 loaded_event_.Signal(); |
31 } | 33 } |
32 | 34 |
33 void OnKeyLoaded(const std::vector<net::CanonicalCookie*>& cookies) { | 35 void OnKeyLoaded(const std::vector<net::CanonicalCookie*>& cookies) { |
34 cookies_ = cookies; | 36 cookies_ = cookies; |
35 key_loaded_event_.Signal(); | 37 key_loaded_event_.Signal(); |
36 } | 38 } |
37 | 39 |
38 void Load() { | 40 void Load() { |
39 store_->Load(base::Bind(&SQLitePersistentCookieStorePerfTest::OnLoaded, | 41 store_->Load(base::Bind(&SQLitePersistentCookieStorePerfTest::OnLoaded, |
40 base::Unretained(this))); | 42 base::Unretained(this))); |
41 loaded_event_.Wait(); | 43 loaded_event_.Wait(); |
42 } | 44 } |
43 | 45 |
44 scoped_refptr<base::SequencedTaskRunner> background_task_runner() { | 46 virtual void SetUp() { |
45 return pool_owner_->pool()->GetSequencedTaskRunner( | 47 db_thread_.Start(); |
46 pool_owner_->pool()->GetNamedSequenceToken("background")); | 48 io_thread_.Start(); |
47 } | |
48 | |
49 scoped_refptr<base::SequencedTaskRunner> client_task_runner() { | |
50 return pool_owner_->pool()->GetSequencedTaskRunner( | |
51 pool_owner_->pool()->GetNamedSequenceToken("client")); | |
52 } | |
53 | |
54 virtual void SetUp() OVERRIDE { | |
55 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
56 store_ = new SQLitePersistentCookieStore( | 50 store_ = new SQLitePersistentCookieStore( |
57 temp_dir_.path().Append(chrome::kCookieFilename), | 51 temp_dir_.path().Append(chrome::kCookieFilename), |
58 client_task_runner(), | |
59 background_task_runner(), | |
60 false, NULL); | 52 false, NULL); |
61 std::vector<net::CanonicalCookie*> cookies; | 53 std::vector<net::CanonicalCookie*> cookies; |
62 Load(); | 54 Load(); |
63 ASSERT_EQ(0u, cookies_.size()); | 55 ASSERT_EQ(0u, cookies_.size()); |
64 // Creates 15000 cookies from 300 eTLD+1s. | 56 // Creates 15000 cookies from 300 eTLD+1s. |
65 base::Time t = base::Time::Now(); | 57 base::Time t = base::Time::Now(); |
66 for (int domain_num = 0; domain_num < 300; domain_num++) { | 58 for (int domain_num = 0; domain_num < 300; domain_num++) { |
67 std::string domain_name(base::StringPrintf(".domain_%d.com", domain_num)); | 59 std::string domain_name(base::StringPrintf(".domain_%d.com", domain_num)); |
68 GURL gurl("www" + domain_name); | 60 GURL gurl("www" + domain_name); |
69 for (int cookie_num = 0; cookie_num < 50; ++cookie_num) { | 61 for (int cookie_num = 0; cookie_num < 50; ++cookie_num) { |
70 t += base::TimeDelta::FromInternalValue(10); | 62 t += base::TimeDelta::FromInternalValue(10); |
71 store_->AddCookie( | 63 store_->AddCookie( |
72 net::CanonicalCookie(gurl, | 64 net::CanonicalCookie(gurl, |
73 base::StringPrintf("Cookie_%d", cookie_num), "1", | 65 base::StringPrintf("Cookie_%d", cookie_num), "1", |
74 domain_name, "/", std::string(), std::string(), | 66 domain_name, "/", std::string(), std::string(), |
75 t, t, t, false, false)); | 67 t, t, t, false, false)); |
76 } | 68 } |
77 } | 69 } |
78 // Replace the store effectively destroying the current one and forcing it | 70 // Replace the store effectively destroying the current one and forcing it |
79 // to write its data to disk. | 71 // to write its data to disk. |
80 store_ = NULL; | 72 store_ = NULL; |
81 | 73 scoped_refptr<base::ThreadTestHelper> helper( |
82 // Shut down the pool, causing deferred (no-op) commits to be discarded. | 74 new base::ThreadTestHelper( |
83 pool_owner_->pool()->Shutdown(); | 75 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
84 // ~SequencedWorkerPoolOwner blocks on pool shutdown. | 76 // Make sure we wait until the destructor has run. |
85 pool_owner_.reset(new base::SequencedWorkerPoolOwner(1, "pool")); | 77 ASSERT_TRUE(helper->Run()); |
86 | 78 |
87 store_ = new SQLitePersistentCookieStore( | 79 store_ = new SQLitePersistentCookieStore( |
88 temp_dir_.path().Append(chrome::kCookieFilename), | 80 temp_dir_.path().Append(chrome::kCookieFilename), false, NULL); |
89 client_task_runner(), | |
90 background_task_runner(), | |
91 false, NULL); | |
92 } | |
93 | |
94 virtual void TearDown() OVERRIDE { | |
95 store_ = NULL; | |
96 pool_owner_->pool()->Shutdown(); | |
97 } | 81 } |
98 | 82 |
99 protected: | 83 protected: |
100 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; | 84 content::TestBrowserThread db_thread_; |
| 85 content::TestBrowserThread io_thread_; |
101 base::WaitableEvent loaded_event_; | 86 base::WaitableEvent loaded_event_; |
102 base::WaitableEvent key_loaded_event_; | 87 base::WaitableEvent key_loaded_event_; |
103 std::vector<net::CanonicalCookie*> cookies_; | 88 std::vector<net::CanonicalCookie*> cookies_; |
104 base::ScopedTempDir temp_dir_; | 89 base::ScopedTempDir temp_dir_; |
105 scoped_refptr<SQLitePersistentCookieStore> store_; | 90 scoped_refptr<SQLitePersistentCookieStore> store_; |
106 }; | 91 }; |
107 | 92 |
108 // Test the performance of priority load of cookies for a specfic domain key | 93 // Test the performance of priority load of cookies for a specfic domain key |
109 TEST_F(SQLitePersistentCookieStorePerfTest, TestLoadForKeyPerformance) { | 94 TEST_F(SQLitePersistentCookieStorePerfTest, TestLoadForKeyPerformance) { |
110 for (int domain_num = 0; domain_num < 3; ++domain_num) { | 95 for (int domain_num = 0; domain_num < 3; ++domain_num) { |
(...skipping 11 matching lines...) Expand all Loading... |
122 } | 107 } |
123 | 108 |
124 // Test the performance of load | 109 // Test the performance of load |
125 TEST_F(SQLitePersistentCookieStorePerfTest, TestLoadPerformance) { | 110 TEST_F(SQLitePersistentCookieStorePerfTest, TestLoadPerformance) { |
126 PerfTimeLogger timer("Load all cookies"); | 111 PerfTimeLogger timer("Load all cookies"); |
127 Load(); | 112 Load(); |
128 timer.Done(); | 113 timer.Done(); |
129 | 114 |
130 ASSERT_EQ(15000U, cookies_.size()); | 115 ASSERT_EQ(15000U, cookies_.size()); |
131 } | 116 } |
OLD | NEW |