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