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

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

Issue 1471073007: Reorganize histograms for persistence. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shmem-alloc
Patch Set: addressed review comments by Alexei Created 5 years 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
« no previous file with comments | « base/metrics/sample_vector.cc ('k') | base/metrics/sparse_histogram.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/sample_vector.h" 5 #include "base/metrics/sample_vector.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/metrics/bucket_ranges.h" 10 #include "base/metrics/bucket_ranges.h"
11 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace base { 14 namespace base {
15 namespace { 15 namespace {
16 16
17 TEST(SampleVectorTest, AccumulateTest) { 17 TEST(SampleVectorTest, AccumulateTest) {
18 // Custom buckets: [1, 5) [5, 10) 18 // Custom buckets: [1, 5) [5, 10)
19 BucketRanges ranges(3); 19 BucketRanges ranges(3);
20 ranges.set_range(0, 1); 20 ranges.set_range(0, 1);
21 ranges.set_range(1, 5); 21 ranges.set_range(1, 5);
22 ranges.set_range(2, 10); 22 ranges.set_range(2, 10);
23 SampleVector samples(&ranges); 23 SampleVector samples(1, &ranges);
24 24
25 samples.Accumulate(1, 200); 25 samples.Accumulate(1, 200);
26 samples.Accumulate(2, -300); 26 samples.Accumulate(2, -300);
27 EXPECT_EQ(-100, samples.GetCountAtIndex(0)); 27 EXPECT_EQ(-100, samples.GetCountAtIndex(0));
28 28
29 samples.Accumulate(5, 200); 29 samples.Accumulate(5, 200);
30 EXPECT_EQ(200, samples.GetCountAtIndex(1)); 30 EXPECT_EQ(200, samples.GetCountAtIndex(1));
31 31
32 EXPECT_EQ(600, samples.sum()); 32 EXPECT_EQ(600, samples.sum());
33 EXPECT_EQ(100, samples.redundant_count()); 33 EXPECT_EQ(100, samples.redundant_count());
34 EXPECT_EQ(samples.TotalCount(), samples.redundant_count()); 34 EXPECT_EQ(samples.TotalCount(), samples.redundant_count());
35 35
36 samples.Accumulate(5, -100); 36 samples.Accumulate(5, -100);
37 EXPECT_EQ(100, samples.GetCountAtIndex(1)); 37 EXPECT_EQ(100, samples.GetCountAtIndex(1));
38 38
39 EXPECT_EQ(100, samples.sum()); 39 EXPECT_EQ(100, samples.sum());
40 EXPECT_EQ(0, samples.redundant_count()); 40 EXPECT_EQ(0, samples.redundant_count());
41 EXPECT_EQ(samples.TotalCount(), samples.redundant_count()); 41 EXPECT_EQ(samples.TotalCount(), samples.redundant_count());
42 } 42 }
43 43
44 TEST(SampleVectorTest, AddSubtractTest) { 44 TEST(SampleVectorTest, AddSubtractTest) {
45 // Custom buckets: [0, 1) [1, 2) [2, 3) [3, INT_MAX) 45 // Custom buckets: [0, 1) [1, 2) [2, 3) [3, INT_MAX)
46 BucketRanges ranges(5); 46 BucketRanges ranges(5);
47 ranges.set_range(0, 0); 47 ranges.set_range(0, 0);
48 ranges.set_range(1, 1); 48 ranges.set_range(1, 1);
49 ranges.set_range(2, 2); 49 ranges.set_range(2, 2);
50 ranges.set_range(3, 3); 50 ranges.set_range(3, 3);
51 ranges.set_range(4, INT_MAX); 51 ranges.set_range(4, INT_MAX);
52 52
53 SampleVector samples1(&ranges); 53 SampleVector samples1(1, &ranges);
54 samples1.Accumulate(0, 100); 54 samples1.Accumulate(0, 100);
55 samples1.Accumulate(2, 100); 55 samples1.Accumulate(2, 100);
56 samples1.Accumulate(4, 100); 56 samples1.Accumulate(4, 100);
57 EXPECT_EQ(600, samples1.sum()); 57 EXPECT_EQ(600, samples1.sum());
58 EXPECT_EQ(300, samples1.TotalCount()); 58 EXPECT_EQ(300, samples1.TotalCount());
59 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); 59 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
60 60
61 SampleVector samples2(&ranges); 61 SampleVector samples2(2, &ranges);
62 samples2.Accumulate(1, 200); 62 samples2.Accumulate(1, 200);
63 samples2.Accumulate(2, 200); 63 samples2.Accumulate(2, 200);
64 samples2.Accumulate(4, 200); 64 samples2.Accumulate(4, 200);
65 EXPECT_EQ(1400, samples2.sum()); 65 EXPECT_EQ(1400, samples2.sum());
66 EXPECT_EQ(600, samples2.TotalCount()); 66 EXPECT_EQ(600, samples2.TotalCount());
67 EXPECT_EQ(samples2.redundant_count(), samples2.TotalCount()); 67 EXPECT_EQ(samples2.redundant_count(), samples2.TotalCount());
68 68
69 samples1.Add(samples2); 69 samples1.Add(samples2);
70 EXPECT_EQ(100, samples1.GetCountAtIndex(0)); 70 EXPECT_EQ(100, samples1.GetCountAtIndex(0));
71 EXPECT_EQ(200, samples1.GetCountAtIndex(1)); 71 EXPECT_EQ(200, samples1.GetCountAtIndex(1));
(...skipping 12 matching lines...) Expand all
84 EXPECT_EQ(300, samples1.TotalCount()); 84 EXPECT_EQ(300, samples1.TotalCount());
85 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); 85 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
86 } 86 }
87 87
88 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST 88 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
89 TEST(SampleVectorDeathTest, BucketIndexTest) { 89 TEST(SampleVectorDeathTest, BucketIndexTest) {
90 // 8 buckets with exponential layout: 90 // 8 buckets with exponential layout:
91 // [0, 1) [1, 2) [2, 4) [4, 8) [8, 16) [16, 32) [32, 64) [64, INT_MAX) 91 // [0, 1) [1, 2) [2, 4) [4, 8) [8, 16) [16, 32) [32, 64) [64, INT_MAX)
92 BucketRanges ranges(9); 92 BucketRanges ranges(9);
93 Histogram::InitializeBucketRanges(1, 64, &ranges); 93 Histogram::InitializeBucketRanges(1, 64, &ranges);
94 SampleVector samples(&ranges); 94 SampleVector samples(1, &ranges);
95 95
96 // Normal case 96 // Normal case
97 samples.Accumulate(0, 1); 97 samples.Accumulate(0, 1);
98 samples.Accumulate(3, 2); 98 samples.Accumulate(3, 2);
99 samples.Accumulate(64, 3); 99 samples.Accumulate(64, 3);
100 EXPECT_EQ(1, samples.GetCount(0)); 100 EXPECT_EQ(1, samples.GetCount(0));
101 EXPECT_EQ(2, samples.GetCount(2)); 101 EXPECT_EQ(2, samples.GetCount(2));
102 EXPECT_EQ(3, samples.GetCount(65)); 102 EXPECT_EQ(3, samples.GetCount(65));
103 103
104 // Extreme case. 104 // Extreme case.
105 EXPECT_DEATH(samples.Accumulate(INT_MIN, 100), ""); 105 EXPECT_DEATH(samples.Accumulate(INT_MIN, 100), "");
106 EXPECT_DEATH(samples.Accumulate(-1, 100), ""); 106 EXPECT_DEATH(samples.Accumulate(-1, 100), "");
107 EXPECT_DEATH(samples.Accumulate(INT_MAX, 100), ""); 107 EXPECT_DEATH(samples.Accumulate(INT_MAX, 100), "");
108 108
109 // Custom buckets: [1, 5) [5, 10) 109 // Custom buckets: [1, 5) [5, 10)
110 // Note, this is not a valid BucketRanges for Histogram because it does not 110 // Note, this is not a valid BucketRanges for Histogram because it does not
111 // have overflow buckets. 111 // have overflow buckets.
112 BucketRanges ranges2(3); 112 BucketRanges ranges2(3);
113 ranges2.set_range(0, 1); 113 ranges2.set_range(0, 1);
114 ranges2.set_range(1, 5); 114 ranges2.set_range(1, 5);
115 ranges2.set_range(2, 10); 115 ranges2.set_range(2, 10);
116 SampleVector samples2(&ranges2); 116 SampleVector samples2(2, &ranges2);
117 117
118 // Normal case. 118 // Normal case.
119 samples2.Accumulate(1, 1); 119 samples2.Accumulate(1, 1);
120 samples2.Accumulate(4, 1); 120 samples2.Accumulate(4, 1);
121 samples2.Accumulate(5, 2); 121 samples2.Accumulate(5, 2);
122 samples2.Accumulate(9, 2); 122 samples2.Accumulate(9, 2);
123 EXPECT_EQ(2, samples2.GetCount(1)); 123 EXPECT_EQ(2, samples2.GetCount(1));
124 EXPECT_EQ(4, samples2.GetCount(5)); 124 EXPECT_EQ(4, samples2.GetCount(5));
125 125
126 // Extreme case. 126 // Extreme case.
127 EXPECT_DEATH(samples2.Accumulate(0, 100), ""); 127 EXPECT_DEATH(samples2.Accumulate(0, 100), "");
128 EXPECT_DEATH(samples2.Accumulate(10, 100), ""); 128 EXPECT_DEATH(samples2.Accumulate(10, 100), "");
129 } 129 }
130 130
131 TEST(SampleVectorDeathTest, AddSubtractBucketNotMatchTest) { 131 TEST(SampleVectorDeathTest, AddSubtractBucketNotMatchTest) {
132 // Custom buckets 1: [1, 3) [3, 5) 132 // Custom buckets 1: [1, 3) [3, 5)
133 BucketRanges ranges1(3); 133 BucketRanges ranges1(3);
134 ranges1.set_range(0, 1); 134 ranges1.set_range(0, 1);
135 ranges1.set_range(1, 3); 135 ranges1.set_range(1, 3);
136 ranges1.set_range(2, 5); 136 ranges1.set_range(2, 5);
137 SampleVector samples1(&ranges1); 137 SampleVector samples1(1, &ranges1);
138 138
139 // Custom buckets 2: [0, 1) [1, 3) [3, 6) [6, 7) 139 // Custom buckets 2: [0, 1) [1, 3) [3, 6) [6, 7)
140 BucketRanges ranges2(5); 140 BucketRanges ranges2(5);
141 ranges2.set_range(0, 0); 141 ranges2.set_range(0, 0);
142 ranges2.set_range(1, 1); 142 ranges2.set_range(1, 1);
143 ranges2.set_range(2, 3); 143 ranges2.set_range(2, 3);
144 ranges2.set_range(3, 6); 144 ranges2.set_range(3, 6);
145 ranges2.set_range(4, 7); 145 ranges2.set_range(4, 7);
146 SampleVector samples2(&ranges2); 146 SampleVector samples2(2, &ranges2);
147 147
148 samples2.Accumulate(1, 100); 148 samples2.Accumulate(1, 100);
149 samples1.Add(samples2); 149 samples1.Add(samples2);
150 EXPECT_EQ(100, samples1.GetCountAtIndex(0)); 150 EXPECT_EQ(100, samples1.GetCountAtIndex(0));
151 151
152 // Extra bucket in the beginning. 152 // Extra bucket in the beginning.
153 samples2.Accumulate(0, 100); 153 samples2.Accumulate(0, 100);
154 EXPECT_DEATH(samples1.Add(samples2), ""); 154 EXPECT_DEATH(samples1.Add(samples2), "");
155 EXPECT_DEATH(samples1.Subtract(samples2), ""); 155 EXPECT_DEATH(samples1.Subtract(samples2), "");
156 156
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 EXPECT_EQ(2, min); 202 EXPECT_EQ(2, min);
203 EXPECT_EQ(3, max); 203 EXPECT_EQ(3, max);
204 EXPECT_EQ(2, count); 204 EXPECT_EQ(2, count);
205 EXPECT_TRUE(it.GetBucketIndex(&index)); 205 EXPECT_TRUE(it.GetBucketIndex(&index));
206 EXPECT_EQ(2u, index); 206 EXPECT_EQ(2u, index);
207 207
208 it.Next(); 208 it.Next();
209 EXPECT_TRUE(it.Done()); 209 EXPECT_TRUE(it.Done());
210 210
211 // Create iterator from SampleVector. 211 // Create iterator from SampleVector.
212 SampleVector samples(&ranges); 212 SampleVector samples(1, &ranges);
213 samples.Accumulate(0, 0); 213 samples.Accumulate(0, 0);
214 samples.Accumulate(1, 1); 214 samples.Accumulate(1, 1);
215 samples.Accumulate(2, 2); 215 samples.Accumulate(2, 2);
216 samples.Accumulate(3, 3); 216 samples.Accumulate(3, 3);
217 scoped_ptr<SampleCountIterator> it2 = samples.Iterator(); 217 scoped_ptr<SampleCountIterator> it2 = samples.Iterator();
218 218
219 int i; 219 int i;
220 for (i = 1; !it2->Done(); i++, it2->Next()) { 220 for (i = 1; !it2->Done(); i++, it2->Next()) {
221 it2->Get(&min, &max, &count); 221 it2->Get(&min, &max, &count);
222 EXPECT_EQ(i, min); 222 EXPECT_EQ(i, min);
223 EXPECT_EQ(i + 1, max); 223 EXPECT_EQ(i + 1, max);
224 EXPECT_EQ(i, count); 224 EXPECT_EQ(i, count);
225 225
226 size_t index; 226 size_t index;
227 EXPECT_TRUE(it2->GetBucketIndex(&index)); 227 EXPECT_TRUE(it2->GetBucketIndex(&index));
228 EXPECT_EQ(static_cast<size_t>(i), index); 228 EXPECT_EQ(static_cast<size_t>(i), index);
229 } 229 }
230 EXPECT_EQ(4, i); 230 EXPECT_EQ(4, i);
231 } 231 }
232 232
233 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST 233 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
234 234
235 TEST(SampleVectorIteratorDeathTest, IterateDoneTest) { 235 TEST(SampleVectorIteratorDeathTest, IterateDoneTest) {
236 BucketRanges ranges(5); 236 BucketRanges ranges(5);
237 ranges.set_range(0, 0); 237 ranges.set_range(0, 0);
238 ranges.set_range(1, 1); 238 ranges.set_range(1, 1);
239 ranges.set_range(2, 2); 239 ranges.set_range(2, 2);
240 ranges.set_range(3, 3); 240 ranges.set_range(3, 3);
241 ranges.set_range(4, INT_MAX); 241 ranges.set_range(4, INT_MAX);
242 SampleVector samples(&ranges); 242 SampleVector samples(1, &ranges);
243 243
244 scoped_ptr<SampleCountIterator> it = samples.Iterator(); 244 scoped_ptr<SampleCountIterator> it = samples.Iterator();
245 245
246 EXPECT_TRUE(it->Done()); 246 EXPECT_TRUE(it->Done());
247 247
248 HistogramBase::Sample min; 248 HistogramBase::Sample min;
249 HistogramBase::Sample max; 249 HistogramBase::Sample max;
250 HistogramBase::Count count; 250 HistogramBase::Count count;
251 EXPECT_DEATH(it->Get(&min, &max, &count), ""); 251 EXPECT_DEATH(it->Get(&min, &max, &count), "");
252 252
253 EXPECT_DEATH(it->Next(), ""); 253 EXPECT_DEATH(it->Next(), "");
254 254
255 samples.Accumulate(2, 100); 255 samples.Accumulate(2, 100);
256 it = samples.Iterator(); 256 it = samples.Iterator();
257 EXPECT_FALSE(it->Done()); 257 EXPECT_FALSE(it->Done());
258 } 258 }
259 259
260 #endif 260 #endif
261 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST 261 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
262 262
263 } // namespace 263 } // namespace
264 } // namespace base 264 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/sample_vector.cc ('k') | base/metrics/sparse_histogram.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698