| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <time.h> | 5 #include <time.h> |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <sstream> | 8 #include <sstream> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 | 52 |
| 53 private: | 53 private: |
| 54 DnsMaster* master_; | 54 DnsMaster* master_; |
| 55 const NameList hosts_; | 55 const NameList hosts_; |
| 56 HelperTimer* timer_; | 56 HelperTimer* timer_; |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 class DnsMasterTest : public testing::Test { | 59 class DnsMasterTest : public testing::Test { |
| 60 public: | 60 public: |
| 61 DnsMasterTest() | 61 DnsMasterTest() |
| 62 : host_resolver_(new net::MockHostResolver()), | 62 : host_resolver_(new net::MockCachingHostResolver()), |
| 63 default_max_queueing_delay_(TimeDelta::FromMilliseconds( | 63 default_max_queueing_delay_(TimeDelta::FromMilliseconds( |
| 64 DnsPrefetcherInit::kMaxQueueingDelayMs)) { | 64 DnsPrefetcherInit::kMaxQueueingDelayMs)) { |
| 65 } | 65 } |
| 66 | 66 |
| 67 protected: | 67 protected: |
| 68 virtual void SetUp() { | 68 virtual void SetUp() { |
| 69 #if defined(OS_WIN) | 69 #if defined(OS_WIN) |
| 70 net::EnsureWinsockInit(); | 70 net::EnsureWinsockInit(); |
| 71 #endif | 71 #endif |
| 72 // Since we are using a caching HostResolver, the following latencies will |
| 73 // only be incurred by the first request, after which the result will be |
| 74 // cached internally by |host_resolver_|. |
| 72 net::RuleBasedHostResolverProc* rules = host_resolver_->rules(); | 75 net::RuleBasedHostResolverProc* rules = host_resolver_->rules(); |
| 73 rules->AddRuleWithLatency("www.google.com", "127.0.0.1", 50); | 76 rules->AddRuleWithLatency("www.google.com", "127.0.0.1", 50); |
| 74 rules->AddRuleWithLatency("gmail.google.com.com", "127.0.0.1", 70); | 77 rules->AddRuleWithLatency("gmail.google.com.com", "127.0.0.1", 70); |
| 75 rules->AddRuleWithLatency("mail.google.com", "127.0.0.1", 44); | 78 rules->AddRuleWithLatency("mail.google.com", "127.0.0.1", 44); |
| 76 rules->AddRuleWithLatency("gmail.com", "127.0.0.1", 63); | 79 rules->AddRuleWithLatency("gmail.com", "127.0.0.1", 63); |
| 77 } | 80 } |
| 78 | 81 |
| 79 void WaitForResolution(DnsMaster* master, const NameList& hosts) { | 82 void WaitForResolution(DnsMaster* master, const NameList& hosts) { |
| 80 HelperTimer* timer = new HelperTimer(); | 83 HelperTimer* timer = new HelperTimer(); |
| 81 timer->Start(TimeDelta::FromMilliseconds(100), | 84 timer->Start(TimeDelta::FromMilliseconds(100), |
| 82 new WaitForResolutionHelper(master, hosts, timer), | 85 new WaitForResolutionHelper(master, hosts, timer), |
| 83 &WaitForResolutionHelper::Run); | 86 &WaitForResolutionHelper::Run); |
| 84 MessageLoop::current()->Run(); | 87 MessageLoop::current()->Run(); |
| 85 } | 88 } |
| 86 | 89 |
| 87 private: | 90 private: |
| 88 // IMPORTANT: do not move this below |host_resolver_|; the host resolver | 91 // IMPORTANT: do not move this below |host_resolver_|; the host resolver |
| 89 // must not outlive the message loop, otherwise bad things can happen | 92 // must not outlive the message loop, otherwise bad things can happen |
| 90 // (like posting to a deleted message loop). | 93 // (like posting to a deleted message loop). |
| 91 MessageLoop loop; | 94 MessageLoop loop; |
| 92 | 95 |
| 93 protected: | 96 protected: |
| 94 scoped_refptr<net::MockHostResolver> host_resolver_; | 97 scoped_refptr<net::MockCachingHostResolver> host_resolver_; |
| 95 | 98 |
| 96 // Shorthand to access TimeDelta of DnsPrefetcherInit::kMaxQueueingDelayMs. | 99 // Shorthand to access TimeDelta of DnsPrefetcherInit::kMaxQueueingDelayMs. |
| 97 // (It would be a static constant... except style rules preclude that :-/ ). | 100 // (It would be a static constant... except style rules preclude that :-/ ). |
| 98 const TimeDelta default_max_queueing_delay_; | 101 const TimeDelta default_max_queueing_delay_; |
| 99 }; | 102 }; |
| 100 | 103 |
| 101 //------------------------------------------------------------------------------ | 104 //------------------------------------------------------------------------------ |
| 102 // Provide a function to create unique (nonexistant) domains at *every* call. | |
| 103 //------------------------------------------------------------------------------ | |
| 104 static std::string GetNonexistantDomain() { | |
| 105 static std::string postfix = ".google.com"; | |
| 106 static std::string prefix = "www."; | |
| 107 static std::string mid = "datecount"; | |
| 108 | |
| 109 static int counter = 0; // Make sure its unique. | |
| 110 time_t number = time(NULL); | |
| 111 std::ostringstream result; | |
| 112 result << prefix << number << mid << ++counter << postfix; | |
| 113 return result.str(); | |
| 114 } | |
| 115 | |
| 116 //------------------------------------------------------------------------------ | |
| 117 // Use a blocking function to contrast results we get via async services. | |
| 118 //------------------------------------------------------------------------------ | |
| 119 TimeDelta BlockingDnsLookup(net::HostResolver* resolver, | |
| 120 const std::string& hostname) { | |
| 121 Time start = Time::Now(); | |
| 122 | |
| 123 net::AddressList addresses; | |
| 124 net::HostResolver::RequestInfo info(hostname, 80); | |
| 125 resolver->Resolve(info, &addresses, NULL, NULL); | |
| 126 | |
| 127 return Time::Now() - start; | |
| 128 } | |
| 129 | |
| 130 //------------------------------------------------------------------------------ | |
| 131 | |
| 132 // First test to be sure the OS is caching lookups, which is the whole premise | |
| 133 // of DNS prefetching. | |
| 134 TEST_F(DnsMasterTest, OsCachesLookupsTest) { | |
| 135 host_resolver_->rules()->AllowDirectLookup("*.google.com"); | |
| 136 | |
| 137 const Time start = Time::Now(); | |
| 138 int all_lookups = 0; | |
| 139 int lookups_with_improvement = 0; | |
| 140 // This test can be really flaky on Linux. It should run in much shorter time, | |
| 141 // but sometimes it won't and we don't like bogus failures. | |
| 142 while (Time::Now() - start < TimeDelta::FromMinutes(1)) { | |
| 143 std::string badname; | |
| 144 badname = GetNonexistantDomain(); | |
| 145 | |
| 146 TimeDelta duration = BlockingDnsLookup(host_resolver_, badname); | |
| 147 | |
| 148 // Produce more than one result and remove the largest one | |
| 149 // to reduce flakiness. | |
| 150 std::vector<TimeDelta> cached_results; | |
| 151 for (int j = 0; j < 3; j++) | |
| 152 cached_results.push_back(BlockingDnsLookup(host_resolver_, badname)); | |
| 153 std::sort(cached_results.begin(), cached_results.end()); | |
| 154 cached_results.pop_back(); | |
| 155 | |
| 156 TimeDelta cached_sum = TimeDelta::FromSeconds(0); | |
| 157 for (std::vector<TimeDelta>::const_iterator j = cached_results.begin(); | |
| 158 j != cached_results.end(); ++j) | |
| 159 cached_sum += *j; | |
| 160 TimeDelta cached_duration = cached_sum / cached_results.size(); | |
| 161 | |
| 162 all_lookups++; | |
| 163 if (cached_duration < duration) | |
| 164 lookups_with_improvement++; | |
| 165 if (all_lookups >= 10) | |
| 166 if (lookups_with_improvement * 100 > all_lookups * 75) | |
| 167 // Okay, we see the improvement for more than 75% of all lookups. | |
| 168 return; | |
| 169 } | |
| 170 FAIL() << "No substantial improvement in lookup time."; | |
| 171 } | |
| 172 | 105 |
| 173 TEST_F(DnsMasterTest, StartupShutdownTest) { | 106 TEST_F(DnsMasterTest, StartupShutdownTest) { |
| 174 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, | 107 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, |
| 175 MessageLoop::current(), default_max_queueing_delay_, | 108 MessageLoop::current(), default_max_queueing_delay_, |
| 176 DnsPrefetcherInit::kMaxConcurrentLookups); | 109 DnsPrefetcherInit::kMaxConcurrentLookups); |
| 177 testing_master->Shutdown(); | 110 testing_master->Shutdown(); |
| 178 } | 111 } |
| 179 | 112 |
| 180 TEST_F(DnsMasterTest, BenefitLookupTest) { | 113 TEST_F(DnsMasterTest, BenefitLookupTest) { |
| 181 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, | 114 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 EXPECT_EQ(queue.Pop(), "startup"); | 553 EXPECT_EQ(queue.Pop(), "startup"); |
| 621 EXPECT_EQ(queue.Pop(), "omni"); | 554 EXPECT_EQ(queue.Pop(), "omni"); |
| 622 | 555 |
| 623 EXPECT_TRUE(queue.IsEmpty()); | 556 EXPECT_TRUE(queue.IsEmpty()); |
| 624 } | 557 } |
| 625 | 558 |
| 626 | 559 |
| 627 | 560 |
| 628 | 561 |
| 629 } // namespace chrome_browser_net | 562 } // namespace chrome_browser_net |
| OLD | NEW |