Commit 6a7dfb3c authored by groby@chromium.org's avatar groby@chromium.org

Revert 242121 "Reland "Add a HistogramRecorder class and use cas..."

As per request from lpromero, reverting since it might be failing a
test - see

http://build.chromium.org/p/chromium.memory.fyi/builders/Windows%20Tests%20%28tsan%29/builds/17153/steps/memory%20test%3A%20base_unittests/logs/stdio

[ RUN      ] HistogramTest.BasicTest
[3824:2780:1220/111332:4747750:FATAL:statistics_recorder.cc(270)] Check failed: !histograms_.

TBR=lpromero@chromium.org

> Reland "Add a HistogramRecorder class and use cases."
> 
> This CL adds a utility class to streamline the task of testing that metrics have been updated as expected. Specifically, the HistogramRecorder class allows a client to obtain the differential value of a given histogram in the interval since the given HistogramRecorder instance was created.
> 
> This reverts commit b957026f.
> 
> BUG=232414
> TBR=rsleevi, mark, groby, thakis
> 
> Review URL: https://codereview.chromium.org/19866004

TBR=lpromero@chromium.org

Review URL: https://codereview.chromium.org/120273002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@242176 0039d316-1c4b-4281-b951-d872f2087c98
parent b639a833
...@@ -640,7 +640,6 @@ ...@@ -640,7 +640,6 @@
'template_util_unittest.cc', 'template_util_unittest.cc',
'test/expectations/expectation_unittest.cc', 'test/expectations/expectation_unittest.cc',
'test/expectations/parser_unittest.cc', 'test/expectations/parser_unittest.cc',
'test/histogram_recorder_unittest.cc',
'test/test_reg_util_win_unittest.cc', 'test/test_reg_util_win_unittest.cc',
'test/trace_event_analyzer_unittest.cc', 'test/trace_event_analyzer_unittest.cc',
'threading/non_thread_safe_unittest.cc', 'threading/non_thread_safe_unittest.cc',
...@@ -907,8 +906,6 @@ ...@@ -907,8 +906,6 @@
'test/expectations/parser.h', 'test/expectations/parser.h',
'test/gtest_xml_util.cc', 'test/gtest_xml_util.cc',
'test/gtest_xml_util.h', 'test/gtest_xml_util.h',
'test/histogram_recorder.cc',
'test/histogram_recorder.h',
'test/launcher/test_launcher.cc', 'test/launcher/test_launcher.cc',
'test/launcher/test_launcher.h', 'test/launcher/test_launcher.h',
'test/launcher/test_result.cc', 'test/launcher/test_result.cc',
......
...@@ -70,9 +70,9 @@ class BASE_EXPORT StatisticsRecorder { ...@@ -70,9 +70,9 @@ class BASE_EXPORT StatisticsRecorder {
static HistogramBase* FindHistogram(const std::string& name); static HistogramBase* FindHistogram(const std::string& name);
// GetSnapshot copies some of the pointers to registered histograms into the // GetSnapshot copies some of the pointers to registered histograms into the
// caller supplied vector (Histograms). Only histograms which have |query| as // caller supplied vector (Histograms). Only histograms with names matching
// a substring are copied (an empty string will process all registered // query are returned. The query must be a substring of histogram name for its
// histograms). // pointer to be copied.
static void GetSnapshot(const std::string& query, Histograms* snapshot); static void GetSnapshot(const std::string& query, Histograms* snapshot);
private: private:
......
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/test/histogram_recorder.h"
#include "base/metrics/histogram.h"
#include "base/metrics/statistics_recorder.h"
#include "base/stl_util.h"
namespace base {
// static
void HistogramRecorder::Initialize() {
// Ensure that StatisticsRecorder is initialized.
StatisticsRecorder::Initialize();
}
HistogramRecorder::HistogramRecorder() {
// Record any histogram data that exists when the object is created so it can
// be subtracted later.
StatisticsRecorder::Histograms histograms;
StatisticsRecorder::GetSnapshot("", &histograms);
for (size_t i = 0; i < histograms.size(); i++) {
original_samples_[histograms[i]->histogram_name()] =
histograms[i]->SnapshotSamples().release();
}
}
HistogramRecorder::~HistogramRecorder() {
STLDeleteValues(&original_samples_);
}
// static
bool HistogramRecorder::IsActive() {
return StatisticsRecorder::IsActive();
}
scoped_ptr<HistogramSamples>
HistogramRecorder::GetHistogramSamplesSinceCreation(
const std::string& histogram_name) {
HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name);
if (!histogram)
return scoped_ptr<HistogramSamples>();
scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples());
HistogramSamples* named_original_samples = original_samples_[histogram_name];
if (named_original_samples)
named_samples->Subtract(*named_original_samples);
return named_samples.Pass();
}
} // namespace base
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_TEST_HISTOGRAM_RECORDER_H_
#define BASE_TEST_HISTOGRAM_RECORDER_H_
#include <map>
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram_samples.h"
namespace base {
// This class acts as a differential reader for histogram samples, enabling
// tests to check that metrics were recorded as they should be.
class HistogramRecorder {
public:
// Initializes the HistogramRecorder system.
static void Initialize();
HistogramRecorder();
virtual ~HistogramRecorder();
// Returns whether the HistogramRecorder has been initialized.
static bool IsActive();
// Returns the histogram data accumulated since this instance was created.
// Returns NULL if no samples are available.
scoped_ptr<HistogramSamples> GetHistogramSamplesSinceCreation(
const std::string& histogram_name);
private:
// Used to determine the histogram changes made during this instance's
// lifecycle. This isntance takes ownership of the samples, which are deleted
// when the instance is destroyed.
std::map<std::string, HistogramSamples*> original_samples_;
DISALLOW_COPY_AND_ASSIGN(HistogramRecorder);
};
} // namespace base
#endif // BASE_TEST_HISTOGRAM_RECORDER_H_
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h"
#include "base/test/histogram_recorder.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
class HistogramRecorderTest : public testing::Test {
public:
static void SetUpTestCase() {
HistogramRecorder::Initialize();
}
};
TEST_F(HistogramRecorderTest, Scope) {
// Record a histogram before the creation of the recorder.
UMA_HISTOGRAM_BOOLEAN("Test", true);
HistogramRecorder recorder;
// Verify that no histogram is recorded.
scoped_ptr<HistogramSamples> samples(
recorder.GetHistogramSamplesSinceCreation("Test"));
EXPECT_TRUE(samples);
EXPECT_EQ(0, samples->TotalCount());
// Record a histogram after the creation of the recorder.
UMA_HISTOGRAM_BOOLEAN("Test", true);
// Verify that one histogram is recorded.
samples = recorder.GetHistogramSamplesSinceCreation("Test");
EXPECT_TRUE(samples);
EXPECT_EQ(1, samples->TotalCount());
}
} // namespace base
...@@ -7,9 +7,10 @@ ...@@ -7,9 +7,10 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h" #include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/test/histogram_recorder.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_WIN) #if defined(OS_WIN)
...@@ -17,51 +18,55 @@ ...@@ -17,51 +18,55 @@
#include "base/win/windows_version.h" #include "base/win/windows_version.h"
#endif #endif
using base::HistogramBase;
using base::HistogramSamples;
using base::StatisticsRecorder;
class SpellcheckHostMetricsTest : public testing::Test { class SpellcheckHostMetricsTest : public testing::Test {
public: public:
SpellcheckHostMetricsTest() : loop_(base::MessageLoop::TYPE_DEFAULT) { SpellcheckHostMetricsTest() : loop_(base::MessageLoop::TYPE_DEFAULT) {
} }
static void SetUpTestCase() {
base::HistogramRecorder::Initialize();
}
virtual void SetUp() OVERRIDE { virtual void SetUp() OVERRIDE {
ResetHistogramRecorder(); base::StatisticsRecorder::Initialize();
metrics_.reset(new SpellCheckHostMetrics); metrics_.reset(new SpellCheckHostMetrics);
} }
void ResetHistogramRecorder() {
histogram_recorder_.reset(new base::HistogramRecorder());
}
SpellCheckHostMetrics* metrics() { return metrics_.get(); } SpellCheckHostMetrics* metrics() { return metrics_.get(); }
void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); } void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); }
protected:
scoped_ptr<base::HistogramRecorder> histogram_recorder_;
private: private:
base::MessageLoop loop_; base::MessageLoop loop_;
scoped_ptr<SpellCheckHostMetrics> metrics_; scoped_ptr<SpellCheckHostMetrics> metrics_;
}; };
TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) { TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) {
const char kMetricName[] = "SpellCheck.Enabled"; scoped_ptr<HistogramSamples> baseline;
HistogramBase* histogram =
StatisticsRecorder::FindHistogram("SpellCheck.Enabled");
if (histogram)
baseline = histogram->SnapshotSamples();
metrics()->RecordEnabledStats(false); metrics()->RecordEnabledStats(false);
scoped_ptr<base::HistogramSamples> samples( histogram =
histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName)); StatisticsRecorder::FindHistogram("SpellCheck.Enabled");
ASSERT_TRUE(histogram != NULL);
scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
if (baseline.get())
samples->Subtract(*baseline);
EXPECT_EQ(1, samples->GetCount(0)); EXPECT_EQ(1, samples->GetCount(0));
EXPECT_EQ(0, samples->GetCount(1)); EXPECT_EQ(0, samples->GetCount(1));
ResetHistogramRecorder(); baseline.reset(samples.release());
metrics()->RecordEnabledStats(true); metrics()->RecordEnabledStats(true);
samples = histogram =
histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName); StatisticsRecorder::FindHistogram("SpellCheck.Enabled");
ASSERT_TRUE(histogram != NULL);
samples = histogram->SnapshotSamples();
samples->Subtract(*baseline);
EXPECT_EQ(0, samples->GetCount(0)); EXPECT_EQ(0, samples->GetCount(0));
EXPECT_EQ(1, samples->GetCount(1)); EXPECT_EQ(1, samples->GetCount(1));
} }
...@@ -76,16 +81,21 @@ TEST_F(SpellcheckHostMetricsTest, CustomWordStats) { ...@@ -76,16 +81,21 @@ TEST_F(SpellcheckHostMetricsTest, CustomWordStats) {
// Determine if test failures are due the statistics recorder not being // Determine if test failures are due the statistics recorder not being
// available or because the histogram just isn't there: crbug.com/230534. // available or because the histogram just isn't there: crbug.com/230534.
EXPECT_TRUE(base::HistogramRecorder::IsActive()); EXPECT_TRUE(StatisticsRecorder::IsActive());
ResetHistogramRecorder(); HistogramBase* histogram =
StatisticsRecorder::FindHistogram("SpellCheck.CustomWords");
ASSERT_TRUE(histogram != NULL);
scoped_ptr<HistogramSamples> baseline = histogram->SnapshotSamples();
SpellCheckHostMetrics::RecordCustomWordCountStats(23); SpellCheckHostMetrics::RecordCustomWordCountStats(23);
histogram =
StatisticsRecorder::FindHistogram("SpellCheck.CustomWords");
ASSERT_TRUE(histogram != NULL);
scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
scoped_ptr<base::HistogramSamples> samples( samples->Subtract(*baseline);
histogram_recorder_->GetHistogramSamplesSinceCreation( EXPECT_EQ(23,samples->sum());
"SpellCheck.CustomWords"));
EXPECT_EQ(23, samples->sum());
} }
TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) {
...@@ -103,37 +113,59 @@ TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { ...@@ -103,37 +113,59 @@ TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) {
metrics()->RecordCheckedWordStats(ASCIIToUTF16("test"), false); metrics()->RecordCheckedWordStats(ASCIIToUTF16("test"), false);
RecordWordCountsForTesting(); RecordWordCountsForTesting();
// Restart the recorder. // Get baselines for all affected histograms.
ResetHistogramRecorder(); scoped_ptr<HistogramSamples> baselines[arraysize(histogramName)];
for (size_t i = 0; i < arraysize(histogramName); ++i) {
HistogramBase* histogram =
StatisticsRecorder::FindHistogram(histogramName[i]);
if (histogram)
baselines[i] = histogram->SnapshotSamples();
}
// Nothing changed, so this invocation should not affect any histograms. // Nothing changed, so this invocation should not affect any histograms.
RecordWordCountsForTesting(); RecordWordCountsForTesting();
// Get samples for all affected histograms. // Get samples for all affected histograms.
scoped_ptr<base::HistogramSamples> samples; scoped_ptr<HistogramSamples> samples[arraysize(histogramName)];
for (size_t i = 0; i < arraysize(histogramName); ++i) { for (size_t i = 0; i < arraysize(histogramName); ++i) {
samples = histogram_recorder_->GetHistogramSamplesSinceCreation( HistogramBase* histogram =
histogramName[i]); StatisticsRecorder::FindHistogram(histogramName[i]);
EXPECT_EQ(0, samples->TotalCount()); ASSERT_TRUE(histogram != NULL);
samples[i] = histogram->SnapshotSamples();
if (baselines[i].get())
samples[i]->Subtract(*baselines[i]);
EXPECT_EQ(0, samples[i]->TotalCount());
} }
} }
TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) { TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) {
const char kMetricName[] = "SpellCheck.SpellingService.Enabled"; const char kMetricName[] = "SpellCheck.SpellingService.Enabled";
scoped_ptr<HistogramSamples> baseline;
HistogramBase* histogram = StatisticsRecorder::FindHistogram(kMetricName);
if (histogram)
baseline = histogram->SnapshotSamples();
metrics()->RecordSpellingServiceStats(false); metrics()->RecordSpellingServiceStats(false);
scoped_ptr<base::HistogramSamples> samples( histogram =
histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName)); StatisticsRecorder::FindHistogram(kMetricName);
ASSERT_TRUE(histogram != NULL);
scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
if (baseline.get())
samples->Subtract(*baseline);
EXPECT_EQ(1, samples->GetCount(0)); EXPECT_EQ(1, samples->GetCount(0));
EXPECT_EQ(0, samples->GetCount(1)); EXPECT_EQ(0, samples->GetCount(1));
ResetHistogramRecorder(); baseline.reset(samples.release());
metrics()->RecordSpellingServiceStats(true); metrics()->RecordSpellingServiceStats(true);
samples = histogram =
histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName); StatisticsRecorder::FindHistogram(kMetricName);
ASSERT_TRUE(histogram != NULL);
samples = histogram->SnapshotSamples();
samples->Subtract(*baseline);
EXPECT_EQ(0, samples->GetCount(0)); EXPECT_EQ(0, samples->GetCount(0));
EXPECT_EQ(1, samples->GetCount(1)); EXPECT_EQ(1, samples->GetCount(1));
} }
...@@ -5,14 +5,19 @@ ...@@ -5,14 +5,19 @@
#import "chrome/browser/ui/cocoa/browser/password_generation_bubble_controller.h" #import "chrome/browser/ui/cocoa/browser/password_generation_bubble_controller.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h" #include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/test/histogram_recorder.h"
#include "chrome/browser/ui/cocoa/cocoa_profile_test.h" #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#include "components/autofill/core/browser/password_generator.h" #include "components/autofill/core/browser/password_generator.h"
#include "components/autofill/core/common/password_form.h" #include "components/autofill/core/common/password_form.h"
#include "testing/gtest_mac.h" #include "testing/gtest_mac.h"
using base::HistogramBase;
using base::HistogramSamples;
using base::StatisticsRecorder;
const char kHistogramName[] = "PasswordGeneration.UserActions"; const char kHistogramName[] = "PasswordGeneration.UserActions";
class PasswordGenerationBubbleControllerTest : public CocoaProfileTest { class PasswordGenerationBubbleControllerTest : public CocoaProfileTest {
...@@ -21,7 +26,7 @@ class PasswordGenerationBubbleControllerTest : public CocoaProfileTest { ...@@ -21,7 +26,7 @@ class PasswordGenerationBubbleControllerTest : public CocoaProfileTest {
: controller_(nil) {} : controller_(nil) {}
static void SetUpTestCase() { static void SetUpTestCase() {
base::HistogramRecorder::Initialize(); StatisticsRecorder::Initialize();
} }
virtual void SetUp() { virtual void SetUp() {
...@@ -29,7 +34,10 @@ class PasswordGenerationBubbleControllerTest : public CocoaProfileTest { ...@@ -29,7 +34,10 @@ class PasswordGenerationBubbleControllerTest : public CocoaProfileTest {
generator_.reset(new autofill::PasswordGenerator(20)); generator_.reset(new autofill::PasswordGenerator(20));
histogram_recorder_.reset(new base::HistogramRecorder()); HistogramBase* histogram =
StatisticsRecorder::FindHistogram(kHistogramName);
if (histogram)
original_ = histogram->SnapshotSamples();
SetUpController(); SetUpController();
} }
...@@ -57,16 +65,26 @@ class PasswordGenerationBubbleControllerTest : public CocoaProfileTest { ...@@ -57,16 +65,26 @@ class PasswordGenerationBubbleControllerTest : public CocoaProfileTest {
controller_ = nil; controller_ = nil;
} }
scoped_ptr<base::HistogramSamples> GetHistogramSamples() { HistogramSamples* GetHistogramSamples() {
return histogram_recorder_->GetHistogramSamplesSinceCreation( HistogramBase* histogram =
kHistogramName).Pass(); StatisticsRecorder::FindHistogram(kHistogramName);
if (histogram) {
current_ = histogram->SnapshotSamples();
if (original_.get())
current_->Subtract(*original_.get());
}
return current_.get();
} }
protected: protected:
// Weak. // Weak.
PasswordGenerationBubbleController* controller_; PasswordGenerationBubbleController* controller_;
scoped_ptr<base::HistogramRecorder> histogram_recorder_; // Used to determine the histogram changes made just for this specific
// test run.
scoped_ptr<HistogramSamples> original_;
scoped_ptr<HistogramSamples> current_;
scoped_ptr<autofill::PasswordGenerator> generator_; scoped_ptr<autofill::PasswordGenerator> generator_;
}; };
...@@ -94,7 +112,7 @@ TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) { ...@@ -94,7 +112,7 @@ TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) {
// Do nothing. // Do nothing.
CloseController(); CloseController();
scoped_ptr<base::HistogramSamples> samples(GetHistogramSamples()); HistogramSamples* samples = GetHistogramSamples();
EXPECT_EQ( EXPECT_EQ(
1, 1,
samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); samples->GetCount(autofill::password_generation::IGNORE_FEATURE));
...@@ -142,4 +160,5 @@ TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) { ...@@ -142,4 +160,5 @@ TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) {
1, 1,
samples->GetCount( samples->GetCount(
autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD));
} }
...@@ -5,12 +5,13 @@ ...@@ -5,12 +5,13 @@
#include "net/url_request/url_request_throttler_manager.h" #include "net/url_request/url_request_throttler_manager.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h" #include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "base/pickle.h" #include "base/pickle.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/test/histogram_recorder.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
#include "net/base/request_priority.h" #include "net/base/request_priority.h"
...@@ -28,7 +29,10 @@ namespace net { ...@@ -28,7 +29,10 @@ namespace net {
namespace { namespace {
const char kRequestThrottledHistogramName[] = "Throttling.RequestThrottled"; using base::Histogram;
using base::HistogramBase;
using base::HistogramSamples;
using base::StatisticsRecorder;
class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry {
public: public:
...@@ -172,22 +176,32 @@ class URLRequestThrottlerEntryTest : public testing::Test { ...@@ -172,22 +176,32 @@ class URLRequestThrottlerEntryTest : public testing::Test {
URLRequestThrottlerEntryTest() URLRequestThrottlerEntryTest()
: request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {}
static void SetUpTestCase() {
base::HistogramRecorder::Initialize();
}
virtual void SetUp(); virtual void SetUp();
virtual void TearDown();
// After calling this function, histogram snapshots in |samples_| contain
// only the delta caused by the test case currently running.
void CalculateHistogramDeltas();
TimeTicks now_; TimeTicks now_;
MockURLRequestThrottlerManager manager_; // Dummy object, not used. MockURLRequestThrottlerManager manager_; // Dummy object, not used.
scoped_refptr<MockURLRequestThrottlerEntry> entry_; scoped_refptr<MockURLRequestThrottlerEntry> entry_;
scoped_ptr<base::HistogramRecorder> histogram_recorder_; std::map<std::string, HistogramSamples*> original_samples_;
std::map<std::string, HistogramSamples*> samples_;
TestURLRequestContext context_; TestURLRequestContext context_;
TestURLRequest request_; 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() { void URLRequestThrottlerEntryTest::SetUp() {
request_.SetLoadFlags(0); request_.SetLoadFlags(0);
...@@ -195,7 +209,43 @@ void URLRequestThrottlerEntryTest::SetUp() { ...@@ -195,7 +209,43 @@ void URLRequestThrottlerEntryTest::SetUp() {
entry_ = new MockURLRequestThrottlerEntry(&manager_); entry_ = new MockURLRequestThrottlerEntry(&manager_);
entry_->ResetToBlank(now_); entry_->ResetToBlank(now_);
histogram_recorder_.reset(new base::HistogramRecorder()); 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) { std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) {
...@@ -223,11 +273,9 @@ TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { ...@@ -223,11 +273,9 @@ TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) {
request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE);
EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
scoped_ptr<base::HistogramSamples> samples( CalculateHistogramDeltas();
histogram_recorder_->GetHistogramSamplesSinceCreation( ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0));
kRequestThrottledHistogramName)); ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1));
ASSERT_EQ(1, samples->GetCount(0));
ASSERT_EQ(1, samples->GetCount(1));
} }
TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) {
...@@ -237,11 +285,9 @@ TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { ...@@ -237,11 +285,9 @@ TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) {
entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1));
EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
scoped_ptr<base::HistogramSamples> samples( CalculateHistogramDeltas();
histogram_recorder_->GetHistogramSamplesSinceCreation( ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0));
kRequestThrottledHistogramName)); ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1));
ASSERT_EQ(2, samples->GetCount(0));
ASSERT_EQ(0, samples->GetCount(1));
} }
TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment