| 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" |
| 11 #include "base/metrics/metrics_hashes.h" | 11 #include "base/metrics/metrics_hashes.h" |
| 12 #include "base/metrics/sample_map.h" | 12 #include "base/metrics/sample_map.h" |
| 13 #include "base/metrics/statistics_recorder.h" | 13 #include "base/metrics/statistics_recorder.h" |
| 14 #include "base/strings/string_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 | 18 |
| 18 HistogramTester::HistogramTester() { | 19 HistogramTester::HistogramTester() { |
| 19 StatisticsRecorder::Initialize(); // Safe to call multiple times. | 20 StatisticsRecorder::Initialize(); // Safe to call multiple times. |
| 20 | 21 |
| 21 // Record any histogram data that exists when the object is created so it can | 22 // Record any histogram data that exists when the object is created so it can |
| 22 // be subtracted later. | 23 // be subtracted later. |
| 23 StatisticsRecorder::Histograms histograms; | 24 StatisticsRecorder::Histograms histograms; |
| 24 StatisticsRecorder::GetSnapshot(std::string(), &histograms); | 25 StatisticsRecorder::GetSnapshot(std::string(), &histograms); |
| 25 for (const auto& histogram : histograms) { | 26 for (const auto& histogram : histograms) { |
| 26 histograms_snapshot_[histogram->histogram_name()] = | 27 histograms_snapshot_[histogram->histogram_name()] = |
| 27 histogram->SnapshotSamples(); | 28 histogram->SnapshotSamples(); |
| 28 } | 29 } |
| 29 } | 30 } |
| 30 | 31 |
| 31 HistogramTester::~HistogramTester() { | 32 HistogramTester::~HistogramTester() { |
| 32 } | 33 } |
| 33 | 34 |
| 34 void HistogramTester::ExpectUniqueSample( | 35 void HistogramTester::ExpectUniqueSample( |
| 35 const std::string& name, | 36 const std::string& name, |
| 36 base::HistogramBase::Sample sample, | 37 HistogramBase::Sample sample, |
| 37 base::HistogramBase::Count expected_count) const { | 38 HistogramBase::Count expected_count) const { |
| 38 base::HistogramBase* histogram = | 39 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| 39 base::StatisticsRecorder::FindHistogram(name); | |
| 40 EXPECT_NE(nullptr, histogram) << "Histogram \"" << name | 40 EXPECT_NE(nullptr, histogram) << "Histogram \"" << name |
| 41 << "\" does not exist."; | 41 << "\" does not exist."; |
| 42 | 42 |
| 43 if (histogram) { | 43 if (histogram) { |
| 44 std::unique_ptr<base::HistogramSamples> samples = | 44 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
| 45 histogram->SnapshotSamples(); | |
| 46 CheckBucketCount(name, sample, expected_count, *samples); | 45 CheckBucketCount(name, sample, expected_count, *samples); |
| 47 CheckTotalCount(name, expected_count, *samples); | 46 CheckTotalCount(name, expected_count, *samples); |
| 48 } | 47 } |
| 49 } | 48 } |
| 50 | 49 |
| 51 void HistogramTester::ExpectBucketCount( | 50 void HistogramTester::ExpectBucketCount( |
| 52 const std::string& name, | 51 const std::string& name, |
| 53 base::HistogramBase::Sample sample, | 52 HistogramBase::Sample sample, |
| 54 base::HistogramBase::Count expected_count) const { | 53 HistogramBase::Count expected_count) const { |
| 55 base::HistogramBase* histogram = | 54 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| 56 base::StatisticsRecorder::FindHistogram(name); | |
| 57 EXPECT_NE(nullptr, histogram) << "Histogram \"" << name | 55 EXPECT_NE(nullptr, histogram) << "Histogram \"" << name |
| 58 << "\" does not exist."; | 56 << "\" does not exist."; |
| 59 | 57 |
| 60 if (histogram) { | 58 if (histogram) { |
| 61 std::unique_ptr<base::HistogramSamples> samples = | 59 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
| 62 histogram->SnapshotSamples(); | |
| 63 CheckBucketCount(name, sample, expected_count, *samples); | 60 CheckBucketCount(name, sample, expected_count, *samples); |
| 64 } | 61 } |
| 65 } | 62 } |
| 66 | 63 |
| 67 void HistogramTester::ExpectTotalCount(const std::string& name, | 64 void HistogramTester::ExpectTotalCount(const std::string& name, |
| 68 base::HistogramBase::Count count) const { | 65 HistogramBase::Count count) const { |
| 69 base::HistogramBase* histogram = | 66 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| 70 base::StatisticsRecorder::FindHistogram(name); | |
| 71 if (histogram) { | 67 if (histogram) { |
| 72 std::unique_ptr<base::HistogramSamples> samples = | 68 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
| 73 histogram->SnapshotSamples(); | |
| 74 CheckTotalCount(name, count, *samples); | 69 CheckTotalCount(name, count, *samples); |
| 75 } else { | 70 } else { |
| 76 // No histogram means there were zero samples. | 71 // No histogram means there were zero samples. |
| 77 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; | 72 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; |
| 78 } | 73 } |
| 79 } | 74 } |
| 80 | 75 |
| 81 void HistogramTester::ExpectTimeBucketCount( | 76 void HistogramTester::ExpectTimeBucketCount(const std::string& name, |
| 82 const std::string& name, | 77 TimeDelta sample, |
| 83 base::TimeDelta sample, | 78 HistogramBase::Count count) const { |
| 84 base::HistogramBase::Count count) const { | |
| 85 ExpectBucketCount(name, sample.InMilliseconds(), count); | 79 ExpectBucketCount(name, sample.InMilliseconds(), count); |
| 86 } | 80 } |
| 87 | 81 |
| 88 std::vector<Bucket> HistogramTester::GetAllSamples( | 82 std::vector<Bucket> HistogramTester::GetAllSamples( |
| 89 const std::string& name) const { | 83 const std::string& name) const { |
| 90 std::vector<Bucket> samples; | 84 std::vector<Bucket> samples; |
| 91 std::unique_ptr<HistogramSamples> snapshot = | 85 std::unique_ptr<HistogramSamples> snapshot = |
| 92 GetHistogramSamplesSinceCreation(name); | 86 GetHistogramSamplesSinceCreation(name); |
| 93 if (snapshot) { | 87 if (snapshot) { |
| 94 for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) { | 88 for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) { |
| 95 HistogramBase::Sample sample; | 89 HistogramBase::Sample sample; |
| 96 HistogramBase::Count count; | 90 HistogramBase::Count count; |
| 97 it->Get(&sample, nullptr, &count); | 91 it->Get(&sample, nullptr, &count); |
| 98 samples.push_back(Bucket(sample, count)); | 92 samples.push_back(Bucket(sample, count)); |
| 99 } | 93 } |
| 100 } | 94 } |
| 101 return samples; | 95 return samples; |
| 102 } | 96 } |
| 103 | 97 |
| 104 HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix( | 98 HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix( |
| 105 const std::string& query) const { | 99 const std::string& prefix) const { |
| 106 EXPECT_TRUE(query.find('.') != std::string::npos) | 100 EXPECT_TRUE(prefix.find('.') != std::string::npos) |
| 107 << "|query| ought to contain at least one period, to avoid matching too" | 101 << "|prefix| ought to contain at least one period, to avoid matching too" |
| 108 << " many histograms."; | 102 << " many histograms."; |
| 109 | 103 |
| 110 // Find matches by using the prefix-matching logic built into GetSnapshot(). | 104 // Find candidate matches by using the logic built into GetSnapshot(). |
| 111 StatisticsRecorder::Histograms query_matches; | 105 StatisticsRecorder::Histograms candidate_matches; |
| 112 StatisticsRecorder::GetSnapshot(query, &query_matches); | 106 StatisticsRecorder::GetSnapshot(prefix, &candidate_matches); |
| 113 | 107 |
| 114 CountsMap result; | 108 CountsMap result; |
| 115 for (base::HistogramBase* histogram : query_matches) { | 109 for (HistogramBase* histogram : candidate_matches) { |
| 110 if (!StartsWith(histogram->histogram_name(), prefix, |
| 111 CompareCase::SENSITIVE)) { |
| 112 continue; |
| 113 } |
| 116 std::unique_ptr<HistogramSamples> new_samples = | 114 std::unique_ptr<HistogramSamples> new_samples = |
| 117 GetHistogramSamplesSinceCreation(histogram->histogram_name()); | 115 GetHistogramSamplesSinceCreation(histogram->histogram_name()); |
| 118 // Omit unchanged histograms from the result. | 116 // Omit unchanged histograms from the result. |
| 119 if (new_samples->TotalCount()) { | 117 if (new_samples->TotalCount()) { |
| 120 result[histogram->histogram_name()] = new_samples->TotalCount(); | 118 result[histogram->histogram_name()] = new_samples->TotalCount(); |
| 121 } | 119 } |
| 122 } | 120 } |
| 123 return result; | 121 return result; |
| 124 } | 122 } |
| 125 | 123 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 138 new SampleMap(HashMetricName(histogram_name))); | 136 new SampleMap(HashMetricName(histogram_name))); |
| 139 } | 137 } |
| 140 std::unique_ptr<HistogramSamples> named_samples = | 138 std::unique_ptr<HistogramSamples> named_samples = |
| 141 histogram->SnapshotSamples(); | 139 histogram->SnapshotSamples(); |
| 142 auto original_samples_it = histograms_snapshot_.find(histogram_name); | 140 auto original_samples_it = histograms_snapshot_.find(histogram_name); |
| 143 if (original_samples_it != histograms_snapshot_.end()) | 141 if (original_samples_it != histograms_snapshot_.end()) |
| 144 named_samples->Subtract(*original_samples_it->second.get()); | 142 named_samples->Subtract(*original_samples_it->second.get()); |
| 145 return named_samples; | 143 return named_samples; |
| 146 } | 144 } |
| 147 | 145 |
| 148 void HistogramTester::CheckBucketCount( | 146 void HistogramTester::CheckBucketCount(const std::string& name, |
| 149 const std::string& name, | 147 HistogramBase::Sample sample, |
| 150 base::HistogramBase::Sample sample, | 148 HistogramBase::Count expected_count, |
| 151 base::HistogramBase::Count expected_count, | 149 const HistogramSamples& samples) const { |
| 152 const base::HistogramSamples& samples) const { | |
| 153 int actual_count = samples.GetCount(sample); | 150 int actual_count = samples.GetCount(sample); |
| 154 auto histogram_data = histograms_snapshot_.find(name); | 151 auto histogram_data = histograms_snapshot_.find(name); |
| 155 if (histogram_data != histograms_snapshot_.end()) | 152 if (histogram_data != histograms_snapshot_.end()) |
| 156 actual_count -= histogram_data->second->GetCount(sample); | 153 actual_count -= histogram_data->second->GetCount(sample); |
| 157 | 154 |
| 158 EXPECT_EQ(expected_count, actual_count) | 155 EXPECT_EQ(expected_count, actual_count) |
| 159 << "Histogram \"" << name | 156 << "Histogram \"" << name |
| 160 << "\" does not have the right number of samples (" << expected_count | 157 << "\" does not have the right number of samples (" << expected_count |
| 161 << ") in the expected bucket (" << sample << "). It has (" << actual_count | 158 << ") in the expected bucket (" << sample << "). It has (" << actual_count |
| 162 << ")."; | 159 << ")."; |
| 163 } | 160 } |
| 164 | 161 |
| 165 void HistogramTester::CheckTotalCount( | 162 void HistogramTester::CheckTotalCount(const std::string& name, |
| 166 const std::string& name, | 163 HistogramBase::Count expected_count, |
| 167 base::HistogramBase::Count expected_count, | 164 const HistogramSamples& samples) const { |
| 168 const base::HistogramSamples& samples) const { | |
| 169 int actual_count = samples.TotalCount(); | 165 int actual_count = samples.TotalCount(); |
| 170 auto histogram_data = histograms_snapshot_.find(name); | 166 auto histogram_data = histograms_snapshot_.find(name); |
| 171 if (histogram_data != histograms_snapshot_.end()) | 167 if (histogram_data != histograms_snapshot_.end()) |
| 172 actual_count -= histogram_data->second->TotalCount(); | 168 actual_count -= histogram_data->second->TotalCount(); |
| 173 | 169 |
| 174 EXPECT_EQ(expected_count, actual_count) | 170 EXPECT_EQ(expected_count, actual_count) |
| 175 << "Histogram \"" << name | 171 << "Histogram \"" << name |
| 176 << "\" does not have the right total number of samples (" | 172 << "\" does not have the right total number of samples (" |
| 177 << expected_count << "). It has (" << actual_count << ")."; | 173 << expected_count << "). It has (" << actual_count << ")."; |
| 178 } | 174 } |
| 179 | 175 |
| 180 bool Bucket::operator==(const Bucket& other) const { | 176 bool Bucket::operator==(const Bucket& other) const { |
| 181 return min == other.min && count == other.count; | 177 return min == other.min && count == other.count; |
| 182 } | 178 } |
| 183 | 179 |
| 184 void PrintTo(const Bucket& bucket, std::ostream* os) { | 180 void PrintTo(const Bucket& bucket, std::ostream* os) { |
| 185 *os << "Bucket " << bucket.min << ": " << bucket.count; | 181 *os << "Bucket " << bucket.min << ": " << bucket.count; |
| 186 } | 182 } |
| 187 | 183 |
| 188 } // namespace base | 184 } // namespace base |
| OLD | NEW |