OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "base/metrics/histogram_base.h" | 8 #include "base/metrics/histogram_base.h" |
9 #include "base/metrics/sparse_histogram.h" | 9 #include "base/metrics/sparse_histogram.h" |
10 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
11 #include "base/pickle.h" | 11 #include "base/pickle.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 class HistogramBaseTest : public testing::Test { | 16 class HistogramBaseTest : public testing::Test { |
17 protected: | 17 protected: |
18 HistogramBaseTest() { | 18 HistogramBaseTest() { |
19 // Each test will have a clean state (no Histogram / BucketRanges | 19 // Each test will have a clean state (no Histogram / BucketRanges |
20 // registered). | 20 // registered). |
21 statistics_recorder_ = NULL; | |
22 ResetStatisticsRecorder(); | 21 ResetStatisticsRecorder(); |
23 } | 22 } |
24 | 23 |
25 ~HistogramBaseTest() override { delete statistics_recorder_; } | 24 ~HistogramBaseTest() override { |
25 HistogramBase::report_histogram_ = nullptr; | |
26 delete statistics_recorder_; | |
27 } | |
26 | 28 |
27 void ResetStatisticsRecorder() { | 29 void ResetStatisticsRecorder() { |
28 delete statistics_recorder_; | 30 delete statistics_recorder_; |
29 statistics_recorder_ = new StatisticsRecorder(); | 31 statistics_recorder_ = new StatisticsRecorder(); |
Alexei Svitkine (slow)
2016/03/01 16:41:34
Nit: While you're changing this, can you make this
bcwhite
2016/03/02 19:14:19
StatisticsRecorder is explicit about what classes
Alexei Svitkine (slow)
2016/03/03 17:56:23
I think making the dtor public should be fine, sin
bcwhite
2016/03/07 13:39:25
Done.
| |
30 } | 32 } |
31 | 33 |
34 HistogramBase* GetCreationReportHistogram(StringPiece name) { | |
35 HistogramBase::EnableCreationReportHistogram(name); | |
36 return HistogramBase::report_histogram_; | |
37 } | |
38 | |
32 private: | 39 private: |
33 StatisticsRecorder* statistics_recorder_; | 40 StatisticsRecorder* statistics_recorder_ = nullptr; |
34 }; | 41 }; |
Alexei Svitkine (slow)
2016/03/01 16:41:34
Nit: Add DISALLOW_COPY_AND_ASSIGN().
bcwhite
2016/03/02 19:14:19
Done.
| |
35 | 42 |
36 TEST_F(HistogramBaseTest, DeserializeHistogram) { | 43 TEST_F(HistogramBaseTest, DeserializeHistogram) { |
37 HistogramBase* histogram = Histogram::FactoryGet( | 44 HistogramBase* histogram = Histogram::FactoryGet( |
38 "TestHistogram", 1, 1000, 10, | 45 "TestHistogram", 1, 1000, 10, |
39 (HistogramBase::kUmaTargetedHistogramFlag | | 46 (HistogramBase::kUmaTargetedHistogramFlag | |
40 HistogramBase::kIPCSerializationSourceFlag)); | 47 HistogramBase::kIPCSerializationSourceFlag)); |
41 | 48 |
42 Pickle pickle; | 49 Pickle pickle; |
43 ASSERT_TRUE(histogram->SerializeInfo(&pickle)); | 50 ASSERT_TRUE(histogram->SerializeInfo(&pickle)); |
44 | 51 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 ResetStatisticsRecorder(); | 152 ResetStatisticsRecorder(); |
146 | 153 |
147 PickleIterator iter2(pickle); | 154 PickleIterator iter2(pickle); |
148 deserialized = DeserializeHistogramInfo(&iter2); | 155 deserialized = DeserializeHistogramInfo(&iter2); |
149 EXPECT_TRUE(deserialized); | 156 EXPECT_TRUE(deserialized); |
150 EXPECT_NE(histogram, deserialized); | 157 EXPECT_NE(histogram, deserialized); |
151 EXPECT_EQ("TestHistogram", deserialized->histogram_name()); | 158 EXPECT_EQ("TestHistogram", deserialized->histogram_name()); |
152 EXPECT_EQ(0, deserialized->flags()); | 159 EXPECT_EQ(0, deserialized->flags()); |
153 } | 160 } |
154 | 161 |
162 TEST_F(HistogramBaseTest, CreationReportHistogram) { | |
163 // Enabled creation report. Itself is not included in the report. | |
164 HistogramBase* report = GetCreationReportHistogram("CreationReportTest"); | |
165 ASSERT_TRUE(report); | |
166 | |
167 std::vector<HistogramBase::Sample> ranges; | |
168 ranges.push_back(1); | |
169 ranges.push_back(2); | |
170 ranges.push_back(4); | |
171 ranges.push_back(8); | |
172 ranges.push_back(10); | |
173 | |
174 // Create all histogram types and verify counts. | |
175 Histogram::FactoryGet("CRH-Histogram", 1, 10, 5, 0); | |
176 LinearHistogram::FactoryGet("CRH-Linear", 1, 10, 5, 0); | |
177 BooleanHistogram::FactoryGet("CRH-Boolean", 0); | |
178 CustomHistogram::FactoryGet("CRH-Custom", ranges, 0); | |
179 SparseHistogram::FactoryGet("CRH-Sparse", 0); | |
180 | |
181 scoped_ptr<HistogramSamples> samples = report->SnapshotSamples(); | |
182 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_CREATED)); | |
183 EXPECT_EQ(5, samples->GetCount(HISTOGRAM_REPORT_HISTOGRAM_CREATED)); | |
184 EXPECT_EQ(0, samples->GetCount(HISTOGRAM_REPORT_HISTOGRAM_LOOKUP)); | |
185 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_TYPE_GENERAL)); | |
186 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_TYPE_LINEAR)); | |
187 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_TYPE_BOOLEAN)); | |
188 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_TYPE_CUSTOM)); | |
189 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_TYPE_SPARSE)); | |
190 | |
191 // Create all flag types and verify counts. | |
192 Histogram::FactoryGet("CRH-Histogram-UMA-Targeted", 1, 10, 5, | |
193 HistogramBase::kUmaTargetedHistogramFlag); | |
194 Histogram::FactoryGet("CRH-Histogram-UMA-Stability", 1, 10, 5, | |
195 HistogramBase::kUmaStabilityHistogramFlag); | |
196 SparseHistogram::FactoryGet("CRH-Sparse-UMA-Targeted", | |
197 HistogramBase::kUmaTargetedHistogramFlag); | |
198 SparseHistogram::FactoryGet("CRH-Sparse-UMA-Stability", | |
199 HistogramBase::kUmaStabilityHistogramFlag); | |
200 samples = report->SnapshotSamples(); | |
201 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_CREATED)); | |
202 EXPECT_EQ(9, samples->GetCount(HISTOGRAM_REPORT_HISTOGRAM_CREATED)); | |
203 EXPECT_EQ(0, samples->GetCount(HISTOGRAM_REPORT_HISTOGRAM_LOOKUP)); | |
204 EXPECT_EQ(2, samples->GetCount(HISTOGRAM_REPORT_FLAG_UMA_TARGETED)); | |
205 EXPECT_EQ(2, samples->GetCount(HISTOGRAM_REPORT_FLAG_UMA_STABILITY)); | |
206 | |
207 // Do lookup of existing histograms and verify counts. | |
208 Histogram::FactoryGet("CRH-Histogram", 1, 10, 5, 0); | |
209 LinearHistogram::FactoryGet("CRH-Linear", 1, 10, 5, 0); | |
210 BooleanHistogram::FactoryGet("CRH-Boolean", 0); | |
211 CustomHistogram::FactoryGet("CRH-Custom", ranges, 0); | |
212 SparseHistogram::FactoryGet("CRH-Sparse", 0); | |
213 samples = report->SnapshotSamples(); | |
214 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_CREATED)); | |
215 EXPECT_EQ(9, samples->GetCount(HISTOGRAM_REPORT_HISTOGRAM_CREATED)); | |
216 EXPECT_EQ(5, samples->GetCount(HISTOGRAM_REPORT_HISTOGRAM_LOOKUP)); | |
217 } | |
218 | |
155 } // namespace base | 219 } // namespace base |
OLD | NEW |