Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: net/url_request/url_request_throttler_unittest.cc

Issue 120753002: Add a StatisticsDeltaReader class and use cases (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "net/url_request/url_request_throttler_manager.h" 5 #include "net/url_request/url_request_throttler_manager.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/histogram_samples.h" 8 #include "base/metrics/histogram_samples.h"
10 #include "base/metrics/statistics_recorder.h"
11 #include "base/pickle.h" 9 #include "base/pickle.h"
12 #include "base/stl_util.h" 10 #include "base/stl_util.h"
13 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
13 #include "base/test/histogram_recorder.h"
15 #include "base/time/time.h" 14 #include "base/time/time.h"
16 #include "net/base/load_flags.h" 15 #include "net/base/load_flags.h"
17 #include "net/base/request_priority.h" 16 #include "net/base/request_priority.h"
18 #include "net/base/test_completion_callback.h" 17 #include "net/base/test_completion_callback.h"
19 #include "net/url_request/url_request_context.h" 18 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_test_util.h" 19 #include "net/url_request/url_request_test_util.h"
21 #include "net/url_request/url_request_throttler_header_interface.h" 20 #include "net/url_request/url_request_throttler_header_interface.h"
22 #include "net/url_request/url_request_throttler_test_support.h" 21 #include "net/url_request/url_request_throttler_test_support.h"
23 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
24 23
25 using base::TimeDelta; 24 using base::TimeDelta;
26 using base::TimeTicks; 25 using base::TimeTicks;
27 26
28 namespace net { 27 namespace net {
29 28
30 namespace { 29 namespace {
31 30
32 using base::Histogram; 31 const char kRequestThrottledHistogramName[] = "Throttling.RequestThrottled";
33 using base::HistogramBase;
34 using base::HistogramSamples;
35 using base::StatisticsRecorder;
36 32
37 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { 33 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry {
38 public: 34 public:
39 explicit MockURLRequestThrottlerEntry( 35 explicit MockURLRequestThrottlerEntry(
40 net::URLRequestThrottlerManager* manager) 36 net::URLRequestThrottlerManager* manager)
41 : net::URLRequestThrottlerEntry(manager, std::string()), 37 : net::URLRequestThrottlerEntry(manager, std::string()),
42 mock_backoff_entry_(&backoff_policy_) { 38 mock_backoff_entry_(&backoff_policy_) {
43 InitPolicy(); 39 InitPolicy();
44 } 40 }
45 MockURLRequestThrottlerEntry( 41 MockURLRequestThrottlerEntry(
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 int line; 165 int line;
170 }; 166 };
171 167
172 } // namespace 168 } // namespace
173 169
174 class URLRequestThrottlerEntryTest : public testing::Test { 170 class URLRequestThrottlerEntryTest : public testing::Test {
175 protected: 171 protected:
176 URLRequestThrottlerEntryTest() 172 URLRequestThrottlerEntryTest()
177 : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} 173 : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {}
178 174
175 static void SetUpTestCase() {
176 base::HistogramRecorder::Initialize();
177 }
178
179 virtual void SetUp(); 179 virtual void SetUp();
180 virtual void TearDown();
181
182 // After calling this function, histogram snapshots in |samples_| contain
183 // only the delta caused by the test case currently running.
184 void CalculateHistogramDeltas();
185 180
186 TimeTicks now_; 181 TimeTicks now_;
187 MockURLRequestThrottlerManager manager_; // Dummy object, not used. 182 MockURLRequestThrottlerManager manager_; // Dummy object, not used.
188 scoped_refptr<MockURLRequestThrottlerEntry> entry_; 183 scoped_refptr<MockURLRequestThrottlerEntry> entry_;
189 184
190 std::map<std::string, HistogramSamples*> original_samples_; 185 scoped_ptr<base::HistogramRecorder> histogram_recorder_;
ppi 2013/12/23 16:37:48 I'd drop it and just create an instance in test wh
lpromero 2013/12/23 17:22:54 Done.
191 std::map<std::string, HistogramSamples*> samples_;
192 186
193 TestURLRequestContext context_; 187 TestURLRequestContext context_;
194 TestURLRequest request_; 188 TestURLRequest request_;
195 }; 189 };
196 190
197 // List of all histograms we care about in these unit tests.
198 const char* kHistogramNames[] = {
199 "Throttling.FailureCountAtSuccess",
200 "Throttling.PerceivedDowntime",
201 "Throttling.RequestThrottled",
202 "Throttling.SiteOptedOut",
203 };
204
205 void URLRequestThrottlerEntryTest::SetUp() { 191 void URLRequestThrottlerEntryTest::SetUp() {
206 request_.SetLoadFlags(0); 192 request_.SetLoadFlags(0);
207 193
208 now_ = TimeTicks::Now(); 194 now_ = TimeTicks::Now();
209 entry_ = new MockURLRequestThrottlerEntry(&manager_); 195 entry_ = new MockURLRequestThrottlerEntry(&manager_);
210 entry_->ResetToBlank(now_); 196 entry_->ResetToBlank(now_);
211 197
212 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { 198 histogram_recorder_.reset(new base::HistogramRecorder());
213 // Must retrieve original samples for each histogram for comparison
214 // as other tests may affect them.
215 const char* name = kHistogramNames[i];
216 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
217 if (histogram) {
218 original_samples_[name] = histogram->SnapshotSamples().release();
219 } else {
220 original_samples_[name] = NULL;
221 }
222 }
223 }
224
225 void URLRequestThrottlerEntryTest::TearDown() {
226 STLDeleteValues(&original_samples_);
227 STLDeleteValues(&samples_);
228 }
229
230 void URLRequestThrottlerEntryTest::CalculateHistogramDeltas() {
231 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) {
232 const char* name = kHistogramNames[i];
233 HistogramSamples* original = original_samples_[name];
234
235 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
236 if (histogram) {
237 ASSERT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
238
239 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
240 if (original)
241 samples->Subtract(*original);
242 samples_[name] = samples.release();
243 }
244 }
245
246 // Ensure we don't accidentally use the originals in our tests.
247 STLDeleteValues(&original_samples_);
248 original_samples_.clear();
249 } 199 }
250 200
251 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { 201 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) {
252 return out << time.ToInternalValue(); 202 return out << time.ToInternalValue();
253 } 203 }
254 204
255 TEST_F(URLRequestThrottlerEntryTest, CanThrottleRequest) { 205 TEST_F(URLRequestThrottlerEntryTest, CanThrottleRequest) {
256 TestNetworkDelegate d; 206 TestNetworkDelegate d;
257 context_.set_network_delegate(&d); 207 context_.set_network_delegate(&d);
258 entry_->set_exponential_backoff_release_time( 208 entry_->set_exponential_backoff_release_time(
259 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); 209 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1));
260 210
261 d.set_can_throttle_requests(false); 211 d.set_can_throttle_requests(false);
262 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); 212 EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
263 d.set_can_throttle_requests(true); 213 d.set_can_throttle_requests(true);
264 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); 214 EXPECT_TRUE(entry_->ShouldRejectRequest(request_));
265 } 215 }
266 216
267 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { 217 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) {
268 entry_->set_exponential_backoff_release_time( 218 entry_->set_exponential_backoff_release_time(
269 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); 219 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1));
270 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); 220 EXPECT_TRUE(entry_->ShouldRejectRequest(request_));
271 221
272 // Also end-to-end test the load flags exceptions. 222 // Also end-to-end test the load flags exceptions.
273 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); 223 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE);
274 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); 224 EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
275 225
276 CalculateHistogramDeltas(); 226 scoped_ptr<base::HistogramSamples> samples(
277 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0)); 227 histogram_recorder_->GetHistogramSamplesSinceCreation(
278 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1)); 228 kRequestThrottledHistogramName));
229 ASSERT_EQ(1, samples->GetCount(0));
230 ASSERT_EQ(1, samples->GetCount(1));
279 } 231 }
280 232
281 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { 233 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) {
282 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); 234 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_);
283 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); 235 EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
284 entry_->set_exponential_backoff_release_time( 236 entry_->set_exponential_backoff_release_time(
285 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); 237 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1));
286 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); 238 EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
287 239
288 CalculateHistogramDeltas(); 240 scoped_ptr<base::HistogramSamples> samples(
289 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0)); 241 histogram_recorder_->GetHistogramSamplesSinceCreation(
290 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1)); 242 kRequestThrottledHistogramName));
243 ASSERT_EQ(2, samples->GetCount(0));
244 ASSERT_EQ(0, samples->GetCount(1));
291 } 245 }
292 246
293 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { 247 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) {
294 MockURLRequestThrottlerHeaderAdapter failure_response(503); 248 MockURLRequestThrottlerHeaderAdapter failure_response(503);
295 entry_->UpdateWithResponse(std::string(), &failure_response); 249 entry_->UpdateWithResponse(std::string(), &failure_response);
296 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) 250 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_)
297 << "A failure should increase the release_time"; 251 << "A failure should increase the release_time";
298 } 252 }
299 253
300 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { 254 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) {
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 FAIL(); 523 FAIL();
570 } 524 }
571 525
572 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = 526 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after =
573 manager.RegisterRequestUrl(GURL("http://www.example.com/")); 527 manager.RegisterRequestUrl(GURL("http://www.example.com/"));
574 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); 528 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_));
575 } 529 }
576 } 530 }
577 531
578 } // namespace net 532 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698