OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/test/histogram_tester.h" | 5 #include "base/test/histogram_tester.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/metrics/histogram_samples.h" | 10 #include "base/metrics/histogram_samples.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) { | 88 for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) { |
89 HistogramBase::Sample sample; | 89 HistogramBase::Sample sample; |
90 HistogramBase::Count count; | 90 HistogramBase::Count count; |
91 it->Get(&sample, nullptr, &count); | 91 it->Get(&sample, nullptr, &count); |
92 samples.push_back(Bucket(sample, count)); | 92 samples.push_back(Bucket(sample, count)); |
93 } | 93 } |
94 } | 94 } |
95 return samples; | 95 return samples; |
96 } | 96 } |
97 | 97 |
| 98 HistogramBase::Count HistogramTester::GetBucketCount( |
| 99 const std::string& name, |
| 100 HistogramBase::Sample sample) const { |
| 101 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| 102 EXPECT_NE(nullptr, histogram) |
| 103 << "Histogram \"" << name << "\" does not exist."; |
| 104 HistogramBase::Count count = 0; |
| 105 if (histogram) { |
| 106 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
| 107 GetBucketCountForSamples(name, sample, *samples, &count); |
| 108 } |
| 109 return count; |
| 110 } |
| 111 |
| 112 void HistogramTester::GetBucketCountForSamples( |
| 113 const std::string& name, |
| 114 HistogramBase::Sample sample, |
| 115 const HistogramSamples& samples, |
| 116 HistogramBase::Count* count) const { |
| 117 *count = samples.GetCount(sample); |
| 118 auto histogram_data = histograms_snapshot_.find(name); |
| 119 if (histogram_data != histograms_snapshot_.end()) |
| 120 *count -= histogram_data->second->GetCount(sample); |
| 121 } |
| 122 |
98 HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix( | 123 HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix( |
99 const std::string& prefix) const { | 124 const std::string& prefix) const { |
100 EXPECT_TRUE(prefix.find('.') != std::string::npos) | 125 EXPECT_TRUE(prefix.find('.') != std::string::npos) |
101 << "|prefix| ought to contain at least one period, to avoid matching too" | 126 << "|prefix| ought to contain at least one period, to avoid matching too" |
102 << " many histograms."; | 127 << " many histograms."; |
103 | 128 |
104 // Find candidate matches by using the logic built into GetSnapshot(). | 129 // Find candidate matches by using the logic built into GetSnapshot(). |
105 StatisticsRecorder::Histograms candidate_matches; | 130 StatisticsRecorder::Histograms candidate_matches; |
106 StatisticsRecorder::GetSnapshot(prefix, &candidate_matches); | 131 StatisticsRecorder::GetSnapshot(prefix, &candidate_matches); |
107 | 132 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 auto original_samples_it = histograms_snapshot_.find(histogram_name); | 165 auto original_samples_it = histograms_snapshot_.find(histogram_name); |
141 if (original_samples_it != histograms_snapshot_.end()) | 166 if (original_samples_it != histograms_snapshot_.end()) |
142 named_samples->Subtract(*original_samples_it->second.get()); | 167 named_samples->Subtract(*original_samples_it->second.get()); |
143 return named_samples; | 168 return named_samples; |
144 } | 169 } |
145 | 170 |
146 void HistogramTester::CheckBucketCount(const std::string& name, | 171 void HistogramTester::CheckBucketCount(const std::string& name, |
147 HistogramBase::Sample sample, | 172 HistogramBase::Sample sample, |
148 HistogramBase::Count expected_count, | 173 HistogramBase::Count expected_count, |
149 const HistogramSamples& samples) const { | 174 const HistogramSamples& samples) const { |
150 int actual_count = samples.GetCount(sample); | 175 int actual_count; |
151 auto histogram_data = histograms_snapshot_.find(name); | 176 GetBucketCountForSamples(name, sample, samples, &actual_count); |
152 if (histogram_data != histograms_snapshot_.end()) | |
153 actual_count -= histogram_data->second->GetCount(sample); | |
154 | 177 |
155 EXPECT_EQ(expected_count, actual_count) | 178 EXPECT_EQ(expected_count, actual_count) |
156 << "Histogram \"" << name | 179 << "Histogram \"" << name |
157 << "\" does not have the right number of samples (" << expected_count | 180 << "\" does not have the right number of samples (" << expected_count |
158 << ") in the expected bucket (" << sample << "). It has (" << actual_count | 181 << ") in the expected bucket (" << sample << "). It has (" << actual_count |
159 << ")."; | 182 << ")."; |
160 } | 183 } |
161 | 184 |
162 void HistogramTester::CheckTotalCount(const std::string& name, | 185 void HistogramTester::CheckTotalCount(const std::string& name, |
163 HistogramBase::Count expected_count, | 186 HistogramBase::Count expected_count, |
(...skipping 11 matching lines...) Expand all Loading... |
175 | 198 |
176 bool Bucket::operator==(const Bucket& other) const { | 199 bool Bucket::operator==(const Bucket& other) const { |
177 return min == other.min && count == other.count; | 200 return min == other.min && count == other.count; |
178 } | 201 } |
179 | 202 |
180 void PrintTo(const Bucket& bucket, std::ostream* os) { | 203 void PrintTo(const Bucket& bucket, std::ostream* os) { |
181 *os << "Bucket " << bucket.min << ": " << bucket.count; | 204 *os << "Bucket " << bucket.min << ": " << bucket.count; |
182 } | 205 } |
183 | 206 |
184 } // namespace base | 207 } // namespace base |
OLD | NEW |