Reland the ULONG -> SIZE_T change from 317177
[chromium-blink-merge.git] / base / metrics / sparse_histogram_unittest.cc
blobc29dd5e22549dccf5143e606d7e650d61668184d
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 <string>
7 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/histogram_base.h"
9 #include "base/metrics/histogram_samples.h"
10 #include "base/metrics/sample_map.h"
11 #include "base/metrics/sparse_histogram.h"
12 #include "base/metrics/statistics_recorder.h"
13 #include "base/pickle.h"
14 #include "base/strings/stringprintf.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace base {
19 class SparseHistogramTest : public testing::Test {
20 protected:
21 void SetUp() override {
22 // Each test will have a clean state (no Histogram / BucketRanges
23 // registered).
24 InitializeStatisticsRecorder();
27 void TearDown() override { UninitializeStatisticsRecorder(); }
29 void InitializeStatisticsRecorder() {
30 statistics_recorder_ = new StatisticsRecorder();
33 void UninitializeStatisticsRecorder() {
34 delete statistics_recorder_;
35 statistics_recorder_ = NULL;
38 scoped_ptr<SparseHistogram> NewSparseHistogram(const std::string& name) {
39 return scoped_ptr<SparseHistogram>(new SparseHistogram(name));
42 StatisticsRecorder* statistics_recorder_;
45 TEST_F(SparseHistogramTest, BasicTest) {
46 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
47 scoped_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples());
48 EXPECT_EQ(0, snapshot->TotalCount());
49 EXPECT_EQ(0, snapshot->sum());
51 histogram->Add(100);
52 scoped_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples());
53 EXPECT_EQ(1, snapshot1->TotalCount());
54 EXPECT_EQ(1, snapshot1->GetCount(100));
56 histogram->Add(100);
57 histogram->Add(101);
58 scoped_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples());
59 EXPECT_EQ(3, snapshot2->TotalCount());
60 EXPECT_EQ(2, snapshot2->GetCount(100));
61 EXPECT_EQ(1, snapshot2->GetCount(101));
64 TEST_F(SparseHistogramTest, MacroBasicTest) {
65 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
66 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 200);
67 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
69 StatisticsRecorder::Histograms histograms;
70 StatisticsRecorder::GetHistograms(&histograms);
72 ASSERT_EQ(1U, histograms.size());
73 HistogramBase* sparse_histogram = histograms[0];
75 EXPECT_EQ(SPARSE_HISTOGRAM, sparse_histogram->GetHistogramType());
76 EXPECT_EQ("Sparse", sparse_histogram->histogram_name());
77 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag,
78 sparse_histogram->flags());
80 scoped_ptr<HistogramSamples> samples = sparse_histogram->SnapshotSamples();
81 EXPECT_EQ(3, samples->TotalCount());
82 EXPECT_EQ(2, samples->GetCount(100));
83 EXPECT_EQ(1, samples->GetCount(200));
86 TEST_F(SparseHistogramTest, MacroInLoopTest) {
87 // Unlike the macros in histogram.h, SparseHistogram macros can have a
88 // variable as histogram name.
89 for (int i = 0; i < 2; i++) {
90 std::string name = StringPrintf("Sparse%d", i + 1);
91 UMA_HISTOGRAM_SPARSE_SLOWLY(name, 100);
94 StatisticsRecorder::Histograms histograms;
95 StatisticsRecorder::GetHistograms(&histograms);
96 ASSERT_EQ(2U, histograms.size());
98 std::string name1 = histograms[0]->histogram_name();
99 std::string name2 = histograms[1]->histogram_name();
100 EXPECT_TRUE(("Sparse1" == name1 && "Sparse2" == name2) ||
101 ("Sparse2" == name1 && "Sparse1" == name2));
104 TEST_F(SparseHistogramTest, Serialize) {
105 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
106 histogram->SetFlags(HistogramBase::kIPCSerializationSourceFlag);
108 Pickle pickle;
109 histogram->SerializeInfo(&pickle);
111 PickleIterator iter(pickle);
113 int type;
114 EXPECT_TRUE(iter.ReadInt(&type));
115 EXPECT_EQ(SPARSE_HISTOGRAM, type);
117 std::string name;
118 EXPECT_TRUE(iter.ReadString(&name));
119 EXPECT_EQ("Sparse", name);
121 int flag;
122 EXPECT_TRUE(iter.ReadInt(&flag));
123 EXPECT_EQ(HistogramBase::kIPCSerializationSourceFlag, flag);
125 // No more data in the pickle.
126 EXPECT_FALSE(iter.SkipBytes(1));
129 } // namespace base