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

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

Issue 1876053002: Run SR tests both with and without persistent allocator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix use-after-free in test Created 4 years, 8 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/statistics_recorder.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/statistics_recorder.h" 5 #include "base/metrics/statistics_recorder.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
14 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
15 #include "base/metrics/persistent_histogram_allocator.h" 15 #include "base/metrics/persistent_histogram_allocator.h"
16 #include "base/metrics/sparse_histogram.h" 16 #include "base/metrics/sparse_histogram.h"
17 #include "base/values.h" 17 #include "base/values.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 19
20 namespace base { 20 namespace base {
21 21
22 class StatisticsRecorderTest : public testing::Test { 22 class StatisticsRecorderTest : public testing::TestWithParam<bool> {
23 protected: 23 protected:
24 void SetUp() override { 24 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB
25
26 StatisticsRecorderTest() : use_persistent_histogram_allocator_(GetParam()) {
25 // Get this first so it never gets created in persistent storage and will 27 // Get this first so it never gets created in persistent storage and will
26 // not appear in the StatisticsRecorder after it is re-initialized. 28 // not appear in the StatisticsRecorder after it is re-initialized.
27 PersistentHistogramAllocator::GetCreateHistogramResultHistogram(); 29 PersistentHistogramAllocator::GetCreateHistogramResultHistogram();
30
28 // Each test will have a clean state (no Histogram / BucketRanges 31 // Each test will have a clean state (no Histogram / BucketRanges
29 // registered). 32 // registered).
30 InitializeStatisticsRecorder(); 33 InitializeStatisticsRecorder();
34
35 // Use persistent memory for histograms if so indicated by test parameter.
36 if (use_persistent_histogram_allocator_) {
37 GlobalHistogramAllocator::CreateWithLocalMemory(
38 kAllocatorMemorySize, 0, "StatisticsRecorderTest");
39 }
31 } 40 }
32 41
33 void TearDown() override { 42 ~StatisticsRecorderTest() override {
43 GlobalHistogramAllocator::ReleaseForTesting();
34 UninitializeStatisticsRecorder(); 44 UninitializeStatisticsRecorder();
35 GlobalHistogramAllocator::ReleaseForTesting();
36 } 45 }
37 46
38 void InitializeStatisticsRecorder() { 47 void InitializeStatisticsRecorder() {
39 DCHECK(!statistics_recorder_); 48 DCHECK(!statistics_recorder_);
40 StatisticsRecorder::UninitializeForTesting(); 49 StatisticsRecorder::UninitializeForTesting();
41 statistics_recorder_.reset(new StatisticsRecorder()); 50 statistics_recorder_.reset(new StatisticsRecorder());
42 } 51 }
43 52
44 void UninitializeStatisticsRecorder() { 53 void UninitializeStatisticsRecorder() {
45 statistics_recorder_.reset(); 54 statistics_recorder_.reset();
46 StatisticsRecorder::UninitializeForTesting(); 55 StatisticsRecorder::UninitializeForTesting();
47 } 56 }
48 57
49 Histogram* CreateHistogram(const std::string& name, 58 Histogram* CreateHistogram(const std::string& name,
50 HistogramBase::Sample min, 59 HistogramBase::Sample min,
51 HistogramBase::Sample max, 60 HistogramBase::Sample max,
52 size_t bucket_count) { 61 size_t bucket_count) {
53 BucketRanges* ranges = new BucketRanges(bucket_count + 1); 62 BucketRanges* ranges = new BucketRanges(bucket_count + 1);
54 Histogram::InitializeBucketRanges(min, max, ranges); 63 Histogram::InitializeBucketRanges(min, max, ranges);
55 const BucketRanges* registered_ranges = 64 const BucketRanges* registered_ranges =
56 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges); 65 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
57 return new Histogram(name, min, max, registered_ranges); 66 return new Histogram(name, min, max, registered_ranges);
58 } 67 }
59 68
60 void DeleteHistogram(HistogramBase* histogram) { 69 void DeleteHistogram(HistogramBase* histogram) {
61 delete histogram; 70 delete histogram;
62 } 71 }
63 72
73 int CountIterableHistograms(StatisticsRecorder::HistogramIterator* iter) {
74 int count = 0;
75 for (; *iter != StatisticsRecorder::end(); ++*iter) {
76 ++count;
77 }
78 return count;
79 }
80
81 const bool use_persistent_histogram_allocator_;
82
64 std::unique_ptr<StatisticsRecorder> statistics_recorder_; 83 std::unique_ptr<StatisticsRecorder> statistics_recorder_;
84 std::unique_ptr<GlobalHistogramAllocator> old_global_allocator_;
85
86 private:
87 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorderTest);
65 }; 88 };
66 89
67 TEST_F(StatisticsRecorderTest, NotInitialized) { 90 // Run all HistogramTest cases with both heap and persistent memory.
91 INSTANTIATE_TEST_CASE_P(Allocator, StatisticsRecorderTest, testing::Bool());
92
93 TEST_P(StatisticsRecorderTest, NotInitialized) {
68 UninitializeStatisticsRecorder(); 94 UninitializeStatisticsRecorder();
69 95
70 ASSERT_FALSE(StatisticsRecorder::IsActive()); 96 ASSERT_FALSE(StatisticsRecorder::IsActive());
71 97
72 StatisticsRecorder::Histograms registered_histograms; 98 StatisticsRecorder::Histograms registered_histograms;
73 std::vector<const BucketRanges*> registered_ranges; 99 std::vector<const BucketRanges*> registered_ranges;
74 100
75 StatisticsRecorder::GetHistograms(&registered_histograms); 101 StatisticsRecorder::GetHistograms(&registered_histograms);
76 EXPECT_EQ(0u, registered_histograms.size()); 102 EXPECT_EQ(0u, registered_histograms.size());
77 103
78 Histogram* histogram = CreateHistogram("TestHistogram", 1, 1000, 10); 104 Histogram* histogram = CreateHistogram("TestHistogram", 1, 1000, 10);
79 105
80 // When StatisticsRecorder is not initialized, register is a noop. 106 // When StatisticsRecorder is not initialized, register is a noop.
81 EXPECT_EQ(histogram, 107 EXPECT_EQ(histogram,
82 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram)); 108 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram));
83 // Manually delete histogram that was not registered. 109 // Manually delete histogram that was not registered.
84 DeleteHistogram(histogram); 110 DeleteHistogram(histogram);
85 111
86 // RegisterOrDeleteDuplicateRanges is a no-op. 112 // RegisterOrDeleteDuplicateRanges is a no-op.
87 BucketRanges* ranges = new BucketRanges(3); 113 BucketRanges* ranges = new BucketRanges(3);
88 ranges->ResetChecksum(); 114 ranges->ResetChecksum();
89 EXPECT_EQ(ranges, 115 EXPECT_EQ(ranges,
90 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges)); 116 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges));
91 StatisticsRecorder::GetBucketRanges(&registered_ranges); 117 StatisticsRecorder::GetBucketRanges(&registered_ranges);
92 EXPECT_EQ(0u, registered_ranges.size()); 118 EXPECT_EQ(0u, registered_ranges.size());
93 } 119 }
94 120
95 TEST_F(StatisticsRecorderTest, RegisterBucketRanges) { 121 TEST_P(StatisticsRecorderTest, RegisterBucketRanges) {
96 std::vector<const BucketRanges*> registered_ranges; 122 std::vector<const BucketRanges*> registered_ranges;
97 123
98 BucketRanges* ranges1 = new BucketRanges(3); 124 BucketRanges* ranges1 = new BucketRanges(3);
99 ranges1->ResetChecksum(); 125 ranges1->ResetChecksum();
100 BucketRanges* ranges2 = new BucketRanges(4); 126 BucketRanges* ranges2 = new BucketRanges(4);
101 ranges2->ResetChecksum(); 127 ranges2->ResetChecksum();
102 128
103 // Register new ranges. 129 // Register new ranges.
104 EXPECT_EQ(ranges1, 130 EXPECT_EQ(ranges1,
105 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges1)); 131 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges1));
(...skipping 17 matching lines...) Expand all
123 // Register ranges with same values. 149 // Register ranges with same values.
124 BucketRanges* ranges3 = new BucketRanges(3); 150 BucketRanges* ranges3 = new BucketRanges(3);
125 ranges3->ResetChecksum(); 151 ranges3->ResetChecksum();
126 EXPECT_EQ(ranges1, // returning ranges1 152 EXPECT_EQ(ranges1, // returning ranges1
127 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges3)); 153 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges3));
128 registered_ranges.clear(); 154 registered_ranges.clear();
129 StatisticsRecorder::GetBucketRanges(&registered_ranges); 155 StatisticsRecorder::GetBucketRanges(&registered_ranges);
130 ASSERT_EQ(2u, registered_ranges.size()); 156 ASSERT_EQ(2u, registered_ranges.size());
131 } 157 }
132 158
133 TEST_F(StatisticsRecorderTest, RegisterHistogram) { 159 TEST_P(StatisticsRecorderTest, RegisterHistogram) {
134 // Create a Histogram that was not registered. 160 // Create a Histogram that was not registered.
135 Histogram* histogram = CreateHistogram("TestHistogram", 1, 1000, 10); 161 Histogram* histogram = CreateHistogram("TestHistogram", 1, 1000, 10);
136 162
137 StatisticsRecorder::Histograms registered_histograms; 163 StatisticsRecorder::Histograms registered_histograms;
138 StatisticsRecorder::GetHistograms(&registered_histograms); 164 StatisticsRecorder::GetHistograms(&registered_histograms);
139 EXPECT_EQ(0u, registered_histograms.size()); 165 EXPECT_EQ(0u, registered_histograms.size());
140 166
141 // Register the Histogram. 167 // Register the Histogram.
142 EXPECT_EQ(histogram, 168 EXPECT_EQ(histogram,
143 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram)); 169 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram));
144 StatisticsRecorder::GetHistograms(&registered_histograms); 170 StatisticsRecorder::GetHistograms(&registered_histograms);
145 EXPECT_EQ(1u, registered_histograms.size()); 171 EXPECT_EQ(1u, registered_histograms.size());
146 172
147 // Register the same Histogram again. 173 // Register the same Histogram again.
148 EXPECT_EQ(histogram, 174 EXPECT_EQ(histogram,
149 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram)); 175 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram));
150 registered_histograms.clear(); 176 registered_histograms.clear();
151 StatisticsRecorder::GetHistograms(&registered_histograms); 177 StatisticsRecorder::GetHistograms(&registered_histograms);
152 EXPECT_EQ(1u, registered_histograms.size()); 178 EXPECT_EQ(1u, registered_histograms.size());
153 } 179 }
154 180
155 TEST_F(StatisticsRecorderTest, FindHistogram) { 181 TEST_P(StatisticsRecorderTest, FindHistogram) {
156 HistogramBase* histogram1 = Histogram::FactoryGet( 182 HistogramBase* histogram1 = Histogram::FactoryGet(
157 "TestHistogram1", 1, 1000, 10, HistogramBase::kNoFlags); 183 "TestHistogram1", 1, 1000, 10, HistogramBase::kNoFlags);
158 HistogramBase* histogram2 = Histogram::FactoryGet( 184 HistogramBase* histogram2 = Histogram::FactoryGet(
159 "TestHistogram2", 1, 1000, 10, HistogramBase::kNoFlags); 185 "TestHistogram2", 1, 1000, 10, HistogramBase::kNoFlags);
160 186
161 EXPECT_EQ(histogram1, StatisticsRecorder::FindHistogram("TestHistogram1")); 187 EXPECT_EQ(histogram1, StatisticsRecorder::FindHistogram("TestHistogram1"));
162 EXPECT_EQ(histogram2, StatisticsRecorder::FindHistogram("TestHistogram2")); 188 EXPECT_EQ(histogram2, StatisticsRecorder::FindHistogram("TestHistogram2"));
163 EXPECT_TRUE(StatisticsRecorder::FindHistogram("TestHistogram") == NULL); 189 EXPECT_FALSE(StatisticsRecorder::FindHistogram("TestHistogram"));
190
191 // Create a new global allocator using the same memory as the old one. Any
192 // old one is kept around so the memory doesn't get released.
193 old_global_allocator_ = GlobalHistogramAllocator::ReleaseForTesting();
194 if (use_persistent_histogram_allocator_) {
195 GlobalHistogramAllocator::CreateWithPersistentMemory(
196 const_cast<void*>(old_global_allocator_->data()),
197 old_global_allocator_->length(), 0, old_global_allocator_->Id(),
198 old_global_allocator_->Name());
199 }
200
201 // Reset statistics-recorder to validate operation from a clean start.
202 UninitializeStatisticsRecorder();
203 InitializeStatisticsRecorder();
204
205 if (use_persistent_histogram_allocator_) {
206 EXPECT_TRUE(StatisticsRecorder::FindHistogram("TestHistogram1"));
207 EXPECT_TRUE(StatisticsRecorder::FindHistogram("TestHistogram2"));
208 } else {
209 EXPECT_FALSE(StatisticsRecorder::FindHistogram("TestHistogram1"));
210 EXPECT_FALSE(StatisticsRecorder::FindHistogram("TestHistogram2"));
211 }
212 EXPECT_FALSE(StatisticsRecorder::FindHistogram("TestHistogram"));
164 } 213 }
165 214
166 TEST_F(StatisticsRecorderTest, GetSnapshot) { 215 TEST_P(StatisticsRecorderTest, GetSnapshot) {
167 Histogram::FactoryGet("TestHistogram1", 1, 1000, 10, Histogram::kNoFlags); 216 Histogram::FactoryGet("TestHistogram1", 1, 1000, 10, Histogram::kNoFlags);
168 Histogram::FactoryGet("TestHistogram2", 1, 1000, 10, Histogram::kNoFlags); 217 Histogram::FactoryGet("TestHistogram2", 1, 1000, 10, Histogram::kNoFlags);
169 Histogram::FactoryGet("TestHistogram3", 1, 1000, 10, Histogram::kNoFlags); 218 Histogram::FactoryGet("TestHistogram3", 1, 1000, 10, Histogram::kNoFlags);
170 219
171 StatisticsRecorder::Histograms snapshot; 220 StatisticsRecorder::Histograms snapshot;
172 StatisticsRecorder::GetSnapshot("Test", &snapshot); 221 StatisticsRecorder::GetSnapshot("Test", &snapshot);
173 EXPECT_EQ(3u, snapshot.size()); 222 EXPECT_EQ(3u, snapshot.size());
174 223
175 snapshot.clear(); 224 snapshot.clear();
176 StatisticsRecorder::GetSnapshot("1", &snapshot); 225 StatisticsRecorder::GetSnapshot("1", &snapshot);
177 EXPECT_EQ(1u, snapshot.size()); 226 EXPECT_EQ(1u, snapshot.size());
178 227
179 snapshot.clear(); 228 snapshot.clear();
180 StatisticsRecorder::GetSnapshot("hello", &snapshot); 229 StatisticsRecorder::GetSnapshot("hello", &snapshot);
181 EXPECT_EQ(0u, snapshot.size()); 230 EXPECT_EQ(0u, snapshot.size());
182 } 231 }
183 232
184 TEST_F(StatisticsRecorderTest, RegisterHistogramWithFactoryGet) { 233 TEST_P(StatisticsRecorderTest, RegisterHistogramWithFactoryGet) {
185 StatisticsRecorder::Histograms registered_histograms; 234 StatisticsRecorder::Histograms registered_histograms;
186 235
187 StatisticsRecorder::GetHistograms(&registered_histograms); 236 StatisticsRecorder::GetHistograms(&registered_histograms);
188 ASSERT_EQ(0u, registered_histograms.size()); 237 ASSERT_EQ(0u, registered_histograms.size());
189 238
190 // Create a histogram. 239 // Create a histogram.
191 HistogramBase* histogram = Histogram::FactoryGet( 240 HistogramBase* histogram = Histogram::FactoryGet(
192 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags); 241 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
193 registered_histograms.clear(); 242 registered_histograms.clear();
194 StatisticsRecorder::GetHistograms(&registered_histograms); 243 StatisticsRecorder::GetHistograms(&registered_histograms);
(...skipping 25 matching lines...) Expand all
220 std::vector<int> custom_ranges; 269 std::vector<int> custom_ranges;
221 custom_ranges.push_back(1); 270 custom_ranges.push_back(1);
222 custom_ranges.push_back(5); 271 custom_ranges.push_back(5);
223 histogram = CustomHistogram::FactoryGet( 272 histogram = CustomHistogram::FactoryGet(
224 "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags); 273 "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags);
225 registered_histograms.clear(); 274 registered_histograms.clear();
226 StatisticsRecorder::GetHistograms(&registered_histograms); 275 StatisticsRecorder::GetHistograms(&registered_histograms);
227 EXPECT_EQ(4u, registered_histograms.size()); 276 EXPECT_EQ(4u, registered_histograms.size());
228 } 277 }
229 278
230 TEST_F(StatisticsRecorderTest, RegisterHistogramWithMacros) { 279 TEST_P(StatisticsRecorderTest, RegisterHistogramWithMacros) {
280 // Macros cache pointers and so tests that use them can only be run once.
281 // Stop immediately if this test has run previously.
282 static bool already_run = false;
283 if (already_run)
284 return;
285 already_run = true;
286
231 StatisticsRecorder::Histograms registered_histograms; 287 StatisticsRecorder::Histograms registered_histograms;
232 288
233 HistogramBase* histogram = Histogram::FactoryGet( 289 HistogramBase* histogram = Histogram::FactoryGet(
234 "TestHistogramCounts", 1, 1000000, 50, HistogramBase::kNoFlags); 290 "TestHistogramCounts", 1, 1000000, 50, HistogramBase::kNoFlags);
235 291
236 // The histogram we got from macro is the same as from FactoryGet. 292 // The histogram we got from macro is the same as from FactoryGet.
237 LOCAL_HISTOGRAM_COUNTS("TestHistogramCounts", 30); 293 LOCAL_HISTOGRAM_COUNTS("TestHistogramCounts", 30);
238 registered_histograms.clear(); 294 registered_histograms.clear();
239 StatisticsRecorder::GetHistograms(&registered_histograms); 295 StatisticsRecorder::GetHistograms(&registered_histograms);
240 ASSERT_EQ(1u, registered_histograms.size()); 296 ASSERT_EQ(1u, registered_histograms.size());
241 EXPECT_EQ(histogram, registered_histograms[0]); 297 EXPECT_EQ(histogram, registered_histograms[0]);
242 298
243 LOCAL_HISTOGRAM_TIMES("TestHistogramTimes", TimeDelta::FromDays(1)); 299 LOCAL_HISTOGRAM_TIMES("TestHistogramTimes", TimeDelta::FromDays(1));
244 LOCAL_HISTOGRAM_ENUMERATION("TestHistogramEnumeration", 20, 200); 300 LOCAL_HISTOGRAM_ENUMERATION("TestHistogramEnumeration", 20, 200);
245 301
246 registered_histograms.clear(); 302 registered_histograms.clear();
247 StatisticsRecorder::GetHistograms(&registered_histograms); 303 StatisticsRecorder::GetHistograms(&registered_histograms);
248 EXPECT_EQ(3u, registered_histograms.size()); 304 EXPECT_EQ(3u, registered_histograms.size());
249 } 305 }
250 306
251 TEST_F(StatisticsRecorderTest, BucketRangesSharing) { 307 TEST_P(StatisticsRecorderTest, BucketRangesSharing) {
252 std::vector<const BucketRanges*> ranges; 308 std::vector<const BucketRanges*> ranges;
253 StatisticsRecorder::GetBucketRanges(&ranges); 309 StatisticsRecorder::GetBucketRanges(&ranges);
254 EXPECT_EQ(0u, ranges.size()); 310 EXPECT_EQ(0u, ranges.size());
255 311
256 Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags); 312 Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags);
257 Histogram::FactoryGet("Histogram2", 1, 64, 8, HistogramBase::kNoFlags); 313 Histogram::FactoryGet("Histogram2", 1, 64, 8, HistogramBase::kNoFlags);
258 314
259 StatisticsRecorder::GetBucketRanges(&ranges); 315 StatisticsRecorder::GetBucketRanges(&ranges);
260 EXPECT_EQ(1u, ranges.size()); 316 EXPECT_EQ(1u, ranges.size());
261 317
262 Histogram::FactoryGet("Histogram3", 1, 64, 16, HistogramBase::kNoFlags); 318 Histogram::FactoryGet("Histogram3", 1, 64, 16, HistogramBase::kNoFlags);
263 319
264 ranges.clear(); 320 ranges.clear();
265 StatisticsRecorder::GetBucketRanges(&ranges); 321 StatisticsRecorder::GetBucketRanges(&ranges);
266 EXPECT_EQ(2u, ranges.size()); 322 EXPECT_EQ(2u, ranges.size());
267 } 323 }
268 324
269 TEST_F(StatisticsRecorderTest, ToJSON) { 325 TEST_P(StatisticsRecorderTest, ToJSON) {
270 LOCAL_HISTOGRAM_COUNTS("TestHistogram1", 30); 326 Histogram::FactoryGet("TestHistogram1", 1, 1000, 50, HistogramBase::kNoFlags)
271 LOCAL_HISTOGRAM_COUNTS("TestHistogram1", 40); 327 ->Add(30);
272 LOCAL_HISTOGRAM_COUNTS("TestHistogram2", 30); 328 Histogram::FactoryGet("TestHistogram1", 1, 1000, 50, HistogramBase::kNoFlags)
273 LOCAL_HISTOGRAM_COUNTS("TestHistogram2", 40); 329 ->Add(40);
330 Histogram::FactoryGet("TestHistogram2", 1, 1000, 50, HistogramBase::kNoFlags)
331 ->Add(30);
332 Histogram::FactoryGet("TestHistogram2", 1, 1000, 50, HistogramBase::kNoFlags)
333 ->Add(40);
274 334
275 std::string json(StatisticsRecorder::ToJSON(std::string())); 335 std::string json(StatisticsRecorder::ToJSON(std::string()));
276 336
277 // Check for valid JSON. 337 // Check for valid JSON.
278 std::unique_ptr<Value> root = JSONReader::Read(json); 338 std::unique_ptr<Value> root = JSONReader::Read(json);
279 ASSERT_TRUE(root.get()); 339 ASSERT_TRUE(root.get());
280 340
281 DictionaryValue* root_dict = NULL; 341 DictionaryValue* root_dict = NULL;
282 ASSERT_TRUE(root->GetAsDictionary(&root_dict)); 342 ASSERT_TRUE(root->GetAsDictionary(&root_dict));
283 343
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 EXPECT_EQ("TestHistogram2", histogram_name); 378 EXPECT_EQ("TestHistogram2", histogram_name);
319 379
320 json.clear(); 380 json.clear();
321 UninitializeStatisticsRecorder(); 381 UninitializeStatisticsRecorder();
322 382
323 // No data should be returned. 383 // No data should be returned.
324 json = StatisticsRecorder::ToJSON(query); 384 json = StatisticsRecorder::ToJSON(query);
325 EXPECT_TRUE(json.empty()); 385 EXPECT_TRUE(json.empty());
326 } 386 }
327 387
328 TEST_F(StatisticsRecorderTest, IterationTest) { 388 TEST_P(StatisticsRecorderTest, IterationTest) {
329 StatisticsRecorder::Histograms registered_histograms; 389 Histogram::FactoryGet("IterationTest1", 1, 64, 16, HistogramBase::kNoFlags);
330 LOCAL_HISTOGRAM_COUNTS("TestHistogram.IterationTest1", 30); 390 Histogram::FactoryGet("IterationTest2", 1, 64, 16, HistogramBase::kNoFlags);
331 GlobalHistogramAllocator::CreateWithLocalMemory(64 << 10 /* 64 KiB */, 0, "");
332 LOCAL_HISTOGRAM_COUNTS("TestHistogram.IterationTest2", 30);
333 391
334 StatisticsRecorder::HistogramIterator i1 = StatisticsRecorder::begin(true); 392 StatisticsRecorder::HistogramIterator i1 = StatisticsRecorder::begin(true);
335 EXPECT_NE(StatisticsRecorder::end(), i1); 393 EXPECT_EQ(2, CountIterableHistograms(&i1));
336 EXPECT_NE(StatisticsRecorder::end(), ++i1);
337 EXPECT_EQ(StatisticsRecorder::end(), ++i1);
338 394
339 StatisticsRecorder::HistogramIterator i2 = StatisticsRecorder::begin(false); 395 StatisticsRecorder::HistogramIterator i2 = StatisticsRecorder::begin(false);
340 EXPECT_NE(StatisticsRecorder::end(), i2); 396 EXPECT_EQ(use_persistent_histogram_allocator_ ? 0 : 2,
341 EXPECT_EQ(StatisticsRecorder::end(), ++i2); 397 CountIterableHistograms(&i2));
398
399 // Create a new global allocator using the same memory as the old one. Any
400 // old one is kept around so the memory doesn't get released.
401 old_global_allocator_ = GlobalHistogramAllocator::ReleaseForTesting();
402 if (use_persistent_histogram_allocator_) {
403 GlobalHistogramAllocator::CreateWithPersistentMemory(
404 const_cast<void*>(old_global_allocator_->data()),
405 old_global_allocator_->length(), 0, old_global_allocator_->Id(),
406 old_global_allocator_->Name());
407 }
408
409 // Reset statistics-recorder to validate operation from a clean start.
410 UninitializeStatisticsRecorder();
411 InitializeStatisticsRecorder();
412
413 StatisticsRecorder::HistogramIterator i3 = StatisticsRecorder::begin(true);
414 EXPECT_EQ(use_persistent_histogram_allocator_ ? 2 : 0,
415 CountIterableHistograms(&i3));
416
417 StatisticsRecorder::HistogramIterator i4 = StatisticsRecorder::begin(false);
418 EXPECT_EQ(0, CountIterableHistograms(&i4));
342 } 419 }
343 420
344 namespace { 421 namespace {
345 422
346 // CallbackCheckWrapper is simply a convenient way to check and store that 423 // CallbackCheckWrapper is simply a convenient way to check and store that
347 // a callback was actually run. 424 // a callback was actually run.
348 struct CallbackCheckWrapper { 425 struct CallbackCheckWrapper {
349 CallbackCheckWrapper() : called(false), last_histogram_value(0) {} 426 CallbackCheckWrapper() : called(false), last_histogram_value(0) {}
350 427
351 void OnHistogramChanged(base::HistogramBase::Sample histogram_value) { 428 void OnHistogramChanged(base::HistogramBase::Sample histogram_value) {
352 called = true; 429 called = true;
353 last_histogram_value = histogram_value; 430 last_histogram_value = histogram_value;
354 } 431 }
355 432
356 bool called; 433 bool called;
357 base::HistogramBase::Sample last_histogram_value; 434 base::HistogramBase::Sample last_histogram_value;
358 }; 435 };
359 436
360 } // namespace 437 } // namespace
361 438
362 // Check that you can't overwrite the callback with another. 439 // Check that you can't overwrite the callback with another.
363 TEST_F(StatisticsRecorderTest, SetCallbackFailsWithoutHistogramTest) { 440 TEST_P(StatisticsRecorderTest, SetCallbackFailsWithoutHistogramTest) {
364 CallbackCheckWrapper callback_wrapper; 441 CallbackCheckWrapper callback_wrapper;
365 442
366 bool result = base::StatisticsRecorder::SetCallback( 443 bool result = base::StatisticsRecorder::SetCallback(
367 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 444 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
368 base::Unretained(&callback_wrapper))); 445 base::Unretained(&callback_wrapper)));
369 EXPECT_TRUE(result); 446 EXPECT_TRUE(result);
370 447
371 result = base::StatisticsRecorder::SetCallback( 448 result = base::StatisticsRecorder::SetCallback(
372 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 449 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
373 base::Unretained(&callback_wrapper))); 450 base::Unretained(&callback_wrapper)));
374 EXPECT_FALSE(result); 451 EXPECT_FALSE(result);
375 } 452 }
376 453
377 // Check that you can't overwrite the callback with another. 454 // Check that you can't overwrite the callback with another.
378 TEST_F(StatisticsRecorderTest, SetCallbackFailsWithHistogramTest) { 455 TEST_P(StatisticsRecorderTest, SetCallbackFailsWithHistogramTest) {
379 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, 456 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10,
380 HistogramBase::kNoFlags); 457 HistogramBase::kNoFlags);
381 EXPECT_TRUE(histogram); 458 EXPECT_TRUE(histogram);
382 459
383 CallbackCheckWrapper callback_wrapper; 460 CallbackCheckWrapper callback_wrapper;
384 461
385 bool result = base::StatisticsRecorder::SetCallback( 462 bool result = base::StatisticsRecorder::SetCallback(
386 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 463 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
387 base::Unretained(&callback_wrapper))); 464 base::Unretained(&callback_wrapper)));
388 EXPECT_TRUE(result); 465 EXPECT_TRUE(result);
389 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists, 466 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists,
390 base::HistogramBase::kCallbackExists); 467 base::HistogramBase::kCallbackExists);
391 468
392 result = base::StatisticsRecorder::SetCallback( 469 result = base::StatisticsRecorder::SetCallback(
393 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 470 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
394 base::Unretained(&callback_wrapper))); 471 base::Unretained(&callback_wrapper)));
395 EXPECT_FALSE(result); 472 EXPECT_FALSE(result);
396 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists, 473 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists,
397 base::HistogramBase::kCallbackExists); 474 base::HistogramBase::kCallbackExists);
398 475
399 histogram->Add(1); 476 histogram->Add(1);
400 477
401 EXPECT_TRUE(callback_wrapper.called); 478 EXPECT_TRUE(callback_wrapper.called);
402 } 479 }
403 480
404 // Check that you can't overwrite the callback with another. 481 // Check that you can't overwrite the callback with another.
405 TEST_F(StatisticsRecorderTest, ClearCallbackSuceedsWithHistogramTest) { 482 TEST_P(StatisticsRecorderTest, ClearCallbackSuceedsWithHistogramTest) {
406 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, 483 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10,
407 HistogramBase::kNoFlags); 484 HistogramBase::kNoFlags);
408 EXPECT_TRUE(histogram); 485 EXPECT_TRUE(histogram);
409 486
410 CallbackCheckWrapper callback_wrapper; 487 CallbackCheckWrapper callback_wrapper;
411 488
412 bool result = base::StatisticsRecorder::SetCallback( 489 bool result = base::StatisticsRecorder::SetCallback(
413 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 490 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
414 base::Unretained(&callback_wrapper))); 491 base::Unretained(&callback_wrapper)));
415 EXPECT_TRUE(result); 492 EXPECT_TRUE(result);
416 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists, 493 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists,
417 base::HistogramBase::kCallbackExists); 494 base::HistogramBase::kCallbackExists);
418 495
419 base::StatisticsRecorder::ClearCallback("TestHistogram"); 496 base::StatisticsRecorder::ClearCallback("TestHistogram");
420 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists, 0); 497 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists, 0);
421 498
422 histogram->Add(1); 499 histogram->Add(1);
423 500
424 EXPECT_FALSE(callback_wrapper.called); 501 EXPECT_FALSE(callback_wrapper.called);
425 } 502 }
426 503
427 // Check that callback is used. 504 // Check that callback is used.
428 TEST_F(StatisticsRecorderTest, CallbackUsedTest) { 505 TEST_P(StatisticsRecorderTest, CallbackUsedTest) {
429 { 506 {
430 HistogramBase* histogram = Histogram::FactoryGet( 507 HistogramBase* histogram = Histogram::FactoryGet(
431 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags); 508 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
432 EXPECT_TRUE(histogram); 509 EXPECT_TRUE(histogram);
433 510
434 CallbackCheckWrapper callback_wrapper; 511 CallbackCheckWrapper callback_wrapper;
435 512
436 base::StatisticsRecorder::SetCallback( 513 base::StatisticsRecorder::SetCallback(
437 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 514 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
438 base::Unretained(&callback_wrapper))); 515 base::Unretained(&callback_wrapper)));
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 base::Unretained(&callback_wrapper))); 569 base::Unretained(&callback_wrapper)));
493 570
494 custom_histogram->Add(1); 571 custom_histogram->Add(1);
495 572
496 EXPECT_TRUE(callback_wrapper.called); 573 EXPECT_TRUE(callback_wrapper.called);
497 EXPECT_EQ(callback_wrapper.last_histogram_value, 1); 574 EXPECT_EQ(callback_wrapper.last_histogram_value, 1);
498 } 575 }
499 } 576 }
500 577
501 // Check that setting a callback before the histogram exists works. 578 // Check that setting a callback before the histogram exists works.
502 TEST_F(StatisticsRecorderTest, CallbackUsedBeforeHistogramCreatedTest) { 579 TEST_P(StatisticsRecorderTest, CallbackUsedBeforeHistogramCreatedTest) {
503 CallbackCheckWrapper callback_wrapper; 580 CallbackCheckWrapper callback_wrapper;
504 581
505 base::StatisticsRecorder::SetCallback( 582 base::StatisticsRecorder::SetCallback(
506 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 583 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
507 base::Unretained(&callback_wrapper))); 584 base::Unretained(&callback_wrapper)));
508 585
509 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, 586 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10,
510 HistogramBase::kNoFlags); 587 HistogramBase::kNoFlags);
511 EXPECT_TRUE(histogram); 588 EXPECT_TRUE(histogram);
512 histogram->Add(1); 589 histogram->Add(1);
513 590
514 EXPECT_TRUE(callback_wrapper.called); 591 EXPECT_TRUE(callback_wrapper.called);
515 EXPECT_EQ(callback_wrapper.last_histogram_value, 1); 592 EXPECT_EQ(callback_wrapper.last_histogram_value, 1);
516 } 593 }
517 594
518 } // namespace base 595 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/statistics_recorder.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698