| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 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 | 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_sample_map.h" | 5 #include "base/metrics/persistent_sample_map.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/metrics/persistent_histogram_allocator.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 11 |
| 11 namespace base { | 12 namespace base { |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 TEST(PersistentSampleMapTest, AccumulateTest) { | 15 TEST(PersistentSampleMapTest, AccumulateTest) { |
| 15 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB | 16 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 16 | 17 |
| 17 HistogramSamples::Metadata* meta = | 18 HistogramSamples::Metadata meta; |
| 18 allocator.GetAsObject<HistogramSamples::Metadata>( | 19 PersistentSparseHistogramDataManager manager(&allocator); |
| 19 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); | 20 PersistentSampleMap samples(1, &manager, &meta); |
| 20 PersistentSampleMap samples(1, &allocator, meta); | |
| 21 | 21 |
| 22 samples.Accumulate(1, 100); | 22 samples.Accumulate(1, 100); |
| 23 samples.Accumulate(2, 200); | 23 samples.Accumulate(2, 200); |
| 24 samples.Accumulate(1, -200); | 24 samples.Accumulate(1, -200); |
| 25 EXPECT_EQ(-100, samples.GetCount(1)); | 25 EXPECT_EQ(-100, samples.GetCount(1)); |
| 26 EXPECT_EQ(200, samples.GetCount(2)); | 26 EXPECT_EQ(200, samples.GetCount(2)); |
| 27 | 27 |
| 28 EXPECT_EQ(300, samples.sum()); | 28 EXPECT_EQ(300, samples.sum()); |
| 29 EXPECT_EQ(100, samples.TotalCount()); | 29 EXPECT_EQ(100, samples.TotalCount()); |
| 30 EXPECT_EQ(samples.redundant_count(), samples.TotalCount()); | 30 EXPECT_EQ(samples.redundant_count(), samples.TotalCount()); |
| 31 } | 31 } |
| 32 | 32 |
| 33 TEST(PersistentSampleMapTest, Accumulate_LargeValuesDontOverflow) { | 33 TEST(PersistentSampleMapTest, Accumulate_LargeValuesDontOverflow) { |
| 34 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB | 34 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 35 | 35 |
| 36 HistogramSamples::Metadata* meta = | 36 HistogramSamples::Metadata meta; |
| 37 allocator.GetAsObject<HistogramSamples::Metadata>( | 37 PersistentSparseHistogramDataManager manager(&allocator); |
| 38 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); | 38 PersistentSampleMap samples(1, &manager, &meta); |
| 39 PersistentSampleMap samples(1, &allocator, meta); | |
| 40 | 39 |
| 41 samples.Accumulate(250000000, 100); | 40 samples.Accumulate(250000000, 100); |
| 42 samples.Accumulate(500000000, 200); | 41 samples.Accumulate(500000000, 200); |
| 43 samples.Accumulate(250000000, -200); | 42 samples.Accumulate(250000000, -200); |
| 44 EXPECT_EQ(-100, samples.GetCount(250000000)); | 43 EXPECT_EQ(-100, samples.GetCount(250000000)); |
| 45 EXPECT_EQ(200, samples.GetCount(500000000)); | 44 EXPECT_EQ(200, samples.GetCount(500000000)); |
| 46 | 45 |
| 47 EXPECT_EQ(75000000000LL, samples.sum()); | 46 EXPECT_EQ(75000000000LL, samples.sum()); |
| 48 EXPECT_EQ(100, samples.TotalCount()); | 47 EXPECT_EQ(100, samples.TotalCount()); |
| 49 EXPECT_EQ(samples.redundant_count(), samples.TotalCount()); | 48 EXPECT_EQ(samples.redundant_count(), samples.TotalCount()); |
| 50 } | 49 } |
| 51 | 50 |
| 52 TEST(PersistentSampleMapTest, AddSubtractTest) { | 51 TEST(PersistentSampleMapTest, AddSubtractTest) { |
| 53 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB | 52 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 54 | 53 |
| 55 HistogramSamples::Metadata* meta1 = | 54 HistogramSamples::Metadata meta1; |
| 56 allocator.GetAsObject<HistogramSamples::Metadata>( | 55 PersistentSparseHistogramDataManager manager1(&allocator); |
| 57 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); | 56 PersistentSampleMap samples1(1, &manager1, &meta1); |
| 58 HistogramSamples::Metadata* meta2 = | |
| 59 allocator.GetAsObject<HistogramSamples::Metadata>( | |
| 60 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); | |
| 61 PersistentSampleMap samples1(1, &allocator, meta1); | |
| 62 PersistentSampleMap samples2(2, &allocator, meta2); | |
| 63 | |
| 64 samples1.Accumulate(1, 100); | 57 samples1.Accumulate(1, 100); |
| 65 samples1.Accumulate(2, 100); | 58 samples1.Accumulate(2, 100); |
| 66 samples1.Accumulate(3, 100); | 59 samples1.Accumulate(3, 100); |
| 67 | 60 |
| 61 HistogramSamples::Metadata meta2; |
| 62 PersistentSparseHistogramDataManager manager2(&allocator); |
| 63 PersistentSampleMap samples2(2, &manager2, &meta2); |
| 68 samples2.Accumulate(1, 200); | 64 samples2.Accumulate(1, 200); |
| 69 samples2.Accumulate(2, 200); | 65 samples2.Accumulate(2, 200); |
| 70 samples2.Accumulate(4, 200); | 66 samples2.Accumulate(4, 200); |
| 71 | 67 |
| 72 samples1.Add(samples2); | 68 samples1.Add(samples2); |
| 73 EXPECT_EQ(300, samples1.GetCount(1)); | 69 EXPECT_EQ(300, samples1.GetCount(1)); |
| 74 EXPECT_EQ(300, samples1.GetCount(2)); | 70 EXPECT_EQ(300, samples1.GetCount(2)); |
| 75 EXPECT_EQ(100, samples1.GetCount(3)); | 71 EXPECT_EQ(100, samples1.GetCount(3)); |
| 76 EXPECT_EQ(200, samples1.GetCount(4)); | 72 EXPECT_EQ(200, samples1.GetCount(4)); |
| 77 EXPECT_EQ(2000, samples1.sum()); | 73 EXPECT_EQ(2000, samples1.sum()); |
| 78 EXPECT_EQ(900, samples1.TotalCount()); | 74 EXPECT_EQ(900, samples1.TotalCount()); |
| 79 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); | 75 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); |
| 80 | 76 |
| 81 samples1.Subtract(samples2); | 77 samples1.Subtract(samples2); |
| 82 EXPECT_EQ(100, samples1.GetCount(1)); | 78 EXPECT_EQ(100, samples1.GetCount(1)); |
| 83 EXPECT_EQ(100, samples1.GetCount(2)); | 79 EXPECT_EQ(100, samples1.GetCount(2)); |
| 84 EXPECT_EQ(100, samples1.GetCount(3)); | 80 EXPECT_EQ(100, samples1.GetCount(3)); |
| 85 EXPECT_EQ(0, samples1.GetCount(4)); | 81 EXPECT_EQ(0, samples1.GetCount(4)); |
| 86 EXPECT_EQ(600, samples1.sum()); | 82 EXPECT_EQ(600, samples1.sum()); |
| 87 EXPECT_EQ(300, samples1.TotalCount()); | 83 EXPECT_EQ(300, samples1.TotalCount()); |
| 88 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); | 84 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); |
| 89 } | 85 } |
| 90 | 86 |
| 91 TEST(PersistentSampleMapTest, PersistenceTest) { | 87 TEST(PersistentSampleMapTest, PersistenceTest) { |
| 92 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB | 88 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 93 | 89 |
| 94 HistogramSamples::Metadata* meta12 = | 90 HistogramSamples::Metadata meta12; |
| 95 allocator.GetAsObject<HistogramSamples::Metadata>( | 91 PersistentSparseHistogramDataManager manager1(&allocator); |
| 96 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); | 92 PersistentSampleMap samples1(12, &manager1, &meta12); |
| 97 PersistentSampleMap samples1(12, &allocator, meta12); | |
| 98 samples1.Accumulate(1, 100); | 93 samples1.Accumulate(1, 100); |
| 99 samples1.Accumulate(2, 200); | 94 samples1.Accumulate(2, 200); |
| 100 samples1.Accumulate(1, -200); | 95 samples1.Accumulate(1, -200); |
| 101 EXPECT_EQ(-100, samples1.GetCount(1)); | 96 EXPECT_EQ(-100, samples1.GetCount(1)); |
| 102 EXPECT_EQ(200, samples1.GetCount(2)); | 97 EXPECT_EQ(200, samples1.GetCount(2)); |
| 103 EXPECT_EQ(300, samples1.sum()); | 98 EXPECT_EQ(300, samples1.sum()); |
| 104 EXPECT_EQ(100, samples1.TotalCount()); | 99 EXPECT_EQ(100, samples1.TotalCount()); |
| 105 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); | 100 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); |
| 106 | 101 |
| 107 PersistentSampleMap samples2(12, &allocator, meta12); | 102 PersistentSparseHistogramDataManager manager2(&allocator); |
| 103 PersistentSampleMap samples2(12, &manager2, &meta12); |
| 108 EXPECT_EQ(samples1.id(), samples2.id()); | 104 EXPECT_EQ(samples1.id(), samples2.id()); |
| 109 EXPECT_EQ(samples1.sum(), samples2.sum()); | 105 EXPECT_EQ(samples1.sum(), samples2.sum()); |
| 110 EXPECT_EQ(samples1.redundant_count(), samples2.redundant_count()); | 106 EXPECT_EQ(samples1.redundant_count(), samples2.redundant_count()); |
| 111 EXPECT_EQ(samples1.TotalCount(), samples2.TotalCount()); | 107 EXPECT_EQ(samples1.TotalCount(), samples2.TotalCount()); |
| 112 EXPECT_EQ(-100, samples2.GetCount(1)); | 108 EXPECT_EQ(-100, samples2.GetCount(1)); |
| 113 EXPECT_EQ(200, samples2.GetCount(2)); | 109 EXPECT_EQ(200, samples2.GetCount(2)); |
| 114 EXPECT_EQ(300, samples2.sum()); | 110 EXPECT_EQ(300, samples2.sum()); |
| 115 EXPECT_EQ(100, samples2.TotalCount()); | 111 EXPECT_EQ(100, samples2.TotalCount()); |
| 116 EXPECT_EQ(samples2.redundant_count(), samples2.TotalCount()); | 112 EXPECT_EQ(samples2.redundant_count(), samples2.TotalCount()); |
| 117 | 113 |
| 118 EXPECT_EQ(0, samples2.GetCount(3)); | 114 EXPECT_EQ(0, samples2.GetCount(3)); |
| 119 EXPECT_EQ(0, samples1.GetCount(3)); | 115 EXPECT_EQ(0, samples1.GetCount(3)); |
| 120 samples2.Accumulate(3, 300); | 116 samples2.Accumulate(3, 300); |
| 121 EXPECT_EQ(300, samples2.GetCount(3)); | 117 EXPECT_EQ(300, samples2.GetCount(3)); |
| 122 EXPECT_EQ(300, samples1.GetCount(3)); | 118 EXPECT_EQ(300, samples1.GetCount(3)); |
| 123 EXPECT_EQ(samples1.sum(), samples2.sum()); | 119 EXPECT_EQ(samples1.sum(), samples2.sum()); |
| 124 EXPECT_EQ(samples1.redundant_count(), samples2.redundant_count()); | 120 EXPECT_EQ(samples1.redundant_count(), samples2.redundant_count()); |
| 125 EXPECT_EQ(samples1.TotalCount(), samples2.TotalCount()); | 121 EXPECT_EQ(samples1.TotalCount(), samples2.TotalCount()); |
| 122 |
| 123 EXPECT_EQ(0, samples2.GetCount(4)); |
| 124 EXPECT_EQ(0, samples1.GetCount(4)); |
| 125 samples1.Accumulate(4, 400); |
| 126 EXPECT_EQ(400, samples2.GetCount(4)); |
| 127 EXPECT_EQ(400, samples1.GetCount(4)); |
| 128 samples2.Accumulate(4, 4000); |
| 129 EXPECT_EQ(4400, samples2.GetCount(4)); |
| 130 EXPECT_EQ(4400, samples1.GetCount(4)); |
| 131 EXPECT_EQ(samples1.sum(), samples2.sum()); |
| 132 EXPECT_EQ(samples1.redundant_count(), samples2.redundant_count()); |
| 133 EXPECT_EQ(samples1.TotalCount(), samples2.TotalCount()); |
| 126 } | 134 } |
| 127 | 135 |
| 128 TEST(PersistentSampleMapIteratorTest, IterateTest) { | 136 TEST(PersistentSampleMapIteratorTest, IterateTest) { |
| 129 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB | 137 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 130 | 138 |
| 131 HistogramSamples::Metadata* meta = | 139 HistogramSamples::Metadata meta; |
| 132 allocator.GetAsObject<HistogramSamples::Metadata>( | 140 PersistentSparseHistogramDataManager manager(&allocator); |
| 133 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); | 141 PersistentSampleMap samples(1, &manager, &meta); |
| 134 PersistentSampleMap samples(1, &allocator, meta); | |
| 135 samples.Accumulate(1, 100); | 142 samples.Accumulate(1, 100); |
| 136 samples.Accumulate(2, 200); | 143 samples.Accumulate(2, 200); |
| 137 samples.Accumulate(4, -300); | 144 samples.Accumulate(4, -300); |
| 138 samples.Accumulate(5, 0); | 145 samples.Accumulate(5, 0); |
| 139 | 146 |
| 140 std::unique_ptr<SampleCountIterator> it = samples.Iterator(); | 147 std::unique_ptr<SampleCountIterator> it = samples.Iterator(); |
| 141 | 148 |
| 142 HistogramBase::Sample min; | 149 HistogramBase::Sample min; |
| 143 HistogramBase::Sample max; | 150 HistogramBase::Sample max; |
| 144 HistogramBase::Count count; | 151 HistogramBase::Count count; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 161 EXPECT_EQ(5, max); | 168 EXPECT_EQ(5, max); |
| 162 EXPECT_EQ(-300, count); | 169 EXPECT_EQ(-300, count); |
| 163 | 170 |
| 164 it->Next(); | 171 it->Next(); |
| 165 EXPECT_TRUE(it->Done()); | 172 EXPECT_TRUE(it->Done()); |
| 166 } | 173 } |
| 167 | 174 |
| 168 TEST(PersistentSampleMapIteratorTest, SkipEmptyRanges) { | 175 TEST(PersistentSampleMapIteratorTest, SkipEmptyRanges) { |
| 169 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB | 176 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 170 | 177 |
| 171 HistogramSamples::Metadata* meta = | 178 HistogramSamples::Metadata meta1; |
| 172 allocator.GetAsObject<HistogramSamples::Metadata>( | 179 PersistentSparseHistogramDataManager manager1(&allocator); |
| 173 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); | 180 PersistentSampleMap samples1(1, &manager1, &meta1); |
| 174 PersistentSampleMap samples(1, &allocator, meta); | 181 samples1.Accumulate(5, 1); |
| 175 samples.Accumulate(5, 1); | 182 samples1.Accumulate(10, 2); |
| 176 samples.Accumulate(10, 2); | 183 samples1.Accumulate(15, 3); |
| 177 samples.Accumulate(15, 3); | 184 samples1.Accumulate(20, 4); |
| 178 samples.Accumulate(20, 4); | 185 samples1.Accumulate(25, 5); |
| 179 samples.Accumulate(25, 5); | |
| 180 | 186 |
| 181 HistogramSamples::Metadata* meta2 = | 187 HistogramSamples::Metadata meta2; |
| 182 allocator.GetAsObject<HistogramSamples::Metadata>( | 188 PersistentSparseHistogramDataManager manager2(&allocator); |
| 183 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); | 189 PersistentSampleMap samples2(2, &manager2, &meta2); |
| 184 PersistentSampleMap samples2(2, &allocator, meta2); | |
| 185 samples2.Accumulate(5, 1); | 190 samples2.Accumulate(5, 1); |
| 186 samples2.Accumulate(20, 4); | 191 samples2.Accumulate(20, 4); |
| 187 samples2.Accumulate(25, 5); | 192 samples2.Accumulate(25, 5); |
| 188 | 193 |
| 189 samples.Subtract(samples2); | 194 samples1.Subtract(samples2); |
| 190 | 195 |
| 191 std::unique_ptr<SampleCountIterator> it = samples.Iterator(); | 196 std::unique_ptr<SampleCountIterator> it = samples1.Iterator(); |
| 192 EXPECT_FALSE(it->Done()); | 197 EXPECT_FALSE(it->Done()); |
| 193 | 198 |
| 194 HistogramBase::Sample min; | 199 HistogramBase::Sample min; |
| 195 HistogramBase::Sample max; | 200 HistogramBase::Sample max; |
| 196 HistogramBase::Count count; | 201 HistogramBase::Count count; |
| 197 | 202 |
| 198 it->Get(&min, &max, &count); | 203 it->Get(&min, &max, &count); |
| 199 EXPECT_EQ(10, min); | 204 EXPECT_EQ(10, min); |
| 200 EXPECT_EQ(11, max); | 205 EXPECT_EQ(11, max); |
| 201 EXPECT_EQ(2, count); | 206 EXPECT_EQ(2, count); |
| 202 | 207 |
| 203 it->Next(); | 208 it->Next(); |
| 204 EXPECT_FALSE(it->Done()); | 209 EXPECT_FALSE(it->Done()); |
| 205 | 210 |
| 206 it->Get(&min, &max, &count); | 211 it->Get(&min, &max, &count); |
| 207 EXPECT_EQ(15, min); | 212 EXPECT_EQ(15, min); |
| 208 EXPECT_EQ(16, max); | 213 EXPECT_EQ(16, max); |
| 209 EXPECT_EQ(3, count); | 214 EXPECT_EQ(3, count); |
| 210 | 215 |
| 211 it->Next(); | 216 it->Next(); |
| 212 EXPECT_TRUE(it->Done()); | 217 EXPECT_TRUE(it->Done()); |
| 213 } | 218 } |
| 214 | 219 |
| 215 // Only run this test on builds that support catching a DCHECK crash. | 220 // Only run this test on builds that support catching a DCHECK crash. |
| 216 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST | 221 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST |
| 217 TEST(PersistentSampleMapIteratorDeathTest, IterateDoneTest) { | 222 TEST(PersistentSampleMapIteratorDeathTest, IterateDoneTest) { |
| 218 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB | 223 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 219 | 224 |
| 220 HistogramSamples::Metadata* meta = | 225 HistogramSamples::Metadata meta; |
| 221 allocator.GetAsObject<HistogramSamples::Metadata>( | 226 PersistentSparseHistogramDataManager manager(&allocator); |
| 222 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); | 227 PersistentSampleMap samples(1, &manager, &meta); |
| 223 PersistentSampleMap samples(1, &allocator, meta); | |
| 224 | 228 |
| 225 std::unique_ptr<SampleCountIterator> it = samples.Iterator(); | 229 std::unique_ptr<SampleCountIterator> it = samples.Iterator(); |
| 226 | 230 |
| 227 EXPECT_TRUE(it->Done()); | 231 EXPECT_TRUE(it->Done()); |
| 228 | 232 |
| 229 HistogramBase::Sample min; | 233 HistogramBase::Sample min; |
| 230 HistogramBase::Sample max; | 234 HistogramBase::Sample max; |
| 231 HistogramBase::Count count; | 235 HistogramBase::Count count; |
| 232 EXPECT_DEATH(it->Get(&min, &max, &count), ""); | 236 EXPECT_DEATH(it->Get(&min, &max, &count), ""); |
| 233 | 237 |
| 234 EXPECT_DEATH(it->Next(), ""); | 238 EXPECT_DEATH(it->Next(), ""); |
| 235 | 239 |
| 236 samples.Accumulate(1, 100); | 240 samples.Accumulate(1, 100); |
| 237 it = samples.Iterator(); | 241 it = samples.Iterator(); |
| 238 EXPECT_FALSE(it->Done()); | 242 EXPECT_FALSE(it->Done()); |
| 239 } | 243 } |
| 240 #endif | 244 #endif |
| 241 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST | 245 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST |
| 242 | 246 |
| 243 } // namespace | 247 } // namespace |
| 244 } // namespace base | 248 } // namespace base |
| OLD | NEW |