diff options
author | lpromero@chromium.org <lpromero@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-18 18:50:49 +0000 |
---|---|---|
committer | lpromero@chromium.org <lpromero@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-18 18:50:49 +0000 |
commit | 3a0e3d498cdd0be4659e03dd05888b4ae15f4993 (patch) | |
tree | eadf56c3b27d928faf3e6f98373f6eb67bafe739 /base | |
parent | ead7fb005dfc43c7c82aeec569377cac227c0cdb (diff) | |
download | chromium_src-3a0e3d498cdd0be4659e03dd05888b4ae15f4993.zip chromium_src-3a0e3d498cdd0be4659e03dd05888b4ae15f4993.tar.gz chromium_src-3a0e3d498cdd0be4659e03dd05888b4ae15f4993.tar.bz2 |
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.
BUG=232414
Review URL: https://chromiumcodereview.appspot.com/18337014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@212378 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 3 | ||||
-rw-r--r-- | base/metrics/statistics_recorder.h | 6 | ||||
-rw-r--r-- | base/test/histogram_recorder.cc | 42 | ||||
-rw-r--r-- | base/test/histogram_recorder.h | 35 | ||||
-rw-r--r-- | base/test/histogram_recorder_unittest.cc | 34 |
5 files changed, 117 insertions, 3 deletions
diff --git a/base/base.gyp b/base/base.gyp index e4db68d..c177606 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -596,6 +596,7 @@ 'template_util_unittest.cc', 'test/expectations/expectation_unittest.cc', 'test/expectations/parser_unittest.cc', + 'test/histogram_recorder_unittest.cc', 'test/trace_event_analyzer_unittest.cc', 'threading/non_thread_safe_unittest.cc', 'threading/platform_thread_unittest.cc', @@ -863,6 +864,8 @@ 'test/expectations/expectation.h', 'test/expectations/parser.cc', 'test/expectations/parser.h', + 'test/histogram_recorder.cc', + 'test/histogram_recorder.h', 'test/mock_chrome_application_mac.h', 'test/mock_chrome_application_mac.mm', 'test/mock_devices_changed_observer.cc', diff --git a/base/metrics/statistics_recorder.h b/base/metrics/statistics_recorder.h index 9a55225..744be01 100644 --- a/base/metrics/statistics_recorder.h +++ b/base/metrics/statistics_recorder.h @@ -66,9 +66,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 with names matching - // query are returned. The query must be a substring of histogram name for its - // pointer to be copied. + // caller supplied vector (Histograms). Only histograms which have query as + // a substring are copied (an empty string will process all registered + // histograms). static void GetSnapshot(const std::string& query, Histograms* snapshot); private: diff --git a/base/test/histogram_recorder.cc b/base/test/histogram_recorder.cc new file mode 100644 index 0000000..500f16c --- /dev/null +++ b/base/test/histogram_recorder.cc @@ -0,0 +1,42 @@ +// 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 { + +HistogramRecorder::HistogramRecorder() { + // Record any histogram data that exists when the object is created so it can + // be subtracted later. + StatisticsRecorder::Initialize(); + 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_); +} + +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..85139bd --- /dev/null +++ b/base/test/histogram_recorder.h @@ -0,0 +1,35 @@ +// 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/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 sent as they should be. +class HistogramRecorder { + public: + HistogramRecorder(); + virtual ~HistogramRecorder(); + + // 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. + std::map<std::string, HistogramSamples*> original_samples_; +}; + +} // 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..a69c6b0 --- /dev/null +++ b/base/test/histogram_recorder_unittest.cc @@ -0,0 +1,34 @@ +// 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 {}; + +TEST_F(HistogramRecorderTest, Scope) { + // Send a histogram before the creation of the recorder. + UMA_HISTOGRAM_BOOLEAN("Test", true); + + HistogramRecorder recorder; + + // Verify that no histogram is recorded. + EXPECT_FALSE(recorder.GetHistogramSamplesSinceCreation("Test")); + + // Send a histogram after the creation of the recorder. + UMA_HISTOGRAM_BOOLEAN("Test", true); + + // Verify that one histogram is recorded. + scoped_ptr<HistogramSamples> samples( + recorder.GetHistogramSamplesSinceCreation("Test")); + EXPECT_TRUE(samples); + EXPECT_EQ(1, samples->TotalCount()); +} + +} // namespace base |