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() { |
| 20 | StatisticsRecorder::Initialize(); // Safe to call multiple times. |
| 21 | |
| 22 | // Record any histogram data that exists when the object is created so it can |
| 23 | // be subtracted later. |
| 24 | StatisticsRecorder::Histograms histograms; |
| 25 | StatisticsRecorder::GetSnapshot(std::string(), &histograms); |
vmpstr | 6cd4472 | 2017-02-14 06:11:41 +0900 | [diff] [blame^] | 26 | for (const auto* histogram : histograms) { |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 27 | histograms_snapshot_[histogram->histogram_name()] = |
| 28 | histogram->SnapshotSamples(); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | |
| 32 | HistogramTester::~HistogramTester() { |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void HistogramTester::ExpectUniqueSample( |
| 36 | const std::string& name, |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 37 | HistogramBase::Sample sample, |
| 38 | HistogramBase::Count expected_count) const { |
| 39 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 40 | EXPECT_NE(nullptr, histogram) << "Histogram \"" << name |
| 41 | << "\" does not exist."; |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 42 | |
| 43 | if (histogram) { |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 44 | std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 45 | CheckBucketCount(name, sample, expected_count, *samples); |
| 46 | CheckTotalCount(name, expected_count, *samples); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void HistogramTester::ExpectBucketCount( |
| 51 | const std::string& name, |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 52 | HistogramBase::Sample sample, |
| 53 | HistogramBase::Count expected_count) const { |
| 54 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 55 | EXPECT_NE(nullptr, histogram) << "Histogram \"" << name |
| 56 | << "\" does not exist."; |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 57 | |
| 58 | if (histogram) { |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 59 | std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 60 | CheckBucketCount(name, sample, expected_count, *samples); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void HistogramTester::ExpectTotalCount(const std::string& name, |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 65 | HistogramBase::Count count) const { |
| 66 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 67 | if (histogram) { |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 68 | std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 69 | CheckTotalCount(name, count, *samples); |
| 70 | } else { |
| 71 | // No histogram means there were zero samples. |
| 72 | EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; |
| 73 | } |
| 74 | } |
| 75 | |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 76 | void HistogramTester::ExpectTimeBucketCount(const std::string& name, |
| 77 | TimeDelta sample, |
| 78 | HistogramBase::Count count) const { |
nikunjb | 7961de9 | 2016-11-15 07:06:25 +0900 | [diff] [blame] | 79 | ExpectBucketCount(name, sample.InMilliseconds(), count); |
| 80 | } |
| 81 | |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 82 | std::vector<Bucket> HistogramTester::GetAllSamples( |
| 83 | const std::string& name) const { |
twifkak | 120eb32 | 2015-07-01 04:13:21 +0900 | [diff] [blame] | 84 | std::vector<Bucket> samples; |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 85 | std::unique_ptr<HistogramSamples> snapshot = |
twifkak | 120eb32 | 2015-07-01 04:13:21 +0900 | [diff] [blame] | 86 | GetHistogramSamplesSinceCreation(name); |
twifkak | 0d2133a | 2015-08-11 01:39:33 +0900 | [diff] [blame] | 87 | if (snapshot) { |
| 88 | for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) { |
| 89 | HistogramBase::Sample sample; |
| 90 | HistogramBase::Count count; |
| 91 | it->Get(&sample, nullptr, &count); |
| 92 | samples.push_back(Bucket(sample, count)); |
| 93 | } |
twifkak | 120eb32 | 2015-07-01 04:13:21 +0900 | [diff] [blame] | 94 | } |
| 95 | return samples; |
| 96 | } |
| 97 | |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 98 | HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix( |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 99 | const std::string& prefix) const { |
| 100 | EXPECT_TRUE(prefix.find('.') != std::string::npos) |
| 101 | << "|prefix| ought to contain at least one period, to avoid matching too" |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 102 | << " many histograms."; |
| 103 | |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 104 | // Find candidate matches by using the logic built into GetSnapshot(). |
| 105 | StatisticsRecorder::Histograms candidate_matches; |
| 106 | StatisticsRecorder::GetSnapshot(prefix, &candidate_matches); |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 107 | |
| 108 | CountsMap result; |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 109 | for (HistogramBase* histogram : candidate_matches) { |
| 110 | if (!StartsWith(histogram->histogram_name(), prefix, |
| 111 | CompareCase::SENSITIVE)) { |
| 112 | continue; |
| 113 | } |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 114 | std::unique_ptr<HistogramSamples> new_samples = |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 115 | GetHistogramSamplesSinceCreation(histogram->histogram_name()); |
| 116 | // Omit unchanged histograms from the result. |
| 117 | if (new_samples->TotalCount()) { |
| 118 | result[histogram->histogram_name()] = new_samples->TotalCount(); |
| 119 | } |
| 120 | } |
| 121 | return result; |
| 122 | } |
| 123 | |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 124 | std::unique_ptr<HistogramSamples> |
| 125 | HistogramTester::GetHistogramSamplesSinceCreation( |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 126 | const std::string& histogram_name) const { |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 127 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); |
vabr | 4df640b | 2015-08-03 17:12:54 +0900 | [diff] [blame] | 128 | // Whether the histogram exists or not may not depend on the current test |
| 129 | // calling this method, but rather on which tests ran before and possibly |
| 130 | // generated a histogram or not (see http://crbug.com/473689). To provide a |
| 131 | // response which is independent of the previously run tests, this method |
| 132 | // creates empty samples in the absence of the histogram, rather than |
| 133 | // returning null. |
bcwhite | ca01de9 | 2015-12-11 03:36:34 +0900 | [diff] [blame] | 134 | if (!histogram) { |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 135 | return std::unique_ptr<HistogramSamples>( |
bcwhite | ca01de9 | 2015-12-11 03:36:34 +0900 | [diff] [blame] | 136 | new SampleMap(HashMetricName(histogram_name))); |
| 137 | } |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 138 | std::unique_ptr<HistogramSamples> named_samples = |
| 139 | histogram->SnapshotSamples(); |
nick | b1c693a | 2015-07-28 03:29:59 +0900 | [diff] [blame] | 140 | auto original_samples_it = histograms_snapshot_.find(histogram_name); |
| 141 | if (original_samples_it != histograms_snapshot_.end()) |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 142 | named_samples->Subtract(*original_samples_it->second.get()); |
danakj | 800d2ea | 2015-11-25 14:29:58 +0900 | [diff] [blame] | 143 | return named_samples; |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 144 | } |
| 145 | |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 146 | void HistogramTester::CheckBucketCount(const std::string& name, |
| 147 | HistogramBase::Sample sample, |
| 148 | HistogramBase::Count expected_count, |
| 149 | const HistogramSamples& samples) const { |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 150 | int actual_count = samples.GetCount(sample); |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 151 | auto histogram_data = histograms_snapshot_.find(name); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 152 | if (histogram_data != histograms_snapshot_.end()) |
| 153 | actual_count -= histogram_data->second->GetCount(sample); |
| 154 | |
| 155 | EXPECT_EQ(expected_count, actual_count) |
| 156 | << "Histogram \"" << name |
| 157 | << "\" does not have the right number of samples (" << expected_count |
| 158 | << ") in the expected bucket (" << sample << "). It has (" << actual_count |
| 159 | << ")."; |
| 160 | } |
| 161 | |
pkalinnikov | 5b63bda | 2017-01-14 03:30:38 +0900 | [diff] [blame] | 162 | void HistogramTester::CheckTotalCount(const std::string& name, |
| 163 | HistogramBase::Count expected_count, |
| 164 | const HistogramSamples& samples) const { |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 165 | int actual_count = samples.TotalCount(); |
avi | ace57a8 | 2016-10-27 13:10:51 +0900 | [diff] [blame] | 166 | auto histogram_data = histograms_snapshot_.find(name); |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 167 | if (histogram_data != histograms_snapshot_.end()) |
| 168 | actual_count -= histogram_data->second->TotalCount(); |
| 169 | |
| 170 | EXPECT_EQ(expected_count, actual_count) |
| 171 | << "Histogram \"" << name |
| 172 | << "\" does not have the right total number of samples (" |
| 173 | << expected_count << "). It has (" << actual_count << ")."; |
| 174 | } |
| 175 | |
twifkak | 120eb32 | 2015-07-01 04:13:21 +0900 | [diff] [blame] | 176 | bool Bucket::operator==(const Bucket& other) const { |
| 177 | return min == other.min && count == other.count; |
| 178 | } |
| 179 | |
| 180 | void PrintTo(const Bucket& bucket, std::ostream* os) { |
| 181 | *os << "Bucket " << bucket.min << ": " << bucket.count; |
| 182 | } |
| 183 | |
mlerman@chromium.org | a798068 | 2014-08-22 07:00:38 +0900 | [diff] [blame] | 184 | } // namespace base |