mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "base/test/histogram_tester.h" |
| 6 | |
avi | f09d539 | 2015-12-24 12:28:02 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 9 | #include "base/metrics/histogram.h" |
| 10 | #include "base/metrics/histogram_samples.h" |
bcwhite | ca01de9 | 2015-12-11 03:36:34 +0900 | [diff] [blame] | 11 | #include "base/metrics/metrics_hashes.h" |
vabr | 4df640b | 2015-08-03 17:12:54 +0900 | [diff] [blame] | 12 | #include "base/metrics/sample_map.h" |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 13 | #include "base/metrics/statistics_recorder.h" |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | HistogramTester::HistogramTester() { |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 20 | // 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); |
vmpstr | 6cd4472 | 2017-02-14 06:11:41 +0900 | [diff] [blame] | 24 | for (const auto* histogram : histograms) { |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 25 | histograms_snapshot_[histogram->histogram_name()] = |
| 26 | histogram->SnapshotSamples(); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | |
Chris Watkins | d155d9f | 2017-11-29 16:16:38 +0900 | [diff] [blame] | 30 | HistogramTester::~HistogramTester() = default; |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 31 | |
| 32 | void HistogramTester::ExpectUniqueSample( |
| 33 | const std::string& name, |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 34 | HistogramBase::Sample sample, |
| 35 | HistogramBase::Count expected_count) const { |
| 36 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 37 | EXPECT_NE(nullptr, histogram) << "Histogram \"" << name |
| 38 | << "\" does not exist."; |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 39 | |
| 40 | if (histogram) { |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 41 | std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 42 | CheckBucketCount(name, sample, expected_count, *samples); |
| 43 | CheckTotalCount(name, expected_count, *samples); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void HistogramTester::ExpectBucketCount( |
| 48 | const std::string& name, |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 49 | HistogramBase::Sample sample, |
| 50 | HistogramBase::Count expected_count) const { |
| 51 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 52 | EXPECT_NE(nullptr, histogram) << "Histogram \"" << name |
| 53 | << "\" does not exist."; |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 54 | |
| 55 | if (histogram) { |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 56 | std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 57 | CheckBucketCount(name, sample, expected_count, *samples); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void HistogramTester::ExpectTotalCount(const std::string& name, |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 62 | HistogramBase::Count count) const { |
| 63 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 64 | if (histogram) { |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 65 | std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 66 | 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 | |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 73 | void HistogramTester::ExpectTimeBucketCount(const std::string& name, |
| 74 | TimeDelta sample, |
| 75 | HistogramBase::Count count) const { |
nikunjb | 7961de9 | 2016-11-15 07:06:25 +0900 | [diff] [blame] | 76 | ExpectBucketCount(name, sample.InMilliseconds(), count); |
| 77 | } |
| 78 | |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 79 | std::vector<Bucket> HistogramTester::GetAllSamples( |
| 80 | const std::string& name) const { |
twifkak | 120eb32 | 2015-07-01 04:13:21 +0900 | [diff] [blame] | 81 | std::vector<Bucket> samples; |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 82 | std::unique_ptr<HistogramSamples> snapshot = |
twifkak | 120eb32 | 2015-07-01 04:13:21 +0900 | [diff] [blame] | 83 | GetHistogramSamplesSinceCreation(name); |
twifkak | 0d2133a | 2015-08-11 01:39:33 +0900 | [diff] [blame] | 84 | 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 | } |
twifkak | 120eb32 | 2015-07-01 04:13:21 +0900 | [diff] [blame] | 91 | } |
| 92 | return samples; |
| 93 | } |
| 94 | |
lunalu | 1f21075 | 2017-05-20 13:25:28 +0900 | [diff] [blame] | 95 | HistogramBase::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 | |
| 109 | void 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 | |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 120 | HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix( |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 121 | 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" |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 124 | << " many histograms."; |
| 125 | |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 126 | // Find candidate matches by using the logic built into GetSnapshot(). |
| 127 | StatisticsRecorder::Histograms candidate_matches; |
| 128 | StatisticsRecorder::GetSnapshot(prefix, &candidate_matches); |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 129 | |
| 130 | CountsMap result; |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 131 | for (HistogramBase* histogram : candidate_matches) { |
| 132 | if (!StartsWith(histogram->histogram_name(), prefix, |
| 133 | CompareCase::SENSITIVE)) { |
| 134 | continue; |
| 135 | } |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 136 | std::unique_ptr<HistogramSamples> new_samples = |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 137 | 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 | |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 146 | std::unique_ptr<HistogramSamples> |
| 147 | HistogramTester::GetHistogramSamplesSinceCreation( |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 148 | const std::string& histogram_name) const { |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 149 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); |
vabr | 4df640b | 2015-08-03 17:12:54 +0900 | [diff] [blame] | 150 | // 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. |
bcwhite | ca01de9 | 2015-12-11 03:36:34 +0900 | [diff] [blame] | 156 | if (!histogram) { |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 157 | return std::unique_ptr<HistogramSamples>( |
bcwhite | ca01de9 | 2015-12-11 03:36:34 +0900 | [diff] [blame] | 158 | new SampleMap(HashMetricName(histogram_name))); |
| 159 | } |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 160 | std::unique_ptr<HistogramSamples> named_samples = |
| 161 | histogram->SnapshotSamples(); |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 162 | auto original_samples_it = histograms_snapshot_.find(histogram_name); |
| 163 | if (original_samples_it != histograms_snapshot_.end()) |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 164 | named_samples->Subtract(*original_samples_it->second.get()); |
danakj | 800d2ea | 2015-11-25 14:29:58 +0900 | [diff] [blame] | 165 | return named_samples; |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 166 | } |
| 167 | |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 168 | void HistogramTester::CheckBucketCount(const std::string& name, |
| 169 | HistogramBase::Sample sample, |
| 170 | HistogramBase::Count expected_count, |
| 171 | const HistogramSamples& samples) const { |
lunalu | 1f21075 | 2017-05-20 13:25:28 +0900 | [diff] [blame] | 172 | int actual_count; |
| 173 | GetBucketCountForSamples(name, sample, samples, &actual_count); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 174 | |
| 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 | |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 182 | void HistogramTester::CheckTotalCount(const std::string& name, |
| 183 | HistogramBase::Count expected_count, |
| 184 | const HistogramSamples& samples) const { |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 185 | int actual_count = samples.TotalCount(); |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 186 | auto histogram_data = histograms_snapshot_.find(name); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 187 | 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 | |
twifkak | 120eb32 | 2015-07-01 04:13:21 +0900 | [diff] [blame] | 196 | bool Bucket::operator==(const Bucket& other) const { |
| 197 | return min == other.min && count == other.count; |
| 198 | } |
| 199 | |
| 200 | void PrintTo(const Bucket& bucket, std::ostream* os) { |
| 201 | *os << "Bucket " << bucket.min << ": " << bucket.count; |
| 202 | } |
| 203 | |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 204 | } // namespace base |