Add ICU message format support
[chromium-blink-merge.git] / base / test / histogram_tester.cc
blob051f599f45a360f7afc31f897c051260a34fecde
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/test/histogram_tester.h"
7 #include "base/metrics/histogram.h"
8 #include "base/metrics/histogram_samples.h"
9 #include "base/metrics/sample_map.h"
10 #include "base/metrics/statistics_recorder.h"
11 #include "base/stl_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace base {
16 HistogramTester::HistogramTester() {
17 StatisticsRecorder::Initialize(); // Safe to call multiple times.
19 // Record any histogram data that exists when the object is created so it can
20 // be subtracted later.
21 StatisticsRecorder::Histograms histograms;
22 StatisticsRecorder::GetSnapshot(std::string(), &histograms);
23 for (size_t i = 0; i < histograms.size(); ++i) {
24 histograms_snapshot_[histograms[i]->histogram_name()] =
25 histograms[i]->SnapshotSamples().release();
29 HistogramTester::~HistogramTester() {
30 STLDeleteValues(&histograms_snapshot_);
33 void HistogramTester::ExpectUniqueSample(
34 const std::string& name,
35 base::HistogramBase::Sample sample,
36 base::HistogramBase::Count expected_count) const {
37 base::HistogramBase* histogram =
38 base::StatisticsRecorder::FindHistogram(name);
39 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram)
40 << "Histogram \"" << name << "\" does not exist.";
42 if (histogram) {
43 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
44 CheckBucketCount(name, sample, expected_count, *samples);
45 CheckTotalCount(name, expected_count, *samples);
49 void HistogramTester::ExpectBucketCount(
50 const std::string& name,
51 base::HistogramBase::Sample sample,
52 base::HistogramBase::Count expected_count) const {
53 base::HistogramBase* histogram =
54 base::StatisticsRecorder::FindHistogram(name);
55 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram)
56 << "Histogram \"" << name << "\" does not exist.";
58 if (histogram) {
59 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
60 CheckBucketCount(name, sample, expected_count, *samples);
64 void HistogramTester::ExpectTotalCount(const std::string& name,
65 base::HistogramBase::Count count) const {
66 base::HistogramBase* histogram =
67 base::StatisticsRecorder::FindHistogram(name);
68 if (histogram) {
69 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
70 CheckTotalCount(name, count, *samples);
71 } else {
72 // No histogram means there were zero samples.
73 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist.";
77 std::vector<Bucket> HistogramTester::GetAllSamples(
78 const std::string& name) const {
79 std::vector<Bucket> samples;
80 scoped_ptr<HistogramSamples> snapshot =
81 GetHistogramSamplesSinceCreation(name);
82 for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) {
83 HistogramBase::Sample sample;
84 HistogramBase::Count count;
85 it->Get(&sample, nullptr, &count);
86 samples.push_back(Bucket(sample, count));
88 return samples;
91 HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix(
92 const std::string& query) const {
93 EXPECT_TRUE(query.find('.') != std::string::npos)
94 << "|query| ought to contain at least one period, to avoid matching too"
95 << " many histograms.";
97 // Find matches by using the prefix-matching logic built into GetSnapshot().
98 StatisticsRecorder::Histograms query_matches;
99 StatisticsRecorder::GetSnapshot(query, &query_matches);
101 CountsMap result;
102 for (base::HistogramBase* histogram : query_matches) {
103 scoped_ptr<HistogramSamples> new_samples =
104 GetHistogramSamplesSinceCreation(histogram->histogram_name());
105 // Omit unchanged histograms from the result.
106 if (new_samples->TotalCount()) {
107 result[histogram->histogram_name()] = new_samples->TotalCount();
110 return result;
113 scoped_ptr<HistogramSamples> HistogramTester::GetHistogramSamplesSinceCreation(
114 const std::string& histogram_name) const {
115 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name);
116 // Whether the histogram exists or not may not depend on the current test
117 // calling this method, but rather on which tests ran before and possibly
118 // generated a histogram or not (see http://crbug.com/473689). To provide a
119 // response which is independent of the previously run tests, this method
120 // creates empty samples in the absence of the histogram, rather than
121 // returning null.
122 if (!histogram)
123 return scoped_ptr<HistogramSamples>(new SampleMap);
124 scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples());
125 auto original_samples_it = histograms_snapshot_.find(histogram_name);
126 if (original_samples_it != histograms_snapshot_.end())
127 named_samples->Subtract(*original_samples_it->second);
128 return named_samples.Pass();
131 void HistogramTester::CheckBucketCount(
132 const std::string& name,
133 base::HistogramBase::Sample sample,
134 base::HistogramBase::Count expected_count,
135 const base::HistogramSamples& samples) const {
136 int actual_count = samples.GetCount(sample);
137 std::map<std::string, HistogramSamples*>::const_iterator histogram_data;
138 histogram_data = histograms_snapshot_.find(name);
139 if (histogram_data != histograms_snapshot_.end())
140 actual_count -= histogram_data->second->GetCount(sample);
142 EXPECT_EQ(expected_count, actual_count)
143 << "Histogram \"" << name
144 << "\" does not have the right number of samples (" << expected_count
145 << ") in the expected bucket (" << sample << "). It has (" << actual_count
146 << ").";
149 void HistogramTester::CheckTotalCount(
150 const std::string& name,
151 base::HistogramBase::Count expected_count,
152 const base::HistogramSamples& samples) const {
153 int actual_count = samples.TotalCount();
154 std::map<std::string, HistogramSamples*>::const_iterator histogram_data;
155 histogram_data = histograms_snapshot_.find(name);
156 if (histogram_data != histograms_snapshot_.end())
157 actual_count -= histogram_data->second->TotalCount();
159 EXPECT_EQ(expected_count, actual_count)
160 << "Histogram \"" << name
161 << "\" does not have the right total number of samples ("
162 << expected_count << "). It has (" << actual_count << ").";
165 bool Bucket::operator==(const Bucket& other) const {
166 return min == other.min && count == other.count;
169 void PrintTo(const Bucket& bucket, std::ostream* os) {
170 *os << "Bucket " << bucket.min << ": " << bucket.count;
173 } // namespace base