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