Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: base/metrics/persistent_sample_map_unittest.cc

Issue 1734033003: Add support for persistent sparse histograms. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge common code for sparse/regular histogram creation Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_sample_map.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace base {
11 namespace {
12
13 TEST(PersistentSampleMapTest, AccumulateTest) {
14 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB
15
16 HistogramSamples::Metadata* meta =
17 allocator.GetAsObject<HistogramSamples::Metadata>(
18 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0);
19 PersistentSampleMap samples(1, &allocator, meta);
20
21 samples.Accumulate(1, 100);
22 samples.Accumulate(2, 200);
23 samples.Accumulate(1, -200);
24 EXPECT_EQ(-100, samples.GetCount(1));
25 EXPECT_EQ(200, samples.GetCount(2));
26
27 EXPECT_EQ(300, samples.sum());
28 EXPECT_EQ(100, samples.TotalCount());
29 EXPECT_EQ(samples.redundant_count(), samples.TotalCount());
30 }
31
32 TEST(PersistentSampleMapTest, Accumulate_LargeValuesDontOverflow) {
33 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB
34
35 HistogramSamples::Metadata* meta =
36 allocator.GetAsObject<HistogramSamples::Metadata>(
37 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0);
38 PersistentSampleMap samples(1, &allocator, meta);
39
40 samples.Accumulate(250000000, 100);
41 samples.Accumulate(500000000, 200);
42 samples.Accumulate(250000000, -200);
43 EXPECT_EQ(-100, samples.GetCount(250000000));
44 EXPECT_EQ(200, samples.GetCount(500000000));
45
46 EXPECT_EQ(75000000000LL, samples.sum());
47 EXPECT_EQ(100, samples.TotalCount());
48 EXPECT_EQ(samples.redundant_count(), samples.TotalCount());
49 }
50
51 TEST(PersistentSampleMapTest, AddSubtractTest) {
52 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB
53
54 HistogramSamples::Metadata* meta1 =
55 allocator.GetAsObject<HistogramSamples::Metadata>(
56 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0);
57 HistogramSamples::Metadata* meta2 =
58 allocator.GetAsObject<HistogramSamples::Metadata>(
59 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0);
60 PersistentSampleMap samples1(1, &allocator, meta1);
61 PersistentSampleMap samples2(2, &allocator, meta2);
62
63 samples1.Accumulate(1, 100);
64 samples1.Accumulate(2, 100);
65 samples1.Accumulate(3, 100);
66
67 samples2.Accumulate(1, 200);
68 samples2.Accumulate(2, 200);
69 samples2.Accumulate(4, 200);
70
71 samples1.Add(samples2);
72 EXPECT_EQ(300, samples1.GetCount(1));
73 EXPECT_EQ(300, samples1.GetCount(2));
74 EXPECT_EQ(100, samples1.GetCount(3));
75 EXPECT_EQ(200, samples1.GetCount(4));
76 EXPECT_EQ(2000, samples1.sum());
77 EXPECT_EQ(900, samples1.TotalCount());
78 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
79
80 samples1.Subtract(samples2);
81 EXPECT_EQ(100, samples1.GetCount(1));
82 EXPECT_EQ(100, samples1.GetCount(2));
83 EXPECT_EQ(100, samples1.GetCount(3));
84 EXPECT_EQ(0, samples1.GetCount(4));
85 EXPECT_EQ(600, samples1.sum());
86 EXPECT_EQ(300, samples1.TotalCount());
87 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
88 }
89
90 TEST(PersistentSampleMapTest, PersistenceTest) {
91 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB
92
93 HistogramSamples::Metadata* meta12 =
94 allocator.GetAsObject<HistogramSamples::Metadata>(
95 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0);
96 PersistentSampleMap samples1(12, &allocator, meta12);
97 samples1.Accumulate(1, 100);
98 samples1.Accumulate(2, 200);
99 samples1.Accumulate(1, -200);
100 EXPECT_EQ(-100, samples1.GetCount(1));
101 EXPECT_EQ(200, samples1.GetCount(2));
102 EXPECT_EQ(300, samples1.sum());
103 EXPECT_EQ(100, samples1.TotalCount());
104 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
105
106 PersistentSampleMap samples2(12, &allocator, meta12);
107 EXPECT_EQ(samples1.id(), samples2.id());
108 EXPECT_EQ(samples1.sum(), samples2.sum());
109 EXPECT_EQ(samples1.redundant_count(), samples2.redundant_count());
110 EXPECT_EQ(samples1.TotalCount(), samples2.TotalCount());
111 EXPECT_EQ(-100, samples2.GetCount(1));
112 EXPECT_EQ(200, samples2.GetCount(2));
113 EXPECT_EQ(300, samples2.sum());
114 EXPECT_EQ(100, samples2.TotalCount());
115 EXPECT_EQ(samples2.redundant_count(), samples2.TotalCount());
116
117 EXPECT_EQ(0, samples2.GetCount(3));
118 EXPECT_EQ(0, samples1.GetCount(3));
119 samples2.Accumulate(3, 300);
120 EXPECT_EQ(300, samples2.GetCount(3));
121 EXPECT_EQ(300, samples1.GetCount(3));
122 EXPECT_EQ(samples1.sum(), samples2.sum());
123 EXPECT_EQ(samples1.redundant_count(), samples2.redundant_count());
124 EXPECT_EQ(samples1.TotalCount(), samples2.TotalCount());
125 }
126
127 TEST(PersistentSampleMapIteratorTest, IterateTest) {
128 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB
129
130 HistogramSamples::Metadata* meta =
131 allocator.GetAsObject<HistogramSamples::Metadata>(
132 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0);
133 PersistentSampleMap samples(1, &allocator, meta);
134 samples.Accumulate(1, 100);
135 samples.Accumulate(2, 200);
136 samples.Accumulate(4, -300);
137 samples.Accumulate(5, 0);
138
139 scoped_ptr<SampleCountIterator> it = samples.Iterator();
140
141 HistogramBase::Sample min;
142 HistogramBase::Sample max;
143 HistogramBase::Count count;
144
145 it->Get(&min, &max, &count);
146 EXPECT_EQ(1, min);
147 EXPECT_EQ(2, max);
148 EXPECT_EQ(100, count);
149 EXPECT_FALSE(it->GetBucketIndex(NULL));
150
151 it->Next();
152 it->Get(&min, &max, &count);
153 EXPECT_EQ(2, min);
154 EXPECT_EQ(3, max);
155 EXPECT_EQ(200, count);
156
157 it->Next();
158 it->Get(&min, &max, &count);
159 EXPECT_EQ(4, min);
160 EXPECT_EQ(5, max);
161 EXPECT_EQ(-300, count);
162
163 it->Next();
164 EXPECT_TRUE(it->Done());
165 }
166
167 TEST(PersistentSampleMapIteratorTest, SkipEmptyRanges) {
168 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB
169
170 HistogramSamples::Metadata* meta =
171 allocator.GetAsObject<HistogramSamples::Metadata>(
172 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0);
173 PersistentSampleMap samples(1, &allocator, meta);
174 samples.Accumulate(5, 1);
175 samples.Accumulate(10, 2);
176 samples.Accumulate(15, 3);
177 samples.Accumulate(20, 4);
178 samples.Accumulate(25, 5);
179
180 HistogramSamples::Metadata* meta2 =
181 allocator.GetAsObject<HistogramSamples::Metadata>(
182 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0);
183 PersistentSampleMap samples2(2, &allocator, meta2);
184 samples2.Accumulate(5, 1);
185 samples2.Accumulate(20, 4);
186 samples2.Accumulate(25, 5);
187
188 samples.Subtract(samples2);
189
190 scoped_ptr<SampleCountIterator> it = samples.Iterator();
191 EXPECT_FALSE(it->Done());
192
193 HistogramBase::Sample min;
194 HistogramBase::Sample max;
195 HistogramBase::Count count;
196
197 it->Get(&min, &max, &count);
198 EXPECT_EQ(10, min);
199 EXPECT_EQ(11, max);
200 EXPECT_EQ(2, count);
201
202 it->Next();
203 EXPECT_FALSE(it->Done());
204
205 it->Get(&min, &max, &count);
206 EXPECT_EQ(15, min);
207 EXPECT_EQ(16, max);
208 EXPECT_EQ(3, count);
209
210 it->Next();
211 EXPECT_TRUE(it->Done());
212 }
213
214 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
Alexei Svitkine (slow) 2016/03/08 19:46:25 Add a comment about this above it.
bcwhite 2016/03/09 01:16:52 Done.
215
Alexei Svitkine (slow) 2016/03/08 19:46:25 No empty line.
bcwhite 2016/03/09 01:16:52 Done.
216 TEST(PersistentSampleMapIteratorDeathTest, IterateDoneTest) {
217 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB
218
219 HistogramSamples::Metadata* meta =
220 allocator.GetAsObject<HistogramSamples::Metadata>(
221 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0);
222 PersistentSampleMap samples(1, &allocator, meta);
223
224 scoped_ptr<SampleCountIterator> it = samples.Iterator();
225
226 EXPECT_TRUE(it->Done());
227
228 HistogramBase::Sample min;
229 HistogramBase::Sample max;
230 HistogramBase::Count count;
231 EXPECT_DEATH(it->Get(&min, &max, &count), "");
232
233 EXPECT_DEATH(it->Next(), "");
234
235 samples.Accumulate(1, 100);
236 it = samples.Iterator();
237 EXPECT_FALSE(it->Done());
238 }
239
240 #endif
241 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
242
243 } // namespace
244 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698