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 <string> 6 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" 16 17 namespace base { 18 19 class SparseHistogramTest : public testing::Test { 20 protected: 21 virtual void SetUp() { 22 // Each test will have a clean state (no Histogram / BucketRanges 23 // registered). 24 InitializeStatisticsRecorder(); 25 } 26 27 virtual void TearDown() { 28 UninitializeStatisticsRecorder(); 29 } 30 31 void InitializeStatisticsRecorder() { 32 statistics_recorder_ = new StatisticsRecorder(); 33 } 34 35 void UninitializeStatisticsRecorder() { 36 delete statistics_recorder_; 37 statistics_recorder_ = NULL; 38 } 39 40 scoped_ptr<SparseHistogram> NewSparseHistogram(const std::string& name) { 41 return scoped_ptr<SparseHistogram>(new SparseHistogram(name)); 42 } 43 44 StatisticsRecorder* statistics_recorder_; 45 }; 46 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()); 52 53 histogram->Add(100); 54 scoped_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples()); 55 EXPECT_EQ(1, snapshot1->TotalCount()); 56 EXPECT_EQ(1, snapshot1->GetCount(100)); 57 58 histogram->Add(100); 59 histogram->Add(101); 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)); 64 } 65 66 TEST_F(SparseHistogramTest, MacroBasicTest) { 67 HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); 68 HISTOGRAM_SPARSE_SLOWLY("Sparse", 200); 69 HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); 70 71 StatisticsRecorder::Histograms histograms; 72 StatisticsRecorder::GetHistograms(&histograms); 73 74 ASSERT_EQ(1U, histograms.size()); 75 HistogramBase* sparse_histogram = histograms[0]; 76 77 EXPECT_EQ(SPARSE_HISTOGRAM, sparse_histogram->GetHistogramType()); 78 EXPECT_EQ("Sparse", sparse_histogram->histogram_name()); 79 EXPECT_EQ(HistogramBase::kNoFlags, sparse_histogram->flags()); 80 81 scoped_ptr<HistogramSamples> samples = sparse_histogram->SnapshotSamples(); 82 EXPECT_EQ(3, samples->TotalCount()); 83 EXPECT_EQ(2, samples->GetCount(100)); 84 EXPECT_EQ(1, samples->GetCount(200)); 85 } 86 87 TEST_F(SparseHistogramTest, MacroUmaTest) { 88 UMA_HISTOGRAM_SPARSE_SLOWLY("Uma", 100); 89 90 StatisticsRecorder::Histograms histograms; 91 StatisticsRecorder::GetHistograms(&histograms); 92 93 ASSERT_EQ(1U, histograms.size()); 94 HistogramBase* sparse_histogram = histograms[0]; 95 96 EXPECT_EQ("Uma", sparse_histogram->histogram_name()); 97 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, 98 sparse_histogram->flags()); 99 } 100 101 TEST_F(SparseHistogramTest, MacroInLoopTest) { 102 // Unlike the macros in histogram.h, SparseHistogram macros can have a 103 // variable as histogram name. 104 for (int i = 0; i < 2; i++) { 105 std::string name = StringPrintf("Sparse%d", i + 1); 106 UMA_HISTOGRAM_SPARSE_SLOWLY(name, 100); 107 } 108 109 StatisticsRecorder::Histograms histograms; 110 StatisticsRecorder::GetHistograms(&histograms); 111 ASSERT_EQ(2U, histograms.size()); 112 113 std::string name1 = histograms[0]->histogram_name(); 114 std::string name2 = histograms[1]->histogram_name(); 115 EXPECT_TRUE(("Sparse1" == name1 && "Sparse2" == name2) || 116 ("Sparse2" == name1 && "Sparse1" == name2)); 117 } 118 119 TEST_F(SparseHistogramTest, Serialize) { 120 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); 121 histogram->SetFlags(HistogramBase::kIPCSerializationSourceFlag); 122 123 Pickle pickle; 124 histogram->SerializeInfo(&pickle); 125 126 PickleIterator iter(pickle); 127 128 int type; 129 EXPECT_TRUE(iter.ReadInt(&type)); 130 EXPECT_EQ(SPARSE_HISTOGRAM, type); 131 132 std::string name; 133 EXPECT_TRUE(iter.ReadString(&name)); 134 EXPECT_EQ("Sparse", name); 135 136 int flag; 137 EXPECT_TRUE(iter.ReadInt(&flag)); 138 EXPECT_EQ(HistogramBase::kIPCSerializationSourceFlag, flag); 139 140 // No more data in the pickle. 141 EXPECT_FALSE(iter.SkipBytes(1)); 142 } 143 144 } // namespace base 145