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 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); 68 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 200); 69 UMA_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::kUmaTargetedHistogramFlag, 80 sparse_histogram->flags()); 81 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)); 86 } 87 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); 94 } 95 96 StatisticsRecorder::Histograms histograms; 97 StatisticsRecorder::GetHistograms(&histograms); 98 ASSERT_EQ(2U, histograms.size()); 99 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)); 104 } 105 106 TEST_F(SparseHistogramTest, Serialize) { 107 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); 108 histogram->SetFlags(HistogramBase::kIPCSerializationSourceFlag); 109 110 Pickle pickle; 111 histogram->SerializeInfo(&pickle); 112 113 PickleIterator iter(pickle); 114 115 int type; 116 EXPECT_TRUE(iter.ReadInt(&type)); 117 EXPECT_EQ(SPARSE_HISTOGRAM, type); 118 119 std::string name; 120 EXPECT_TRUE(iter.ReadString(&name)); 121 EXPECT_EQ("Sparse", name); 122 123 int flag; 124 EXPECT_TRUE(iter.ReadInt(&flag)); 125 EXPECT_EQ(HistogramBase::kIPCSerializationSourceFlag, flag); 126 127 // No more data in the pickle. 128 EXPECT_FALSE(iter.SkipBytes(1)); 129 } 130 131 } // namespace base 132