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) { | 76 std::vector<Bucket> HistogramTester::GetAllSamples( |
| 77 const std::string& name) const { |
77 std::vector<Bucket> samples; | 78 std::vector<Bucket> samples; |
78 scoped_ptr<HistogramSamples> snapshot = | 79 scoped_ptr<HistogramSamples> snapshot = |
79 GetHistogramSamplesSinceCreation(name); | 80 GetHistogramSamplesSinceCreation(name); |
80 for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) { | 81 for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) { |
81 HistogramBase::Sample sample; | 82 HistogramBase::Sample sample; |
82 HistogramBase::Count count; | 83 HistogramBase::Count count; |
83 it->Get(&sample, nullptr, &count); | 84 it->Get(&sample, nullptr, &count); |
84 samples.push_back(Bucket(sample, count)); | 85 samples.push_back(Bucket(sample, count)); |
85 } | 86 } |
86 return samples; | 87 return samples; |
87 } | 88 } |
88 | 89 |
| 90 HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix( |
| 91 const std::string& query) const { |
| 92 EXPECT_TRUE(query.find('.') != std::string::npos) |
| 93 << "|query| ought to contain at least one period, to avoid matching too" |
| 94 << " many histograms."; |
| 95 |
| 96 // Find matches by using the prefix-matching logic built into GetSnapshot(). |
| 97 StatisticsRecorder::Histograms query_matches; |
| 98 StatisticsRecorder::GetSnapshot(query, &query_matches); |
| 99 |
| 100 CountsMap result; |
| 101 for (base::HistogramBase* histogram : query_matches) { |
| 102 scoped_ptr<HistogramSamples> new_samples = |
| 103 GetHistogramSamplesSinceCreation(histogram->histogram_name()); |
| 104 // Omit unchanged histograms from the result. |
| 105 if (new_samples->TotalCount()) { |
| 106 result[histogram->histogram_name()] = new_samples->TotalCount(); |
| 107 } |
| 108 } |
| 109 return result; |
| 110 } |
| 111 |
89 scoped_ptr<HistogramSamples> HistogramTester::GetHistogramSamplesSinceCreation( | 112 scoped_ptr<HistogramSamples> HistogramTester::GetHistogramSamplesSinceCreation( |
90 const std::string& histogram_name) { | 113 const std::string& histogram_name) const { |
91 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); | 114 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); |
92 if (!histogram) | 115 if (!histogram) |
93 return scoped_ptr<HistogramSamples>(); | 116 return scoped_ptr<HistogramSamples>(); |
94 scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples()); | 117 scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples()); |
95 HistogramSamples* named_original_samples = | 118 auto original_samples_it = histograms_snapshot_.find(histogram_name); |
96 histograms_snapshot_[histogram_name]; | 119 if (original_samples_it != histograms_snapshot_.end()) |
97 if (named_original_samples) | 120 named_samples->Subtract(*original_samples_it->second); |
98 named_samples->Subtract(*named_original_samples); | |
99 return named_samples.Pass(); | 121 return named_samples.Pass(); |
100 } | 122 } |
101 | 123 |
102 void HistogramTester::CheckBucketCount( | 124 void HistogramTester::CheckBucketCount( |
103 const std::string& name, | 125 const std::string& name, |
104 base::HistogramBase::Sample sample, | 126 base::HistogramBase::Sample sample, |
105 base::HistogramBase::Count expected_count, | 127 base::HistogramBase::Count expected_count, |
106 const base::HistogramSamples& samples) const { | 128 const base::HistogramSamples& samples) const { |
107 int actual_count = samples.GetCount(sample); | 129 int actual_count = samples.GetCount(sample); |
108 std::map<std::string, HistogramSamples*>::const_iterator histogram_data; | 130 std::map<std::string, HistogramSamples*>::const_iterator histogram_data; |
(...skipping 26 matching lines...) Expand all Loading... |
135 | 157 |
136 bool Bucket::operator==(const Bucket& other) const { | 158 bool Bucket::operator==(const Bucket& other) const { |
137 return min == other.min && count == other.count; | 159 return min == other.min && count == other.count; |
138 } | 160 } |
139 | 161 |
140 void PrintTo(const Bucket& bucket, std::ostream* os) { | 162 void PrintTo(const Bucket& bucket, std::ostream* os) { |
141 *os << "Bucket " << bucket.min << ": " << bucket.count; | 163 *os << "Bucket " << bucket.min << ": " << bucket.count; |
142 } | 164 } |
143 | 165 |
144 } // namespace base | 166 } // namespace base |
OLD | NEW |