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/histogram.h" | 5 #include "base/metrics/histogram.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 #include <climits> | 12 #include <climits> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
17 #include "base/metrics/bucket_ranges.h" | 17 #include "base/metrics/bucket_ranges.h" |
18 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
| 19 #include "base/metrics/histogram_persistence.h" |
| 20 #include "base/metrics/persistent_memory_allocator.h" |
19 #include "base/metrics/sample_vector.h" | 21 #include "base/metrics/sample_vector.h" |
20 #include "base/metrics/statistics_recorder.h" | 22 #include "base/metrics/statistics_recorder.h" |
21 #include "base/pickle.h" | 23 #include "base/pickle.h" |
22 #include "base/time/time.h" | 24 #include "base/time/time.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
24 | 26 |
25 namespace base { | 27 namespace base { |
26 | 28 |
27 class HistogramTest : public testing::Test { | 29 class HistogramTest : public testing::Test { |
28 protected: | 30 protected: |
| 31 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB |
| 32 |
| 33 HistogramTest() {} |
| 34 |
29 void SetUp() override { | 35 void SetUp() override { |
30 // Each test will have a clean state (no Histogram / BucketRanges | 36 // Each test will have a clean state (no Histogram / BucketRanges |
31 // registered). | 37 // registered). |
32 InitializeStatisticsRecorder(); | 38 InitializeStatisticsRecorder(); |
| 39 // By getting the results-histogram before any persistent allocator |
| 40 // is attached, that histogram is guaranteed not to be stored in |
| 41 // any persistent memory segment (which simplifies some tests). |
| 42 GetCreateHistogramResultHistogram(); |
33 } | 43 } |
34 | 44 |
35 void TearDown() override { UninitializeStatisticsRecorder(); } | 45 void TearDown() override { |
| 46 UninitializeStatisticsRecorder(); |
| 47 DestroyPersistentMemoryAllocator(); |
| 48 } |
36 | 49 |
37 void InitializeStatisticsRecorder() { | 50 void InitializeStatisticsRecorder() { |
38 statistics_recorder_ = new StatisticsRecorder(); | 51 statistics_recorder_ = new StatisticsRecorder(); |
39 } | 52 } |
40 | 53 |
41 void UninitializeStatisticsRecorder() { | 54 void UninitializeStatisticsRecorder() { |
42 delete statistics_recorder_; | 55 delete statistics_recorder_; |
43 statistics_recorder_ = NULL; | 56 statistics_recorder_ = NULL; |
44 } | 57 } |
45 | 58 |
| 59 void CreatePersistentMemoryAllocator() { |
| 60 if (!allocator_memory_) |
| 61 allocator_memory_.reset(new char[kAllocatorMemorySize]); |
| 62 |
| 63 SetPersistentHistogramMemoryAllocator(nullptr); |
| 64 memset(allocator_memory_.get(), 0, kAllocatorMemorySize); |
| 65 SetPersistentHistogramMemoryAllocator( |
| 66 new PersistentMemoryAllocator( |
| 67 allocator_memory_.get(), kAllocatorMemorySize, 0, |
| 68 0, "HistogramAllocatorTest", false)); |
| 69 allocator_ = GetPersistentHistogramMemoryAllocator(); |
| 70 } |
| 71 |
| 72 void DestroyPersistentMemoryAllocator() { |
| 73 allocator_ = nullptr; |
| 74 SetPersistentHistogramMemoryAllocator(nullptr); |
| 75 } |
| 76 |
46 StatisticsRecorder* statistics_recorder_; | 77 StatisticsRecorder* statistics_recorder_; |
| 78 scoped_ptr<char[]> allocator_memory_; |
| 79 PersistentMemoryAllocator* allocator_; |
| 80 |
| 81 private: |
| 82 DISALLOW_COPY_AND_ASSIGN(HistogramTest); |
47 }; | 83 }; |
48 | 84 |
49 // Check for basic syntax and use. | 85 // Check for basic syntax and use. |
50 TEST_F(HistogramTest, BasicTest) { | 86 TEST_F(HistogramTest, BasicTest) { |
51 // Try basic construction | 87 // Try basic construction |
52 HistogramBase* histogram = Histogram::FactoryGet( | 88 HistogramBase* histogram = Histogram::FactoryGet( |
53 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags); | 89 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags); |
54 EXPECT_TRUE(histogram); | 90 EXPECT_TRUE(histogram); |
55 | 91 |
56 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( | 92 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( |
57 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags); | 93 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags); |
58 EXPECT_TRUE(linear_histogram); | 94 EXPECT_TRUE(linear_histogram); |
59 | 95 |
60 std::vector<int> custom_ranges; | 96 std::vector<int> custom_ranges; |
61 custom_ranges.push_back(1); | 97 custom_ranges.push_back(1); |
62 custom_ranges.push_back(5); | 98 custom_ranges.push_back(5); |
63 HistogramBase* custom_histogram = CustomHistogram::FactoryGet( | 99 HistogramBase* custom_histogram = CustomHistogram::FactoryGet( |
64 "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags); | 100 "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags); |
65 EXPECT_TRUE(custom_histogram); | 101 EXPECT_TRUE(custom_histogram); |
66 | 102 |
67 // Use standard macros (but with fixed samples) | 103 // Use standard macros (but with fixed samples) |
68 LOCAL_HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); | 104 LOCAL_HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); |
69 LOCAL_HISTOGRAM_COUNTS("Test3Histogram", 30); | 105 LOCAL_HISTOGRAM_COUNTS("Test3Histogram", 30); |
70 | 106 |
71 LOCAL_HISTOGRAM_ENUMERATION("Test6Histogram", 129, 130); | 107 LOCAL_HISTOGRAM_ENUMERATION("Test6Histogram", 129, 130); |
72 } | 108 } |
73 | 109 |
| 110 // Check for basic syntax and use. |
| 111 TEST_F(HistogramTest, PersistentTest) { |
| 112 CreatePersistentMemoryAllocator(); |
| 113 PersistentMemoryAllocator::MemoryInfo meminfo0; |
| 114 allocator_->GetMemoryInfo(&meminfo0); |
| 115 |
| 116 // Try basic construction |
| 117 HistogramBase* histogram = Histogram::FactoryGet( |
| 118 "TestHistogram", 1, 1000, 10, |
| 119 HistogramBase::kIsPersistent); |
| 120 EXPECT_TRUE(histogram); |
| 121 histogram->CheckName("TestHistogram"); |
| 122 PersistentMemoryAllocator::MemoryInfo meminfo1; |
| 123 allocator_->GetMemoryInfo(&meminfo1); |
| 124 EXPECT_GT(meminfo0.free, meminfo1.free); |
| 125 |
| 126 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( |
| 127 "TestLinearHistogram", 1, 1000, 10, |
| 128 HistogramBase::kIsPersistent); |
| 129 EXPECT_TRUE(linear_histogram); |
| 130 linear_histogram->CheckName("TestLinearHistogram"); |
| 131 PersistentMemoryAllocator::MemoryInfo meminfo2; |
| 132 allocator_->GetMemoryInfo(&meminfo2); |
| 133 EXPECT_GT(meminfo1.free, meminfo2.free); |
| 134 |
| 135 HistogramBase* boolean_histogram = BooleanHistogram::FactoryGet( |
| 136 "TestBooleanHistogram", HistogramBase::kIsPersistent); |
| 137 EXPECT_TRUE(boolean_histogram); |
| 138 boolean_histogram->CheckName("TestBooleanHistogram"); |
| 139 PersistentMemoryAllocator::MemoryInfo meminfo3; |
| 140 allocator_->GetMemoryInfo(&meminfo3); |
| 141 EXPECT_GT(meminfo2.free, meminfo3.free); |
| 142 |
| 143 std::vector<int> custom_ranges; |
| 144 custom_ranges.push_back(1); |
| 145 custom_ranges.push_back(5); |
| 146 HistogramBase* custom_histogram = CustomHistogram::FactoryGet( |
| 147 "TestCustomHistogram", custom_ranges, |
| 148 HistogramBase::kIsPersistent); |
| 149 EXPECT_TRUE(custom_histogram); |
| 150 custom_histogram->CheckName("TestCustomHistogram"); |
| 151 PersistentMemoryAllocator::MemoryInfo meminfo4; |
| 152 allocator_->GetMemoryInfo(&meminfo4); |
| 153 EXPECT_GT(meminfo3.free, meminfo4.free); |
| 154 |
| 155 PersistentMemoryAllocator::Iterator iter; |
| 156 uint32_t type; |
| 157 allocator_->CreateIterator(&iter); |
| 158 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // Histogram |
| 159 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // LinearHistogram |
| 160 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // BooleanHistogram |
| 161 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // CustomHistogram |
| 162 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); |
| 163 |
| 164 // Create a second allocator and have it access the memory of the first. |
| 165 scoped_ptr<HistogramBase> recovered; |
| 166 PersistentMemoryAllocator recovery( |
| 167 allocator_memory_.get(), kAllocatorMemorySize, 0, |
| 168 0, std::string(), false); |
| 169 recovery.CreateIterator(&iter); |
| 170 |
| 171 recovered.reset(GetNextPersistentHistogram(&recovery, &iter)); |
| 172 ASSERT_TRUE(recovered); |
| 173 recovered->CheckName("TestHistogram"); |
| 174 |
| 175 recovered.reset(GetNextPersistentHistogram(&recovery, &iter)); |
| 176 ASSERT_TRUE(recovered); |
| 177 recovered->CheckName("TestLinearHistogram"); |
| 178 |
| 179 recovered.reset(GetNextPersistentHistogram(&recovery, &iter)); |
| 180 ASSERT_TRUE(recovered); |
| 181 recovered->CheckName("TestBooleanHistogram"); |
| 182 |
| 183 recovered.reset(GetNextPersistentHistogram(&recovery, &iter)); |
| 184 ASSERT_TRUE(recovered); |
| 185 recovered->CheckName("TestCustomHistogram"); |
| 186 |
| 187 recovered.reset(GetNextPersistentHistogram(&recovery, &iter)); |
| 188 EXPECT_FALSE(recovered); |
| 189 |
| 190 // Use standard macros (but with fixed samples) |
| 191 LOCAL_HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); |
| 192 LOCAL_HISTOGRAM_COUNTS("Test3Histogram", 30); |
| 193 LOCAL_HISTOGRAM_ENUMERATION("Test6Histogram", 129, 130); |
| 194 } |
| 195 |
74 // Check that the macro correctly matches histograms by name and records their | 196 // Check that the macro correctly matches histograms by name and records their |
75 // data together. | 197 // data together. |
76 TEST_F(HistogramTest, NameMatchTest) { | 198 TEST_F(HistogramTest, NameMatchTest) { |
77 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); | 199 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); |
78 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); | 200 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); |
79 HistogramBase* histogram = LinearHistogram::FactoryGet( | 201 HistogramBase* histogram = LinearHistogram::FactoryGet( |
80 "DuplicatedHistogram", 1, 101, 102, HistogramBase::kNoFlags); | 202 "DuplicatedHistogram", 1, 101, 102, HistogramBase::kNoFlags); |
81 | 203 |
82 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); | 204 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
83 EXPECT_EQ(2, samples->TotalCount()); | 205 EXPECT_EQ(2, samples->TotalCount()); |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 // CustomHistogram needs at least 1 valid range. | 676 // CustomHistogram needs at least 1 valid range. |
555 custom_ranges.clear(); | 677 custom_ranges.clear(); |
556 custom_ranges.push_back(0); | 678 custom_ranges.push_back(0); |
557 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, | 679 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, |
558 HistogramBase::kNoFlags), | 680 HistogramBase::kNoFlags), |
559 ""); | 681 ""); |
560 } | 682 } |
561 #endif | 683 #endif |
562 | 684 |
563 } // namespace base | 685 } // namespace base |
OLD | NEW |