diff options
author | lpromero@chromium.org <lpromero@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-07 16:12:09 +0000 |
---|---|---|
committer | lpromero@chromium.org <lpromero@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-07 16:12:09 +0000 |
commit | 16c7abc10a5585ee9fa0efdeb0723710fed93728 (patch) | |
tree | f4486075af79ef0cfd1d3857749f5a999440ff8c /base | |
parent | 69074ca888e1fa9cdad846333d2267751e44d8ea (diff) | |
download | chromium_src-16c7abc10a5585ee9fa0efdeb0723710fed93728.zip chromium_src-16c7abc10a5585ee9fa0efdeb0723710fed93728.tar.gz chromium_src-16c7abc10a5585ee9fa0efdeb0723710fed93728.tar.bz2 |
Add a StatisticsDeltaReader 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
StatisticsDeltaReader class allows a client to obtain the differential
value of a given histogram in the interval since the given
StatisticsDeltaReader instance was created.
This reverts commit 6a7dfb3cd638829a04e002ea9bc5b18ed3c9c03f.
BUG=232414
TBR=rsleevy, groby
Review URL: https://codereview.chromium.org/120753002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@243303 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 3 | ||||
-rw-r--r-- | base/metrics/statistics_recorder.h | 7 | ||||
-rw-r--r-- | base/test/statistics_delta_reader.cc | 41 | ||||
-rw-r--r-- | base/test/statistics_delta_reader.h | 41 | ||||
-rw-r--r-- | base/test/statistics_delta_reader_unittest.cc | 51 |
5 files changed, 140 insertions, 3 deletions
diff --git a/base/base.gyp b/base/base.gyp index 7c2b54d..96ed56f 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -608,6 +608,7 @@ 'template_util_unittest.cc', 'test/expectations/expectation_unittest.cc', 'test/expectations/parser_unittest.cc', + 'test/statistics_delta_reader_unittest.cc', 'test/test_reg_util_win_unittest.cc', 'test/trace_event_analyzer_unittest.cc', 'threading/non_thread_safe_unittest.cc', @@ -914,6 +915,8 @@ 'test/simple_test_clock.h', 'test/simple_test_tick_clock.cc', 'test/simple_test_tick_clock.h', + 'test/statistics_delta_reader.cc', + 'test/statistics_delta_reader.h', 'test/task_runner_test_template.cc', 'test/task_runner_test_template.h', 'test/test_file_util.cc', diff --git a/base/metrics/statistics_recorder.h b/base/metrics/statistics_recorder.h index 0716e80..5e53f274 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 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: @@ -88,6 +88,7 @@ class BASE_EXPORT StatisticsRecorder { friend class HistogramBaseTest; friend class HistogramTest; friend class SparseHistogramTest; + friend class StatisticsDeltaReaderTest; friend class StatisticsRecorderTest; FRIEND_TEST_ALL_PREFIXES(HistogramDeltaSerializationTest, DeserializeHistogramAndAddSamples); diff --git a/base/test/statistics_delta_reader.cc b/base/test/statistics_delta_reader.cc new file mode 100644 index 0000000..9e77b8b --- /dev/null +++ b/base/test/statistics_delta_reader.cc @@ -0,0 +1,41 @@ +// Copyright 2014 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/statistics_delta_reader.h" + +#include "base/metrics/histogram.h" +#include "base/metrics/statistics_recorder.h" +#include "base/stl_util.h" + +namespace base { + +StatisticsDeltaReader::StatisticsDeltaReader() { + // Record any histogram data that exists when the object is created so it can + // be subtracted later. + StatisticsRecorder::Histograms histograms; + StatisticsRecorder::GetSnapshot(std::string(), &histograms); + for (size_t i = 0; i < histograms.size(); ++i) { + original_samples_[histograms[i]->histogram_name()] = + histograms[i]->SnapshotSamples().release(); + } +} + +StatisticsDeltaReader::~StatisticsDeltaReader() { + STLDeleteValues(&original_samples_); +} + +scoped_ptr<HistogramSamples> + StatisticsDeltaReader::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/statistics_delta_reader.h b/base/test/statistics_delta_reader.h new file mode 100644 index 0000000..c4e4e99 --- /dev/null +++ b/base/test/statistics_delta_reader.h @@ -0,0 +1,41 @@ +// Copyright 2014 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_STATISTICS_DELTA_READER_H_ +#define BASE_TEST_STATISTICS_DELTA_READER_H_ + +#include <map> +#include <string> + +#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. +// Before using this class, StatisticsRecoder must be initialized. +class StatisticsDeltaReader { + public: + StatisticsDeltaReader(); + ~StatisticsDeltaReader(); + + // 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 instance takes ownership of the samples, which are deleted + // when the instance is destroyed. + std::map<std::string, HistogramSamples*> original_samples_; + + DISALLOW_COPY_AND_ASSIGN(StatisticsDeltaReader); +}; + +} // namespace base + +#endif // BASE_TEST_STATISTICS_DELTA_READER_H_ diff --git a/base/test/statistics_delta_reader_unittest.cc b/base/test/statistics_delta_reader_unittest.cc new file mode 100644 index 0000000..acf8d5a --- /dev/null +++ b/base/test/statistics_delta_reader_unittest.cc @@ -0,0 +1,51 @@ +// Copyright 2014 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/metrics/statistics_recorder.h" +#include "base/test/statistics_delta_reader.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace base { + +class StatisticsDeltaReaderTest : public testing::Test { + protected: + virtual void SetUp() OVERRIDE { + // Each test will have a clean state (no Histogram / BucketRanges + // registered). + statistics_recorder_ = new StatisticsRecorder(); + } + + virtual void TearDown() OVERRIDE { + delete statistics_recorder_; + statistics_recorder_ = NULL; + } + + StatisticsRecorder* statistics_recorder_; +}; + +TEST_F(StatisticsDeltaReaderTest, Scope) { + // Record a histogram before the creation of the recorder. + UMA_HISTOGRAM_BOOLEAN("Test", true); + + StatisticsDeltaReader reader; + + // Verify that no histogram is recorded. + scoped_ptr<HistogramSamples> samples( + reader.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 = reader.GetHistogramSamplesSinceCreation("Test"); + EXPECT_TRUE(samples); + EXPECT_EQ(1, samples->TotalCount()); +} + +} // namespace base |