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