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.
5 #include "content/browser/net/sqlite_persistent_cookie_store.h"
8 #include "base/compiler_specific.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/test/perf_time_logger.h"
14 #include "base/test/sequenced_worker_pool_owner.h"
15 #include "base/threading/sequenced_worker_pool.h"
16 #include "content/public/browser/cookie_crypto_delegate.h"
17 #include "net/cookies/canonical_cookie.h"
18 #include "net/cookies/cookie_constants.h"
19 #include "testing/gtest/include/gtest/gtest.h"
26 const base::FilePath::CharType cookie_filename
[] = FILE_PATH_LITERAL("Cookies");
30 class SQLitePersistentCookieStorePerfTest
: public testing::Test
{
32 SQLitePersistentCookieStorePerfTest()
33 : pool_owner_(new base::SequencedWorkerPoolOwner(1, "Background Pool")),
34 loaded_event_(false, false),
35 key_loaded_event_(false, false) {
38 void OnLoaded(const std::vector
<net::CanonicalCookie
*>& cookies
) {
40 loaded_event_
.Signal();
43 void OnKeyLoaded(const std::vector
<net::CanonicalCookie
*>& cookies
) {
45 key_loaded_event_
.Signal();
49 store_
->Load(base::Bind(&SQLitePersistentCookieStorePerfTest::OnLoaded
,
50 base::Unretained(this)));
54 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner() {
55 return pool_owner_
->pool()->GetSequencedTaskRunner(
56 pool_owner_
->pool()->GetNamedSequenceToken("background"));
59 scoped_refptr
<base::SequencedTaskRunner
> client_task_runner() {
60 return pool_owner_
->pool()->GetSequencedTaskRunner(
61 pool_owner_
->pool()->GetNamedSequenceToken("client"));
64 virtual void SetUp() OVERRIDE
{
65 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
66 store_
= new SQLitePersistentCookieStore(
67 temp_dir_
.path().Append(cookie_filename
),
69 background_task_runner(),
71 scoped_ptr
<content::CookieCryptoDelegate
>());
72 std::vector
<net::CanonicalCookie
*> cookies
;
74 ASSERT_EQ(0u, cookies_
.size());
75 // Creates 15000 cookies from 300 eTLD+1s.
76 base::Time t
= base::Time::Now();
77 for (int domain_num
= 0; domain_num
< 300; domain_num
++) {
78 std::string
domain_name(base::StringPrintf(".domain_%d.com", domain_num
));
79 GURL
gurl("www" + domain_name
);
80 for (int cookie_num
= 0; cookie_num
< 50; ++cookie_num
) {
81 t
+= base::TimeDelta::FromInternalValue(10);
83 net::CanonicalCookie(gurl
,
84 base::StringPrintf("Cookie_%d", cookie_num
), "1",
85 domain_name
, "/", t
, t
, t
, false, false,
86 net::COOKIE_PRIORITY_DEFAULT
));
89 // Replace the store effectively destroying the current one and forcing it
90 // to write its data to disk.
93 // Shut down the pool, causing deferred (no-op) commits to be discarded.
94 pool_owner_
->pool()->Shutdown();
95 // ~SequencedWorkerPoolOwner blocks on pool shutdown.
96 pool_owner_
.reset(new base::SequencedWorkerPoolOwner(1, "pool"));
98 store_
= new SQLitePersistentCookieStore(
99 temp_dir_
.path().Append(cookie_filename
),
100 client_task_runner(),
101 background_task_runner(),
103 scoped_ptr
<content::CookieCryptoDelegate
>());
106 virtual void TearDown() OVERRIDE
{
108 pool_owner_
->pool()->Shutdown();
112 scoped_ptr
<base::SequencedWorkerPoolOwner
> pool_owner_
;
113 base::WaitableEvent loaded_event_
;
114 base::WaitableEvent key_loaded_event_
;
115 std::vector
<net::CanonicalCookie
*> cookies_
;
116 base::ScopedTempDir temp_dir_
;
117 scoped_refptr
<SQLitePersistentCookieStore
> store_
;
120 // Test the performance of priority load of cookies for a specfic domain key
121 TEST_F(SQLitePersistentCookieStorePerfTest
, TestLoadForKeyPerformance
) {
122 for (int domain_num
= 0; domain_num
< 3; ++domain_num
) {
123 std::string
domain_name(base::StringPrintf("domain_%d.com", domain_num
));
124 base::PerfTimeLogger
timer(
125 ("Load cookies for the eTLD+1 " + domain_name
).c_str());
126 store_
->LoadCookiesForKey(domain_name
,
127 base::Bind(&SQLitePersistentCookieStorePerfTest::OnKeyLoaded
,
128 base::Unretained(this)));
129 key_loaded_event_
.Wait();
132 ASSERT_EQ(50U, cookies_
.size());
136 // Test the performance of load
137 TEST_F(SQLitePersistentCookieStorePerfTest
, TestLoadPerformance
) {
138 base::PerfTimeLogger
timer("Load all cookies");
142 ASSERT_EQ(15000U, cookies_
.size());
145 } // namespace content