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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/metrics/histogram_persistence.h" |
11 #include "base/metrics/sparse_histogram.h" | 12 #include "base/metrics/sparse_histogram.h" |
12 #include "base/metrics/statistics_recorder.h" | 13 #include "base/metrics/statistics_recorder.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 | 18 |
18 class StatisticsRecorderTest : public testing::Test { | 19 class StatisticsRecorderTest : public testing::Test { |
19 protected: | 20 protected: |
20 void SetUp() override { | 21 void SetUp() override { |
21 // Each test will have a clean state (no Histogram / BucketRanges | 22 // Each test will have a clean state (no Histogram / BucketRanges |
22 // registered). | 23 // registered). |
23 InitializeStatisticsRecorder(); | 24 InitializeStatisticsRecorder(); |
24 } | 25 } |
25 | 26 |
26 void TearDown() override { UninitializeStatisticsRecorder(); } | 27 void TearDown() override { |
| 28 UninitializeStatisticsRecorder(); |
| 29 SetPersistentHistogramMemoryAllocator(nullptr); |
| 30 } |
27 | 31 |
28 void InitializeStatisticsRecorder() { | 32 void InitializeStatisticsRecorder() { |
29 statistics_recorder_ = new StatisticsRecorder(); | 33 statistics_recorder_ = new StatisticsRecorder(); |
30 } | 34 } |
31 | 35 |
32 void UninitializeStatisticsRecorder() { | 36 void UninitializeStatisticsRecorder() { |
33 delete statistics_recorder_; | 37 delete statistics_recorder_; |
34 statistics_recorder_ = NULL; | 38 statistics_recorder_ = NULL; |
35 } | 39 } |
36 | 40 |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 EXPECT_EQ("TestHistogram2", histogram_name); | 310 EXPECT_EQ("TestHistogram2", histogram_name); |
307 | 311 |
308 json.clear(); | 312 json.clear(); |
309 UninitializeStatisticsRecorder(); | 313 UninitializeStatisticsRecorder(); |
310 | 314 |
311 // No data should be returned. | 315 // No data should be returned. |
312 json = StatisticsRecorder::ToJSON(query); | 316 json = StatisticsRecorder::ToJSON(query); |
313 EXPECT_TRUE(json.empty()); | 317 EXPECT_TRUE(json.empty()); |
314 } | 318 } |
315 | 319 |
| 320 TEST_F(StatisticsRecorderTest, IterationTest) { |
| 321 StatisticsRecorder::Histograms registered_histograms; |
| 322 LOCAL_HISTOGRAM_COUNTS("TestHistogram.IterationTest1", 30); |
| 323 SetPersistentHistogramMemoryAllocator( |
| 324 new LocalPersistentMemoryAllocator(64 << 10, 0, std::string())); |
| 325 LOCAL_HISTOGRAM_COUNTS("TestHistogram.IterationTest2", 30); |
| 326 |
| 327 StatisticsRecorder::HistogramIterator i1 = StatisticsRecorder::begin(true); |
| 328 EXPECT_NE(StatisticsRecorder::end(), i1); |
| 329 EXPECT_NE(StatisticsRecorder::end(), ++i1); |
| 330 EXPECT_EQ(StatisticsRecorder::end(), ++i1); |
| 331 |
| 332 StatisticsRecorder::HistogramIterator i2 = StatisticsRecorder::begin(false); |
| 333 EXPECT_NE(StatisticsRecorder::end(), i2); |
| 334 EXPECT_EQ(StatisticsRecorder::end(), ++i2); |
| 335 } |
| 336 |
316 namespace { | 337 namespace { |
317 | 338 |
318 // CallbackCheckWrapper is simply a convenient way to check and store that | 339 // CallbackCheckWrapper is simply a convenient way to check and store that |
319 // a callback was actually run. | 340 // a callback was actually run. |
320 struct CallbackCheckWrapper { | 341 struct CallbackCheckWrapper { |
321 CallbackCheckWrapper() : called(false), last_histogram_value(0) {} | 342 CallbackCheckWrapper() : called(false), last_histogram_value(0) {} |
322 | 343 |
323 void OnHistogramChanged(base::HistogramBase::Sample histogram_value) { | 344 void OnHistogramChanged(base::HistogramBase::Sample histogram_value) { |
324 called = true; | 345 called = true; |
325 last_histogram_value = histogram_value; | 346 last_histogram_value = histogram_value; |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, | 502 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, |
482 HistogramBase::kNoFlags); | 503 HistogramBase::kNoFlags); |
483 EXPECT_TRUE(histogram); | 504 EXPECT_TRUE(histogram); |
484 histogram->Add(1); | 505 histogram->Add(1); |
485 | 506 |
486 EXPECT_TRUE(callback_wrapper.called); | 507 EXPECT_TRUE(callback_wrapper.called); |
487 EXPECT_EQ(callback_wrapper.last_histogram_value, 1); | 508 EXPECT_EQ(callback_wrapper.last_histogram_value, 1); |
488 } | 509 } |
489 | 510 |
490 } // namespace base | 511 } // namespace base |
OLD | NEW |