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 "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 | 16 |
16 class PersistentHistogramAllocatorTest : public testing::Test { | 17 class PersistentHistogramAllocatorTest : public testing::Test { |
17 protected: | 18 protected: |
18 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB | 19 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB |
19 | 20 |
20 PersistentHistogramAllocatorTest() { CreatePersistentHistogramAllocator(); } | 21 PersistentHistogramAllocatorTest() { CreatePersistentHistogramAllocator(); } |
21 ~PersistentHistogramAllocatorTest() override { | 22 ~PersistentHistogramAllocatorTest() override { |
(...skipping 10 matching lines...) Expand all Loading... |
32 allocator_memory_.get(), kAllocatorMemorySize, 0, 0, | 33 allocator_memory_.get(), kAllocatorMemorySize, 0, 0, |
33 "PersistentHistogramAllocatorTest"); | 34 "PersistentHistogramAllocatorTest"); |
34 allocator_ = GlobalHistogramAllocator::Get()->memory_allocator(); | 35 allocator_ = GlobalHistogramAllocator::Get()->memory_allocator(); |
35 } | 36 } |
36 | 37 |
37 void DestroyPersistentHistogramAllocator() { | 38 void DestroyPersistentHistogramAllocator() { |
38 allocator_ = nullptr; | 39 allocator_ = nullptr; |
39 GlobalHistogramAllocator::ReleaseForTesting(); | 40 GlobalHistogramAllocator::ReleaseForTesting(); |
40 } | 41 } |
41 | 42 |
| 43 std::unique_ptr<StatisticsRecorder> CreateLocalStatisticsRecorder() { |
| 44 return WrapUnique(new StatisticsRecorder()); |
| 45 } |
| 46 |
| 47 StatisticsRecorder statistics_recorder_; |
42 std::unique_ptr<char[]> allocator_memory_; | 48 std::unique_ptr<char[]> allocator_memory_; |
43 PersistentMemoryAllocator* allocator_ = nullptr; | 49 PersistentMemoryAllocator* allocator_ = nullptr; |
44 | 50 |
45 private: | 51 private: |
46 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocatorTest); | 52 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocatorTest); |
47 }; | 53 }; |
48 | 54 |
49 TEST_F(PersistentHistogramAllocatorTest, CreateAndIterateTest) { | 55 TEST_F(PersistentHistogramAllocatorTest, CreateAndIterateTest) { |
50 PersistentMemoryAllocator::MemoryInfo meminfo0; | 56 PersistentMemoryAllocator::MemoryInfo meminfo0; |
51 allocator_->GetMemoryInfo(&meminfo0); | 57 allocator_->GetMemoryInfo(&meminfo0); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 recovered->CheckName("TestBooleanHistogram"); | 120 recovered->CheckName("TestBooleanHistogram"); |
115 | 121 |
116 recovered = histogram_iter.GetNext(); | 122 recovered = histogram_iter.GetNext(); |
117 ASSERT_TRUE(recovered); | 123 ASSERT_TRUE(recovered); |
118 recovered->CheckName("TestCustomHistogram"); | 124 recovered->CheckName("TestCustomHistogram"); |
119 | 125 |
120 recovered = histogram_iter.GetNext(); | 126 recovered = histogram_iter.GetNext(); |
121 EXPECT_FALSE(recovered); | 127 EXPECT_FALSE(recovered); |
122 } | 128 } |
123 | 129 |
| 130 TEST_F(PersistentHistogramAllocatorTest, StatisticsRecorderTest) { |
| 131 size_t starting_sr_count = StatisticsRecorder::GetHistogramCount(); |
| 132 |
| 133 // Create a local StatisticsRecorder in which the newly created histogram |
| 134 // will be recorded. |
| 135 std::unique_ptr<StatisticsRecorder> local_sr = |
| 136 CreateLocalStatisticsRecorder(); |
| 137 EXPECT_EQ(0U, StatisticsRecorder::GetHistogramCount()); |
| 138 |
| 139 HistogramBase* histogram = LinearHistogram::FactoryGet( |
| 140 "TestHistogram", 1, 10, 10, HistogramBase::kIsPersistent); |
| 141 EXPECT_TRUE(histogram); |
| 142 EXPECT_EQ(1U, StatisticsRecorder::GetHistogramCount()); |
| 143 histogram->Add(3); |
| 144 histogram->Add(1); |
| 145 histogram->Add(4); |
| 146 histogram->Add(1); |
| 147 histogram->Add(6); |
| 148 |
| 149 // Destroy the local SR and ensure that we're back to the initial state. |
| 150 local_sr.reset(); |
| 151 EXPECT_EQ(starting_sr_count, StatisticsRecorder::GetHistogramCount()); |
| 152 |
| 153 // Create a second allocator and have it access the memory of the first. |
| 154 std::unique_ptr<HistogramBase> recovered; |
| 155 PersistentHistogramAllocator recovery( |
| 156 WrapUnique(new PersistentMemoryAllocator( |
| 157 allocator_memory_.get(), kAllocatorMemorySize, 0, 0, "", false))); |
| 158 PersistentHistogramAllocator::Iterator histogram_iter(&recovery); |
| 159 |
| 160 recovered = histogram_iter.GetNext(); |
| 161 ASSERT_TRUE(recovered); |
| 162 |
| 163 // Merge the recovered histogram to the SR. It will always be a new object. |
| 164 recovery.MergeHistogramToStatisticsRecorder(recovered.get()); |
| 165 EXPECT_EQ(starting_sr_count + 1, StatisticsRecorder::GetHistogramCount()); |
| 166 HistogramBase* found = |
| 167 StatisticsRecorder::FindHistogram(recovered->histogram_name()); |
| 168 ASSERT_TRUE(found); |
| 169 EXPECT_NE(recovered.get(), found); |
| 170 |
| 171 // Ensure that the data got merged, too. |
| 172 std::unique_ptr<HistogramSamples> snapshot = found->SnapshotSamples(); |
| 173 EXPECT_EQ(recovered->SnapshotSamples()->TotalCount(), snapshot->TotalCount()); |
| 174 EXPECT_EQ(1, snapshot->GetCount(3)); |
| 175 EXPECT_EQ(2, snapshot->GetCount(1)); |
| 176 EXPECT_EQ(1, snapshot->GetCount(4)); |
| 177 EXPECT_EQ(1, snapshot->GetCount(6)); |
| 178 } |
| 179 |
124 } // namespace base | 180 } // namespace base |
OLD | NEW |