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