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

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: 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
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_)
Alexei Svitkine (slow) 2016/04/11 14:51:34 Nit: {}
bcwhite 2016/04/12 00:32:02 Done.
37 GlobalHistogramAllocator::CreateWithLocalMemory(
38 kAllocatorMemorySize, 0, "StatisticsRecorderTest");
31 } 39 }
32 40
33 void TearDown() override { 41 ~StatisticsRecorderTest() override {
42 GlobalHistogramAllocator::ReleaseForTesting();
34 UninitializeStatisticsRecorder(); 43 UninitializeStatisticsRecorder();
35 GlobalHistogramAllocator::ReleaseForTesting();
36 } 44 }
37 45
38 void InitializeStatisticsRecorder() { 46 void InitializeStatisticsRecorder() {
39 DCHECK(!statistics_recorder_); 47 DCHECK(!statistics_recorder_);
40 StatisticsRecorder::UninitializeForTesting(); 48 StatisticsRecorder::UninitializeForTesting();
41 statistics_recorder_.reset(new StatisticsRecorder()); 49 statistics_recorder_.reset(new StatisticsRecorder());
42 } 50 }
43 51
44 void UninitializeStatisticsRecorder() { 52 void UninitializeStatisticsRecorder() {
45 statistics_recorder_.reset(); 53 statistics_recorder_.reset();
46 StatisticsRecorder::UninitializeForTesting(); 54 StatisticsRecorder::UninitializeForTesting();
47 } 55 }
48 56
49 Histogram* CreateHistogram(const std::string& name, 57 Histogram* CreateHistogram(const std::string& name,
50 HistogramBase::Sample min, 58 HistogramBase::Sample min,
51 HistogramBase::Sample max, 59 HistogramBase::Sample max,
52 size_t bucket_count) { 60 size_t bucket_count) {
53 BucketRanges* ranges = new BucketRanges(bucket_count + 1); 61 BucketRanges* ranges = new BucketRanges(bucket_count + 1);
54 Histogram::InitializeBucketRanges(min, max, ranges); 62 Histogram::InitializeBucketRanges(min, max, ranges);
55 const BucketRanges* registered_ranges = 63 const BucketRanges* registered_ranges =
56 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges); 64 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
57 return new Histogram(name, min, max, registered_ranges); 65 return new Histogram(name, min, max, registered_ranges);
58 } 66 }
59 67
60 void DeleteHistogram(HistogramBase* histogram) { 68 void DeleteHistogram(HistogramBase* histogram) {
61 delete histogram; 69 delete histogram;
62 } 70 }
63 71
72 int CountIterableHistograms(StatisticsRecorder::HistogramIterator& iter) {
Alexei Svitkine (slow) 2016/04/11 14:51:34 Nit: Non-const ref as function params are discoura
bcwhite 2016/04/12 00:32:02 Done.
73 int count = 0;
74 for (; iter != StatisticsRecorder::end(); ++iter) {
75 ++count;
76 }
77 return count;
78 }
79
80 const bool use_persistent_histogram_allocator_;
81
64 std::unique_ptr<StatisticsRecorder> statistics_recorder_; 82 std::unique_ptr<StatisticsRecorder> statistics_recorder_;
83
84 private:
85 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorderTest);
65 }; 86 };
66 87
67 TEST_F(StatisticsRecorderTest, NotInitialized) { 88 // Run all HistogramTest cases with both heap and persistent memory.
89 INSTANTIATE_TEST_CASE_P(Allocator, StatisticsRecorderTest, testing::Bool());
90
91 TEST_P(StatisticsRecorderTest, NotInitialized) {
68 UninitializeStatisticsRecorder(); 92 UninitializeStatisticsRecorder();
69 93
70 ASSERT_FALSE(StatisticsRecorder::IsActive()); 94 ASSERT_FALSE(StatisticsRecorder::IsActive());
71 95
72 StatisticsRecorder::Histograms registered_histograms; 96 StatisticsRecorder::Histograms registered_histograms;
73 std::vector<const BucketRanges*> registered_ranges; 97 std::vector<const BucketRanges*> registered_ranges;
74 98
75 StatisticsRecorder::GetHistograms(&registered_histograms); 99 StatisticsRecorder::GetHistograms(&registered_histograms);
76 EXPECT_EQ(0u, registered_histograms.size()); 100 EXPECT_EQ(0u, registered_histograms.size());
77 101
78 Histogram* histogram = CreateHistogram("TestHistogram", 1, 1000, 10); 102 Histogram* histogram = CreateHistogram("TestHistogram", 1, 1000, 10);
79 103
80 // When StatisticsRecorder is not initialized, register is a noop. 104 // When StatisticsRecorder is not initialized, register is a noop.
81 EXPECT_EQ(histogram, 105 EXPECT_EQ(histogram,
82 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram)); 106 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram));
83 // Manually delete histogram that was not registered. 107 // Manually delete histogram that was not registered.
84 DeleteHistogram(histogram); 108 DeleteHistogram(histogram);
85 109
86 // RegisterOrDeleteDuplicateRanges is a no-op. 110 // RegisterOrDeleteDuplicateRanges is a no-op.
87 BucketRanges* ranges = new BucketRanges(3); 111 BucketRanges* ranges = new BucketRanges(3);
88 ranges->ResetChecksum(); 112 ranges->ResetChecksum();
89 EXPECT_EQ(ranges, 113 EXPECT_EQ(ranges,
90 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges)); 114 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges));
91 StatisticsRecorder::GetBucketRanges(&registered_ranges); 115 StatisticsRecorder::GetBucketRanges(&registered_ranges);
92 EXPECT_EQ(0u, registered_ranges.size()); 116 EXPECT_EQ(0u, registered_ranges.size());
93 } 117 }
94 118
95 TEST_F(StatisticsRecorderTest, RegisterBucketRanges) { 119 TEST_P(StatisticsRecorderTest, RegisterBucketRanges) {
96 std::vector<const BucketRanges*> registered_ranges; 120 std::vector<const BucketRanges*> registered_ranges;
97 121
98 BucketRanges* ranges1 = new BucketRanges(3); 122 BucketRanges* ranges1 = new BucketRanges(3);
99 ranges1->ResetChecksum(); 123 ranges1->ResetChecksum();
100 BucketRanges* ranges2 = new BucketRanges(4); 124 BucketRanges* ranges2 = new BucketRanges(4);
101 ranges2->ResetChecksum(); 125 ranges2->ResetChecksum();
102 126
103 // Register new ranges. 127 // Register new ranges.
104 EXPECT_EQ(ranges1, 128 EXPECT_EQ(ranges1,
105 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges1)); 129 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges1));
(...skipping 17 matching lines...) Expand all
123 // Register ranges with same values. 147 // Register ranges with same values.
124 BucketRanges* ranges3 = new BucketRanges(3); 148 BucketRanges* ranges3 = new BucketRanges(3);
125 ranges3->ResetChecksum(); 149 ranges3->ResetChecksum();
126 EXPECT_EQ(ranges1, // returning ranges1 150 EXPECT_EQ(ranges1, // returning ranges1
127 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges3)); 151 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges3));
128 registered_ranges.clear(); 152 registered_ranges.clear();
129 StatisticsRecorder::GetBucketRanges(&registered_ranges); 153 StatisticsRecorder::GetBucketRanges(&registered_ranges);
130 ASSERT_EQ(2u, registered_ranges.size()); 154 ASSERT_EQ(2u, registered_ranges.size());
131 } 155 }
132 156
133 TEST_F(StatisticsRecorderTest, RegisterHistogram) { 157 TEST_P(StatisticsRecorderTest, RegisterHistogram) {
134 // Create a Histogram that was not registered. 158 // Create a Histogram that was not registered.
135 Histogram* histogram = CreateHistogram("TestHistogram", 1, 1000, 10); 159 Histogram* histogram = CreateHistogram("TestHistogram", 1, 1000, 10);
136 160
137 StatisticsRecorder::Histograms registered_histograms; 161 StatisticsRecorder::Histograms registered_histograms;
138 StatisticsRecorder::GetHistograms(&registered_histograms); 162 StatisticsRecorder::GetHistograms(&registered_histograms);
139 EXPECT_EQ(0u, registered_histograms.size()); 163 EXPECT_EQ(0u, registered_histograms.size());
140 164
141 // Register the Histogram. 165 // Register the Histogram.
142 EXPECT_EQ(histogram, 166 EXPECT_EQ(histogram,
143 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram)); 167 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram));
144 StatisticsRecorder::GetHistograms(&registered_histograms); 168 StatisticsRecorder::GetHistograms(&registered_histograms);
145 EXPECT_EQ(1u, registered_histograms.size()); 169 EXPECT_EQ(1u, registered_histograms.size());
146 170
147 // Register the same Histogram again. 171 // Register the same Histogram again.
148 EXPECT_EQ(histogram, 172 EXPECT_EQ(histogram,
149 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram)); 173 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram));
150 registered_histograms.clear(); 174 registered_histograms.clear();
151 StatisticsRecorder::GetHistograms(&registered_histograms); 175 StatisticsRecorder::GetHistograms(&registered_histograms);
152 EXPECT_EQ(1u, registered_histograms.size()); 176 EXPECT_EQ(1u, registered_histograms.size());
153 } 177 }
154 178
155 TEST_F(StatisticsRecorderTest, FindHistogram) { 179 TEST_P(StatisticsRecorderTest, FindHistogram) {
156 HistogramBase* histogram1 = Histogram::FactoryGet( 180 HistogramBase* histogram1 = Histogram::FactoryGet(
157 "TestHistogram1", 1, 1000, 10, HistogramBase::kNoFlags); 181 "TestHistogram1", 1, 1000, 10, HistogramBase::kNoFlags);
158 HistogramBase* histogram2 = Histogram::FactoryGet( 182 HistogramBase* histogram2 = Histogram::FactoryGet(
159 "TestHistogram2", 1, 1000, 10, HistogramBase::kNoFlags); 183 "TestHistogram2", 1, 1000, 10, HistogramBase::kNoFlags);
160 184
161 EXPECT_EQ(histogram1, StatisticsRecorder::FindHistogram("TestHistogram1")); 185 EXPECT_EQ(histogram1, StatisticsRecorder::FindHistogram("TestHistogram1"));
162 EXPECT_EQ(histogram2, StatisticsRecorder::FindHistogram("TestHistogram2")); 186 EXPECT_EQ(histogram2, StatisticsRecorder::FindHistogram("TestHistogram2"));
163 EXPECT_TRUE(StatisticsRecorder::FindHistogram("TestHistogram") == NULL); 187 EXPECT_FALSE(StatisticsRecorder::FindHistogram("TestHistogram"));
188
189 // Create a new global allocator using the same memory as the old one. Any
190 // old one is kept around so the memory doesn't get released. The two
191 // objects will get released in a backwards order but this is okay because
192 // the destructors aren't allowed to access the underlying memory segment.
193 std::unique_ptr<GlobalHistogramAllocator> old_global =
194 GlobalHistogramAllocator::ReleaseForTesting();
195 if (use_persistent_histogram_allocator_) {
196 GlobalHistogramAllocator::CreateWithPersistentMemory(
197 const_cast<void*>(old_global->data()), old_global->length(), 0,
198 old_global->Id(), old_global->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. The two
401 // objects will get released in a backwards order but this is okay because
402 // the destructors aren't allowed to access the underlying memory segment.
403 std::unique_ptr<GlobalHistogramAllocator> old_global =
404 GlobalHistogramAllocator::ReleaseForTesting();
405 if (use_persistent_histogram_allocator_) {
406 GlobalHistogramAllocator::CreateWithPersistentMemory(
407 const_cast<void*>(old_global->data()), old_global->length(), 0,
408 old_global->Id(), old_global->Name());
409 }
410
411 // Reset statistics-recorder to validate operation from a clean start.
412 UninitializeStatisticsRecorder();
413 InitializeStatisticsRecorder();
414
415 StatisticsRecorder::HistogramIterator i3 = StatisticsRecorder::begin(true);
416 EXPECT_EQ(use_persistent_histogram_allocator_ ? 2 : 0,
417 CountIterableHistograms(i3));
418
419 StatisticsRecorder::HistogramIterator i4 = StatisticsRecorder::begin(false);
420 EXPECT_EQ(0, CountIterableHistograms(i4));
342 } 421 }
343 422
344 namespace { 423 namespace {
345 424
346 // CallbackCheckWrapper is simply a convenient way to check and store that 425 // CallbackCheckWrapper is simply a convenient way to check and store that
347 // a callback was actually run. 426 // a callback was actually run.
348 struct CallbackCheckWrapper { 427 struct CallbackCheckWrapper {
349 CallbackCheckWrapper() : called(false), last_histogram_value(0) {} 428 CallbackCheckWrapper() : called(false), last_histogram_value(0) {}
350 429
351 void OnHistogramChanged(base::HistogramBase::Sample histogram_value) { 430 void OnHistogramChanged(base::HistogramBase::Sample histogram_value) {
352 called = true; 431 called = true;
353 last_histogram_value = histogram_value; 432 last_histogram_value = histogram_value;
354 } 433 }
355 434
356 bool called; 435 bool called;
357 base::HistogramBase::Sample last_histogram_value; 436 base::HistogramBase::Sample last_histogram_value;
358 }; 437 };
359 438
360 } // namespace 439 } // namespace
361 440
362 // Check that you can't overwrite the callback with another. 441 // Check that you can't overwrite the callback with another.
363 TEST_F(StatisticsRecorderTest, SetCallbackFailsWithoutHistogramTest) { 442 TEST_P(StatisticsRecorderTest, SetCallbackFailsWithoutHistogramTest) {
364 CallbackCheckWrapper callback_wrapper; 443 CallbackCheckWrapper callback_wrapper;
365 444
366 bool result = base::StatisticsRecorder::SetCallback( 445 bool result = base::StatisticsRecorder::SetCallback(
367 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 446 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
368 base::Unretained(&callback_wrapper))); 447 base::Unretained(&callback_wrapper)));
369 EXPECT_TRUE(result); 448 EXPECT_TRUE(result);
370 449
371 result = base::StatisticsRecorder::SetCallback( 450 result = base::StatisticsRecorder::SetCallback(
372 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 451 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
373 base::Unretained(&callback_wrapper))); 452 base::Unretained(&callback_wrapper)));
374 EXPECT_FALSE(result); 453 EXPECT_FALSE(result);
375 } 454 }
376 455
377 // Check that you can't overwrite the callback with another. 456 // Check that you can't overwrite the callback with another.
378 TEST_F(StatisticsRecorderTest, SetCallbackFailsWithHistogramTest) { 457 TEST_P(StatisticsRecorderTest, SetCallbackFailsWithHistogramTest) {
379 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, 458 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10,
380 HistogramBase::kNoFlags); 459 HistogramBase::kNoFlags);
381 EXPECT_TRUE(histogram); 460 EXPECT_TRUE(histogram);
382 461
383 CallbackCheckWrapper callback_wrapper; 462 CallbackCheckWrapper callback_wrapper;
384 463
385 bool result = base::StatisticsRecorder::SetCallback( 464 bool result = base::StatisticsRecorder::SetCallback(
386 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 465 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
387 base::Unretained(&callback_wrapper))); 466 base::Unretained(&callback_wrapper)));
388 EXPECT_TRUE(result); 467 EXPECT_TRUE(result);
389 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists, 468 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists,
390 base::HistogramBase::kCallbackExists); 469 base::HistogramBase::kCallbackExists);
391 470
392 result = base::StatisticsRecorder::SetCallback( 471 result = base::StatisticsRecorder::SetCallback(
393 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 472 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
394 base::Unretained(&callback_wrapper))); 473 base::Unretained(&callback_wrapper)));
395 EXPECT_FALSE(result); 474 EXPECT_FALSE(result);
396 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists, 475 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists,
397 base::HistogramBase::kCallbackExists); 476 base::HistogramBase::kCallbackExists);
398 477
399 histogram->Add(1); 478 histogram->Add(1);
400 479
401 EXPECT_TRUE(callback_wrapper.called); 480 EXPECT_TRUE(callback_wrapper.called);
402 } 481 }
403 482
404 // Check that you can't overwrite the callback with another. 483 // Check that you can't overwrite the callback with another.
405 TEST_F(StatisticsRecorderTest, ClearCallbackSuceedsWithHistogramTest) { 484 TEST_P(StatisticsRecorderTest, ClearCallbackSuceedsWithHistogramTest) {
406 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, 485 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10,
407 HistogramBase::kNoFlags); 486 HistogramBase::kNoFlags);
408 EXPECT_TRUE(histogram); 487 EXPECT_TRUE(histogram);
409 488
410 CallbackCheckWrapper callback_wrapper; 489 CallbackCheckWrapper callback_wrapper;
411 490
412 bool result = base::StatisticsRecorder::SetCallback( 491 bool result = base::StatisticsRecorder::SetCallback(
413 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 492 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
414 base::Unretained(&callback_wrapper))); 493 base::Unretained(&callback_wrapper)));
415 EXPECT_TRUE(result); 494 EXPECT_TRUE(result);
416 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists, 495 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists,
417 base::HistogramBase::kCallbackExists); 496 base::HistogramBase::kCallbackExists);
418 497
419 base::StatisticsRecorder::ClearCallback("TestHistogram"); 498 base::StatisticsRecorder::ClearCallback("TestHistogram");
420 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists, 0); 499 EXPECT_EQ(histogram->flags() & base::HistogramBase::kCallbackExists, 0);
421 500
422 histogram->Add(1); 501 histogram->Add(1);
423 502
424 EXPECT_FALSE(callback_wrapper.called); 503 EXPECT_FALSE(callback_wrapper.called);
425 } 504 }
426 505
427 // Check that callback is used. 506 // Check that callback is used.
428 TEST_F(StatisticsRecorderTest, CallbackUsedTest) { 507 TEST_P(StatisticsRecorderTest, CallbackUsedTest) {
429 { 508 {
430 HistogramBase* histogram = Histogram::FactoryGet( 509 HistogramBase* histogram = Histogram::FactoryGet(
431 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags); 510 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
432 EXPECT_TRUE(histogram); 511 EXPECT_TRUE(histogram);
433 512
434 CallbackCheckWrapper callback_wrapper; 513 CallbackCheckWrapper callback_wrapper;
435 514
436 base::StatisticsRecorder::SetCallback( 515 base::StatisticsRecorder::SetCallback(
437 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 516 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
438 base::Unretained(&callback_wrapper))); 517 base::Unretained(&callback_wrapper)));
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 base::Unretained(&callback_wrapper))); 571 base::Unretained(&callback_wrapper)));
493 572
494 custom_histogram->Add(1); 573 custom_histogram->Add(1);
495 574
496 EXPECT_TRUE(callback_wrapper.called); 575 EXPECT_TRUE(callback_wrapper.called);
497 EXPECT_EQ(callback_wrapper.last_histogram_value, 1); 576 EXPECT_EQ(callback_wrapper.last_histogram_value, 1);
498 } 577 }
499 } 578 }
500 579
501 // Check that setting a callback before the histogram exists works. 580 // Check that setting a callback before the histogram exists works.
502 TEST_F(StatisticsRecorderTest, CallbackUsedBeforeHistogramCreatedTest) { 581 TEST_P(StatisticsRecorderTest, CallbackUsedBeforeHistogramCreatedTest) {
503 CallbackCheckWrapper callback_wrapper; 582 CallbackCheckWrapper callback_wrapper;
504 583
505 base::StatisticsRecorder::SetCallback( 584 base::StatisticsRecorder::SetCallback(
506 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged, 585 "TestHistogram", base::Bind(&CallbackCheckWrapper::OnHistogramChanged,
507 base::Unretained(&callback_wrapper))); 586 base::Unretained(&callback_wrapper)));
508 587
509 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, 588 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10,
510 HistogramBase::kNoFlags); 589 HistogramBase::kNoFlags);
511 EXPECT_TRUE(histogram); 590 EXPECT_TRUE(histogram);
512 histogram->Add(1); 591 histogram->Add(1);
513 592
514 EXPECT_TRUE(callback_wrapper.called); 593 EXPECT_TRUE(callback_wrapper.called);
515 EXPECT_EQ(callback_wrapper.last_histogram_value, 1); 594 EXPECT_EQ(callback_wrapper.last_histogram_value, 1);
516 } 595 }
517 596
518 } // namespace base 597 } // namespace base
OLDNEW
« base/metrics/statistics_recorder.cc ('K') | « base/metrics/statistics_recorder.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698