This source file includes following definitions.
- key_loaded_event_
- OnLoaded
- OnKeyLoaded
- Load
- background_task_runner
- client_task_runner
- SetUp
- TearDown
- TEST_F
- TEST_F
#include "content/browser/net/sqlite_persistent_cookie_store.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/files/scoped_temp_dir.h"
#include "base/sequenced_task_runner.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/perf_time_logger.h"
#include "base/test/sequenced_worker_pool_owner.h"
#include "base/threading/sequenced_worker_pool.h"
#include "content/public/browser/cookie_crypto_delegate.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace content {
namespace {
const base::FilePath::CharType cookie_filename[] = FILE_PATH_LITERAL("Cookies");
}
class SQLitePersistentCookieStorePerfTest : public testing::Test {
public:
SQLitePersistentCookieStorePerfTest()
: pool_owner_(new base::SequencedWorkerPoolOwner(1, "Background Pool")),
loaded_event_(false, false),
key_loaded_event_(false, false) {
}
void OnLoaded(const std::vector<net::CanonicalCookie*>& cookies) {
cookies_ = cookies;
loaded_event_.Signal();
}
void OnKeyLoaded(const std::vector<net::CanonicalCookie*>& cookies) {
cookies_ = cookies;
key_loaded_event_.Signal();
}
void Load() {
store_->Load(base::Bind(&SQLitePersistentCookieStorePerfTest::OnLoaded,
base::Unretained(this)));
loaded_event_.Wait();
}
scoped_refptr<base::SequencedTaskRunner> background_task_runner() {
return pool_owner_->pool()->GetSequencedTaskRunner(
pool_owner_->pool()->GetNamedSequenceToken("background"));
}
scoped_refptr<base::SequencedTaskRunner> client_task_runner() {
return pool_owner_->pool()->GetSequencedTaskRunner(
pool_owner_->pool()->GetNamedSequenceToken("client"));
}
virtual void SetUp() OVERRIDE {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
store_ = new SQLitePersistentCookieStore(
temp_dir_.path().Append(cookie_filename),
client_task_runner(),
background_task_runner(),
false, NULL, NULL);
std::vector<net::CanonicalCookie*> cookies;
Load();
ASSERT_EQ(0u, cookies_.size());
base::Time t = base::Time::Now();
for (int domain_num = 0; domain_num < 300; domain_num++) {
std::string domain_name(base::StringPrintf(".domain_%d.com", domain_num));
GURL gurl("www" + domain_name);
for (int cookie_num = 0; cookie_num < 50; ++cookie_num) {
t += base::TimeDelta::FromInternalValue(10);
store_->AddCookie(
net::CanonicalCookie(gurl,
base::StringPrintf("Cookie_%d", cookie_num), "1",
domain_name, "/", t, t, t, false, false,
net::COOKIE_PRIORITY_DEFAULT));
}
}
store_ = NULL;
pool_owner_->pool()->Shutdown();
pool_owner_.reset(new base::SequencedWorkerPoolOwner(1, "pool"));
store_ = new SQLitePersistentCookieStore(
temp_dir_.path().Append(cookie_filename),
client_task_runner(),
background_task_runner(),
false, NULL, NULL);
}
virtual void TearDown() OVERRIDE {
store_ = NULL;
pool_owner_->pool()->Shutdown();
}
protected:
scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_;
base::WaitableEvent loaded_event_;
base::WaitableEvent key_loaded_event_;
std::vector<net::CanonicalCookie*> cookies_;
base::ScopedTempDir temp_dir_;
scoped_refptr<SQLitePersistentCookieStore> store_;
};
TEST_F(SQLitePersistentCookieStorePerfTest, TestLoadForKeyPerformance) {
for (int domain_num = 0; domain_num < 3; ++domain_num) {
std::string domain_name(base::StringPrintf("domain_%d.com", domain_num));
base::PerfTimeLogger timer(
("Load cookies for the eTLD+1 " + domain_name).c_str());
store_->LoadCookiesForKey(domain_name,
base::Bind(&SQLitePersistentCookieStorePerfTest::OnKeyLoaded,
base::Unretained(this)));
key_loaded_event_.Wait();
timer.Done();
ASSERT_EQ(50U, cookies_.size());
}
}
TEST_F(SQLitePersistentCookieStorePerfTest, TestLoadPerformance) {
base::PerfTimeLogger timer("Load all cookies");
Load();
timer.Done();
ASSERT_EQ(15000U, cookies_.size());
}
}