| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/persistent_histogram_allocator.h" | 5 #include "base/metrics/persistent_histogram_allocator.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/metrics/bucket_ranges.h" | 9 #include "base/metrics/bucket_ranges.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/metrics/persistent_memory_allocator.h" | 11 #include "base/metrics/persistent_memory_allocator.h" |
| 12 #include "base/metrics/statistics_recorder.h" | 12 #include "base/metrics/statistics_recorder.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 | 16 |
| 17 class PersistentHistogramAllocatorTest : public testing::Test { | 17 class PersistentHistogramAllocatorTest : public testing::Test { |
| 18 protected: | 18 protected: |
| 19 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB | 19 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB |
| 20 | 20 |
| 21 PersistentHistogramAllocatorTest() { CreatePersistentHistogramAllocator(); } | 21 PersistentHistogramAllocatorTest() |
| 22 : statistics_recorder_(StatisticsRecorder::CreateTemporaryForTesting()) { |
| 23 CreatePersistentHistogramAllocator(); |
| 24 } |
| 22 ~PersistentHistogramAllocatorTest() override { | 25 ~PersistentHistogramAllocatorTest() override { |
| 23 DestroyPersistentHistogramAllocator(); | 26 DestroyPersistentHistogramAllocator(); |
| 24 } | 27 } |
| 25 | 28 |
| 26 void CreatePersistentHistogramAllocator() { | 29 void CreatePersistentHistogramAllocator() { |
| 27 allocator_memory_.reset(new char[kAllocatorMemorySize]); | 30 allocator_memory_.reset(new char[kAllocatorMemorySize]); |
| 28 | 31 |
| 29 GlobalHistogramAllocator::ReleaseForTesting(); | 32 GlobalHistogramAllocator::ReleaseForTesting(); |
| 30 memset(allocator_memory_.get(), 0, kAllocatorMemorySize); | 33 memset(allocator_memory_.get(), 0, kAllocatorMemorySize); |
| 31 GlobalHistogramAllocator::GetCreateHistogramResultHistogram(); | 34 GlobalHistogramAllocator::GetCreateHistogramResultHistogram(); |
| 32 GlobalHistogramAllocator::CreateWithPersistentMemory( | 35 GlobalHistogramAllocator::CreateWithPersistentMemory( |
| 33 allocator_memory_.get(), kAllocatorMemorySize, 0, 0, | 36 allocator_memory_.get(), kAllocatorMemorySize, 0, 0, |
| 34 "PersistentHistogramAllocatorTest"); | 37 "PersistentHistogramAllocatorTest"); |
| 35 allocator_ = GlobalHistogramAllocator::Get()->memory_allocator(); | 38 allocator_ = GlobalHistogramAllocator::Get()->memory_allocator(); |
| 36 } | 39 } |
| 37 | 40 |
| 38 void DestroyPersistentHistogramAllocator() { | 41 void DestroyPersistentHistogramAllocator() { |
| 39 allocator_ = nullptr; | 42 allocator_ = nullptr; |
| 40 GlobalHistogramAllocator::ReleaseForTesting(); | 43 GlobalHistogramAllocator::ReleaseForTesting(); |
| 41 } | 44 } |
| 42 | 45 |
| 43 std::unique_ptr<StatisticsRecorder> CreateLocalStatisticsRecorder() { | 46 std::unique_ptr<StatisticsRecorder> statistics_recorder_; |
| 44 return WrapUnique(new StatisticsRecorder()); | |
| 45 } | |
| 46 | |
| 47 StatisticsRecorder statistics_recorder_; | |
| 48 std::unique_ptr<char[]> allocator_memory_; | 47 std::unique_ptr<char[]> allocator_memory_; |
| 49 PersistentMemoryAllocator* allocator_ = nullptr; | 48 PersistentMemoryAllocator* allocator_ = nullptr; |
| 50 | 49 |
| 51 private: | 50 private: |
| 52 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocatorTest); | 51 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocatorTest); |
| 53 }; | 52 }; |
| 54 | 53 |
| 55 TEST_F(PersistentHistogramAllocatorTest, CreateAndIterateTest) { | 54 TEST_F(PersistentHistogramAllocatorTest, CreateAndIterateTest) { |
| 56 PersistentMemoryAllocator::MemoryInfo meminfo0; | 55 PersistentMemoryAllocator::MemoryInfo meminfo0; |
| 57 allocator_->GetMemoryInfo(&meminfo0); | 56 allocator_->GetMemoryInfo(&meminfo0); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 recovered = histogram_iter.GetNext(); | 125 recovered = histogram_iter.GetNext(); |
| 127 EXPECT_FALSE(recovered); | 126 EXPECT_FALSE(recovered); |
| 128 } | 127 } |
| 129 | 128 |
| 130 TEST_F(PersistentHistogramAllocatorTest, StatisticsRecorderTest) { | 129 TEST_F(PersistentHistogramAllocatorTest, StatisticsRecorderTest) { |
| 131 size_t starting_sr_count = StatisticsRecorder::GetHistogramCount(); | 130 size_t starting_sr_count = StatisticsRecorder::GetHistogramCount(); |
| 132 | 131 |
| 133 // Create a local StatisticsRecorder in which the newly created histogram | 132 // Create a local StatisticsRecorder in which the newly created histogram |
| 134 // will be recorded. | 133 // will be recorded. |
| 135 std::unique_ptr<StatisticsRecorder> local_sr = | 134 std::unique_ptr<StatisticsRecorder> local_sr = |
| 136 CreateLocalStatisticsRecorder(); | 135 StatisticsRecorder::CreateTemporaryForTesting(); |
| 137 EXPECT_EQ(0U, StatisticsRecorder::GetHistogramCount()); | 136 EXPECT_EQ(0U, StatisticsRecorder::GetHistogramCount()); |
| 138 | 137 |
| 139 HistogramBase* histogram = LinearHistogram::FactoryGet( | 138 HistogramBase* histogram = LinearHistogram::FactoryGet( |
| 140 "TestHistogram", 1, 10, 10, HistogramBase::kIsPersistent); | 139 "TestHistogram", 1, 10, 10, HistogramBase::kIsPersistent); |
| 141 EXPECT_TRUE(histogram); | 140 EXPECT_TRUE(histogram); |
| 142 EXPECT_EQ(1U, StatisticsRecorder::GetHistogramCount()); | 141 EXPECT_EQ(1U, StatisticsRecorder::GetHistogramCount()); |
| 143 histogram->Add(3); | 142 histogram->Add(3); |
| 144 histogram->Add(1); | 143 histogram->Add(1); |
| 145 histogram->Add(4); | 144 histogram->Add(4); |
| 146 histogram->Add(1); | 145 histogram->Add(1); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 171 // Ensure that the data got merged, too. | 170 // Ensure that the data got merged, too. |
| 172 std::unique_ptr<HistogramSamples> snapshot = found->SnapshotSamples(); | 171 std::unique_ptr<HistogramSamples> snapshot = found->SnapshotSamples(); |
| 173 EXPECT_EQ(recovered->SnapshotSamples()->TotalCount(), snapshot->TotalCount()); | 172 EXPECT_EQ(recovered->SnapshotSamples()->TotalCount(), snapshot->TotalCount()); |
| 174 EXPECT_EQ(1, snapshot->GetCount(3)); | 173 EXPECT_EQ(1, snapshot->GetCount(3)); |
| 175 EXPECT_EQ(2, snapshot->GetCount(1)); | 174 EXPECT_EQ(2, snapshot->GetCount(1)); |
| 176 EXPECT_EQ(1, snapshot->GetCount(4)); | 175 EXPECT_EQ(1, snapshot->GetCount(4)); |
| 177 EXPECT_EQ(1, snapshot->GetCount(6)); | 176 EXPECT_EQ(1, snapshot->GetCount(6)); |
| 178 } | 177 } |
| 179 | 178 |
| 180 } // namespace base | 179 } // namespace base |
| OLD | NEW |