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

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

Issue 18337014: Add a HistogramRecorder class and use cases. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove explicit keyword. Created 7 years, 5 months 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/test_completion_callback.h" 16 #include "net/base/test_completion_callback.h"
18 #include "net/url_request/url_request_context.h" 17 #include "net/url_request/url_request_context.h"
19 #include "net/url_request/url_request_test_util.h" 18 #include "net/url_request/url_request_test_util.h"
20 #include "net/url_request/url_request_throttler_header_interface.h" 19 #include "net/url_request/url_request_throttler_header_interface.h"
21 #include "net/url_request/url_request_throttler_test_support.h" 20 #include "net/url_request/url_request_throttler_test_support.h"
22 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
23 22
24 using base::TimeDelta; 23 using base::TimeDelta;
25 using base::TimeTicks; 24 using base::TimeTicks;
26 25
27 namespace net { 26 namespace net {
28 27
29 namespace { 28 namespace {
30 29
31 using base::Histogram; 30 const char* kRequestThrottledHistogramName = "Throttling.RequestThrottled";
32 using base::HistogramBase;
33 using base::HistogramSamples;
34 using base::StatisticsRecorder;
35 31
36 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { 32 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry {
37 public: 33 public:
38 explicit MockURLRequestThrottlerEntry( 34 explicit MockURLRequestThrottlerEntry(
39 net::URLRequestThrottlerManager* manager) 35 net::URLRequestThrottlerManager* manager)
40 : net::URLRequestThrottlerEntry(manager, std::string()), 36 : net::URLRequestThrottlerEntry(manager, std::string()),
41 mock_backoff_entry_(&backoff_policy_) { 37 mock_backoff_entry_(&backoff_policy_) {
42 InitPolicy(); 38 InitPolicy();
43 } 39 }
44 MockURLRequestThrottlerEntry( 40 MockURLRequestThrottlerEntry(
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 }; 165 };
170 166
171 } // namespace 167 } // namespace
172 168
173 class URLRequestThrottlerEntryTest : public testing::Test { 169 class URLRequestThrottlerEntryTest : public testing::Test {
174 protected: 170 protected:
175 URLRequestThrottlerEntryTest() : request_(GURL(), NULL, &context_, NULL) { 171 URLRequestThrottlerEntryTest() : request_(GURL(), NULL, &context_, NULL) {
176 } 172 }
177 173
178 virtual void SetUp(); 174 virtual void SetUp();
179 virtual void TearDown();
180
181 // After calling this function, histogram snapshots in |samples_| contain
182 // only the delta caused by the test case currently running.
183 void CalculateHistogramDeltas();
184 175
185 TimeTicks now_; 176 TimeTicks now_;
186 MockURLRequestThrottlerManager manager_; // Dummy object, not used. 177 MockURLRequestThrottlerManager manager_; // Dummy object, not used.
187 scoped_refptr<MockURLRequestThrottlerEntry> entry_; 178 scoped_refptr<MockURLRequestThrottlerEntry> entry_;
188 179
189 std::map<std::string, HistogramSamples*> original_samples_; 180 scoped_ptr<base::HistogramRecorder> histogram_recorder_;
190 std::map<std::string, HistogramSamples*> samples_;
191 181
192 TestURLRequestContext context_; 182 TestURLRequestContext context_;
193 TestURLRequest request_; 183 TestURLRequest request_;
194 }; 184 };
195 185
196 // List of all histograms we care about in these unit tests.
197 const char* kHistogramNames[] = {
198 "Throttling.FailureCountAtSuccess",
199 "Throttling.PerceivedDowntime",
200 "Throttling.RequestThrottled",
201 "Throttling.SiteOptedOut",
202 };
203
204 void URLRequestThrottlerEntryTest::SetUp() { 186 void URLRequestThrottlerEntryTest::SetUp() {
205 request_.set_load_flags(0); 187 request_.set_load_flags(0);
206 188
207 now_ = TimeTicks::Now(); 189 now_ = TimeTicks::Now();
208 entry_ = new MockURLRequestThrottlerEntry(&manager_); 190 entry_ = new MockURLRequestThrottlerEntry(&manager_);
209 entry_->ResetToBlank(now_); 191 entry_->ResetToBlank(now_);
210 192
211 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { 193 histogram_recorder_.reset(new base::HistogramRecorder());
212 // Must retrieve original samples for each histogram for comparison
213 // as other tests may affect them.
214 const char* name = kHistogramNames[i];
215 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
216 if (histogram) {
217 original_samples_[name] = histogram->SnapshotSamples().release();
218 } else {
219 original_samples_[name] = NULL;
220 }
221 }
222 }
223
224 void URLRequestThrottlerEntryTest::TearDown() {
225 STLDeleteValues(&original_samples_);
226 STLDeleteValues(&samples_);
227 }
228
229 void URLRequestThrottlerEntryTest::CalculateHistogramDeltas() {
230 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) {
231 const char* name = kHistogramNames[i];
232 HistogramSamples* original = original_samples_[name];
233
234 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
235 if (histogram) {
236 ASSERT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
237
238 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
239 if (original)
240 samples->Subtract(*original);
241 samples_[name] = samples.release();
242 }
243 }
244
245 // Ensure we don't accidentally use the originals in our tests.
246 STLDeleteValues(&original_samples_);
247 original_samples_.clear();
248 } 194 }
249 195
250 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { 196 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) {
251 return out << time.ToInternalValue(); 197 return out << time.ToInternalValue();
252 } 198 }
253 199
254 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { 200 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) {
255 entry_->set_exponential_backoff_release_time( 201 entry_->set_exponential_backoff_release_time(
256 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); 202 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1));
257 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); 203 EXPECT_TRUE(entry_->ShouldRejectRequest(request_));
258 204
259 // Also end-to-end test the load flags exceptions. 205 // Also end-to-end test the load flags exceptions.
260 request_.set_load_flags(LOAD_MAYBE_USER_GESTURE); 206 request_.set_load_flags(LOAD_MAYBE_USER_GESTURE);
261 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); 207 EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
262 208
263 CalculateHistogramDeltas(); 209 scoped_ptr<base::HistogramSamples> samples(
264 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0)); 210 histogram_recorder_->GetHistogramSamplesSinceCreation(
265 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1)); 211 kRequestThrottledHistogramName));
212 ASSERT_EQ(1, samples->GetCount(0));
213 ASSERT_EQ(1, samples->GetCount(1));
266 } 214 }
267 215
268 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { 216 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) {
269 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); 217 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_);
270 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); 218 EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
271 entry_->set_exponential_backoff_release_time( 219 entry_->set_exponential_backoff_release_time(
272 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); 220 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1));
273 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); 221 EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
274 222
275 CalculateHistogramDeltas(); 223 scoped_ptr<base::HistogramSamples> samples(
276 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0)); 224 histogram_recorder_->GetHistogramSamplesSinceCreation(
277 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1)); 225 kRequestThrottledHistogramName));
226 ASSERT_EQ(2, samples->GetCount(0));
227 ASSERT_EQ(0, samples->GetCount(1));
278 } 228 }
279 229
280 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { 230 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) {
281 MockURLRequestThrottlerHeaderAdapter failure_response(503); 231 MockURLRequestThrottlerHeaderAdapter failure_response(503);
282 entry_->UpdateWithResponse(std::string(), &failure_response); 232 entry_->UpdateWithResponse(std::string(), &failure_response);
283 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) 233 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_)
284 << "A failure should increase the release_time"; 234 << "A failure should increase the release_time";
285 } 235 }
286 236
287 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { 237 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) {
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 FAIL(); 507 FAIL();
558 } 508 }
559 509
560 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = 510 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after =
561 manager.RegisterRequestUrl(GURL("http://www.example.com/")); 511 manager.RegisterRequestUrl(GURL("http://www.example.com/"));
562 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); 512 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_));
563 } 513 }
564 } 514 }
565 515
566 } // namespace net 516 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698