Add ICU message format support
[chromium-blink-merge.git] / base / metrics / sparse_histogram_unittest.cc
blob83cf5d372472fd25161f7bd725d5f0a152bff126
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 "base/metrics/sparse_histogram.h"
7 #include <string>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/metrics/histogram_base.h"
11 #include "base/metrics/histogram_samples.h"
12 #include "base/metrics/sample_map.h"
13 #include "base/metrics/statistics_recorder.h"
14 #include "base/pickle.h"
15 #include "base/strings/stringprintf.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace base {
20 class SparseHistogramTest : public testing::Test {
21 protected:
22 void SetUp() override {
23 // Each test will have a clean state (no Histogram / BucketRanges
24 // registered).
25 InitializeStatisticsRecorder();
28 void TearDown() override { UninitializeStatisticsRecorder(); }
30 void InitializeStatisticsRecorder() {
31 statistics_recorder_ = new StatisticsRecorder();
34 void UninitializeStatisticsRecorder() {
35 delete statistics_recorder_;
36 statistics_recorder_ = NULL;
39 scoped_ptr<SparseHistogram> NewSparseHistogram(const std::string& name) {
40 return scoped_ptr<SparseHistogram>(new SparseHistogram(name));
43 StatisticsRecorder* statistics_recorder_;
46 TEST_F(SparseHistogramTest, BasicTest) {
47 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
48 scoped_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples());
49 EXPECT_EQ(0, snapshot->TotalCount());
50 EXPECT_EQ(0, snapshot->sum());
52 histogram->Add(100);
53 scoped_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples());
54 EXPECT_EQ(1, snapshot1->TotalCount());
55 EXPECT_EQ(1, snapshot1->GetCount(100));
57 histogram->Add(100);
58 histogram->Add(101);
59 scoped_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples());
60 EXPECT_EQ(3, snapshot2->TotalCount());
61 EXPECT_EQ(2, snapshot2->GetCount(100));
62 EXPECT_EQ(1, snapshot2->GetCount(101));
65 TEST_F(SparseHistogramTest, BasicTestAddCount) {
66 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
67 scoped_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples());
68 EXPECT_EQ(0, snapshot->TotalCount());
69 EXPECT_EQ(0, snapshot->sum());
71 histogram->AddCount(100, 15);
72 scoped_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples());
73 EXPECT_EQ(15, snapshot1->TotalCount());
74 EXPECT_EQ(15, snapshot1->GetCount(100));
76 histogram->AddCount(100, 15);
77 histogram->AddCount(101, 25);
78 scoped_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples());
79 EXPECT_EQ(55, snapshot2->TotalCount());
80 EXPECT_EQ(30, snapshot2->GetCount(100));
81 EXPECT_EQ(25, snapshot2->GetCount(101));
84 TEST_F(SparseHistogramTest, MacroBasicTest) {
85 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
86 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 200);
87 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
89 StatisticsRecorder::Histograms histograms;
90 StatisticsRecorder::GetHistograms(&histograms);
92 ASSERT_EQ(1U, histograms.size());
93 HistogramBase* sparse_histogram = histograms[0];
95 EXPECT_EQ(SPARSE_HISTOGRAM, sparse_histogram->GetHistogramType());
96 EXPECT_EQ("Sparse", sparse_histogram->histogram_name());
97 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag,
98 sparse_histogram->flags());
100 scoped_ptr<HistogramSamples> samples = sparse_histogram->SnapshotSamples();
101 EXPECT_EQ(3, samples->TotalCount());
102 EXPECT_EQ(2, samples->GetCount(100));
103 EXPECT_EQ(1, samples->GetCount(200));
106 TEST_F(SparseHistogramTest, MacroInLoopTest) {
107 // Unlike the macros in histogram.h, SparseHistogram macros can have a
108 // variable as histogram name.
109 for (int i = 0; i < 2; i++) {
110 std::string name = StringPrintf("Sparse%d", i + 1);
111 UMA_HISTOGRAM_SPARSE_SLOWLY(name, 100);
114 StatisticsRecorder::Histograms histograms;
115 StatisticsRecorder::GetHistograms(&histograms);
116 ASSERT_EQ(2U, histograms.size());
118 std::string name1 = histograms[0]->histogram_name();
119 std::string name2 = histograms[1]->histogram_name();
120 EXPECT_TRUE(("Sparse1" == name1 && "Sparse2" == name2) ||
121 ("Sparse2" == name1 && "Sparse1" == name2));
124 TEST_F(SparseHistogramTest, Serialize) {
125 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
126 histogram->SetFlags(HistogramBase::kIPCSerializationSourceFlag);
128 Pickle pickle;
129 histogram->SerializeInfo(&pickle);
131 PickleIterator iter(pickle);
133 int type;
134 EXPECT_TRUE(iter.ReadInt(&type));
135 EXPECT_EQ(SPARSE_HISTOGRAM, type);
137 std::string name;
138 EXPECT_TRUE(iter.ReadString(&name));
139 EXPECT_EQ("Sparse", name);
141 int flag;
142 EXPECT_TRUE(iter.ReadInt(&flag));
143 EXPECT_EQ(HistogramBase::kIPCSerializationSourceFlag, flag);
145 // No more data in the pickle.
146 EXPECT_FALSE(iter.SkipBytes(1));
149 } // namespace base