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