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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | 54 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
55 EXPECT_NE(nullptr, histogram) << "Histogram \"" << name | 55 EXPECT_NE(nullptr, histogram) << "Histogram \"" << name |
56 << "\" does not exist."; | 56 << "\" does not exist."; |
57 | 57 |
58 if (histogram) { | 58 if (histogram) { |
59 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); | 59 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
60 CheckBucketCount(name, sample, expected_count, *samples); | 60 CheckBucketCount(name, sample, expected_count, *samples); |
61 } | 61 } |
62 } | 62 } |
63 | 63 |
64 void HistogramTester::ExpectTotalCountExcept(const std::string& name, | |
Nico
2017/05/09 19:36:45
Move this below ExpectTotalCount() so order in cc
lunalu1
2017/05/10 19:39:38
Done.
| |
65 HistogramBase::Sample sample, | |
66 HistogramBase::Count count) const { | |
67 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | |
68 EXPECT_NE(nullptr, histogram) | |
69 << "Histogram \"" << name << "\" does not exist."; | |
70 if (histogram) { | |
71 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); | |
72 CheckTotalCountExcept(name, sample, count, *samples); | |
73 } else { | |
74 // No histogram means there were zero samples. | |
75 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; | |
76 } | |
77 } | |
78 | |
64 void HistogramTester::ExpectTotalCount(const std::string& name, | 79 void HistogramTester::ExpectTotalCount(const std::string& name, |
65 HistogramBase::Count count) const { | 80 HistogramBase::Count count) const { |
66 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | 81 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
67 if (histogram) { | 82 if (histogram) { |
68 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); | 83 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
69 CheckTotalCount(name, count, *samples); | 84 CheckTotalCount(name, count, *samples); |
70 } else { | 85 } else { |
71 // No histogram means there were zero samples. | 86 // No histogram means there were zero samples. |
72 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; | 87 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; |
73 } | 88 } |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 auto histogram_data = histograms_snapshot_.find(name); | 181 auto histogram_data = histograms_snapshot_.find(name); |
167 if (histogram_data != histograms_snapshot_.end()) | 182 if (histogram_data != histograms_snapshot_.end()) |
168 actual_count -= histogram_data->second->TotalCount(); | 183 actual_count -= histogram_data->second->TotalCount(); |
169 | 184 |
170 EXPECT_EQ(expected_count, actual_count) | 185 EXPECT_EQ(expected_count, actual_count) |
171 << "Histogram \"" << name | 186 << "Histogram \"" << name |
172 << "\" does not have the right total number of samples (" | 187 << "\" does not have the right total number of samples (" |
173 << expected_count << "). It has (" << actual_count << ")."; | 188 << expected_count << "). It has (" << actual_count << ")."; |
174 } | 189 } |
175 | 190 |
191 void HistogramTester::CheckTotalCountExcept( | |
Nico
2017/05/09 19:36:45
The new code in this file looks pretty similar to
lunalu1
2017/05/10 19:39:38
IMHO, we can implement getters (privately if we do
| |
192 const std::string& name, | |
193 HistogramBase::Sample sample, | |
194 HistogramBase::Count expected_count, | |
195 const HistogramSamples& samples) const { | |
196 int actual_total_count = samples.TotalCount(); | |
197 int actual_bucket_count = samples.GetCount(sample); | |
198 | |
199 auto histogram_data = histograms_snapshot_.find(name); | |
200 if (histogram_data != histograms_snapshot_.end()) { | |
201 actual_total_count -= histogram_data->second->TotalCount(); | |
202 actual_bucket_count -= histogram_data->second->GetCount(sample); | |
203 } | |
204 int actual_count = actual_total_count - actual_bucket_count; | |
205 | |
206 EXPECT_EQ(expected_count, actual_count) | |
207 << "Histogram \"" << name | |
208 << "\" does not have the right total number of samples (" | |
209 << expected_count << "). It has (" << actual_count << ")."; | |
210 } | |
211 | |
176 bool Bucket::operator==(const Bucket& other) const { | 212 bool Bucket::operator==(const Bucket& other) const { |
177 return min == other.min && count == other.count; | 213 return min == other.min && count == other.count; |
178 } | 214 } |
179 | 215 |
180 void PrintTo(const Bucket& bucket, std::ostream* os) { | 216 void PrintTo(const Bucket& bucket, std::ostream* os) { |
181 *os << "Bucket " << bucket.min << ": " << bucket.count; | 217 *os << "Bucket " << bucket.min << ": " << bucket.count; |
182 } | 218 } |
183 | 219 |
184 } // namespace base | 220 } // namespace base |
OLD | NEW |