diff options
author | nick <nick@chromium.org> | 2015-07-27 11:29:59 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-27 18:30:32 +0000 |
commit | dd65d77f2f586bb890b9bd3574e7ceb7f9691763 (patch) | |
tree | d7f535d1c6498b5e920f76c12180494b389f94ad /base | |
parent | de9412b8832db0c6e1e283e9f8fce53eb4fd4e67 (diff) | |
download | chromium_src-dd65d77f2f586bb890b9bd3574e7ceb7f9691763.zip chromium_src-dd65d77f2f586bb890b9bd3574e7ceb7f9691763.tar.gz chromium_src-dd65d77f2f586bb890b9bd3574e7ceb7f9691763.tar.bz2 |
base/test/histogram_tester.h: Add a way to query a whole family of related
histograms at once.
Use this to clean up site_isolation_stats_gatherer_browsertest.cc
BUG=481066
Review URL: https://codereview.chromium.org/1190423006
Cr-Commit-Position: refs/heads/master@{#340510}
Diffstat (limited to 'base')
-rw-r--r-- | base/test/histogram_tester.cc | 34 | ||||
-rw-r--r-- | base/test/histogram_tester.h | 26 |
2 files changed, 52 insertions, 8 deletions
diff --git a/base/test/histogram_tester.cc b/base/test/histogram_tester.cc index 0eba3a9..0bff2f8 100644 --- a/base/test/histogram_tester.cc +++ b/base/test/histogram_tester.cc @@ -73,7 +73,8 @@ void HistogramTester::ExpectTotalCount(const std::string& name, } } -std::vector<Bucket> HistogramTester::GetAllSamples(const std::string& name) { +std::vector<Bucket> HistogramTester::GetAllSamples( + const std::string& name) const { std::vector<Bucket> samples; scoped_ptr<HistogramSamples> snapshot = GetHistogramSamplesSinceCreation(name); @@ -86,16 +87,37 @@ std::vector<Bucket> HistogramTester::GetAllSamples(const std::string& name) { return samples; } +HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix( + const std::string& query) const { + EXPECT_TRUE(query.find('.') != std::string::npos) + << "|query| ought to contain at least one period, to avoid matching too" + << " many histograms."; + + // Find matches by using the prefix-matching logic built into GetSnapshot(). + StatisticsRecorder::Histograms query_matches; + StatisticsRecorder::GetSnapshot(query, &query_matches); + + CountsMap result; + for (base::HistogramBase* histogram : query_matches) { + scoped_ptr<HistogramSamples> new_samples = + GetHistogramSamplesSinceCreation(histogram->histogram_name()); + // Omit unchanged histograms from the result. + if (new_samples->TotalCount()) { + result[histogram->histogram_name()] = new_samples->TotalCount(); + } + } + return result; +} + scoped_ptr<HistogramSamples> HistogramTester::GetHistogramSamplesSinceCreation( - const std::string& histogram_name) { + const std::string& histogram_name) const { HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); if (!histogram) return scoped_ptr<HistogramSamples>(); scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples()); - HistogramSamples* named_original_samples = - histograms_snapshot_[histogram_name]; - if (named_original_samples) - named_samples->Subtract(*named_original_samples); + auto original_samples_it = histograms_snapshot_.find(histogram_name); + if (original_samples_it != histograms_snapshot_.end()) + named_samples->Subtract(*original_samples_it->second); return named_samples.Pass(); } diff --git a/base/test/histogram_tester.h b/base/test/histogram_tester.h index 7ac7ca6..cfab3c6 100644 --- a/base/test/histogram_tester.h +++ b/base/test/histogram_tester.h @@ -26,6 +26,8 @@ class HistogramSamples; // getting logged as intended. class HistogramTester { public: + using CountsMap = std::map<std::string, base::HistogramBase::Count>; + // The constructor will call StatisticsRecorder::Initialize() for you. Also, // this takes a snapshot of all current histograms counts. HistogramTester(); @@ -67,12 +69,32 @@ class HistogramTester { // slightly less helpful failure message: // EXPECT_EQ(expected_buckets, // histogram_tester.GetAllSamples("HistogramName")); - std::vector<Bucket> GetAllSamples(const std::string& name); + std::vector<Bucket> GetAllSamples(const std::string& name) const; + + // Finds histograms whose names start with |query|, and returns them along + // with the counts of any samples added since the creation of this object. + // Histograms that are unchanged are omitted from the result. The return value + // is a map whose keys are the histogram name, and whose values are the sample + // count. + // + // This is useful for cases where the code under test is choosing among a + // family of related histograms and incrementing one of them. Typically you + // should pass the result of this function directly to EXPECT_THAT. + // + // Example usage, using gmock (which produces better failure messages): + // #include "testing/gmock/include/gmock/gmock.h" + // ... + // base::HistogramTester::CountsMap expected_counts; + // expected_counts["MyMetric.A"] = 1; + // expected_counts["MyMetric.B"] = 1; + // EXPECT_THAT(histogram_tester.GetTotalCountsForPrefix("MyMetric."), + // testing::ContainerEq(expected_counts)); + CountsMap GetTotalCountsForPrefix(const std::string& query) const; // Access a modified HistogramSamples containing only what has been logged // to the histogram since the creation of this object. scoped_ptr<HistogramSamples> GetHistogramSamplesSinceCreation( - const std::string& histogram_name); + const std::string& histogram_name) const; private: // Verifies and asserts that value in the |sample| bucket matches the |