blob: 8949133b71cc02494049ae8ae4351270bd692398 [file] [log] [blame]
mlerman@chromium.orga7980682014-08-22 07:00:38 +09001// 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.
4
5#include "base/test/histogram_tester.h"
6
avif09d5392015-12-24 12:28:02 +09007#include <stddef.h>
8
mlerman@chromium.orga7980682014-08-22 07:00:38 +09009#include "base/metrics/histogram.h"
10#include "base/metrics/histogram_samples.h"
bcwhiteca01de92015-12-11 03:36:34 +090011#include "base/metrics/metrics_hashes.h"
vabr4df640b2015-08-03 17:12:54 +090012#include "base/metrics/sample_map.h"
mlerman@chromium.orga7980682014-08-22 07:00:38 +090013#include "base/metrics/statistics_recorder.h"
pkalinnikov5b63bda2017-01-14 03:30:38 +090014#include "base/strings/string_util.h"
mlerman@chromium.orga7980682014-08-22 07:00:38 +090015#include "testing/gtest/include/gtest/gtest.h"
16
17namespace base {
18
19HistogramTester::HistogramTester() {
mlerman@chromium.orga7980682014-08-22 07:00:38 +090020 // Record any histogram data that exists when the object is created so it can
21 // be subtracted later.
22 StatisticsRecorder::Histograms histograms;
23 StatisticsRecorder::GetSnapshot(std::string(), &histograms);
vmpstr6cd44722017-02-14 06:11:41 +090024 for (const auto* histogram : histograms) {
aviace57a82016-10-27 13:10:51 +090025 histograms_snapshot_[histogram->histogram_name()] =
26 histogram->SnapshotSamples();
mlerman@chromium.orga7980682014-08-22 07:00:38 +090027 }
28}
29
Chris Watkinsd155d9f2017-11-29 16:16:38 +090030HistogramTester::~HistogramTester() = default;
mlerman@chromium.orga7980682014-08-22 07:00:38 +090031
32void HistogramTester::ExpectUniqueSample(
33 const std::string& name,
pkalinnikov5b63bda2017-01-14 03:30:38 +090034 HistogramBase::Sample sample,
35 HistogramBase::Count expected_count) const {
36 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
aviace57a82016-10-27 13:10:51 +090037 EXPECT_NE(nullptr, histogram) << "Histogram \"" << name
38 << "\" does not exist.";
mlerman@chromium.orga7980682014-08-22 07:00:38 +090039
40 if (histogram) {
pkalinnikov5b63bda2017-01-14 03:30:38 +090041 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
mlerman@chromium.orga7980682014-08-22 07:00:38 +090042 CheckBucketCount(name, sample, expected_count, *samples);
43 CheckTotalCount(name, expected_count, *samples);
44 }
45}
46
47void HistogramTester::ExpectBucketCount(
48 const std::string& name,
pkalinnikov5b63bda2017-01-14 03:30:38 +090049 HistogramBase::Sample sample,
50 HistogramBase::Count expected_count) const {
51 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
aviace57a82016-10-27 13:10:51 +090052 EXPECT_NE(nullptr, histogram) << "Histogram \"" << name
53 << "\" does not exist.";
mlerman@chromium.orga7980682014-08-22 07:00:38 +090054
55 if (histogram) {
pkalinnikov5b63bda2017-01-14 03:30:38 +090056 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
mlerman@chromium.orga7980682014-08-22 07:00:38 +090057 CheckBucketCount(name, sample, expected_count, *samples);
58 }
59}
60
61void HistogramTester::ExpectTotalCount(const std::string& name,
pkalinnikov5b63bda2017-01-14 03:30:38 +090062 HistogramBase::Count count) const {
63 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
mlerman@chromium.orga7980682014-08-22 07:00:38 +090064 if (histogram) {
pkalinnikov5b63bda2017-01-14 03:30:38 +090065 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
mlerman@chromium.orga7980682014-08-22 07:00:38 +090066 CheckTotalCount(name, count, *samples);
67 } else {
68 // No histogram means there were zero samples.
69 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist.";
70 }
71}
72
pkalinnikov5b63bda2017-01-14 03:30:38 +090073void HistogramTester::ExpectTimeBucketCount(const std::string& name,
74 TimeDelta sample,
75 HistogramBase::Count count) const {
nikunjb7961de92016-11-15 07:06:25 +090076 ExpectBucketCount(name, sample.InMilliseconds(), count);
77}
78
nickb1c693a2015-07-28 03:29:59 +090079std::vector<Bucket> HistogramTester::GetAllSamples(
80 const std::string& name) const {
twifkak120eb322015-07-01 04:13:21 +090081 std::vector<Bucket> samples;
dchengcc8e4d82016-04-05 06:25:51 +090082 std::unique_ptr<HistogramSamples> snapshot =
twifkak120eb322015-07-01 04:13:21 +090083 GetHistogramSamplesSinceCreation(name);
twifkak0d2133a2015-08-11 01:39:33 +090084 if (snapshot) {
85 for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) {
86 HistogramBase::Sample sample;
87 HistogramBase::Count count;
88 it->Get(&sample, nullptr, &count);
89 samples.push_back(Bucket(sample, count));
90 }
twifkak120eb322015-07-01 04:13:21 +090091 }
92 return samples;
93}
94
lunalu1f210752017-05-20 13:25:28 +090095HistogramBase::Count HistogramTester::GetBucketCount(
96 const std::string& name,
97 HistogramBase::Sample sample) const {
98 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
99 EXPECT_NE(nullptr, histogram)
100 << "Histogram \"" << name << "\" does not exist.";
101 HistogramBase::Count count = 0;
102 if (histogram) {
103 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
104 GetBucketCountForSamples(name, sample, *samples, &count);
105 }
106 return count;
107}
108
109void HistogramTester::GetBucketCountForSamples(
110 const std::string& name,
111 HistogramBase::Sample sample,
112 const HistogramSamples& samples,
113 HistogramBase::Count* count) const {
114 *count = samples.GetCount(sample);
115 auto histogram_data = histograms_snapshot_.find(name);
116 if (histogram_data != histograms_snapshot_.end())
117 *count -= histogram_data->second->GetCount(sample);
118}
119
nickb1c693a2015-07-28 03:29:59 +0900120HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix(
pkalinnikov5b63bda2017-01-14 03:30:38 +0900121 const std::string& prefix) const {
122 EXPECT_TRUE(prefix.find('.') != std::string::npos)
123 << "|prefix| ought to contain at least one period, to avoid matching too"
nickb1c693a2015-07-28 03:29:59 +0900124 << " many histograms.";
125
pkalinnikov5b63bda2017-01-14 03:30:38 +0900126 // Find candidate matches by using the logic built into GetSnapshot().
127 StatisticsRecorder::Histograms candidate_matches;
128 StatisticsRecorder::GetSnapshot(prefix, &candidate_matches);
nickb1c693a2015-07-28 03:29:59 +0900129
130 CountsMap result;
pkalinnikov5b63bda2017-01-14 03:30:38 +0900131 for (HistogramBase* histogram : candidate_matches) {
132 if (!StartsWith(histogram->histogram_name(), prefix,
133 CompareCase::SENSITIVE)) {
134 continue;
135 }
dchengcc8e4d82016-04-05 06:25:51 +0900136 std::unique_ptr<HistogramSamples> new_samples =
nickb1c693a2015-07-28 03:29:59 +0900137 GetHistogramSamplesSinceCreation(histogram->histogram_name());
138 // Omit unchanged histograms from the result.
139 if (new_samples->TotalCount()) {
140 result[histogram->histogram_name()] = new_samples->TotalCount();
141 }
142 }
143 return result;
144}
145
dchengcc8e4d82016-04-05 06:25:51 +0900146std::unique_ptr<HistogramSamples>
147HistogramTester::GetHistogramSamplesSinceCreation(
nickb1c693a2015-07-28 03:29:59 +0900148 const std::string& histogram_name) const {
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900149 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name);
vabr4df640b2015-08-03 17:12:54 +0900150 // Whether the histogram exists or not may not depend on the current test
151 // calling this method, but rather on which tests ran before and possibly
152 // generated a histogram or not (see http://crbug.com/473689). To provide a
153 // response which is independent of the previously run tests, this method
154 // creates empty samples in the absence of the histogram, rather than
155 // returning null.
bcwhiteca01de92015-12-11 03:36:34 +0900156 if (!histogram) {
dchengcc8e4d82016-04-05 06:25:51 +0900157 return std::unique_ptr<HistogramSamples>(
bcwhiteca01de92015-12-11 03:36:34 +0900158 new SampleMap(HashMetricName(histogram_name)));
159 }
aviace57a82016-10-27 13:10:51 +0900160 std::unique_ptr<HistogramSamples> named_samples =
161 histogram->SnapshotSamples();
nickb1c693a2015-07-28 03:29:59 +0900162 auto original_samples_it = histograms_snapshot_.find(histogram_name);
163 if (original_samples_it != histograms_snapshot_.end())
aviace57a82016-10-27 13:10:51 +0900164 named_samples->Subtract(*original_samples_it->second.get());
danakj800d2ea2015-11-25 14:29:58 +0900165 return named_samples;
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900166}
167
pkalinnikov5b63bda2017-01-14 03:30:38 +0900168void HistogramTester::CheckBucketCount(const std::string& name,
169 HistogramBase::Sample sample,
170 HistogramBase::Count expected_count,
171 const HistogramSamples& samples) const {
lunalu1f210752017-05-20 13:25:28 +0900172 int actual_count;
173 GetBucketCountForSamples(name, sample, samples, &actual_count);
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900174
175 EXPECT_EQ(expected_count, actual_count)
176 << "Histogram \"" << name
177 << "\" does not have the right number of samples (" << expected_count
178 << ") in the expected bucket (" << sample << "). It has (" << actual_count
179 << ").";
180}
181
pkalinnikov5b63bda2017-01-14 03:30:38 +0900182void HistogramTester::CheckTotalCount(const std::string& name,
183 HistogramBase::Count expected_count,
184 const HistogramSamples& samples) const {
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900185 int actual_count = samples.TotalCount();
aviace57a82016-10-27 13:10:51 +0900186 auto histogram_data = histograms_snapshot_.find(name);
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900187 if (histogram_data != histograms_snapshot_.end())
188 actual_count -= histogram_data->second->TotalCount();
189
190 EXPECT_EQ(expected_count, actual_count)
191 << "Histogram \"" << name
192 << "\" does not have the right total number of samples ("
193 << expected_count << "). It has (" << actual_count << ").";
194}
195
twifkak120eb322015-07-01 04:13:21 +0900196bool Bucket::operator==(const Bucket& other) const {
197 return min == other.min && count == other.count;
198}
199
200void PrintTo(const Bucket& bucket, std::ostream* os) {
201 *os << "Bucket " << bucket.min << ": " << bucket.count;
202}
203
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900204} // namespace base