diff options
author | lpromero@chromium.org <lpromero@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-20 17:46:47 +0000 |
---|---|---|
committer | lpromero@chromium.org <lpromero@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-20 17:46:47 +0000 |
commit | bbc2e4c6ab8afb95f5e63b7a02f8ef60c71dec9b (patch) | |
tree | 228ab5edfa0163c9c80daf33daa226b3d9fa6299 /base/test | |
parent | 473bdc43543599e19a749a530b839fac5f865e0c (diff) | |
download | chromium_src-bbc2e4c6ab8afb95f5e63b7a02f8ef60c71dec9b.zip chromium_src-bbc2e4c6ab8afb95f5e63b7a02f8ef60c71dec9b.tar.gz chromium_src-bbc2e4c6ab8afb95f5e63b7a02f8ef60c71dec9b.tar.bz2 |
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
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@242121 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/test')
-rw-r--r-- | base/test/histogram_recorder.cc | 52 | ||||
-rw-r--r-- | base/test/histogram_recorder.h | 44 | ||||
-rw-r--r-- | base/test/histogram_recorder_unittest.cc | 41 |
3 files changed, 137 insertions, 0 deletions
diff --git a/base/test/histogram_recorder.cc b/base/test/histogram_recorder.cc new file mode 100644 index 0000000..ee852f7 --- /dev/null +++ b/base/test/histogram_recorder.cc @@ -0,0 +1,52 @@ +// 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 new file mode 100644 index 0000000..f867b1e --- /dev/null +++ b/base/test/histogram_recorder.h @@ -0,0 +1,44 @@ +// 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 new file mode 100644 index 0000000..f4cdf49 --- /dev/null +++ b/base/test/histogram_recorder_unittest.cc @@ -0,0 +1,41 @@ +// 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 |