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

Unified 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: Address isherman's feedback (and bump copyright year). Created 6 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/ui/cocoa/browser/password_generation_bubble_controller_unittest.mm ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/url_request/url_request_throttler_unittest.cc
diff --git a/net/url_request/url_request_throttler_unittest.cc b/net/url_request/url_request_throttler_unittest.cc
index b964b10855101e1943d5240f1f0353911f31a8e4..ebc917ffb4c55d3e9b732691120210673f0582ef 100644
--- a/net/url_request/url_request_throttler_unittest.cc
+++ b/net/url_request/url_request_throttler_unittest.cc
@@ -5,13 +5,13 @@
#include "net/url_request/url_request_throttler_manager.h"
#include "base/memory/scoped_ptr.h"
-#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "base/pickle.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
+#include "base/test/statistics_delta_reader.h"
#include "base/time/time.h"
#include "net/base/load_flags.h"
#include "net/base/request_priority.h"
@@ -29,10 +29,7 @@ namespace net {
namespace {
-using base::Histogram;
-using base::HistogramBase;
-using base::HistogramSamples;
-using base::StatisticsRecorder;
+const char kRequestThrottledHistogramName[] = "Throttling.RequestThrottled";
class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry {
public:
@@ -176,76 +173,26 @@ class URLRequestThrottlerEntryTest : public testing::Test {
URLRequestThrottlerEntryTest()
: request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {}
- virtual void SetUp();
- virtual void TearDown();
+ static void SetUpTestCase() {
+ base::StatisticsRecorder::Initialize();
+ }
- // After calling this function, histogram snapshots in |samples_| contain
- // only the delta caused by the test case currently running.
- void CalculateHistogramDeltas();
+ virtual void SetUp();
TimeTicks now_;
MockURLRequestThrottlerManager manager_; // Dummy object, not used.
scoped_refptr<MockURLRequestThrottlerEntry> entry_;
- std::map<std::string, HistogramSamples*> original_samples_;
- std::map<std::string, HistogramSamples*> samples_;
-
TestURLRequestContext context_;
TestURLRequest request_;
};
-// List of all histograms we care about in these unit tests.
-const char* kHistogramNames[] = {
- "Throttling.FailureCountAtSuccess",
- "Throttling.PerceivedDowntime",
- "Throttling.RequestThrottled",
- "Throttling.SiteOptedOut",
-};
-
void URLRequestThrottlerEntryTest::SetUp() {
request_.SetLoadFlags(0);
now_ = TimeTicks::Now();
entry_ = new MockURLRequestThrottlerEntry(&manager_);
entry_->ResetToBlank(now_);
-
- for (size_t i = 0; i < arraysize(kHistogramNames); ++i) {
- // Must retrieve original samples for each histogram for comparison
- // as other tests may affect them.
- const char* name = kHistogramNames[i];
- HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
- if (histogram) {
- original_samples_[name] = histogram->SnapshotSamples().release();
- } else {
- original_samples_[name] = NULL;
- }
- }
-}
-
-void URLRequestThrottlerEntryTest::TearDown() {
- STLDeleteValues(&original_samples_);
- STLDeleteValues(&samples_);
-}
-
-void URLRequestThrottlerEntryTest::CalculateHistogramDeltas() {
- for (size_t i = 0; i < arraysize(kHistogramNames); ++i) {
- const char* name = kHistogramNames[i];
- HistogramSamples* original = original_samples_[name];
-
- HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
- if (histogram) {
- ASSERT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
-
- scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
- if (original)
- samples->Subtract(*original);
- samples_[name] = samples.release();
- }
- }
-
- // Ensure we don't accidentally use the originals in our tests.
- STLDeleteValues(&original_samples_);
- original_samples_.clear();
}
std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) {
@@ -265,6 +212,7 @@ TEST_F(URLRequestThrottlerEntryTest, CanThrottleRequest) {
}
TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) {
+ base::StatisticsDeltaReader statistics_delta_reader;
entry_->set_exponential_backoff_release_time(
entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1));
EXPECT_TRUE(entry_->ShouldRejectRequest(request_));
@@ -273,21 +221,26 @@ TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) {
request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE);
EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
- CalculateHistogramDeltas();
- ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0));
- ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1));
+ scoped_ptr<base::HistogramSamples> samples(
+ statistics_delta_reader.GetHistogramSamplesSinceCreation(
+ kRequestThrottledHistogramName));
+ ASSERT_EQ(1, samples->GetCount(0));
+ ASSERT_EQ(1, samples->GetCount(1));
}
TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) {
+ base::StatisticsDeltaReader statistics_delta_reader;
entry_->set_exponential_backoff_release_time(entry_->fake_time_now_);
EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
entry_->set_exponential_backoff_release_time(
entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1));
EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
- CalculateHistogramDeltas();
- ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0));
- ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1));
+ scoped_ptr<base::HistogramSamples> samples(
+ statistics_delta_reader.GetHistogramSamplesSinceCreation(
+ kRequestThrottledHistogramName));
+ ASSERT_EQ(2, samples->GetCount(0));
+ ASSERT_EQ(0, samples->GetCount(1));
}
TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) {
« no previous file with comments | « 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