OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/sparse_histogram.h" | 5 #include "base/metrics/sparse_histogram.h" |
6 | 6 |
| 7 #include <memory> |
7 #include <string> | 8 #include <string> |
8 | 9 |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/metrics/histogram_base.h" | 10 #include "base/metrics/histogram_base.h" |
11 #include "base/metrics/histogram_samples.h" | 11 #include "base/metrics/histogram_samples.h" |
12 #include "base/metrics/persistent_histogram_allocator.h" | 12 #include "base/metrics/persistent_histogram_allocator.h" |
13 #include "base/metrics/persistent_memory_allocator.h" | 13 #include "base/metrics/persistent_memory_allocator.h" |
14 #include "base/metrics/sample_map.h" | 14 #include "base/metrics/sample_map.h" |
15 #include "base/metrics/statistics_recorder.h" | 15 #include "base/metrics/statistics_recorder.h" |
16 #include "base/pickle.h" | 16 #include "base/pickle.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 kAllocatorMemorySize, 0, "SparseHistogramAllocatorTest"); | 66 kAllocatorMemorySize, 0, "SparseHistogramAllocatorTest"); |
67 allocator_ = | 67 allocator_ = |
68 PersistentHistogramAllocator::GetGlobalAllocator()->memory_allocator(); | 68 PersistentHistogramAllocator::GetGlobalAllocator()->memory_allocator(); |
69 } | 69 } |
70 | 70 |
71 void DestroyPersistentMemoryAllocator() { | 71 void DestroyPersistentMemoryAllocator() { |
72 allocator_ = nullptr; | 72 allocator_ = nullptr; |
73 PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); | 73 PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); |
74 } | 74 } |
75 | 75 |
76 scoped_ptr<SparseHistogram> NewSparseHistogram(const std::string& name) { | 76 std::unique_ptr<SparseHistogram> NewSparseHistogram(const std::string& name) { |
77 return scoped_ptr<SparseHistogram>(new SparseHistogram(name)); | 77 return std::unique_ptr<SparseHistogram>(new SparseHistogram(name)); |
78 } | 78 } |
79 | 79 |
80 const bool use_persistent_histogram_allocator_; | 80 const bool use_persistent_histogram_allocator_; |
81 | 81 |
82 StatisticsRecorder* statistics_recorder_; | 82 StatisticsRecorder* statistics_recorder_; |
83 scoped_ptr<char[]> allocator_memory_; | 83 std::unique_ptr<char[]> allocator_memory_; |
84 PersistentMemoryAllocator* allocator_ = nullptr; | 84 PersistentMemoryAllocator* allocator_ = nullptr; |
85 | 85 |
86 private: | 86 private: |
87 DISALLOW_COPY_AND_ASSIGN(SparseHistogramTest); | 87 DISALLOW_COPY_AND_ASSIGN(SparseHistogramTest); |
88 }; | 88 }; |
89 | 89 |
90 // Run all HistogramTest cases with both heap and persistent memory. | 90 // Run all HistogramTest cases with both heap and persistent memory. |
91 INSTANTIATE_TEST_CASE_P(HeapAndPersistent, | 91 INSTANTIATE_TEST_CASE_P(HeapAndPersistent, |
92 SparseHistogramTest, | 92 SparseHistogramTest, |
93 testing::Bool()); | 93 testing::Bool()); |
94 | 94 |
95 | 95 |
96 TEST_P(SparseHistogramTest, BasicTest) { | 96 TEST_P(SparseHistogramTest, BasicTest) { |
97 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); | 97 std::unique_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); |
98 scoped_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples()); | 98 std::unique_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples()); |
99 EXPECT_EQ(0, snapshot->TotalCount()); | 99 EXPECT_EQ(0, snapshot->TotalCount()); |
100 EXPECT_EQ(0, snapshot->sum()); | 100 EXPECT_EQ(0, snapshot->sum()); |
101 | 101 |
102 histogram->Add(100); | 102 histogram->Add(100); |
103 scoped_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples()); | 103 std::unique_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples()); |
104 EXPECT_EQ(1, snapshot1->TotalCount()); | 104 EXPECT_EQ(1, snapshot1->TotalCount()); |
105 EXPECT_EQ(1, snapshot1->GetCount(100)); | 105 EXPECT_EQ(1, snapshot1->GetCount(100)); |
106 | 106 |
107 histogram->Add(100); | 107 histogram->Add(100); |
108 histogram->Add(101); | 108 histogram->Add(101); |
109 scoped_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples()); | 109 std::unique_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples()); |
110 EXPECT_EQ(3, snapshot2->TotalCount()); | 110 EXPECT_EQ(3, snapshot2->TotalCount()); |
111 EXPECT_EQ(2, snapshot2->GetCount(100)); | 111 EXPECT_EQ(2, snapshot2->GetCount(100)); |
112 EXPECT_EQ(1, snapshot2->GetCount(101)); | 112 EXPECT_EQ(1, snapshot2->GetCount(101)); |
113 } | 113 } |
114 | 114 |
115 TEST_P(SparseHistogramTest, BasicTestAddCount) { | 115 TEST_P(SparseHistogramTest, BasicTestAddCount) { |
116 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); | 116 std::unique_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); |
117 scoped_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples()); | 117 std::unique_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples()); |
118 EXPECT_EQ(0, snapshot->TotalCount()); | 118 EXPECT_EQ(0, snapshot->TotalCount()); |
119 EXPECT_EQ(0, snapshot->sum()); | 119 EXPECT_EQ(0, snapshot->sum()); |
120 | 120 |
121 histogram->AddCount(100, 15); | 121 histogram->AddCount(100, 15); |
122 scoped_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples()); | 122 std::unique_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples()); |
123 EXPECT_EQ(15, snapshot1->TotalCount()); | 123 EXPECT_EQ(15, snapshot1->TotalCount()); |
124 EXPECT_EQ(15, snapshot1->GetCount(100)); | 124 EXPECT_EQ(15, snapshot1->GetCount(100)); |
125 | 125 |
126 histogram->AddCount(100, 15); | 126 histogram->AddCount(100, 15); |
127 histogram->AddCount(101, 25); | 127 histogram->AddCount(101, 25); |
128 scoped_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples()); | 128 std::unique_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples()); |
129 EXPECT_EQ(55, snapshot2->TotalCount()); | 129 EXPECT_EQ(55, snapshot2->TotalCount()); |
130 EXPECT_EQ(30, snapshot2->GetCount(100)); | 130 EXPECT_EQ(30, snapshot2->GetCount(100)); |
131 EXPECT_EQ(25, snapshot2->GetCount(101)); | 131 EXPECT_EQ(25, snapshot2->GetCount(101)); |
132 } | 132 } |
133 | 133 |
134 TEST_P(SparseHistogramTest, AddCount_LargeValuesDontOverflow) { | 134 TEST_P(SparseHistogramTest, AddCount_LargeValuesDontOverflow) { |
135 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); | 135 std::unique_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); |
136 scoped_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples()); | 136 std::unique_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples()); |
137 EXPECT_EQ(0, snapshot->TotalCount()); | 137 EXPECT_EQ(0, snapshot->TotalCount()); |
138 EXPECT_EQ(0, snapshot->sum()); | 138 EXPECT_EQ(0, snapshot->sum()); |
139 | 139 |
140 histogram->AddCount(1000000000, 15); | 140 histogram->AddCount(1000000000, 15); |
141 scoped_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples()); | 141 std::unique_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples()); |
142 EXPECT_EQ(15, snapshot1->TotalCount()); | 142 EXPECT_EQ(15, snapshot1->TotalCount()); |
143 EXPECT_EQ(15, snapshot1->GetCount(1000000000)); | 143 EXPECT_EQ(15, snapshot1->GetCount(1000000000)); |
144 | 144 |
145 histogram->AddCount(1000000000, 15); | 145 histogram->AddCount(1000000000, 15); |
146 histogram->AddCount(1010000000, 25); | 146 histogram->AddCount(1010000000, 25); |
147 scoped_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples()); | 147 std::unique_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples()); |
148 EXPECT_EQ(55, snapshot2->TotalCount()); | 148 EXPECT_EQ(55, snapshot2->TotalCount()); |
149 EXPECT_EQ(30, snapshot2->GetCount(1000000000)); | 149 EXPECT_EQ(30, snapshot2->GetCount(1000000000)); |
150 EXPECT_EQ(25, snapshot2->GetCount(1010000000)); | 150 EXPECT_EQ(25, snapshot2->GetCount(1010000000)); |
151 EXPECT_EQ(55250000000LL, snapshot2->sum()); | 151 EXPECT_EQ(55250000000LL, snapshot2->sum()); |
152 } | 152 } |
153 | 153 |
154 TEST_P(SparseHistogramTest, MacroBasicTest) { | 154 TEST_P(SparseHistogramTest, MacroBasicTest) { |
155 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); | 155 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); |
156 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 200); | 156 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 200); |
157 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); | 157 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); |
158 | 158 |
159 StatisticsRecorder::Histograms histograms; | 159 StatisticsRecorder::Histograms histograms; |
160 StatisticsRecorder::GetHistograms(&histograms); | 160 StatisticsRecorder::GetHistograms(&histograms); |
161 | 161 |
162 ASSERT_EQ(1U, histograms.size()); | 162 ASSERT_EQ(1U, histograms.size()); |
163 HistogramBase* sparse_histogram = histograms[0]; | 163 HistogramBase* sparse_histogram = histograms[0]; |
164 | 164 |
165 EXPECT_EQ(SPARSE_HISTOGRAM, sparse_histogram->GetHistogramType()); | 165 EXPECT_EQ(SPARSE_HISTOGRAM, sparse_histogram->GetHistogramType()); |
166 EXPECT_EQ("Sparse", sparse_histogram->histogram_name()); | 166 EXPECT_EQ("Sparse", sparse_histogram->histogram_name()); |
167 EXPECT_EQ( | 167 EXPECT_EQ( |
168 HistogramBase::kUmaTargetedHistogramFlag | | 168 HistogramBase::kUmaTargetedHistogramFlag | |
169 (use_persistent_histogram_allocator_ ? HistogramBase::kIsPersistent | 169 (use_persistent_histogram_allocator_ ? HistogramBase::kIsPersistent |
170 : 0), | 170 : 0), |
171 sparse_histogram->flags()); | 171 sparse_histogram->flags()); |
172 | 172 |
173 scoped_ptr<HistogramSamples> samples = sparse_histogram->SnapshotSamples(); | 173 std::unique_ptr<HistogramSamples> samples = |
| 174 sparse_histogram->SnapshotSamples(); |
174 EXPECT_EQ(3, samples->TotalCount()); | 175 EXPECT_EQ(3, samples->TotalCount()); |
175 EXPECT_EQ(2, samples->GetCount(100)); | 176 EXPECT_EQ(2, samples->GetCount(100)); |
176 EXPECT_EQ(1, samples->GetCount(200)); | 177 EXPECT_EQ(1, samples->GetCount(200)); |
177 } | 178 } |
178 | 179 |
179 TEST_P(SparseHistogramTest, MacroInLoopTest) { | 180 TEST_P(SparseHistogramTest, MacroInLoopTest) { |
180 // Unlike the macros in histogram.h, SparseHistogram macros can have a | 181 // Unlike the macros in histogram.h, SparseHistogram macros can have a |
181 // variable as histogram name. | 182 // variable as histogram name. |
182 for (int i = 0; i < 2; i++) { | 183 for (int i = 0; i < 2; i++) { |
183 std::string name = StringPrintf("Sparse%d", i + 1); | 184 std::string name = StringPrintf("Sparse%d", i + 1); |
184 UMA_HISTOGRAM_SPARSE_SLOWLY(name, 100); | 185 UMA_HISTOGRAM_SPARSE_SLOWLY(name, 100); |
185 } | 186 } |
186 | 187 |
187 StatisticsRecorder::Histograms histograms; | 188 StatisticsRecorder::Histograms histograms; |
188 StatisticsRecorder::GetHistograms(&histograms); | 189 StatisticsRecorder::GetHistograms(&histograms); |
189 ASSERT_EQ(2U, histograms.size()); | 190 ASSERT_EQ(2U, histograms.size()); |
190 | 191 |
191 std::string name1 = histograms[0]->histogram_name(); | 192 std::string name1 = histograms[0]->histogram_name(); |
192 std::string name2 = histograms[1]->histogram_name(); | 193 std::string name2 = histograms[1]->histogram_name(); |
193 EXPECT_TRUE(("Sparse1" == name1 && "Sparse2" == name2) || | 194 EXPECT_TRUE(("Sparse1" == name1 && "Sparse2" == name2) || |
194 ("Sparse2" == name1 && "Sparse1" == name2)); | 195 ("Sparse2" == name1 && "Sparse1" == name2)); |
195 } | 196 } |
196 | 197 |
197 TEST_P(SparseHistogramTest, Serialize) { | 198 TEST_P(SparseHistogramTest, Serialize) { |
198 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); | 199 std::unique_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); |
199 histogram->SetFlags(HistogramBase::kIPCSerializationSourceFlag); | 200 histogram->SetFlags(HistogramBase::kIPCSerializationSourceFlag); |
200 | 201 |
201 Pickle pickle; | 202 Pickle pickle; |
202 histogram->SerializeInfo(&pickle); | 203 histogram->SerializeInfo(&pickle); |
203 | 204 |
204 PickleIterator iter(pickle); | 205 PickleIterator iter(pickle); |
205 | 206 |
206 int type; | 207 int type; |
207 EXPECT_TRUE(iter.ReadInt(&type)); | 208 EXPECT_TRUE(iter.ReadInt(&type)); |
208 EXPECT_EQ(SPARSE_HISTOGRAM, type); | 209 EXPECT_EQ(SPARSE_HISTOGRAM, type); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 TimeDelta add_ticks = TimeTicks::Now() - add_start; | 274 TimeDelta add_ticks = TimeTicks::Now() - add_start; |
274 int64_t add_ms = add_ticks.InMilliseconds(); | 275 int64_t add_ms = add_ticks.InMilliseconds(); |
275 | 276 |
276 VLOG(1) << kTestAddCount << " histogram adds took " << add_ms | 277 VLOG(1) << kTestAddCount << " histogram adds took " << add_ms |
277 << "ms or about " | 278 << "ms or about " |
278 << (add_ms * 1000000) / kTestAddCount | 279 << (add_ms * 1000000) / kTestAddCount |
279 << "ns each."; | 280 << "ns each."; |
280 } | 281 } |
281 | 282 |
282 } // namespace base | 283 } // namespace base |
OLD | NEW |