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 "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/logging.h" |
14 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
15 #include "base/metrics/persistent_histogram_allocator.h" | 16 #include "base/metrics/persistent_histogram_allocator.h" |
16 #include "base/metrics/sparse_histogram.h" | 17 #include "base/metrics/sparse_histogram.h" |
17 #include "base/values.h" | 18 #include "base/values.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
| 21 namespace { |
| 22 |
| 23 // Class to make sure any manipulations we do to the min log level are |
| 24 // contained (i.e., do not affect other unit tests). |
| 25 class LogStateSaver { |
| 26 public: |
| 27 LogStateSaver() : old_min_log_level_(logging::GetMinLogLevel()) {} |
| 28 |
| 29 ~LogStateSaver() { |
| 30 logging::SetMinLogLevel(old_min_log_level_); |
| 31 logging::SetLogAssertHandler(nullptr); |
| 32 } |
| 33 |
| 34 private: |
| 35 int old_min_log_level_; |
| 36 |
| 37 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); |
| 38 }; |
| 39 |
| 40 } // namespace |
| 41 |
20 namespace base { | 42 namespace base { |
21 | 43 |
22 class StatisticsRecorderTest : public testing::TestWithParam<bool> { | 44 class StatisticsRecorderTest : public testing::TestWithParam<bool> { |
23 protected: | 45 protected: |
24 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB | 46 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB |
25 | 47 |
26 StatisticsRecorderTest() : use_persistent_histogram_allocator_(GetParam()) { | 48 StatisticsRecorderTest() : use_persistent_histogram_allocator_(GetParam()) { |
27 // Get this first so it never gets created in persistent storage and will | 49 // Get this first so it never gets created in persistent storage and will |
28 // not appear in the StatisticsRecorder after it is re-initialized. | 50 // not appear in the StatisticsRecorder after it is re-initialized. |
29 PersistentHistogramAllocator::GetCreateHistogramResultHistogram(); | 51 PersistentHistogramAllocator::GetCreateHistogramResultHistogram(); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 } | 93 } |
72 | 94 |
73 int CountIterableHistograms(StatisticsRecorder::HistogramIterator* iter) { | 95 int CountIterableHistograms(StatisticsRecorder::HistogramIterator* iter) { |
74 int count = 0; | 96 int count = 0; |
75 for (; *iter != StatisticsRecorder::end(); ++*iter) { | 97 for (; *iter != StatisticsRecorder::end(); ++*iter) { |
76 ++count; | 98 ++count; |
77 } | 99 } |
78 return count; | 100 return count; |
79 } | 101 } |
80 | 102 |
| 103 void InitLogOnShutdown() { |
| 104 DCHECK(statistics_recorder_); |
| 105 statistics_recorder_->InitLogOnShutdownWithoutLock(); |
| 106 } |
| 107 |
| 108 bool VLogInitialized() { |
| 109 DCHECK(statistics_recorder_); |
| 110 return statistics_recorder_->vlog_initialized_; |
| 111 } |
| 112 |
81 const bool use_persistent_histogram_allocator_; | 113 const bool use_persistent_histogram_allocator_; |
82 | 114 |
83 std::unique_ptr<StatisticsRecorder> statistics_recorder_; | 115 std::unique_ptr<StatisticsRecorder> statistics_recorder_; |
84 std::unique_ptr<GlobalHistogramAllocator> old_global_allocator_; | 116 std::unique_ptr<GlobalHistogramAllocator> old_global_allocator_; |
85 | 117 |
86 private: | 118 private: |
| 119 LogStateSaver log_state_saver_; |
| 120 |
87 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorderTest); | 121 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorderTest); |
88 }; | 122 }; |
89 | 123 |
90 // Run all HistogramTest cases with both heap and persistent memory. | 124 // Run all HistogramTest cases with both heap and persistent memory. |
91 INSTANTIATE_TEST_CASE_P(Allocator, StatisticsRecorderTest, testing::Bool()); | 125 INSTANTIATE_TEST_CASE_P(Allocator, StatisticsRecorderTest, testing::Bool()); |
92 | 126 |
93 TEST_P(StatisticsRecorderTest, NotInitialized) { | 127 TEST_P(StatisticsRecorderTest, NotInitialized) { |
94 UninitializeStatisticsRecorder(); | 128 UninitializeStatisticsRecorder(); |
95 | 129 |
96 ASSERT_FALSE(StatisticsRecorder::IsActive()); | 130 ASSERT_FALSE(StatisticsRecorder::IsActive()); |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 | 619 |
586 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, | 620 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, |
587 HistogramBase::kNoFlags); | 621 HistogramBase::kNoFlags); |
588 EXPECT_TRUE(histogram); | 622 EXPECT_TRUE(histogram); |
589 histogram->Add(1); | 623 histogram->Add(1); |
590 | 624 |
591 EXPECT_TRUE(callback_wrapper.called); | 625 EXPECT_TRUE(callback_wrapper.called); |
592 EXPECT_EQ(callback_wrapper.last_histogram_value, 1); | 626 EXPECT_EQ(callback_wrapper.last_histogram_value, 1); |
593 } | 627 } |
594 | 628 |
| 629 TEST_P(StatisticsRecorderTest, LogOnShutdownNotInitialized) { |
| 630 UninitializeStatisticsRecorder(); |
| 631 logging::SetMinLogLevel(logging::LOG_WARNING); |
| 632 InitializeStatisticsRecorder(); |
| 633 EXPECT_FALSE(VLOG_IS_ON(1)); |
| 634 EXPECT_FALSE(VLogInitialized()); |
| 635 InitLogOnShutdown(); |
| 636 EXPECT_FALSE(VLogInitialized()); |
| 637 } |
| 638 |
| 639 TEST_P(StatisticsRecorderTest, LogOnShutdownInitializedExplicitly) { |
| 640 UninitializeStatisticsRecorder(); |
| 641 logging::SetMinLogLevel(logging::LOG_WARNING); |
| 642 InitializeStatisticsRecorder(); |
| 643 EXPECT_FALSE(VLOG_IS_ON(1)); |
| 644 EXPECT_FALSE(VLogInitialized()); |
| 645 logging::SetMinLogLevel(logging::LOG_VERBOSE); |
| 646 EXPECT_TRUE(VLOG_IS_ON(1)); |
| 647 InitLogOnShutdown(); |
| 648 EXPECT_TRUE(VLogInitialized()); |
| 649 } |
| 650 |
| 651 TEST_P(StatisticsRecorderTest, LogOnShutdownInitialized) { |
| 652 UninitializeStatisticsRecorder(); |
| 653 logging::SetMinLogLevel(logging::LOG_VERBOSE); |
| 654 InitializeStatisticsRecorder(); |
| 655 EXPECT_TRUE(VLOG_IS_ON(1)); |
| 656 EXPECT_TRUE(VLogInitialized()); |
| 657 } |
| 658 |
595 } // namespace base | 659 } // namespace base |
OLD | NEW |