summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgroby@chromium.org <groby@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-20 21:14:21 +0000
committergroby@chromium.org <groby@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-20 21:14:21 +0000
commit6a7dfb3cd638829a04e002ea9bc5b18ed3c9c03f (patch)
tree51028572b1a54200e108c954c74d4a8fc98558d6
parentb639a833a5f3533509a85bb45c407297b6aa82d2 (diff)
downloadchromium_src-6a7dfb3cd638829a04e002ea9bc5b18ed3c9c03f.zip
chromium_src-6a7dfb3cd638829a04e002ea9bc5b18ed3c9c03f.tar.gz
chromium_src-6a7dfb3cd638829a04e002ea9bc5b18ed3c9c03f.tar.bz2
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 b957026f43ef9464a8cdd5a240867c807b2281da. > > 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
-rw-r--r--base/base.gyp3
-rw-r--r--base/metrics/statistics_recorder.h6
-rw-r--r--base/test/histogram_recorder.cc52
-rw-r--r--base/test/histogram_recorder.h44
-rw-r--r--base/test/histogram_recorder_unittest.cc41
-rw-r--r--chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc104
-rw-r--r--chrome/browser/ui/cocoa/browser/password_generation_bubble_controller_unittest.mm35
-rw-r--r--net/url_request/url_request_throttler_unittest.cc82
8 files changed, 162 insertions, 205 deletions
diff --git a/base/base.gyp b/base/base.gyp
index 1bd8e53..1c2e931 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -640,7 +640,6 @@
'template_util_unittest.cc',
'test/expectations/expectation_unittest.cc',
'test/expectations/parser_unittest.cc',
- 'test/histogram_recorder_unittest.cc',
'test/test_reg_util_win_unittest.cc',
'test/trace_event_analyzer_unittest.cc',
'threading/non_thread_safe_unittest.cc',
@@ -907,8 +906,6 @@
'test/expectations/parser.h',
'test/gtest_xml_util.cc',
'test/gtest_xml_util.h',
- 'test/histogram_recorder.cc',
- 'test/histogram_recorder.h',
'test/launcher/test_launcher.cc',
'test/launcher/test_launcher.h',
'test/launcher/test_result.cc',
diff --git a/base/metrics/statistics_recorder.h b/base/metrics/statistics_recorder.h
index 9cf1de7..0716e80 100644
--- a/base/metrics/statistics_recorder.h
+++ b/base/metrics/statistics_recorder.h
@@ -70,9 +70,9 @@ class BASE_EXPORT StatisticsRecorder {
static HistogramBase* FindHistogram(const std::string& name);
// GetSnapshot copies some of the pointers to registered histograms into the
- // caller supplied vector (Histograms). Only histograms which have |query| as
- // a substring are copied (an empty string will process all registered
- // histograms).
+ // caller supplied vector (Histograms). Only histograms with names matching
+ // query are returned. The query must be a substring of histogram name for its
+ // pointer to be copied.
static void GetSnapshot(const std::string& query, Histograms* snapshot);
private:
diff --git a/base/test/histogram_recorder.cc b/base/test/histogram_recorder.cc
deleted file mode 100644
index ee852f7..0000000
--- a/base/test/histogram_recorder.cc
+++ /dev/null
@@ -1,52 +0,0 @@
-// 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
diff --git a/base/test/histogram_recorder.h b/base/test/histogram_recorder.h
deleted file mode 100644
index f867b1e..0000000
--- a/base/test/histogram_recorder.h
+++ /dev/null
@@ -1,44 +0,0 @@
-// 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_
diff --git a/base/test/histogram_recorder_unittest.cc b/base/test/histogram_recorder_unittest.cc
deleted file mode 100644
index f4cdf49..0000000
--- a/base/test/histogram_recorder_unittest.cc
+++ /dev/null
@@ -1,41 +0,0 @@
-// 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
diff --git a/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc b/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc
index f702723..ab7348a 100644
--- a/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc
+++ b/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc
@@ -7,9 +7,10 @@
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
+#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h"
+#include "base/metrics/statistics_recorder.h"
#include "base/strings/utf_string_conversions.h"
-#include "base/test/histogram_recorder.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_WIN)
@@ -17,51 +18,55 @@
#include "base/win/windows_version.h"
#endif
+using base::HistogramBase;
+using base::HistogramSamples;
+using base::StatisticsRecorder;
+
class SpellcheckHostMetricsTest : public testing::Test {
public:
SpellcheckHostMetricsTest() : loop_(base::MessageLoop::TYPE_DEFAULT) {
}
- static void SetUpTestCase() {
- base::HistogramRecorder::Initialize();
- }
-
virtual void SetUp() OVERRIDE {
- ResetHistogramRecorder();
+ base::StatisticsRecorder::Initialize();
metrics_.reset(new SpellCheckHostMetrics);
}
- void ResetHistogramRecorder() {
- histogram_recorder_.reset(new base::HistogramRecorder());
- }
-
SpellCheckHostMetrics* metrics() { return metrics_.get(); }
void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); }
- protected:
- scoped_ptr<base::HistogramRecorder> histogram_recorder_;
-
private:
base::MessageLoop loop_;
scoped_ptr<SpellCheckHostMetrics> metrics_;
};
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);
- scoped_ptr<base::HistogramSamples> samples(
- histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName));
+ histogram =
+ 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(0, samples->GetCount(1));
- ResetHistogramRecorder();
+ baseline.reset(samples.release());
metrics()->RecordEnabledStats(true);
- samples =
- histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName);
+ histogram =
+ StatisticsRecorder::FindHistogram("SpellCheck.Enabled");
+ ASSERT_TRUE(histogram != NULL);
+ samples = histogram->SnapshotSamples();
+ samples->Subtract(*baseline);
EXPECT_EQ(0, samples->GetCount(0));
EXPECT_EQ(1, samples->GetCount(1));
}
@@ -76,16 +81,21 @@ TEST_F(SpellcheckHostMetricsTest, CustomWordStats) {
// Determine if test failures are due the statistics recorder not being
// 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);
+ histogram =
+ StatisticsRecorder::FindHistogram("SpellCheck.CustomWords");
+ ASSERT_TRUE(histogram != NULL);
+ scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
- scoped_ptr<base::HistogramSamples> samples(
- histogram_recorder_->GetHistogramSamplesSinceCreation(
- "SpellCheck.CustomWords"));
- EXPECT_EQ(23, samples->sum());
+ samples->Subtract(*baseline);
+ EXPECT_EQ(23,samples->sum());
}
TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) {
@@ -103,37 +113,59 @@ TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) {
metrics()->RecordCheckedWordStats(ASCIIToUTF16("test"), false);
RecordWordCountsForTesting();
- // Restart the recorder.
- ResetHistogramRecorder();
+ // Get baselines for all affected histograms.
+ 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.
RecordWordCountsForTesting();
// 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) {
- samples = histogram_recorder_->GetHistogramSamplesSinceCreation(
- histogramName[i]);
- EXPECT_EQ(0, samples->TotalCount());
+ HistogramBase* histogram =
+ StatisticsRecorder::FindHistogram(histogramName[i]);
+ 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) {
const char kMetricName[] = "SpellCheck.SpellingService.Enabled";
+ scoped_ptr<HistogramSamples> baseline;
+ HistogramBase* histogram = StatisticsRecorder::FindHistogram(kMetricName);
+ if (histogram)
+ baseline = histogram->SnapshotSamples();
metrics()->RecordSpellingServiceStats(false);
- scoped_ptr<base::HistogramSamples> samples(
- histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName));
+ histogram =
+ 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(0, samples->GetCount(1));
- ResetHistogramRecorder();
+ baseline.reset(samples.release());
metrics()->RecordSpellingServiceStats(true);
- samples =
- histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName);
+ histogram =
+ StatisticsRecorder::FindHistogram(kMetricName);
+ ASSERT_TRUE(histogram != NULL);
+ samples = histogram->SnapshotSamples();
+ samples->Subtract(*baseline);
EXPECT_EQ(0, samples->GetCount(0));
EXPECT_EQ(1, samples->GetCount(1));
}
diff --git a/chrome/browser/ui/cocoa/browser/password_generation_bubble_controller_unittest.mm b/chrome/browser/ui/cocoa/browser/password_generation_bubble_controller_unittest.mm
index b9af977..c3211714 100644
--- a/chrome/browser/ui/cocoa/browser/password_generation_bubble_controller_unittest.mm
+++ b/chrome/browser/ui/cocoa/browser/password_generation_bubble_controller_unittest.mm
@@ -5,14 +5,19 @@
#import "chrome/browser/ui/cocoa/browser/password_generation_bubble_controller.h"
#include "base/logging.h"
+#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h"
+#include "base/metrics/statistics_recorder.h"
#include "base/strings/sys_string_conversions.h"
-#include "base/test/histogram_recorder.h"
#include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#include "components/autofill/core/browser/password_generator.h"
#include "components/autofill/core/common/password_form.h"
#include "testing/gtest_mac.h"
+using base::HistogramBase;
+using base::HistogramSamples;
+using base::StatisticsRecorder;
+
const char kHistogramName[] = "PasswordGeneration.UserActions";
class PasswordGenerationBubbleControllerTest : public CocoaProfileTest {
@@ -21,7 +26,7 @@ class PasswordGenerationBubbleControllerTest : public CocoaProfileTest {
: controller_(nil) {}
static void SetUpTestCase() {
- base::HistogramRecorder::Initialize();
+ StatisticsRecorder::Initialize();
}
virtual void SetUp() {
@@ -29,7 +34,10 @@ class PasswordGenerationBubbleControllerTest : public CocoaProfileTest {
generator_.reset(new autofill::PasswordGenerator(20));
- histogram_recorder_.reset(new base::HistogramRecorder());
+ HistogramBase* histogram =
+ StatisticsRecorder::FindHistogram(kHistogramName);
+ if (histogram)
+ original_ = histogram->SnapshotSamples();
SetUpController();
}
@@ -57,16 +65,26 @@ class PasswordGenerationBubbleControllerTest : public CocoaProfileTest {
controller_ = nil;
}
- scoped_ptr<base::HistogramSamples> GetHistogramSamples() {
- return histogram_recorder_->GetHistogramSamplesSinceCreation(
- kHistogramName).Pass();
+ HistogramSamples* GetHistogramSamples() {
+ HistogramBase* histogram =
+ StatisticsRecorder::FindHistogram(kHistogramName);
+ if (histogram) {
+ current_ = histogram->SnapshotSamples();
+ if (original_.get())
+ current_->Subtract(*original_.get());
+ }
+ return current_.get();
}
protected:
// Weak.
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_;
};
@@ -94,7 +112,7 @@ TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) {
// Do nothing.
CloseController();
- scoped_ptr<base::HistogramSamples> samples(GetHistogramSamples());
+ HistogramSamples* samples = GetHistogramSamples();
EXPECT_EQ(
1,
samples->GetCount(autofill::password_generation::IGNORE_FEATURE));
@@ -142,4 +160,5 @@ TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) {
1,
samples->GetCount(
autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD));
+
}
diff --git a/net/url_request/url_request_throttler_unittest.cc b/net/url_request/url_request_throttler_unittest.cc
index 52b3a0a..b964b10 100644
--- a/net/url_request/url_request_throttler_unittest.cc
+++ b/net/url_request/url_request_throttler_unittest.cc
@@ -5,12 +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/histogram_recorder.h"
#include "base/time/time.h"
#include "net/base/load_flags.h"
#include "net/base/request_priority.h"
@@ -28,7 +29,10 @@ namespace net {
namespace {
-const char kRequestThrottledHistogramName[] = "Throttling.RequestThrottled";
+using base::Histogram;
+using base::HistogramBase;
+using base::HistogramSamples;
+using base::StatisticsRecorder;
class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry {
public:
@@ -172,22 +176,32 @@ class URLRequestThrottlerEntryTest : public testing::Test {
URLRequestThrottlerEntryTest()
: request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {}
- static void SetUpTestCase() {
- base::HistogramRecorder::Initialize();
- }
-
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_;
MockURLRequestThrottlerManager manager_; // Dummy object, not used.
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_;
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);
@@ -195,7 +209,43 @@ void URLRequestThrottlerEntryTest::SetUp() {
entry_ = new MockURLRequestThrottlerEntry(&manager_);
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) {
@@ -223,11 +273,9 @@ TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) {
request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE);
EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
- scoped_ptr<base::HistogramSamples> samples(
- histogram_recorder_->GetHistogramSamplesSinceCreation(
- kRequestThrottledHistogramName));
- ASSERT_EQ(1, samples->GetCount(0));
- ASSERT_EQ(1, samples->GetCount(1));
+ CalculateHistogramDeltas();
+ ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0));
+ ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1));
}
TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) {
@@ -237,11 +285,9 @@ TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) {
entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1));
EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
- scoped_ptr<base::HistogramSamples> samples(
- histogram_recorder_->GetHistogramSamplesSinceCreation(
- kRequestThrottledHistogramName));
- ASSERT_EQ(2, samples->GetCount(0));
- ASSERT_EQ(0, samples->GetCount(1));
+ CalculateHistogramDeltas();
+ ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0));
+ ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1));
}
TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) {