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; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 HistogramBase::Sample sample; | 96 HistogramBase::Sample sample; |
96 HistogramBase::Count count; | 97 HistogramBase::Count count; |
97 it->Get(&sample, nullptr, &count); | 98 it->Get(&sample, nullptr, &count); |
98 samples.push_back(Bucket(sample, count)); | 99 samples.push_back(Bucket(sample, count)); |
99 } | 100 } |
100 } | 101 } |
101 return samples; | 102 return samples; |
102 } | 103 } |
103 | 104 |
104 HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix( | 105 HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix( |
105 const std::string& query) const { | 106 const std::string& query) const { |
Ilya Sherman
2017/01/12 20:32:41
nit: While you're fixing up this method, please up
pkalinnikov
2017/01/13 10:28:59
Done.
| |
106 EXPECT_TRUE(query.find('.') != std::string::npos) | 107 EXPECT_TRUE(query.find('.') != std::string::npos) |
107 << "|query| ought to contain at least one period, to avoid matching too" | 108 << "|query| ought to contain at least one period, to avoid matching too" |
108 << " many histograms."; | 109 << " many histograms."; |
109 | 110 |
110 // Find matches by using the prefix-matching logic built into GetSnapshot(). | 111 // Find matches by using the prefix-matching logic built into GetSnapshot(). |
Paweł Hajdan Jr.
2017/01/11 20:45:00
This suggests prefix-matching logic in GetSnapshot
Ilya Sherman
2017/01/12 20:32:41
This comment is not quite correct. StatisticsReco
pkalinnikov
2017/01/13 10:28:59
Updated the comment.
| |
111 StatisticsRecorder::Histograms query_matches; | 112 StatisticsRecorder::Histograms query_matches; |
112 StatisticsRecorder::GetSnapshot(query, &query_matches); | 113 StatisticsRecorder::GetSnapshot(query, &query_matches); |
113 | 114 |
114 CountsMap result; | 115 CountsMap result; |
115 for (base::HistogramBase* histogram : query_matches) { | 116 for (base::HistogramBase* histogram : query_matches) { |
Ilya Sherman
2017/01/12 20:32:41
Optional nit: While you're here, it would be nice
pkalinnikov
2017/01/13 10:28:59
Done here and throughout this file and its header.
| |
117 if (!StartsWith(histogram->histogram_name(), query, CompareCase::SENSITIVE)) | |
118 continue; | |
116 std::unique_ptr<HistogramSamples> new_samples = | 119 std::unique_ptr<HistogramSamples> new_samples = |
117 GetHistogramSamplesSinceCreation(histogram->histogram_name()); | 120 GetHistogramSamplesSinceCreation(histogram->histogram_name()); |
118 // Omit unchanged histograms from the result. | 121 // Omit unchanged histograms from the result. |
119 if (new_samples->TotalCount()) { | 122 if (new_samples->TotalCount()) { |
120 result[histogram->histogram_name()] = new_samples->TotalCount(); | 123 result[histogram->histogram_name()] = new_samples->TotalCount(); |
121 } | 124 } |
122 } | 125 } |
123 return result; | 126 return result; |
124 } | 127 } |
125 | 128 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 | 182 |
180 bool Bucket::operator==(const Bucket& other) const { | 183 bool Bucket::operator==(const Bucket& other) const { |
181 return min == other.min && count == other.count; | 184 return min == other.min && count == other.count; |
182 } | 185 } |
183 | 186 |
184 void PrintTo(const Bucket& bucket, std::ostream* os) { | 187 void PrintTo(const Bucket& bucket, std::ostream* os) { |
185 *os << "Bucket " << bucket.min << ": " << bucket.count; | 188 *os << "Bucket " << bucket.min << ": " << bucket.count; |
186 } | 189 } |
187 | 190 |
188 } // namespace base | 191 } // namespace base |
OLD | NEW |