OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/metrics/persistent_histogram_allocator.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/metrics/bucket_ranges.h" |
| 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/metrics/persistent_memory_allocator.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace base { |
| 14 |
| 15 class PersistentHistogramAllocatorTest : public testing::Test { |
| 16 protected: |
| 17 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB |
| 18 |
| 19 PersistentHistogramAllocatorTest() {} |
| 20 |
| 21 void SetUp() override { CreatePersistentHistogramAllocator(); } |
| 22 |
| 23 void TearDown() override { DestroyPersistentHistogramAllocator(); } |
| 24 |
| 25 void CreatePersistentHistogramAllocator() { |
| 26 if (!allocator_memory_) |
| 27 allocator_memory_.reset(new char[kAllocatorMemorySize]); |
| 28 |
| 29 PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); |
| 30 memset(allocator_memory_.get(), 0, kAllocatorMemorySize); |
| 31 PersistentHistogramAllocator::GetCreateHistogramResultHistogram(); |
| 32 PersistentHistogramAllocator::SetGlobalAllocator( |
| 33 make_scoped_ptr(new PersistentHistogramAllocator( |
| 34 make_scoped_ptr(new PersistentMemoryAllocator( |
| 35 allocator_memory_.get(), kAllocatorMemorySize, 0, 0, |
| 36 "PersistentHistogramAllocatorTest", false))))); |
| 37 allocator_ = |
| 38 PersistentHistogramAllocator::GetGlobalAllocator()->memory_allocator(); |
| 39 } |
| 40 |
| 41 void DestroyPersistentHistogramAllocator() { |
| 42 allocator_ = nullptr; |
| 43 PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); |
| 44 } |
| 45 |
| 46 scoped_ptr<char[]> allocator_memory_; |
| 47 PersistentMemoryAllocator* allocator_ = nullptr; |
| 48 |
| 49 private: |
| 50 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocatorTest); |
| 51 }; |
| 52 |
| 53 TEST_F(PersistentHistogramAllocatorTest, CreateAndIterateTest) { |
| 54 PersistentMemoryAllocator::MemoryInfo meminfo0; |
| 55 allocator_->GetMemoryInfo(&meminfo0); |
| 56 |
| 57 // Try basic construction |
| 58 HistogramBase* histogram = Histogram::FactoryGet( |
| 59 "TestHistogram", 1, 1000, 10, HistogramBase::kIsPersistent); |
| 60 EXPECT_TRUE(histogram); |
| 61 histogram->CheckName("TestHistogram"); |
| 62 PersistentMemoryAllocator::MemoryInfo meminfo1; |
| 63 allocator_->GetMemoryInfo(&meminfo1); |
| 64 EXPECT_GT(meminfo0.free, meminfo1.free); |
| 65 |
| 66 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( |
| 67 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kIsPersistent); |
| 68 EXPECT_TRUE(linear_histogram); |
| 69 linear_histogram->CheckName("TestLinearHistogram"); |
| 70 PersistentMemoryAllocator::MemoryInfo meminfo2; |
| 71 allocator_->GetMemoryInfo(&meminfo2); |
| 72 EXPECT_GT(meminfo1.free, meminfo2.free); |
| 73 |
| 74 HistogramBase* boolean_histogram = BooleanHistogram::FactoryGet( |
| 75 "TestBooleanHistogram", HistogramBase::kIsPersistent); |
| 76 EXPECT_TRUE(boolean_histogram); |
| 77 boolean_histogram->CheckName("TestBooleanHistogram"); |
| 78 PersistentMemoryAllocator::MemoryInfo meminfo3; |
| 79 allocator_->GetMemoryInfo(&meminfo3); |
| 80 EXPECT_GT(meminfo2.free, meminfo3.free); |
| 81 |
| 82 std::vector<int> custom_ranges; |
| 83 custom_ranges.push_back(1); |
| 84 custom_ranges.push_back(5); |
| 85 HistogramBase* custom_histogram = CustomHistogram::FactoryGet( |
| 86 "TestCustomHistogram", custom_ranges, HistogramBase::kIsPersistent); |
| 87 EXPECT_TRUE(custom_histogram); |
| 88 custom_histogram->CheckName("TestCustomHistogram"); |
| 89 PersistentMemoryAllocator::MemoryInfo meminfo4; |
| 90 allocator_->GetMemoryInfo(&meminfo4); |
| 91 EXPECT_GT(meminfo3.free, meminfo4.free); |
| 92 |
| 93 PersistentMemoryAllocator::Iterator iter; |
| 94 uint32_t type; |
| 95 allocator_->CreateIterator(&iter); |
| 96 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // Histogram |
| 97 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // LinearHistogram |
| 98 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // BooleanHistogram |
| 99 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // CustomHistogram |
| 100 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); |
| 101 |
| 102 // Create a second allocator and have it access the memory of the first. |
| 103 scoped_ptr<HistogramBase> recovered; |
| 104 PersistentHistogramAllocator recovery( |
| 105 make_scoped_ptr(new PersistentMemoryAllocator( |
| 106 allocator_memory_.get(), kAllocatorMemorySize, 0, 0, "", false))); |
| 107 PersistentHistogramAllocator::Iterator histogram_iter; |
| 108 recovery.CreateIterator(&histogram_iter); |
| 109 |
| 110 recovered = recovery.GetNextHistogram(&histogram_iter); |
| 111 ASSERT_TRUE(recovered); |
| 112 recovered->CheckName("TestHistogram"); |
| 113 |
| 114 recovered = recovery.GetNextHistogram(&histogram_iter); |
| 115 ASSERT_TRUE(recovered); |
| 116 recovered->CheckName("TestLinearHistogram"); |
| 117 |
| 118 recovered = recovery.GetNextHistogram(&histogram_iter); |
| 119 ASSERT_TRUE(recovered); |
| 120 recovered->CheckName("TestBooleanHistogram"); |
| 121 |
| 122 recovered = recovery.GetNextHistogram(&histogram_iter); |
| 123 ASSERT_TRUE(recovered); |
| 124 recovered->CheckName("TestCustomHistogram"); |
| 125 |
| 126 recovered = recovery.GetNextHistogram(&histogram_iter); |
| 127 EXPECT_FALSE(recovered); |
| 128 } |
| 129 |
| 130 } // namespace base |
OLD | NEW |