Roll src/third_party/WebKit 60624dd:2936779 (svn 202421:202423)
[chromium-blink-merge.git] / base / metrics / statistics_recorder_unittest.cc
blobb18c5803893864259862f3b4bf116ebaf5739665
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.
5 #include <vector>
7 #include "base/bind.h"
8 #include "base/json/json_reader.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/metrics/histogram_macros.h"
11 #include "base/metrics/sparse_histogram.h"
12 #include "base/metrics/statistics_recorder.h"
13 #include "base/values.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace base {
18 class StatisticsRecorderTest : public testing::Test {
19 protected:
20 void SetUp() override {
21 // Each test will have a clean state (no Histogram / BucketRanges
22 // registered).
23 InitializeStatisticsRecorder();
26 void TearDown() override { 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) {
49 delete 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(&registered_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.
69 EXPECT_EQ(histogram,
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();
77 EXPECT_EQ(ranges,
78 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges));
79 StatisticsRecorder::GetBucketRanges(&registered_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.
92 EXPECT_EQ(ranges1,
93 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges1));
94 EXPECT_EQ(ranges2,
95 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges2));
96 StatisticsRecorder::GetBucketRanges(&registered_ranges);
97 ASSERT_EQ(2u, registered_ranges.size());
99 // Register some ranges again.
100 EXPECT_EQ(ranges1,
101 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges1));
102 registered_ranges.clear();
103 StatisticsRecorder::GetBucketRanges(&registered_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(&registered_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(&registered_histograms);
127 EXPECT_EQ(0u, registered_histograms.size());
129 // Register the Histogram.
130 EXPECT_EQ(histogram,
131 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram));
132 StatisticsRecorder::GetHistograms(&registered_histograms);
133 EXPECT_EQ(1u, registered_histograms.size());
135 // Register the same Histogram again.
136 EXPECT_EQ(histogram,
137 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram));
138 registered_histograms.clear();
139 StatisticsRecorder::GetHistograms(&registered_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());
163 snapshot.clear();
164 StatisticsRecorder::GetSnapshot("1", &snapshot);
165 EXPECT_EQ(1u, snapshot.size());
167 snapshot.clear();
168 StatisticsRecorder::GetSnapshot("hello", &snapshot);
169 EXPECT_EQ(0u, snapshot.size());
172 TEST_F(StatisticsRecorderTest, RegisterHistogramWithFactoryGet) {
173 StatisticsRecorder::Histograms registered_histograms;
175 StatisticsRecorder::GetHistograms(&registered_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(&registered_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(&registered_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(&registered_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(&registered_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(&registered_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 LOCAL_HISTOGRAM_COUNTS("TestHistogramCounts", 30);
226 registered_histograms.clear();
227 StatisticsRecorder::GetHistograms(&registered_histograms);
228 ASSERT_EQ(1u, registered_histograms.size());
229 EXPECT_EQ(histogram, registered_histograms[0]);
231 LOCAL_HISTOGRAM_TIMES("TestHistogramTimes", TimeDelta::FromDays(1));
232 LOCAL_HISTOGRAM_ENUMERATION("TestHistogramEnumeration", 20, 200);
234 registered_histograms.clear();
235 StatisticsRecorder::GetHistograms(&registered_histograms);
236 EXPECT_EQ(3u, registered_histograms.size());
239 TEST_F(StatisticsRecorderTest, BucketRangesSharing) {
240 std::vector<const BucketRanges*> ranges;
241 StatisticsRecorder::GetBucketRanges(&ranges);
242 EXPECT_EQ(0u, ranges.size());
244 Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags);
245 Histogram::FactoryGet("Histogram2", 1, 64, 8, HistogramBase::kNoFlags);
247 StatisticsRecorder::GetBucketRanges(&ranges);
248 EXPECT_EQ(1u, ranges.size());
250 Histogram::FactoryGet("Histogram3", 1, 64, 16, HistogramBase::kNoFlags);
252 ranges.clear();
253 StatisticsRecorder::GetBucketRanges(&ranges);
254 EXPECT_EQ(2u, ranges.size());
257 TEST_F(StatisticsRecorderTest, ToJSON) {
258 LOCAL_HISTOGRAM_COUNTS("TestHistogram1", 30);
259 LOCAL_HISTOGRAM_COUNTS("TestHistogram1", 40);
260 LOCAL_HISTOGRAM_COUNTS("TestHistogram2", 30);
261 LOCAL_HISTOGRAM_COUNTS("TestHistogram2", 40);
263 std::string json(StatisticsRecorder::ToJSON(std::string()));
265 // Check for valid JSON.
266 scoped_ptr<Value> root;
267 root.reset(JSONReader::DeprecatedRead(json));
268 ASSERT_TRUE(root.get());
270 DictionaryValue* root_dict = NULL;
271 ASSERT_TRUE(root->GetAsDictionary(&root_dict));
273 // No query should be set.
274 ASSERT_FALSE(root_dict->HasKey("query"));
276 ListValue* histogram_list = NULL;
277 ASSERT_TRUE(root_dict->GetList("histograms", &histogram_list));
278 ASSERT_EQ(2u, histogram_list->GetSize());
280 // Examine the first histogram.
281 DictionaryValue* histogram_dict = NULL;
282 ASSERT_TRUE(histogram_list->GetDictionary(0, &histogram_dict));
284 int sample_count;
285 ASSERT_TRUE(histogram_dict->GetInteger("count", &sample_count));
286 EXPECT_EQ(2, sample_count);
288 // Test the query filter.
289 std::string query("TestHistogram2");
290 json = StatisticsRecorder::ToJSON(query);
292 root.reset(JSONReader::DeprecatedRead(json));
293 ASSERT_TRUE(root.get());
294 ASSERT_TRUE(root->GetAsDictionary(&root_dict));
296 std::string query_value;
297 ASSERT_TRUE(root_dict->GetString("query", &query_value));
298 EXPECT_EQ(query, query_value);
300 ASSERT_TRUE(root_dict->GetList("histograms", &histogram_list));
301 ASSERT_EQ(1u, histogram_list->GetSize());
303 ASSERT_TRUE(histogram_list->GetDictionary(0, &histogram_dict));
305 std::string histogram_name;
306 ASSERT_TRUE(histogram_dict->GetString("name", &histogram_name));
307 EXPECT_EQ("TestHistogram2", histogram_name);
309 json.clear();
310 UninitializeStatisticsRecorder();
312 // No data should be returned.
313 json = StatisticsRecorder::ToJSON(query);
314 EXPECT_TRUE(json.empty());
317 namespace {
319 // CallbackCheckWrapper is simply a convenient way to check and store that
320 // a callback was actually run.
321 struct CallbackCheckWrapper {
322 CallbackCheckWrapper() : called(false), last_histogram_value(0) {}
324 void OnHistogramChanged(base::HistogramBase::Sample histogram_value) {
325 called = true;
326 last_histogram_value = histogram_value;
329 bool called;
330 base::HistogramBase::Sample last_histogram_value;
333 } // namespace
335 // Check that you can't overwrite the callback with another.
336 TEST_F(StatisticsRecorderTest, SetCallbackFailsWithoutHistogramTest) {
337 CallbackCheckWrapper callback_wrapper;
339 bool result = base::StatisticsRecorder::SetCallback(
340 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
341 base::Unretained(&callback_wrapper)));
342 EXPECT_TRUE(result);
344 result = base::StatisticsRecorder::SetCallback(
345 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
346 base::Unretained(&callback_wrapper)));
347 EXPECT_FALSE(result);
350 // Check that you can't overwrite the callback with another.
351 TEST_F(StatisticsRecorderTest, SetCallbackFailsWithHistogramTest) {
352 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10,
353 HistogramBase::kNoFlags);
354 EXPECT_TRUE(histogram);
356 CallbackCheckWrapper callback_wrapper;
358 bool result = base::StatisticsRecorder::SetCallback(
359 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
360 base::Unretained(&callback_wrapper)));
361 EXPECT_TRUE(result);
362 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists,
363 base::HistogramBase::kCallbackExists);
365 result = base::StatisticsRecorder::SetCallback(
366 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
367 base::Unretained(&callback_wrapper)));
368 EXPECT_FALSE(result);
369 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists,
370 base::HistogramBase::kCallbackExists);
372 histogram->Add(1);
374 EXPECT_TRUE(callback_wrapper.called);
377 // Check that you can't overwrite the callback with another.
378 TEST_F(StatisticsRecorderTest, ClearCallbackSuceedsWithHistogramTest) {
379 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10,
380 HistogramBase::kNoFlags);
381 EXPECT_TRUE(histogram);
383 CallbackCheckWrapper callback_wrapper;
385 bool result = base::StatisticsRecorder::SetCallback(
386 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
387 base::Unretained(&callback_wrapper)));
388 EXPECT_TRUE(result);
389 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists,
390 base::HistogramBase::kCallbackExists);
392 base::StatisticsRecorder::ClearCallback("TestHistogram");
393 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists, 0);
395 histogram->Add(1);
397 EXPECT_FALSE(callback_wrapper.called);
400 // Check that callback is used.
401 TEST_F(StatisticsRecorderTest, CallbackUsedTest) {
403 HistogramBase* histogram = Histogram::FactoryGet(
404 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
405 EXPECT_TRUE(histogram);
407 CallbackCheckWrapper callback_wrapper;
409 base::StatisticsRecorder::SetCallback(
410 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
411 base::Unretained(&callback_wrapper)));
413 histogram->Add(1);
415 EXPECT_TRUE(callback_wrapper.called);
416 EXPECT_EQ(callback_wrapper.last_histogram_value, 1);
420 HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
421 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
423 CallbackCheckWrapper callback_wrapper;
425 base::StatisticsRecorder::SetCallback(
426 "TestLinearHistogram",
427 base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
428 base::Unretained(&callback_wrapper)));
430 linear_histogram->Add(1);
432 EXPECT_TRUE(callback_wrapper.called);
433 EXPECT_EQ(callback_wrapper.last_histogram_value, 1);
437 std::vector<int> custom_ranges;
438 custom_ranges.push_back(1);
439 custom_ranges.push_back(5);
440 HistogramBase* custom_histogram = CustomHistogram::FactoryGet(
441 "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags);
443 CallbackCheckWrapper callback_wrapper;
445 base::StatisticsRecorder::SetCallback(
446 "TestCustomHistogram",
447 base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
448 base::Unretained(&callback_wrapper)));
450 custom_histogram->Add(1);
452 EXPECT_TRUE(callback_wrapper.called);
453 EXPECT_EQ(callback_wrapper.last_histogram_value, 1);
457 HistogramBase* custom_histogram = SparseHistogram::FactoryGet(
458 "TestSparseHistogram", HistogramBase::kNoFlags);
460 CallbackCheckWrapper callback_wrapper;
462 base::StatisticsRecorder::SetCallback(
463 "TestSparseHistogram",
464 base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
465 base::Unretained(&callback_wrapper)));
467 custom_histogram->Add(1);
469 EXPECT_TRUE(callback_wrapper.called);
470 EXPECT_EQ(callback_wrapper.last_histogram_value, 1);
474 // Check that setting a callback before the histogram exists works.
475 TEST_F(StatisticsRecorderTest, CallbackUsedBeforeHistogramCreatedTest) {
476 CallbackCheckWrapper callback_wrapper;
478 base::StatisticsRecorder::SetCallback(
479 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
480 base::Unretained(&callback_wrapper)));
482 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10,
483 HistogramBase::kNoFlags);
484 EXPECT_TRUE(histogram);
485 histogram->Add(1);
487 EXPECT_TRUE(callback_wrapper.called);
488 EXPECT_EQ(callback_wrapper.last_histogram_value, 1);
491 } // namespace base