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