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 "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "base/metrics/histogram_samples.h" | 8 #include "base/metrics/histogram_samples.h" |
9 #include "base/metrics/statistics_recorder.h" | 9 #include "base/metrics/statistics_recorder.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 base::StatisticsRecorder::FindHistogram(name); | 66 base::StatisticsRecorder::FindHistogram(name); |
67 if (histogram) { | 67 if (histogram) { |
68 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); | 68 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); |
69 CheckTotalCount(name, count, *samples); | 69 CheckTotalCount(name, count, *samples); |
70 } else { | 70 } else { |
71 // No histogram means there were zero samples. | 71 // No histogram means there were zero samples. |
72 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; | 72 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 std::vector<Bucket> HistogramTester::GetAllSamples(const std::string& name) { | |
77 std::vector<Bucket> samples; | |
78 scoped_ptr<HistogramSamples> snapshot = | |
79 GetHistogramSamplesSinceCreation(name); | |
80 for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) { | |
81 HistogramBase::Sample sample; | |
82 HistogramBase::Count count; | |
83 it->Get(&sample, nullptr, &count); | |
84 samples.push_back(Bucket(sample, count)); | |
85 } | |
86 return samples; | |
87 } | |
88 | |
76 scoped_ptr<HistogramSamples> HistogramTester::GetHistogramSamplesSinceCreation( | 89 scoped_ptr<HistogramSamples> HistogramTester::GetHistogramSamplesSinceCreation( |
77 const std::string& histogram_name) { | 90 const std::string& histogram_name) { |
78 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); | 91 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); |
79 if (!histogram) | 92 if (!histogram) |
80 return scoped_ptr<HistogramSamples>(); | 93 return scoped_ptr<HistogramSamples>(); |
81 scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples()); | 94 scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples()); |
82 HistogramSamples* named_original_samples = | 95 HistogramSamples* named_original_samples = |
83 histograms_snapshot_[histogram_name]; | 96 histograms_snapshot_[histogram_name]; |
84 if (named_original_samples) | 97 if (named_original_samples) |
85 named_samples->Subtract(*named_original_samples); | 98 named_samples->Subtract(*named_original_samples); |
(...skipping 27 matching lines...) Expand all Loading... | |
113 histogram_data = histograms_snapshot_.find(name); | 126 histogram_data = histograms_snapshot_.find(name); |
114 if (histogram_data != histograms_snapshot_.end()) | 127 if (histogram_data != histograms_snapshot_.end()) |
115 actual_count -= histogram_data->second->TotalCount(); | 128 actual_count -= histogram_data->second->TotalCount(); |
116 | 129 |
117 EXPECT_EQ(expected_count, actual_count) | 130 EXPECT_EQ(expected_count, actual_count) |
118 << "Histogram \"" << name | 131 << "Histogram \"" << name |
119 << "\" does not have the right total number of samples (" | 132 << "\" does not have the right total number of samples (" |
120 << expected_count << "). It has (" << actual_count << ")."; | 133 << expected_count << "). It has (" << actual_count << ")."; |
121 } | 134 } |
122 | 135 |
136 bool Bucket::operator==(const Bucket& other) const { | |
137 return min_ == other.min_ && count_ == other.count_; | |
138 } | |
139 | |
140 void PrintTo(const Bucket& bucket, std::ostream* os) { | |
141 *os << bucket.count_ << (bucket.count_ == 1 ? " sample " : " samples ") | |
142 << "in bucket " << bucket.min_; | |
Ilya Sherman
2015/06/27 07:25:19
WDYT of something shorter, like the following?
twifkak
2015/06/29 21:52:29
That doesn't seem to be any clearer than a pair. H
Ilya Sherman
2015/06/29 22:05:34
Yeah, I think that's a good compromise between cla
| |
143 } | |
144 | |
123 } // namespace base | 145 } // namespace base |
OLD | NEW |