Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: base/metrics/sparse_histogram_unittest.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/metrics/sparse_histogram.cc ('k') | base/metrics/statistics_recorder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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:
22 void SetUp() override {
23 // Each test will have a clean state (no Histogram / BucketRanges
24 // registered).
25 InitializeStatisticsRecorder();
26 }
27
28 void TearDown() override { UninitializeStatisticsRecorder(); }
29
30 void InitializeStatisticsRecorder() {
31 statistics_recorder_ = new StatisticsRecorder();
32 }
33
34 void UninitializeStatisticsRecorder() {
35 delete statistics_recorder_;
36 statistics_recorder_ = NULL;
37 }
38
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
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
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);
69
70 StatisticsRecorder::Histograms histograms;
71 StatisticsRecorder::GetHistograms(&histograms);
72
73 ASSERT_EQ(1U, histograms.size());
74 HistogramBase* sparse_histogram = histograms[0];
75
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());
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, 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);
93 }
94
95 StatisticsRecorder::Histograms histograms;
96 StatisticsRecorder::GetHistograms(&histograms);
97 ASSERT_EQ(2U, histograms.size());
98
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));
103 }
104
105 TEST_F(SparseHistogramTest, Serialize) {
106 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
107 histogram->SetFlags(HistogramBase::kIPCSerializationSourceFlag);
108
109 Pickle pickle;
110 histogram->SerializeInfo(&pickle);
111
112 PickleIterator iter(pickle);
113
114 int type;
115 EXPECT_TRUE(iter.ReadInt(&type));
116 EXPECT_EQ(SPARSE_HISTOGRAM, type);
117
118 std::string name;
119 EXPECT_TRUE(iter.ReadString(&name));
120 EXPECT_EQ("Sparse", name);
121
122 int flag;
123 EXPECT_TRUE(iter.ReadInt(&flag));
124 EXPECT_EQ(HistogramBase::kIPCSerializationSourceFlag, flag);
125
126 // No more data in the pickle.
127 EXPECT_FALSE(iter.SkipBytes(1));
128 }
129
130 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/sparse_histogram.cc ('k') | base/metrics/statistics_recorder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698