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
3 // found in the LICENSE file.
7 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/statistics_recorder.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 class StatisticsRecorderTest
: public testing::Test
{
16 virtual void SetUp() {
17 // Each test will have a clean state (no Histogram / BucketRanges
19 InitializeStatisticsRecorder();
22 virtual void TearDown() {
23 UninitializeStatisticsRecorder();
26 void InitializeStatisticsRecorder() {
27 statistics_recorder_
= new StatisticsRecorder();
30 void UninitializeStatisticsRecorder() {
31 delete statistics_recorder_
;
32 statistics_recorder_
= NULL
;
35 Histogram
* CreateHistogram(const std::string
& name
,
36 HistogramBase::Sample min
,
37 HistogramBase::Sample max
,
38 size_t bucket_count
) {
39 BucketRanges
* ranges
= new BucketRanges(bucket_count
+ 1);
40 Histogram::InitializeBucketRanges(min
, max
, ranges
);
41 const BucketRanges
* registered_ranges
=
42 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges
);
43 return new Histogram(name
, min
, max
, registered_ranges
);
46 void DeleteHistogram(HistogramBase
* histogram
) {
50 StatisticsRecorder
* statistics_recorder_
;
53 TEST_F(StatisticsRecorderTest
, NotInitialized
) {
54 UninitializeStatisticsRecorder();
56 ASSERT_FALSE(StatisticsRecorder::IsActive());
58 StatisticsRecorder::Histograms registered_histograms
;
59 std::vector
<const BucketRanges
*> registered_ranges
;
61 StatisticsRecorder::GetHistograms(®istered_histograms
);
62 EXPECT_EQ(0u, registered_histograms
.size());
64 Histogram
* histogram
= CreateHistogram("TestHistogram", 1, 1000, 10);
66 // When StatisticsRecorder is not initialized, register is a noop.
68 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram
));
69 // Manually delete histogram that was not registered.
70 DeleteHistogram(histogram
);
72 // RegisterOrDeleteDuplicateRanges is a no-op.
73 BucketRanges
* ranges
= new BucketRanges(3);;
74 ranges
->ResetChecksum();
76 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges
));
77 StatisticsRecorder::GetBucketRanges(®istered_ranges
);
78 EXPECT_EQ(0u, registered_ranges
.size());
81 TEST_F(StatisticsRecorderTest
, RegisterBucketRanges
) {
82 std::vector
<const BucketRanges
*> registered_ranges
;
84 BucketRanges
* ranges1
= new BucketRanges(3);;
85 ranges1
->ResetChecksum();
86 BucketRanges
* ranges2
= new BucketRanges(4);;
87 ranges2
->ResetChecksum();
89 // Register new ranges.
91 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges1
));
93 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges2
));
94 StatisticsRecorder::GetBucketRanges(®istered_ranges
);
95 ASSERT_EQ(2u, registered_ranges
.size());
97 // Register some ranges again.
99 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges1
));
100 registered_ranges
.clear();
101 StatisticsRecorder::GetBucketRanges(®istered_ranges
);
102 ASSERT_EQ(2u, registered_ranges
.size());
103 // Make sure the ranges is still the one we know.
104 ASSERT_EQ(3u, ranges1
->size());
105 EXPECT_EQ(0, ranges1
->range(0));
106 EXPECT_EQ(0, ranges1
->range(1));
107 EXPECT_EQ(0, ranges1
->range(2));
109 // Register ranges with same values.
110 BucketRanges
* ranges3
= new BucketRanges(3);;
111 ranges3
->ResetChecksum();
112 EXPECT_EQ(ranges1
, // returning ranges1
113 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges3
));
114 registered_ranges
.clear();
115 StatisticsRecorder::GetBucketRanges(®istered_ranges
);
116 ASSERT_EQ(2u, registered_ranges
.size());
119 TEST_F(StatisticsRecorderTest
, RegisterHistogram
) {
120 // Create a Histogram that was not registered.
121 Histogram
* histogram
= CreateHistogram("TestHistogram", 1, 1000, 10);
123 StatisticsRecorder::Histograms registered_histograms
;
124 StatisticsRecorder::GetHistograms(®istered_histograms
);
125 EXPECT_EQ(0u, registered_histograms
.size());
127 // Register the Histogram.
129 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram
));
130 StatisticsRecorder::GetHistograms(®istered_histograms
);
131 EXPECT_EQ(1u, registered_histograms
.size());
133 // Register the same Histogram again.
135 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram
));
136 registered_histograms
.clear();
137 StatisticsRecorder::GetHistograms(®istered_histograms
);
138 EXPECT_EQ(1u, registered_histograms
.size());
141 TEST_F(StatisticsRecorderTest
, FindHistogram
) {
142 HistogramBase
* histogram1
= Histogram::FactoryGet(
143 "TestHistogram1", 1, 1000, 10, HistogramBase::kNoFlags
);
144 HistogramBase
* histogram2
= Histogram::FactoryGet(
145 "TestHistogram2", 1, 1000, 10, HistogramBase::kNoFlags
);
147 EXPECT_EQ(histogram1
, StatisticsRecorder::FindHistogram("TestHistogram1"));
148 EXPECT_EQ(histogram2
, StatisticsRecorder::FindHistogram("TestHistogram2"));
149 EXPECT_TRUE(StatisticsRecorder::FindHistogram("TestHistogram") == NULL
);
152 TEST_F(StatisticsRecorderTest
, GetSnapshot
) {
153 Histogram::FactoryGet("TestHistogram1", 1, 1000, 10, Histogram::kNoFlags
);
154 Histogram::FactoryGet("TestHistogram2", 1, 1000, 10, Histogram::kNoFlags
);
155 Histogram::FactoryGet("TestHistogram3", 1, 1000, 10, Histogram::kNoFlags
);
157 StatisticsRecorder::Histograms snapshot
;
158 StatisticsRecorder::GetSnapshot("Test", &snapshot
);
159 EXPECT_EQ(3u, snapshot
.size());
162 StatisticsRecorder::GetSnapshot("1", &snapshot
);
163 EXPECT_EQ(1u, snapshot
.size());
166 StatisticsRecorder::GetSnapshot("hello", &snapshot
);
167 EXPECT_EQ(0u, snapshot
.size());
170 TEST_F(StatisticsRecorderTest
, RegisterHistogramWithFactoryGet
) {
171 StatisticsRecorder::Histograms registered_histograms
;
173 StatisticsRecorder::GetHistograms(®istered_histograms
);
174 ASSERT_EQ(0u, registered_histograms
.size());
176 // Create a histogram.
177 HistogramBase
* histogram
= Histogram::FactoryGet(
178 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags
);
179 registered_histograms
.clear();
180 StatisticsRecorder::GetHistograms(®istered_histograms
);
181 EXPECT_EQ(1u, registered_histograms
.size());
183 // Get an existing histogram.
184 HistogramBase
* histogram2
= Histogram::FactoryGet(
185 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags
);
186 registered_histograms
.clear();
187 StatisticsRecorder::GetHistograms(®istered_histograms
);
188 EXPECT_EQ(1u, registered_histograms
.size());
189 EXPECT_EQ(histogram
, histogram2
);
191 // Create a LinearHistogram.
192 histogram
= LinearHistogram::FactoryGet(
193 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags
);
194 registered_histograms
.clear();
195 StatisticsRecorder::GetHistograms(®istered_histograms
);
196 EXPECT_EQ(2u, registered_histograms
.size());
198 // Create a BooleanHistogram.
199 histogram
= BooleanHistogram::FactoryGet(
200 "TestBooleanHistogram", HistogramBase::kNoFlags
);
201 registered_histograms
.clear();
202 StatisticsRecorder::GetHistograms(®istered_histograms
);
203 EXPECT_EQ(3u, registered_histograms
.size());
205 // Create a CustomHistogram.
206 std::vector
<int> custom_ranges
;
207 custom_ranges
.push_back(1);
208 custom_ranges
.push_back(5);
209 histogram
= CustomHistogram::FactoryGet(
210 "TestCustomHistogram", custom_ranges
, HistogramBase::kNoFlags
);
211 registered_histograms
.clear();
212 StatisticsRecorder::GetHistograms(®istered_histograms
);
213 EXPECT_EQ(4u, registered_histograms
.size());
216 TEST_F(StatisticsRecorderTest
, RegisterHistogramWithMacros
) {
217 StatisticsRecorder::Histograms registered_histograms
;
219 HistogramBase
* histogram
= Histogram::FactoryGet(
220 "TestHistogramCounts", 1, 1000000, 50, HistogramBase::kNoFlags
);
222 // The histogram we got from macro is the same as from FactoryGet.
223 HISTOGRAM_COUNTS("TestHistogramCounts", 30);
224 registered_histograms
.clear();
225 StatisticsRecorder::GetHistograms(®istered_histograms
);
226 ASSERT_EQ(1u, registered_histograms
.size());
227 EXPECT_EQ(histogram
, registered_histograms
[0]);
229 HISTOGRAM_TIMES("TestHistogramTimes", TimeDelta::FromDays(1));
230 HISTOGRAM_ENUMERATION("TestHistogramEnumeration", 20, 200);
232 registered_histograms
.clear();
233 StatisticsRecorder::GetHistograms(®istered_histograms
);
234 EXPECT_EQ(3u, registered_histograms
.size());
236 // Debugging only macros.
237 DHISTOGRAM_TIMES("TestHistogramDebugTimes", TimeDelta::FromDays(1));
238 DHISTOGRAM_COUNTS("TestHistogramDebugCounts", 30);
239 registered_histograms
.clear();
240 StatisticsRecorder::GetHistograms(®istered_histograms
);
242 EXPECT_EQ(5u, registered_histograms
.size());
244 EXPECT_EQ(3u, registered_histograms
.size());
248 TEST_F(StatisticsRecorderTest
, BucketRangesSharing
) {
249 std::vector
<const BucketRanges
*> ranges
;
250 StatisticsRecorder::GetBucketRanges(&ranges
);
251 EXPECT_EQ(0u, ranges
.size());
253 Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags
);
254 Histogram::FactoryGet("Histogram2", 1, 64, 8, HistogramBase::kNoFlags
);
256 StatisticsRecorder::GetBucketRanges(&ranges
);
257 EXPECT_EQ(1u, ranges
.size());
259 Histogram::FactoryGet("Histogram3", 1, 64, 16, HistogramBase::kNoFlags
);
262 StatisticsRecorder::GetBucketRanges(&ranges
);
263 EXPECT_EQ(2u, ranges
.size());