cookie_monster_perftest.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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 <algorithm> 6 7#include "base/bind.h" 8#include "base/message_loop.h" 9#include "base/perftimer.h" 10#include "base/string_util.h" 11#include "base/stringprintf.h" 12#include "googleurl/src/gurl.h" 13#include "net/cookies/canonical_cookie.h" 14#include "net/cookies/cookie_monster.h" 15#include "net/cookies/cookie_monster_store_test.h" 16#include "net/cookies/parsed_cookie.h" 17#include "testing/gtest/include/gtest/gtest.h" 18 19namespace net { 20 21namespace { 22 23const int kNumCookies = 20000; 24const char kCookieLine[] = "A = \"b=;\\\"\" ;secure;;;"; 25const char kGoogleURL[] = "http://www.google.izzle"; 26 27int CountInString(const std::string& str, char c) { 28 return std::count(str.begin(), str.end(), c); 29} 30 31class CookieMonsterTest : public testing::Test { 32 public: 33 CookieMonsterTest() : message_loop_(new base::MessageLoopForIO()) {} 34 35 private: 36 scoped_ptr<base::MessageLoop> message_loop_; 37}; 38 39class BaseCallback { 40 public: 41 BaseCallback() : has_run_(false) {} 42 43 protected: 44 void WaitForCallback() { 45 // Note that the performance tests currently all operate on a loaded cookie 46 // store (or, more precisely, one that has no backing persistent store). 47 // Therefore, callbacks will actually always complete synchronously. If the 48 // tests get more advanced we need to add other means of signaling 49 // completion. 50 base::MessageLoop::current()->RunUntilIdle(); 51 EXPECT_TRUE(has_run_); 52 has_run_ = false; 53 } 54 55 void Run() { 56 has_run_ = true; 57 } 58 59 bool has_run_; 60}; 61 62class SetCookieCallback : public BaseCallback { 63 public: 64 void SetCookie( 65 CookieMonster* cm, const GURL& gurl, const std::string& cookie) { 66 cm->SetCookieWithOptionsAsync(gurl, cookie, options_, base::Bind( 67 &SetCookieCallback::Run, base::Unretained(this))); 68 WaitForCallback(); 69 } 70 private: 71 void Run(bool success) { 72 EXPECT_TRUE(success); 73 BaseCallback::Run(); 74 } 75 net::CookieOptions options_; 76}; 77 78class GetCookiesCallback : public BaseCallback { 79 public: 80 const std::string& GetCookies(CookieMonster* cm, const GURL& gurl) { 81 cm->GetCookiesWithOptionsAsync(gurl, options_, base::Bind( 82 &GetCookiesCallback::Run, base::Unretained(this))); 83 WaitForCallback(); 84 return cookies_; 85 } 86 87 private: 88 void Run(const std::string& cookies) { 89 cookies_ = cookies; 90 BaseCallback::Run(); 91 } 92 std::string cookies_; 93 net::CookieOptions options_; 94}; 95 96} // namespace 97 98TEST(ParsedCookieTest, TestParseCookies) { 99 std::string cookie(kCookieLine); 100 PerfTimeLogger timer("Parsed_cookie_parse_cookies"); 101 for (int i = 0; i < kNumCookies; ++i) { 102 ParsedCookie pc(cookie); 103 EXPECT_TRUE(pc.IsValid()); 104 } 105 timer.Done(); 106} 107 108TEST(ParsedCookieTest, TestParseBigCookies) { 109 std::string cookie(3800, 'z'); 110 cookie += kCookieLine; 111 PerfTimeLogger timer("Parsed_cookie_parse_big_cookies"); 112 for (int i = 0; i < kNumCookies; ++i) { 113 ParsedCookie pc(cookie); 114 EXPECT_TRUE(pc.IsValid()); 115 } 116 timer.Done(); 117} 118 119TEST_F(CookieMonsterTest, TestAddCookiesOnSingleHost) { 120 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 121 std::vector<std::string> cookies; 122 for (int i = 0; i < kNumCookies; i++) { 123 cookies.push_back(base::StringPrintf("a%03d=b", i)); 124 } 125 126 SetCookieCallback setCookieCallback; 127 128 // Add a bunch of cookies on a single host 129 PerfTimeLogger timer("Cookie_monster_add_single_host"); 130 131 for (std::vector<std::string>::const_iterator it = cookies.begin(); 132 it != cookies.end(); ++it) { 133 setCookieCallback.SetCookie(cm, GURL(kGoogleURL), *it); 134 } 135 timer.Done(); 136 137 GetCookiesCallback getCookiesCallback; 138 139 PerfTimeLogger timer2("Cookie_monster_query_single_host"); 140 for (std::vector<std::string>::const_iterator it = cookies.begin(); 141 it != cookies.end(); ++it) { 142 getCookiesCallback.GetCookies(cm, GURL(kGoogleURL)); 143 } 144 timer2.Done(); 145 146 PerfTimeLogger timer3("Cookie_monster_deleteall_single_host"); 147 cm->DeleteAllAsync(CookieMonster::DeleteCallback()); 148 base::MessageLoop::current()->RunUntilIdle(); 149 timer3.Done(); 150} 151 152TEST_F(CookieMonsterTest, TestAddCookieOnManyHosts) { 153 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 154 std::string cookie(kCookieLine); 155 std::vector<GURL> gurls; // just wanna have ffffuunnn 156 for (int i = 0; i < kNumCookies; ++i) { 157 gurls.push_back(GURL(base::StringPrintf("https://a%04d.izzle", i))); 158 } 159 160 SetCookieCallback setCookieCallback; 161 162 // Add a cookie on a bunch of host 163 PerfTimeLogger timer("Cookie_monster_add_many_hosts"); 164 for (std::vector<GURL>::const_iterator it = gurls.begin(); 165 it != gurls.end(); ++it) { 166 setCookieCallback.SetCookie(cm, *it, cookie); 167 } 168 timer.Done(); 169 170 GetCookiesCallback getCookiesCallback; 171 172 PerfTimeLogger timer2("Cookie_monster_query_many_hosts"); 173 for (std::vector<GURL>::const_iterator it = gurls.begin(); 174 it != gurls.end(); ++it) { 175 getCookiesCallback.GetCookies(cm, *it); 176 } 177 timer2.Done(); 178 179 PerfTimeLogger timer3("Cookie_monster_deleteall_many_hosts"); 180 cm->DeleteAllAsync(CookieMonster::DeleteCallback()); 181 base::MessageLoop::current()->RunUntilIdle(); 182 timer3.Done(); 183} 184 185TEST_F(CookieMonsterTest, TestDomainTree) { 186 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 187 GetCookiesCallback getCookiesCallback; 188 SetCookieCallback setCookieCallback; 189 const char* domain_cookie_format_tree = "a=b; domain=%s"; 190 const std::string domain_base("top.com"); 191 192 std::vector<std::string> domain_list; 193 194 // Create a balanced binary tree of domains on which the cookie is set. 195 domain_list.push_back(domain_base); 196 for (int i1 = 0; i1 < 2; i1++) { 197 std::string domain_base_1((i1 ? "a." : "b.") + domain_base); 198 EXPECT_EQ("top.com", cm->GetKey(domain_base_1)); 199 domain_list.push_back(domain_base_1); 200 for (int i2 = 0; i2 < 2; i2++) { 201 std::string domain_base_2((i2 ? "a." : "b.") + domain_base_1); 202 EXPECT_EQ("top.com", cm->GetKey(domain_base_2)); 203 domain_list.push_back(domain_base_2); 204 for (int i3 = 0; i3 < 2; i3++) { 205 std::string domain_base_3((i3 ? "a." : "b.") + domain_base_2); 206 EXPECT_EQ("top.com", cm->GetKey(domain_base_3)); 207 domain_list.push_back(domain_base_3); 208 for (int i4 = 0; i4 < 2; i4++) { 209 std::string domain_base_4((i4 ? "a." : "b.") + domain_base_3); 210 EXPECT_EQ("top.com", cm->GetKey(domain_base_4)); 211 domain_list.push_back(domain_base_4); 212 } 213 } 214 } 215 } 216 217 218 EXPECT_EQ(31u, domain_list.size()); 219 for (std::vector<std::string>::const_iterator it = domain_list.begin(); 220 it != domain_list.end(); it++) { 221 GURL gurl("https://" + *it + "/"); 222 const std::string cookie = base::StringPrintf(domain_cookie_format_tree, 223 it->c_str()); 224 setCookieCallback.SetCookie(cm, gurl, cookie); 225 } 226 EXPECT_EQ(31u, cm->GetAllCookies().size()); 227 228 GURL probe_gurl("https://b.a.b.a.top.com/"); 229 std::string cookie_line = getCookiesCallback.GetCookies(cm, probe_gurl); 230 EXPECT_EQ(5, CountInString(cookie_line, '=')) << "Cookie line: " << 231 cookie_line; 232 PerfTimeLogger timer("Cookie_monster_query_domain_tree"); 233 for (int i = 0; i < kNumCookies; i++) { 234 getCookiesCallback.GetCookies(cm, probe_gurl); 235 } 236 timer.Done(); 237} 238 239TEST_F(CookieMonsterTest, TestDomainLine) { 240 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 241 SetCookieCallback setCookieCallback; 242 GetCookiesCallback getCookiesCallback; 243 std::vector<std::string> domain_list; 244 GURL probe_gurl("https://b.a.b.a.top.com/"); 245 std::string cookie_line; 246 247 // Create a line of 32 domain cookies such that all cookies stored 248 // by effective TLD+1 will apply to probe GURL. 249 // (TLD + 1 is the level above .com/org/net/etc, e.g. "top.com" 250 // or "google.com". "Effective" is added to include sites like 251 // bbc.co.uk, where the effetive TLD+1 is more than one level 252 // below the top level.) 253 domain_list.push_back("a.top.com"); 254 domain_list.push_back("b.a.top.com"); 255 domain_list.push_back("a.b.a.top.com"); 256 domain_list.push_back("b.a.b.a.top.com"); 257 EXPECT_EQ(4u, domain_list.size()); 258 259 const char* domain_cookie_format_line = "a%03d=b; domain=%s"; 260 for (int i = 0; i < 8; i++) { 261 for (std::vector<std::string>::const_iterator it = domain_list.begin(); 262 it != domain_list.end(); it++) { 263 GURL gurl("https://" + *it + "/"); 264 const std::string cookie = base::StringPrintf(domain_cookie_format_line, 265 i, it->c_str()); 266 setCookieCallback.SetCookie(cm, gurl, cookie); 267 } 268 } 269 270 cookie_line = getCookiesCallback.GetCookies(cm, probe_gurl); 271 EXPECT_EQ(32, CountInString(cookie_line, '=')); 272 PerfTimeLogger timer2("Cookie_monster_query_domain_line"); 273 for (int i = 0; i < kNumCookies; i++) { 274 getCookiesCallback.GetCookies(cm, probe_gurl); 275 } 276 timer2.Done(); 277} 278 279TEST_F(CookieMonsterTest, TestImport) { 280 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore); 281 std::vector<CanonicalCookie*> initial_cookies; 282 GetCookiesCallback getCookiesCallback; 283 284 // We want to setup a fairly large backing store, with 300 domains of 50 285 // cookies each. Creation times must be unique. 286 int64 time_tick(base::Time::Now().ToInternalValue()); 287 288 for (int domain_num = 0; domain_num < 300; domain_num++) { 289 std::string domain_name(base::StringPrintf(".Domain_%d.com", domain_num)); 290 std::string gurl("www" + domain_name); 291 for (int cookie_num = 0; cookie_num < 50; cookie_num++) { 292 std::string cookie_line(base::StringPrintf("Cookie_%d=1; Path=/", 293 cookie_num)); 294 AddCookieToList(gurl, cookie_line, 295 base::Time::FromInternalValue(time_tick++), 296 &initial_cookies); 297 } 298 } 299 300 store->SetLoadExpectation(true, initial_cookies); 301 302 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 303 304 // Import will happen on first access. 305 GURL gurl("www.google.com"); 306 CookieOptions options; 307 PerfTimeLogger timer("Cookie_monster_import_from_store"); 308 getCookiesCallback.GetCookies(cm, gurl); 309 timer.Done(); 310 311 // Just confirm keys were set as expected. 312 EXPECT_EQ("domain_1.com", cm->GetKey("www.Domain_1.com")); 313} 314 315TEST_F(CookieMonsterTest, TestGetKey) { 316 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 317 PerfTimeLogger timer("Cookie_monster_get_key"); 318 for (int i = 0; i < kNumCookies; i++) 319 cm->GetKey("www.google.com"); 320 timer.Done(); 321} 322 323// This test is probing for whether garbage collection happens when it 324// shouldn't. This will not in general be visible functionally, since 325// if GC runs twice in a row without any change to the store, the second 326// GC run will not do anything the first one didn't. That's why this is 327// a performance test. The test should be considered to pass if all the 328// times reported are approximately the same--this indicates that no GC 329// happened repeatedly for any case. 330TEST_F(CookieMonsterTest, TestGCTimes) { 331 SetCookieCallback setCookieCallback; 332 333 const struct TestCase { 334 const char* name; 335 size_t num_cookies; 336 size_t num_old_cookies; 337 } test_cases[] = { 338 { 339 // A whole lot of recent cookies; gc shouldn't happen. 340 "all_recent", 341 CookieMonster::kMaxCookies * 2, 342 0, 343 }, { 344 // Some old cookies, but still overflowing max. 345 "mostly_recent", 346 CookieMonster::kMaxCookies * 2, 347 CookieMonster::kMaxCookies / 2, 348 }, { 349 // Old cookies enough to bring us right down to our purge line. 350 "balanced", 351 CookieMonster::kMaxCookies * 2, 352 CookieMonster::kMaxCookies + CookieMonster::kPurgeCookies + 1, 353 }, { 354 "mostly_old", 355 // Old cookies enough to bring below our purge line (which we 356 // shouldn't do). 357 CookieMonster::kMaxCookies * 2, 358 CookieMonster::kMaxCookies * 3 / 4, 359 }, { 360 "less_than_gc_thresh", 361 // Few enough cookies that gc shouldn't happen at all. 362 CookieMonster::kMaxCookies - 5, 363 0, 364 }, 365 }; 366 for (int ci = 0; ci < static_cast<int>(ARRAYSIZE_UNSAFE(test_cases)); ++ci) { 367 const TestCase& test_case(test_cases[ci]); 368 scoped_refptr<CookieMonster> cm( 369 CreateMonsterFromStoreForGC( 370 test_case.num_cookies, test_case.num_old_cookies, 371 CookieMonster::kSafeFromGlobalPurgeDays * 2)); 372 373 GURL gurl("http://google.com"); 374 std::string cookie_line("z=3"); 375 // Trigger the Garbage collection we're allowed. 376 setCookieCallback.SetCookie(cm, gurl, cookie_line); 377 378 PerfTimeLogger timer((std::string("GC_") + test_case.name).c_str()); 379 for (int i = 0; i < kNumCookies; i++) 380 setCookieCallback.SetCookie(cm, gurl, cookie_line); 381 timer.Done(); 382 } 383} 384 385} // namespace net 386