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_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"
19 class SparseHistogramTest
: public testing::Test
{
21 virtual void SetUp() {
22 // Each test will have a clean state (no Histogram / BucketRanges
24 InitializeStatisticsRecorder();
27 virtual void TearDown() {
28 UninitializeStatisticsRecorder();
31 void InitializeStatisticsRecorder() {
32 statistics_recorder_
= new StatisticsRecorder();
35 void UninitializeStatisticsRecorder() {
36 delete statistics_recorder_
;
37 statistics_recorder_
= NULL
;
40 scoped_ptr
<SparseHistogram
> NewSparseHistogram(const std::string
& name
) {
41 return scoped_ptr
<SparseHistogram
>(new SparseHistogram(name
));
44 StatisticsRecorder
* statistics_recorder_
;
47 TEST_F(SparseHistogramTest
, BasicTest
) {
48 scoped_ptr
<SparseHistogram
> histogram(NewSparseHistogram("Sparse"));
49 scoped_ptr
<HistogramSamples
> snapshot(histogram
->SnapshotSamples());
50 EXPECT_EQ(0, snapshot
->TotalCount());
51 EXPECT_EQ(0, snapshot
->sum());
54 scoped_ptr
<HistogramSamples
> snapshot1(histogram
->SnapshotSamples());
55 EXPECT_EQ(1, snapshot1
->TotalCount());
56 EXPECT_EQ(1, snapshot1
->GetCount(100));
60 scoped_ptr
<HistogramSamples
> snapshot2(histogram
->SnapshotSamples());
61 EXPECT_EQ(3, snapshot2
->TotalCount());
62 EXPECT_EQ(2, snapshot2
->GetCount(100));
63 EXPECT_EQ(1, snapshot2
->GetCount(101));
66 TEST_F(SparseHistogramTest
, MacroBasicTest
) {
67 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
68 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 200);
69 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
71 StatisticsRecorder::Histograms histograms
;
72 StatisticsRecorder::GetHistograms(&histograms
);
74 ASSERT_EQ(1U, histograms
.size());
75 HistogramBase
* sparse_histogram
= histograms
[0];
77 EXPECT_EQ(SPARSE_HISTOGRAM
, sparse_histogram
->GetHistogramType());
78 EXPECT_EQ("Sparse", sparse_histogram
->histogram_name());
79 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag
,
80 sparse_histogram
->flags());
82 scoped_ptr
<HistogramSamples
> samples
= sparse_histogram
->SnapshotSamples();
83 EXPECT_EQ(3, samples
->TotalCount());
84 EXPECT_EQ(2, samples
->GetCount(100));
85 EXPECT_EQ(1, samples
->GetCount(200));
88 TEST_F(SparseHistogramTest
, MacroInLoopTest
) {
89 // Unlike the macros in histogram.h, SparseHistogram macros can have a
90 // variable as histogram name.
91 for (int i
= 0; i
< 2; i
++) {
92 std::string name
= StringPrintf("Sparse%d", i
+ 1);
93 UMA_HISTOGRAM_SPARSE_SLOWLY(name
, 100);
96 StatisticsRecorder::Histograms histograms
;
97 StatisticsRecorder::GetHistograms(&histograms
);
98 ASSERT_EQ(2U, histograms
.size());
100 std::string name1
= histograms
[0]->histogram_name();
101 std::string name2
= histograms
[1]->histogram_name();
102 EXPECT_TRUE(("Sparse1" == name1
&& "Sparse2" == name2
) ||
103 ("Sparse2" == name1
&& "Sparse1" == name2
));
106 TEST_F(SparseHistogramTest
, Serialize
) {
107 scoped_ptr
<SparseHistogram
> histogram(NewSparseHistogram("Sparse"));
108 histogram
->SetFlags(HistogramBase::kIPCSerializationSourceFlag
);
111 histogram
->SerializeInfo(&pickle
);
113 PickleIterator
iter(pickle
);
116 EXPECT_TRUE(iter
.ReadInt(&type
));
117 EXPECT_EQ(SPARSE_HISTOGRAM
, type
);
120 EXPECT_TRUE(iter
.ReadString(&name
));
121 EXPECT_EQ("Sparse", name
);
124 EXPECT_TRUE(iter
.ReadInt(&flag
));
125 EXPECT_EQ(HistogramBase::kIPCSerializationSourceFlag
, flag
);
127 // No more data in the pickle.
128 EXPECT_FALSE(iter
.SkipBytes(1));