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/json/json_reader.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/metrics/histogram.h"
10 #include "base/metrics/statistics_recorder.h"
11 #include "base/values.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 class StatisticsRecorderTest
: public testing::Test
{
18 virtual void SetUp() {
19 // Each test will have a clean state (no Histogram / BucketRanges
21 InitializeStatisticsRecorder();
24 virtual void TearDown() {
25 UninitializeStatisticsRecorder();
28 void InitializeStatisticsRecorder() {
29 statistics_recorder_
= new StatisticsRecorder();
32 void UninitializeStatisticsRecorder() {
33 delete statistics_recorder_
;
34 statistics_recorder_
= NULL
;
37 Histogram
* CreateHistogram(const std::string
& name
,
38 HistogramBase::Sample min
,
39 HistogramBase::Sample max
,
40 size_t bucket_count
) {
41 BucketRanges
* ranges
= new BucketRanges(bucket_count
+ 1);
42 Histogram::InitializeBucketRanges(min
, max
, ranges
);
43 const BucketRanges
* registered_ranges
=
44 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges
);
45 return new Histogram(name
, min
, max
, registered_ranges
);
48 void DeleteHistogram(HistogramBase
* histogram
) {
52 StatisticsRecorder
* statistics_recorder_
;
55 TEST_F(StatisticsRecorderTest
, NotInitialized
) {
56 UninitializeStatisticsRecorder();
58 ASSERT_FALSE(StatisticsRecorder::IsActive());
60 StatisticsRecorder::Histograms registered_histograms
;
61 std::vector
<const BucketRanges
*> registered_ranges
;
63 StatisticsRecorder::GetHistograms(®istered_histograms
);
64 EXPECT_EQ(0u, registered_histograms
.size());
66 Histogram
* histogram
= CreateHistogram("TestHistogram", 1, 1000, 10);
68 // When StatisticsRecorder is not initialized, register is a noop.
70 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram
));
71 // Manually delete histogram that was not registered.
72 DeleteHistogram(histogram
);
74 // RegisterOrDeleteDuplicateRanges is a no-op.
75 BucketRanges
* ranges
= new BucketRanges(3);;
76 ranges
->ResetChecksum();
78 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges
));
79 StatisticsRecorder::GetBucketRanges(®istered_ranges
);
80 EXPECT_EQ(0u, registered_ranges
.size());
83 TEST_F(StatisticsRecorderTest
, RegisterBucketRanges
) {
84 std::vector
<const BucketRanges
*> registered_ranges
;
86 BucketRanges
* ranges1
= new BucketRanges(3);;
87 ranges1
->ResetChecksum();
88 BucketRanges
* ranges2
= new BucketRanges(4);;
89 ranges2
->ResetChecksum();
91 // Register new ranges.
93 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges1
));
95 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges2
));
96 StatisticsRecorder::GetBucketRanges(®istered_ranges
);
97 ASSERT_EQ(2u, registered_ranges
.size());
99 // Register some ranges again.
101 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges1
));
102 registered_ranges
.clear();
103 StatisticsRecorder::GetBucketRanges(®istered_ranges
);
104 ASSERT_EQ(2u, registered_ranges
.size());
105 // Make sure the ranges is still the one we know.
106 ASSERT_EQ(3u, ranges1
->size());
107 EXPECT_EQ(0, ranges1
->range(0));
108 EXPECT_EQ(0, ranges1
->range(1));
109 EXPECT_EQ(0, ranges1
->range(2));
111 // Register ranges with same values.
112 BucketRanges
* ranges3
= new BucketRanges(3);;
113 ranges3
->ResetChecksum();
114 EXPECT_EQ(ranges1
, // returning ranges1
115 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges3
));
116 registered_ranges
.clear();
117 StatisticsRecorder::GetBucketRanges(®istered_ranges
);
118 ASSERT_EQ(2u, registered_ranges
.size());
121 TEST_F(StatisticsRecorderTest
, RegisterHistogram
) {
122 // Create a Histogram that was not registered.
123 Histogram
* histogram
= CreateHistogram("TestHistogram", 1, 1000, 10);
125 StatisticsRecorder::Histograms registered_histograms
;
126 StatisticsRecorder::GetHistograms(®istered_histograms
);
127 EXPECT_EQ(0u, registered_histograms
.size());
129 // Register the Histogram.
131 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram
));
132 StatisticsRecorder::GetHistograms(®istered_histograms
);
133 EXPECT_EQ(1u, registered_histograms
.size());
135 // Register the same Histogram again.
137 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram
));
138 registered_histograms
.clear();
139 StatisticsRecorder::GetHistograms(®istered_histograms
);
140 EXPECT_EQ(1u, registered_histograms
.size());
143 TEST_F(StatisticsRecorderTest
, FindHistogram
) {
144 HistogramBase
* histogram1
= Histogram::FactoryGet(
145 "TestHistogram1", 1, 1000, 10, HistogramBase::kNoFlags
);
146 HistogramBase
* histogram2
= Histogram::FactoryGet(
147 "TestHistogram2", 1, 1000, 10, HistogramBase::kNoFlags
);
149 EXPECT_EQ(histogram1
, StatisticsRecorder::FindHistogram("TestHistogram1"));
150 EXPECT_EQ(histogram2
, StatisticsRecorder::FindHistogram("TestHistogram2"));
151 EXPECT_TRUE(StatisticsRecorder::FindHistogram("TestHistogram") == NULL
);
154 TEST_F(StatisticsRecorderTest
, GetSnapshot
) {
155 Histogram::FactoryGet("TestHistogram1", 1, 1000, 10, Histogram::kNoFlags
);
156 Histogram::FactoryGet("TestHistogram2", 1, 1000, 10, Histogram::kNoFlags
);
157 Histogram::FactoryGet("TestHistogram3", 1, 1000, 10, Histogram::kNoFlags
);
159 StatisticsRecorder::Histograms snapshot
;
160 StatisticsRecorder::GetSnapshot("Test", &snapshot
);
161 EXPECT_EQ(3u, snapshot
.size());
164 StatisticsRecorder::GetSnapshot("1", &snapshot
);
165 EXPECT_EQ(1u, snapshot
.size());
168 StatisticsRecorder::GetSnapshot("hello", &snapshot
);
169 EXPECT_EQ(0u, snapshot
.size());
172 TEST_F(StatisticsRecorderTest
, RegisterHistogramWithFactoryGet
) {
173 StatisticsRecorder::Histograms registered_histograms
;
175 StatisticsRecorder::GetHistograms(®istered_histograms
);
176 ASSERT_EQ(0u, registered_histograms
.size());
178 // Create a histogram.
179 HistogramBase
* histogram
= Histogram::FactoryGet(
180 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags
);
181 registered_histograms
.clear();
182 StatisticsRecorder::GetHistograms(®istered_histograms
);
183 EXPECT_EQ(1u, registered_histograms
.size());
185 // Get an existing histogram.
186 HistogramBase
* histogram2
= Histogram::FactoryGet(
187 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags
);
188 registered_histograms
.clear();
189 StatisticsRecorder::GetHistograms(®istered_histograms
);
190 EXPECT_EQ(1u, registered_histograms
.size());
191 EXPECT_EQ(histogram
, histogram2
);
193 // Create a LinearHistogram.
194 histogram
= LinearHistogram::FactoryGet(
195 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags
);
196 registered_histograms
.clear();
197 StatisticsRecorder::GetHistograms(®istered_histograms
);
198 EXPECT_EQ(2u, registered_histograms
.size());
200 // Create a BooleanHistogram.
201 histogram
= BooleanHistogram::FactoryGet(
202 "TestBooleanHistogram", HistogramBase::kNoFlags
);
203 registered_histograms
.clear();
204 StatisticsRecorder::GetHistograms(®istered_histograms
);
205 EXPECT_EQ(3u, registered_histograms
.size());
207 // Create a CustomHistogram.
208 std::vector
<int> custom_ranges
;
209 custom_ranges
.push_back(1);
210 custom_ranges
.push_back(5);
211 histogram
= CustomHistogram::FactoryGet(
212 "TestCustomHistogram", custom_ranges
, HistogramBase::kNoFlags
);
213 registered_histograms
.clear();
214 StatisticsRecorder::GetHistograms(®istered_histograms
);
215 EXPECT_EQ(4u, registered_histograms
.size());
218 TEST_F(StatisticsRecorderTest
, RegisterHistogramWithMacros
) {
219 StatisticsRecorder::Histograms registered_histograms
;
221 HistogramBase
* histogram
= Histogram::FactoryGet(
222 "TestHistogramCounts", 1, 1000000, 50, HistogramBase::kNoFlags
);
224 // The histogram we got from macro is the same as from FactoryGet.
225 HISTOGRAM_COUNTS("TestHistogramCounts", 30);
226 registered_histograms
.clear();
227 StatisticsRecorder::GetHistograms(®istered_histograms
);
228 ASSERT_EQ(1u, registered_histograms
.size());
229 EXPECT_EQ(histogram
, registered_histograms
[0]);
231 HISTOGRAM_TIMES("TestHistogramTimes", TimeDelta::FromDays(1));
232 HISTOGRAM_ENUMERATION("TestHistogramEnumeration", 20, 200);
234 registered_histograms
.clear();
235 StatisticsRecorder::GetHistograms(®istered_histograms
);
236 EXPECT_EQ(3u, registered_histograms
.size());
238 // Debugging only macros.
239 DHISTOGRAM_TIMES("TestHistogramDebugTimes", TimeDelta::FromDays(1));
240 DHISTOGRAM_COUNTS("TestHistogramDebugCounts", 30);
241 registered_histograms
.clear();
242 StatisticsRecorder::GetHistograms(®istered_histograms
);
244 EXPECT_EQ(5u, registered_histograms
.size());
246 EXPECT_EQ(3u, registered_histograms
.size());
250 TEST_F(StatisticsRecorderTest
, BucketRangesSharing
) {
251 std::vector
<const BucketRanges
*> ranges
;
252 StatisticsRecorder::GetBucketRanges(&ranges
);
253 EXPECT_EQ(0u, ranges
.size());
255 Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags
);
256 Histogram::FactoryGet("Histogram2", 1, 64, 8, HistogramBase::kNoFlags
);
258 StatisticsRecorder::GetBucketRanges(&ranges
);
259 EXPECT_EQ(1u, ranges
.size());
261 Histogram::FactoryGet("Histogram3", 1, 64, 16, HistogramBase::kNoFlags
);
264 StatisticsRecorder::GetBucketRanges(&ranges
);
265 EXPECT_EQ(2u, ranges
.size());
268 TEST_F(StatisticsRecorderTest
, ToJSON
) {
269 HISTOGRAM_COUNTS("TestHistogram1", 30);
270 HISTOGRAM_COUNTS("TestHistogram1", 40);
271 HISTOGRAM_COUNTS("TestHistogram2", 30);
272 HISTOGRAM_COUNTS("TestHistogram2", 40);
274 std::string
json(StatisticsRecorder::ToJSON(std::string()));
276 // Check for valid JSON.
277 scoped_ptr
<Value
> root
;
278 root
.reset(JSONReader::Read(json
));
279 ASSERT_TRUE(root
.get());
281 DictionaryValue
* root_dict
= NULL
;
282 ASSERT_TRUE(root
->GetAsDictionary(&root_dict
));
284 // No query should be set.
285 ASSERT_FALSE(root_dict
->HasKey("query"));
287 ListValue
* histogram_list
= NULL
;
288 ASSERT_TRUE(root_dict
->GetList("histograms", &histogram_list
));
289 ASSERT_EQ(2u, histogram_list
->GetSize());
291 // Examine the first histogram.
292 DictionaryValue
* histogram_dict
= NULL
;
293 ASSERT_TRUE(histogram_list
->GetDictionary(0, &histogram_dict
));
296 ASSERT_TRUE(histogram_dict
->GetInteger("count", &sample_count
));
297 EXPECT_EQ(2, sample_count
);
299 // Test the query filter.
300 std::string
query("TestHistogram2");
301 json
= StatisticsRecorder::ToJSON(query
);
303 root
.reset(JSONReader::Read(json
));
304 ASSERT_TRUE(root
.get());
305 ASSERT_TRUE(root
->GetAsDictionary(&root_dict
));
307 std::string query_value
;
308 ASSERT_TRUE(root_dict
->GetString("query", &query_value
));
309 EXPECT_EQ(query
, query_value
);
311 ASSERT_TRUE(root_dict
->GetList("histograms", &histogram_list
));
312 ASSERT_EQ(1u, histogram_list
->GetSize());
314 ASSERT_TRUE(histogram_list
->GetDictionary(0, &histogram_dict
));
316 std::string histogram_name
;
317 ASSERT_TRUE(histogram_dict
->GetString("name", &histogram_name
));
318 EXPECT_EQ("TestHistogram2", histogram_name
);
321 UninitializeStatisticsRecorder();
323 // No data should be returned.
324 json
= StatisticsRecorder::ToJSON(query
);
325 EXPECT_TRUE(json
.empty());