forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse_histogram_unittest.cc
144 lines (112 loc) · 4.32 KB
/
sparse_histogram_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/sample_map.h"
#include "base/metrics/sparse_histogram.h"
#include "base/metrics/statistics_recorder.h"
#include "base/pickle.h"
#include "base/strings/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
class SparseHistogramTest : public testing::Test {
protected:
virtual void SetUp() {
// Each test will have a clean state (no Histogram / BucketRanges
// registered).
InitializeStatisticsRecorder();
}
virtual void TearDown() {
UninitializeStatisticsRecorder();
}
void InitializeStatisticsRecorder() {
statistics_recorder_ = new StatisticsRecorder();
}
void UninitializeStatisticsRecorder() {
delete statistics_recorder_;
statistics_recorder_ = NULL;
}
scoped_ptr<SparseHistogram> NewSparseHistogram(const std::string& name) {
return scoped_ptr<SparseHistogram>(new SparseHistogram(name));
}
StatisticsRecorder* statistics_recorder_;
};
TEST_F(SparseHistogramTest, BasicTest) {
scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
scoped_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples());
EXPECT_EQ(0, snapshot->TotalCount());
EXPECT_EQ(0, snapshot->sum());
histogram->Add(100);
scoped_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples());
EXPECT_EQ(1, snapshot1->TotalCount());
EXPECT_EQ(1, snapshot1->GetCount(100));
histogram->Add(100);
histogram->Add(101);
scoped_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples());
EXPECT_EQ(3, snapshot2->TotalCount());
EXPECT_EQ(2, snapshot2->GetCount(100));
EXPECT_EQ(1, snapshot2->GetCount(101));
}
TEST_F(SparseHistogramTest, MacroBasicTest) {
HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
HISTOGRAM_SPARSE_SLOWLY("Sparse", 200);
HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
StatisticsRecorder::Histograms histograms;
StatisticsRecorder::GetHistograms(&histograms);
ASSERT_EQ(1U, histograms.size());
HistogramBase* sparse_histogram = histograms[0];
EXPECT_EQ(SPARSE_HISTOGRAM, sparse_histogram->GetHistogramType());
EXPECT_EQ("Sparse", sparse_histogram->histogram_name());
EXPECT_EQ(HistogramBase::kNoFlags, sparse_histogram->flags());
scoped_ptr<HistogramSamples> samples = sparse_histogram->SnapshotSamples();
EXPECT_EQ(3, samples->TotalCount());
EXPECT_EQ(2, samples->GetCount(100));
EXPECT_EQ(1, samples->GetCount(200));
}
TEST_F(SparseHistogramTest, MacroUmaTest) {
UMA_HISTOGRAM_SPARSE_SLOWLY("Uma", 100);
StatisticsRecorder::Histograms histograms;
StatisticsRecorder::GetHistograms(&histograms);
ASSERT_EQ(1U, histograms.size());
HistogramBase* sparse_histogram = histograms[0];
EXPECT_EQ("Uma", sparse_histogram->histogram_name());
EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag,
sparse_histogram->flags());
}
TEST_F(SparseHistogramTest, MacroInLoopTest) {
// Unlike the macros in histogram.h, SparseHistogram macros can have a
// variable as histogram name.
for (int i = 0; i < 2; i++) {
std::string name = StringPrintf("Sparse%d", i + 1);
UMA_HISTOGRAM_SPARSE_SLOWLY(name, 100);
}
StatisticsRecorder::Histograms histograms;
StatisticsRecorder::GetHistograms(&histograms);
ASSERT_EQ(2U, histograms.size());
std::string name1 = histograms[0]->histogram_name();
std::string name2 = histograms[1]->histogram_name();
EXPECT_TRUE(("Sparse1" == name1 && "Sparse2" == name2) ||
("Sparse2" == name1 && "Sparse1" == name2));
}
TEST_F(SparseHistogramTest, Serialize) {
scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
histogram->SetFlags(HistogramBase::kIPCSerializationSourceFlag);
Pickle pickle;
histogram->SerializeInfo(&pickle);
PickleIterator iter(pickle);
int type;
EXPECT_TRUE(iter.ReadInt(&type));
EXPECT_EQ(SPARSE_HISTOGRAM, type);
std::string name;
EXPECT_TRUE(iter.ReadString(&name));
EXPECT_EQ("Sparse", name);
int flag;
EXPECT_TRUE(iter.ReadInt(&flag));
EXPECT_EQ(HistogramBase::kIPCSerializationSourceFlag, flag);
// No more data in the pickle.
EXPECT_FALSE(iter.SkipBytes(1));
}
} // namespace base