Don't preload rarely seen large images
[chromium-blink-merge.git] / base / metrics / sparse_histogram_unittest.cc
blobfca4d59e50989075f53680aa5e025b692a3e744c
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, MacroBasicTest) {
66 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
67 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 200);
68 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
70 StatisticsRecorder::Histograms histograms;
71 StatisticsRecorder::GetHistograms(&histograms);
73 ASSERT_EQ(1U, histograms.size());
74 HistogramBase* sparse_histogram = histograms[0];
76 EXPECT_EQ(SPARSE_HISTOGRAM, sparse_histogram->GetHistogramType());
77 EXPECT_EQ("Sparse", sparse_histogram->histogram_name());
78 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag,
79 sparse_histogram->flags());
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));
87 TEST_F(SparseHistogramTest, MacroInLoopTest) {
88 // Unlike the macros in histogram.h, SparseHistogram macros can have a
89 // variable as histogram name.
90 for (int i = 0; i < 2; i++) {
91 std::string name = StringPrintf("Sparse%d", i + 1);
92 UMA_HISTOGRAM_SPARSE_SLOWLY(name, 100);
95 StatisticsRecorder::Histograms histograms;
96 StatisticsRecorder::GetHistograms(&histograms);
97 ASSERT_EQ(2U, histograms.size());
99 std::string name1 = histograms[0]->histogram_name();
100 std::string name2 = histograms[1]->histogram_name();
101 EXPECT_TRUE(("Sparse1" == name1 && "Sparse2" == name2) ||
102 ("Sparse2" == name1 && "Sparse1" == name2));
105 TEST_F(SparseHistogramTest, Serialize) {
106 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
107 histogram->SetFlags(HistogramBase::kIPCSerializationSourceFlag);
109 Pickle pickle;
110 histogram->SerializeInfo(&pickle);
112 PickleIterator iter(pickle);
114 int type;
115 EXPECT_TRUE(iter.ReadInt(&type));
116 EXPECT_EQ(SPARSE_HISTOGRAM, type);
118 std::string name;
119 EXPECT_TRUE(iter.ReadString(&name));
120 EXPECT_EQ("Sparse", name);
122 int flag;
123 EXPECT_TRUE(iter.ReadInt(&flag));
124 EXPECT_EQ(HistogramBase::kIPCSerializationSourceFlag, flag);
126 // No more data in the pickle.
127 EXPECT_FALSE(iter.SkipBytes(1));
130 } // namespace base