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"
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.";
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.";
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
);
69 scoped_ptr
<base::HistogramSamples
> samples(histogram
->SnapshotSamples());
70 CheckTotalCount(name
, count
, *samples
);
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
);
83 for (auto it
= snapshot
->Iterator(); !it
->Done(); it
->Next()) {
84 HistogramBase::Sample sample
;
85 HistogramBase::Count count
;
86 it
->Get(&sample
, nullptr, &count
);
87 samples
.push_back(Bucket(sample
, count
));
93 HistogramTester::CountsMap
HistogramTester::GetTotalCountsForPrefix(
94 const std::string
& query
) const {
95 EXPECT_TRUE(query
.find('.') != std::string::npos
)
96 << "|query| ought to contain at least one period, to avoid matching too"
97 << " many histograms.";
99 // Find matches by using the prefix-matching logic built into GetSnapshot().
100 StatisticsRecorder::Histograms query_matches
;
101 StatisticsRecorder::GetSnapshot(query
, &query_matches
);
104 for (base::HistogramBase
* histogram
: query_matches
) {
105 scoped_ptr
<HistogramSamples
> new_samples
=
106 GetHistogramSamplesSinceCreation(histogram
->histogram_name());
107 // Omit unchanged histograms from the result.
108 if (new_samples
->TotalCount()) {
109 result
[histogram
->histogram_name()] = new_samples
->TotalCount();
115 scoped_ptr
<HistogramSamples
> HistogramTester::GetHistogramSamplesSinceCreation(
116 const std::string
& histogram_name
) const {
117 HistogramBase
* histogram
= StatisticsRecorder::FindHistogram(histogram_name
);
118 // Whether the histogram exists or not may not depend on the current test
119 // calling this method, but rather on which tests ran before and possibly
120 // generated a histogram or not (see http://crbug.com/473689). To provide a
121 // response which is independent of the previously run tests, this method
122 // creates empty samples in the absence of the histogram, rather than
125 return scoped_ptr
<HistogramSamples
>(new SampleMap
);
126 scoped_ptr
<HistogramSamples
> named_samples(histogram
->SnapshotSamples());
127 auto original_samples_it
= histograms_snapshot_
.find(histogram_name
);
128 if (original_samples_it
!= histograms_snapshot_
.end())
129 named_samples
->Subtract(*original_samples_it
->second
);
130 return named_samples
.Pass();
133 void HistogramTester::CheckBucketCount(
134 const std::string
& name
,
135 base::HistogramBase::Sample sample
,
136 base::HistogramBase::Count expected_count
,
137 const base::HistogramSamples
& samples
) const {
138 int actual_count
= samples
.GetCount(sample
);
139 std::map
<std::string
, HistogramSamples
*>::const_iterator histogram_data
;
140 histogram_data
= histograms_snapshot_
.find(name
);
141 if (histogram_data
!= histograms_snapshot_
.end())
142 actual_count
-= histogram_data
->second
->GetCount(sample
);
144 EXPECT_EQ(expected_count
, actual_count
)
145 << "Histogram \"" << name
146 << "\" does not have the right number of samples (" << expected_count
147 << ") in the expected bucket (" << sample
<< "). It has (" << actual_count
151 void HistogramTester::CheckTotalCount(
152 const std::string
& name
,
153 base::HistogramBase::Count expected_count
,
154 const base::HistogramSamples
& samples
) const {
155 int actual_count
= samples
.TotalCount();
156 std::map
<std::string
, HistogramSamples
*>::const_iterator histogram_data
;
157 histogram_data
= histograms_snapshot_
.find(name
);
158 if (histogram_data
!= histograms_snapshot_
.end())
159 actual_count
-= histogram_data
->second
->TotalCount();
161 EXPECT_EQ(expected_count
, actual_count
)
162 << "Histogram \"" << name
163 << "\" does not have the right total number of samples ("
164 << expected_count
<< "). It has (" << actual_count
<< ").";
167 bool Bucket::operator==(const Bucket
& other
) const {
168 return min
== other
.min
&& count
== other
.count
;
171 void PrintTo(const Bucket
& bucket
, std::ostream
* os
) {
172 *os
<< "Bucket " << bucket
.min
<< ": " << bucket
.count
;