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