• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "base/metrics/sparse_histogram.h"
6 
7 #include <string>
8 
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"
17 
18 namespace base {
19 
20 class SparseHistogramTest : public testing::Test {
21  protected:
SetUp()22   void SetUp() override {
23     // Each test will have a clean state (no Histogram / BucketRanges
24     // registered).
25     InitializeStatisticsRecorder();
26   }
27 
TearDown()28   void TearDown() override { UninitializeStatisticsRecorder(); }
29 
InitializeStatisticsRecorder()30   void InitializeStatisticsRecorder() {
31     statistics_recorder_ = new StatisticsRecorder();
32   }
33 
UninitializeStatisticsRecorder()34   void UninitializeStatisticsRecorder() {
35     delete statistics_recorder_;
36     statistics_recorder_ = NULL;
37   }
38 
NewSparseHistogram(const std::string & name)39   scoped_ptr<SparseHistogram> NewSparseHistogram(const std::string& name) {
40     return scoped_ptr<SparseHistogram>(new SparseHistogram(name));
41   }
42 
43   StatisticsRecorder* statistics_recorder_;
44 };
45 
TEST_F(SparseHistogramTest,BasicTest)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());
51 
52   histogram->Add(100);
53   scoped_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples());
54   EXPECT_EQ(1, snapshot1->TotalCount());
55   EXPECT_EQ(1, snapshot1->GetCount(100));
56 
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));
63 }
64 
TEST_F(SparseHistogramTest,BasicTestAddCount)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());
70 
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));
75 
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));
82 }
83 
TEST_F(SparseHistogramTest,MacroBasicTest)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);
88 
89   StatisticsRecorder::Histograms histograms;
90   StatisticsRecorder::GetHistograms(&histograms);
91 
92   ASSERT_EQ(1U, histograms.size());
93   HistogramBase* sparse_histogram = histograms[0];
94 
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());
99 
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));
104 }
105 
TEST_F(SparseHistogramTest,MacroInLoopTest)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);
112   }
113 
114   StatisticsRecorder::Histograms histograms;
115   StatisticsRecorder::GetHistograms(&histograms);
116   ASSERT_EQ(2U, histograms.size());
117 
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));
122 }
123 
TEST_F(SparseHistogramTest,Serialize)124 TEST_F(SparseHistogramTest, Serialize) {
125   scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
126   histogram->SetFlags(HistogramBase::kIPCSerializationSourceFlag);
127 
128   Pickle pickle;
129   histogram->SerializeInfo(&pickle);
130 
131   PickleIterator iter(pickle);
132 
133   int type;
134   EXPECT_TRUE(iter.ReadInt(&type));
135   EXPECT_EQ(SPARSE_HISTOGRAM, type);
136 
137   std::string name;
138   EXPECT_TRUE(iter.ReadString(&name));
139   EXPECT_EQ("Sparse", name);
140 
141   int flag;
142   EXPECT_TRUE(iter.ReadInt(&flag));
143   EXPECT_EQ(HistogramBase::kIPCSerializationSourceFlag, flag);
144 
145   // No more data in the pickle.
146   EXPECT_FALSE(iter.SkipBytes(1));
147 }
148 
149 }  // namespace base
150