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 " << bucket.min << ": " << bucket.count; |
| 142 } |
| 143 |
123 } // namespace base | 144 } // namespace base |
OLD | NEW |