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 25 matching lines...) Expand all Loading... |
36 void HistogramTester::ExpectUniqueSample( | 36 void HistogramTester::ExpectUniqueSample( |
37 const std::string& name, | 37 const std::string& name, |
38 base::HistogramBase::Sample sample, | 38 base::HistogramBase::Sample sample, |
39 base::HistogramBase::Count expected_count) const { | 39 base::HistogramBase::Count expected_count) const { |
40 base::HistogramBase* histogram = | 40 base::HistogramBase* histogram = |
41 base::StatisticsRecorder::FindHistogram(name); | 41 base::StatisticsRecorder::FindHistogram(name); |
42 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram) | 42 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram) |
43 << "Histogram \"" << name << "\" does not exist."; | 43 << "Histogram \"" << name << "\" does not exist."; |
44 | 44 |
45 if (histogram) { | 45 if (histogram) { |
46 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); | 46 std::unique_ptr<base::HistogramSamples> samples( |
| 47 histogram->SnapshotSamples()); |
47 CheckBucketCount(name, sample, expected_count, *samples); | 48 CheckBucketCount(name, sample, expected_count, *samples); |
48 CheckTotalCount(name, expected_count, *samples); | 49 CheckTotalCount(name, expected_count, *samples); |
49 } | 50 } |
50 } | 51 } |
51 | 52 |
52 void HistogramTester::ExpectBucketCount( | 53 void HistogramTester::ExpectBucketCount( |
53 const std::string& name, | 54 const std::string& name, |
54 base::HistogramBase::Sample sample, | 55 base::HistogramBase::Sample sample, |
55 base::HistogramBase::Count expected_count) const { | 56 base::HistogramBase::Count expected_count) const { |
56 base::HistogramBase* histogram = | 57 base::HistogramBase* histogram = |
57 base::StatisticsRecorder::FindHistogram(name); | 58 base::StatisticsRecorder::FindHistogram(name); |
58 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram) | 59 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram) |
59 << "Histogram \"" << name << "\" does not exist."; | 60 << "Histogram \"" << name << "\" does not exist."; |
60 | 61 |
61 if (histogram) { | 62 if (histogram) { |
62 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); | 63 std::unique_ptr<base::HistogramSamples> samples( |
| 64 histogram->SnapshotSamples()); |
63 CheckBucketCount(name, sample, expected_count, *samples); | 65 CheckBucketCount(name, sample, expected_count, *samples); |
64 } | 66 } |
65 } | 67 } |
66 | 68 |
67 void HistogramTester::ExpectTotalCount(const std::string& name, | 69 void HistogramTester::ExpectTotalCount(const std::string& name, |
68 base::HistogramBase::Count count) const { | 70 base::HistogramBase::Count count) const { |
69 base::HistogramBase* histogram = | 71 base::HistogramBase* histogram = |
70 base::StatisticsRecorder::FindHistogram(name); | 72 base::StatisticsRecorder::FindHistogram(name); |
71 if (histogram) { | 73 if (histogram) { |
72 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); | 74 std::unique_ptr<base::HistogramSamples> samples( |
| 75 histogram->SnapshotSamples()); |
73 CheckTotalCount(name, count, *samples); | 76 CheckTotalCount(name, count, *samples); |
74 } else { | 77 } else { |
75 // No histogram means there were zero samples. | 78 // No histogram means there were zero samples. |
76 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; | 79 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; |
77 } | 80 } |
78 } | 81 } |
79 | 82 |
80 std::vector<Bucket> HistogramTester::GetAllSamples( | 83 std::vector<Bucket> HistogramTester::GetAllSamples( |
81 const std::string& name) const { | 84 const std::string& name) const { |
82 std::vector<Bucket> samples; | 85 std::vector<Bucket> samples; |
83 scoped_ptr<HistogramSamples> snapshot = | 86 std::unique_ptr<HistogramSamples> snapshot = |
84 GetHistogramSamplesSinceCreation(name); | 87 GetHistogramSamplesSinceCreation(name); |
85 if (snapshot) { | 88 if (snapshot) { |
86 for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) { | 89 for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) { |
87 HistogramBase::Sample sample; | 90 HistogramBase::Sample sample; |
88 HistogramBase::Count count; | 91 HistogramBase::Count count; |
89 it->Get(&sample, nullptr, &count); | 92 it->Get(&sample, nullptr, &count); |
90 samples.push_back(Bucket(sample, count)); | 93 samples.push_back(Bucket(sample, count)); |
91 } | 94 } |
92 } | 95 } |
93 return samples; | 96 return samples; |
94 } | 97 } |
95 | 98 |
96 HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix( | 99 HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix( |
97 const std::string& query) const { | 100 const std::string& query) const { |
98 EXPECT_TRUE(query.find('.') != std::string::npos) | 101 EXPECT_TRUE(query.find('.') != std::string::npos) |
99 << "|query| ought to contain at least one period, to avoid matching too" | 102 << "|query| ought to contain at least one period, to avoid matching too" |
100 << " many histograms."; | 103 << " many histograms."; |
101 | 104 |
102 // Find matches by using the prefix-matching logic built into GetSnapshot(). | 105 // Find matches by using the prefix-matching logic built into GetSnapshot(). |
103 StatisticsRecorder::Histograms query_matches; | 106 StatisticsRecorder::Histograms query_matches; |
104 StatisticsRecorder::GetSnapshot(query, &query_matches); | 107 StatisticsRecorder::GetSnapshot(query, &query_matches); |
105 | 108 |
106 CountsMap result; | 109 CountsMap result; |
107 for (base::HistogramBase* histogram : query_matches) { | 110 for (base::HistogramBase* histogram : query_matches) { |
108 scoped_ptr<HistogramSamples> new_samples = | 111 std::unique_ptr<HistogramSamples> new_samples = |
109 GetHistogramSamplesSinceCreation(histogram->histogram_name()); | 112 GetHistogramSamplesSinceCreation(histogram->histogram_name()); |
110 // Omit unchanged histograms from the result. | 113 // Omit unchanged histograms from the result. |
111 if (new_samples->TotalCount()) { | 114 if (new_samples->TotalCount()) { |
112 result[histogram->histogram_name()] = new_samples->TotalCount(); | 115 result[histogram->histogram_name()] = new_samples->TotalCount(); |
113 } | 116 } |
114 } | 117 } |
115 return result; | 118 return result; |
116 } | 119 } |
117 | 120 |
118 scoped_ptr<HistogramSamples> HistogramTester::GetHistogramSamplesSinceCreation( | 121 std::unique_ptr<HistogramSamples> |
| 122 HistogramTester::GetHistogramSamplesSinceCreation( |
119 const std::string& histogram_name) const { | 123 const std::string& histogram_name) const { |
120 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); | 124 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); |
121 // Whether the histogram exists or not may not depend on the current test | 125 // Whether the histogram exists or not may not depend on the current test |
122 // calling this method, but rather on which tests ran before and possibly | 126 // calling this method, but rather on which tests ran before and possibly |
123 // generated a histogram or not (see http://crbug.com/473689). To provide a | 127 // generated a histogram or not (see http://crbug.com/473689). To provide a |
124 // response which is independent of the previously run tests, this method | 128 // response which is independent of the previously run tests, this method |
125 // creates empty samples in the absence of the histogram, rather than | 129 // creates empty samples in the absence of the histogram, rather than |
126 // returning null. | 130 // returning null. |
127 if (!histogram) { | 131 if (!histogram) { |
128 return scoped_ptr<HistogramSamples>( | 132 return std::unique_ptr<HistogramSamples>( |
129 new SampleMap(HashMetricName(histogram_name))); | 133 new SampleMap(HashMetricName(histogram_name))); |
130 } | 134 } |
131 scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples()); | 135 std::unique_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples()); |
132 auto original_samples_it = histograms_snapshot_.find(histogram_name); | 136 auto original_samples_it = histograms_snapshot_.find(histogram_name); |
133 if (original_samples_it != histograms_snapshot_.end()) | 137 if (original_samples_it != histograms_snapshot_.end()) |
134 named_samples->Subtract(*original_samples_it->second); | 138 named_samples->Subtract(*original_samples_it->second); |
135 return named_samples; | 139 return named_samples; |
136 } | 140 } |
137 | 141 |
138 void HistogramTester::CheckBucketCount( | 142 void HistogramTester::CheckBucketCount( |
139 const std::string& name, | 143 const std::string& name, |
140 base::HistogramBase::Sample sample, | 144 base::HistogramBase::Sample sample, |
141 base::HistogramBase::Count expected_count, | 145 base::HistogramBase::Count expected_count, |
(...skipping 29 matching lines...) Expand all Loading... |
171 | 175 |
172 bool Bucket::operator==(const Bucket& other) const { | 176 bool Bucket::operator==(const Bucket& other) const { |
173 return min == other.min && count == other.count; | 177 return min == other.min && count == other.count; |
174 } | 178 } |
175 | 179 |
176 void PrintTo(const Bucket& bucket, std::ostream* os) { | 180 void PrintTo(const Bucket& bucket, std::ostream* os) { |
177 *os << "Bucket " << bucket.min << ": " << bucket.count; | 181 *os << "Bucket " << bucket.min << ": " << bucket.count; |
178 } | 182 } |
179 | 183 |
180 } // namespace base | 184 } // namespace base |
OLD | NEW |