OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "base/bind.h" | |
6 #include "base/message_loop.h" | |
7 #include "base/perftimer.h" | |
8 #include "base/scoped_temp_dir.h" | |
9 #include "base/stringprintf.h" | |
10 #include "base/synchronization/waitable_event.h" | |
11 #include "base/test/thread_test_helper.h" | |
12 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" | |
13 #include "chrome/common/chrome_constants.h" | |
14 #include "content/browser/browser_thread.h" | |
15 #include "googleurl/src/gurl.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 class SQLitePersistentCookieStorePerfTest : public testing::Test { | |
19 public: | |
20 SQLitePersistentCookieStorePerfTest() | |
21 : db_thread_(BrowserThread::DB), | |
22 io_thread_(BrowserThread::IO), | |
23 loaded_event_(false, false), | |
24 key_loaded_event_(false, false) { | |
25 } | |
26 | |
27 void OnLoaded( | |
28 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { | |
29 cookies_ = cookies; | |
30 loaded_event_.Signal(); | |
31 } | |
32 | |
33 void OnKeyLoaded( | |
34 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { | |
35 cookies_ = cookies; | |
36 key_loaded_event_.Signal(); | |
37 } | |
38 | |
39 void Load() { | |
40 store_->Load(base::Bind(&SQLitePersistentCookieStorePerfTest::OnLoaded, | |
41 base::Unretained(this))); | |
42 loaded_event_.Wait(); | |
43 } | |
44 | |
45 virtual void SetUp() { | |
46 db_thread_.Start(); | |
47 io_thread_.Start(); | |
48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
49 store_ = new SQLitePersistentCookieStore( | |
50 temp_dir_.path().Append(chrome::kCookieFilename)); | |
51 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | |
52 Load(); | |
53 ASSERT_EQ(0u, cookies_.size()); | |
54 // Creates 15000 cookies from 300 eTLD+1s. | |
55 base::Time t = base::Time::Now(); | |
56 for (int domain_num = 0; domain_num < 300; domain_num++) { | |
57 std::string domain_name(base::StringPrintf(".domain_%d.com", domain_num)); | |
58 GURL gurl("www" + domain_name); | |
59 for (int cookie_num = 0; cookie_num < 50; ++cookie_num) { | |
60 t += base::TimeDelta::FromInternalValue(10); | |
61 store_->AddCookie( | |
62 net::CookieMonster::CanonicalCookie(gurl, | |
63 base::StringPrintf("Cookie_%d", cookie_num), "1", | |
64 domain_name, "/", std::string(), std::string(), | |
65 t, t, t, false, false, true)); | |
66 } | |
67 } | |
68 // Replace the store effectively destroying the current one and forcing it | |
69 // to write it's data to disk. | |
70 store_ = NULL; | |
71 scoped_refptr<base::ThreadTestHelper> helper( | |
72 new base::ThreadTestHelper( | |
73 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
74 // Make sure we wait until the destructor has run. | |
75 ASSERT_TRUE(helper->Run()); | |
76 | |
77 store_ = new SQLitePersistentCookieStore( | |
78 temp_dir_.path().Append(chrome::kCookieFilename)); | |
79 } | |
80 | |
81 protected: | |
82 BrowserThread db_thread_; | |
83 BrowserThread io_thread_; | |
84 base::WaitableEvent loaded_event_; | |
85 base::WaitableEvent key_loaded_event_; | |
86 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; | |
87 ScopedTempDir temp_dir_; | |
88 scoped_refptr<SQLitePersistentCookieStore> store_; | |
89 }; | |
90 | |
91 // Test the performance of priority load of cookies for a specfic domain key | |
92 TEST_F(SQLitePersistentCookieStorePerfTest, TestLoadForKeyPerformance) { | |
93 for (int domain_num = 0; domain_num < 3; ++domain_num) { | |
94 std::string domain_name(base::StringPrintf("domain_%d.com", domain_num)); | |
95 PerfTimeLogger timer( | |
96 ("Load cookies for the eTLD+1 " + domain_name).c_str()); | |
97 store_->LoadCookiesForKey(domain_name, | |
98 base::Bind(&SQLitePersistentCookieStorePerfTest::OnKeyLoaded, | |
99 base::Unretained(this))); | |
100 key_loaded_event_.Wait(); | |
101 timer.Done(); | |
102 | |
103 ASSERT_EQ(50U, cookies_.size()); | |
104 } | |
105 } | |
106 | |
107 // Test the performance of load | |
108 TEST_F(SQLitePersistentCookieStorePerfTest, TestLoadPerformance) { | |
109 PerfTimeLogger timer("Load all cookies"); | |
110 Load(); | |
111 timer.Done(); | |
112 | |
113 ASSERT_EQ(15000U, cookies_.size()); | |
114 } | |
OLD | NEW |