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

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

Issue 19722009: Revert 212378 "Add a HistogramRecorder class and use cases." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: 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
« no previous file with comments | « trunk/src/chrome/browser/ui/cocoa/browser/password_generation_bubble_controller_unittest.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1
2 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 // 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
4 // found in the LICENSE file. 3 // found in the LICENSE file.
5 4
6 #include "net/url_request/url_request_throttler_manager.h" 5 #include "net/url_request/url_request_throttler_manager.h"
7 6
8 #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" 9 #include "base/metrics/histogram_samples.h"
10 #include "base/metrics/statistics_recorder.h"
10 #include "base/pickle.h" 11 #include "base/pickle.h"
11 #include "base/stl_util.h" 12 #include "base/stl_util.h"
12 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
14 #include "base/test/histogram_recorder.h"
15 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "net/base/load_flags.h" 16 #include "net/base/load_flags.h"
17 #include "net/base/test_completion_callback.h" 17 #include "net/base/test_completion_callback.h"
18 #include "net/url_request/url_request_context.h" 18 #include "net/url_request/url_request_context.h"
19 #include "net/url_request/url_request_test_util.h" 19 #include "net/url_request/url_request_test_util.h"
20 #include "net/url_request/url_request_throttler_header_interface.h" 20 #include "net/url_request/url_request_throttler_header_interface.h"
21 #include "net/url_request/url_request_throttler_test_support.h" 21 #include "net/url_request/url_request_throttler_test_support.h"
22 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
23 23
24 using base::TimeDelta; 24 using base::TimeDelta;
25 using base::TimeTicks; 25 using base::TimeTicks;
26 26
27 namespace net { 27 namespace net {
28 28
29 namespace { 29 namespace {
30 30
31 const char kRequestThrottledHistogramName[] = "Throttling.RequestThrottled"; 31 using base::Histogram;
32 using base::HistogramBase;
33 using base::HistogramSamples;
34 using base::StatisticsRecorder;
32 35
33 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { 36 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry {
34 public: 37 public:
35 explicit MockURLRequestThrottlerEntry( 38 explicit MockURLRequestThrottlerEntry(
36 net::URLRequestThrottlerManager* manager) 39 net::URLRequestThrottlerManager* manager)
37 : net::URLRequestThrottlerEntry(manager, std::string()), 40 : net::URLRequestThrottlerEntry(manager, std::string()),
38 mock_backoff_entry_(&backoff_policy_) { 41 mock_backoff_entry_(&backoff_policy_) {
39 InitPolicy(); 42 InitPolicy();
40 } 43 }
41 MockURLRequestThrottlerEntry( 44 MockURLRequestThrottlerEntry(
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 }; 169 };
167 170
168 } // namespace 171 } // namespace
169 172
170 class URLRequestThrottlerEntryTest : public testing::Test { 173 class URLRequestThrottlerEntryTest : public testing::Test {
171 protected: 174 protected:
172 URLRequestThrottlerEntryTest() : request_(GURL(), NULL, &context_, NULL) { 175 URLRequestThrottlerEntryTest() : request_(GURL(), NULL, &context_, NULL) {
173 } 176 }
174 177
175 virtual void SetUp(); 178 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();
176 184
177 TimeTicks now_; 185 TimeTicks now_;
178 MockURLRequestThrottlerManager manager_; // Dummy object, not used. 186 MockURLRequestThrottlerManager manager_; // Dummy object, not used.
179 scoped_refptr<MockURLRequestThrottlerEntry> entry_; 187 scoped_refptr<MockURLRequestThrottlerEntry> entry_;
180 188
181 scoped_ptr<base::HistogramRecorder> histogram_recorder_; 189 std::map<std::string, HistogramSamples*> original_samples_;
190 std::map<std::string, HistogramSamples*> samples_;
182 191
183 TestURLRequestContext context_; 192 TestURLRequestContext context_;
184 TestURLRequest request_; 193 TestURLRequest request_;
185 }; 194 };
186 195
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
187 void URLRequestThrottlerEntryTest::SetUp() { 204 void URLRequestThrottlerEntryTest::SetUp() {
188 request_.set_load_flags(0); 205 request_.set_load_flags(0);
189 206
190 now_ = TimeTicks::Now(); 207 now_ = TimeTicks::Now();
191 entry_ = new MockURLRequestThrottlerEntry(&manager_); 208 entry_ = new MockURLRequestThrottlerEntry(&manager_);
192 entry_->ResetToBlank(now_); 209 entry_->ResetToBlank(now_);
193 210
194 histogram_recorder_.reset(new base::HistogramRecorder()); 211 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) {
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();
195 } 248 }
196 249
197 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { 250 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) {
198 return out << time.ToInternalValue(); 251 return out << time.ToInternalValue();
199 } 252 }
200 253
201 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { 254 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) {
202 entry_->set_exponential_backoff_release_time( 255 entry_->set_exponential_backoff_release_time(
203 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); 256 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1));
204 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); 257 EXPECT_TRUE(entry_->ShouldRejectRequest(request_));
205 258
206 // Also end-to-end test the load flags exceptions. 259 // Also end-to-end test the load flags exceptions.
207 request_.set_load_flags(LOAD_MAYBE_USER_GESTURE); 260 request_.set_load_flags(LOAD_MAYBE_USER_GESTURE);
208 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); 261 EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
209 262
210 scoped_ptr<base::HistogramSamples> samples( 263 CalculateHistogramDeltas();
211 histogram_recorder_->GetHistogramSamplesSinceCreation( 264 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0));
212 kRequestThrottledHistogramName)); 265 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1));
213 ASSERT_EQ(1, samples->GetCount(0));
214 ASSERT_EQ(1, samples->GetCount(1));
215 } 266 }
216 267
217 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { 268 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) {
218 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); 269 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_);
219 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); 270 EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
220 entry_->set_exponential_backoff_release_time( 271 entry_->set_exponential_backoff_release_time(
221 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); 272 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1));
222 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); 273 EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
223 274
224 scoped_ptr<base::HistogramSamples> samples( 275 CalculateHistogramDeltas();
225 histogram_recorder_->GetHistogramSamplesSinceCreation( 276 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0));
226 kRequestThrottledHistogramName)); 277 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1));
227 ASSERT_EQ(2, samples->GetCount(0));
228 ASSERT_EQ(0, samples->GetCount(1));
229 } 278 }
230 279
231 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { 280 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) {
232 MockURLRequestThrottlerHeaderAdapter failure_response(503); 281 MockURLRequestThrottlerHeaderAdapter failure_response(503);
233 entry_->UpdateWithResponse(std::string(), &failure_response); 282 entry_->UpdateWithResponse(std::string(), &failure_response);
234 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) 283 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_)
235 << "A failure should increase the release_time"; 284 << "A failure should increase the release_time";
236 } 285 }
237 286
238 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { 287 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) {
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 FAIL(); 557 FAIL();
509 } 558 }
510 559
511 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = 560 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after =
512 manager.RegisterRequestUrl(GURL("http://www.example.com/")); 561 manager.RegisterRequestUrl(GURL("http://www.example.com/"));
513 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); 562 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_));
514 } 563 }
515 } 564 }
516 565
517 } // namespace net 566 } // namespace net
OLDNEW
« no previous file with comments | « trunk/src/chrome/browser/ui/cocoa/browser/password_generation_bubble_controller_unittest.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698