1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
base / metrics / sample_vector_unittest.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/metrics/sample_vector.h"
#include <limits.h>
#include <stddef.h>
#include <atomic>
#include <memory>
#include <vector>
#include "base/metrics/bucket_ranges.h"
#include "base/metrics/histogram.h"
#include "base/metrics/persistent_memory_allocator.h"
#include "base/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
// This framework class has "friend" access to the SampleVector for accessing
// non-public methods and fields.
class SampleVectorTest : public testing::Test {
public:
bool HasSamplesCounts(const SampleVectorBase& samples) {
return samples.counts().has_value();
}
};
TEST_F(SampleVectorTest, Accumulate) {
// Custom buckets: [1, 5) [5, 10)
BucketRanges ranges(3);
ranges.set_range(0, 1);
ranges.set_range(1, 5);
ranges.set_range(2, 10);
SampleVector samples(1, &ranges);
samples.Accumulate(1, 200);
samples.Accumulate(2, -300);
EXPECT_EQ(-100, samples.GetCountAtIndex(0));
samples.Accumulate(5, 200);
EXPECT_EQ(200, samples.GetCountAtIndex(1));
EXPECT_EQ(600, samples.sum());
EXPECT_EQ(100, samples.redundant_count());
EXPECT_EQ(samples.TotalCount(), samples.redundant_count());
samples.Accumulate(5, -100);
EXPECT_EQ(100, samples.GetCountAtIndex(1));
EXPECT_EQ(100, samples.sum());
EXPECT_EQ(0, samples.redundant_count());
EXPECT_EQ(samples.TotalCount(), samples.redundant_count());
}
TEST_F(SampleVectorTest, Accumulate_LargeValuesDontOverflow) {
// Custom buckets: [1, 250000000) [250000000, 500000000)
BucketRanges ranges(3);
ranges.set_range(0, 1);
ranges.set_range(1, 250000000);
ranges.set_range(2, 500000000);
SampleVector samples(1, &ranges);
samples.Accumulate(240000000, 200);
samples.Accumulate(249999999, -300);
EXPECT_EQ(-100, samples.GetCountAtIndex(0));
samples.Accumulate(250000000, 200);
EXPECT_EQ(200, samples.GetCountAtIndex(1));
EXPECT_EQ(23000000300LL, samples.sum());
EXPECT_EQ(100, samples.redundant_count());
EXPECT_EQ(samples.TotalCount(), samples.redundant_count());
samples.Accumulate(250000000, -100);
EXPECT_EQ(100, samples.GetCountAtIndex(1));
EXPECT_EQ(-1999999700LL, samples.sum());
EXPECT_EQ(0, samples.redundant_count());
EXPECT_EQ(samples.TotalCount(), samples.redundant_count());
}
TEST_F(SampleVectorTest, AddSubtract) {
// Custom buckets: [0, 1) [1, 2) [2, 3) [3, INT_MAX)
BucketRanges ranges(5);
ranges.set_range(0, 0);
ranges.set_range(1, 1);
ranges.set_range(2, 2);
ranges.set_range(3, 3);
ranges.set_range(4, INT_MAX);
SampleVector samples1(1, &ranges);
samples1.Accumulate(0, 100);
samples1.Accumulate(2, 100);
samples1.Accumulate(4, 100);
EXPECT_EQ(600, samples1.sum());
EXPECT_EQ(300, samples1.TotalCount());
EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
SampleVector samples2(2, &ranges);
samples2.Accumulate(1, 200);
samples2.Accumulate(2, 200);
samples2.Accumulate(4, 200);
EXPECT_EQ(1400, samples2.sum());
EXPECT_EQ(600, samples2.TotalCount());
EXPECT_EQ(samples2.redundant_count(), samples2.TotalCount());
samples1.Add(samples2);
EXPECT_EQ(100, samples1.GetCountAtIndex(0));
EXPECT_EQ(200, samples1.GetCountAtIndex(1));
EXPECT_EQ(300, samples1.GetCountAtIndex(2));
EXPECT_EQ(300, samples1.GetCountAtIndex(3));
EXPECT_EQ(2000, samples1.sum());
EXPECT_EQ(900, samples1.TotalCount());
EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
samples1.Subtract(samples2);
EXPECT_EQ(100, samples1.GetCountAtIndex(0));
EXPECT_EQ(0, samples1.GetCountAtIndex(1));
EXPECT_EQ(100, samples1.GetCountAtIndex(2));
EXPECT_EQ(100, samples1.GetCountAtIndex(3));
EXPECT_EQ(600, samples1.sum());
EXPECT_EQ(300, samples1.TotalCount());
EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
}
TEST_F(SampleVectorTest, BucketIndexDeath) {
// 8 buckets with exponential layout:
// [0, 1) [1, 2) [2, 4) [4, 8) [8, 16) [16, 32) [32, 64) [64, INT_MAX)
BucketRanges ranges(9);
Histogram::InitializeBucketRanges(1, 64, &ranges);
SampleVector samples(1, &ranges);
// Normal case
samples.Accumulate(0, 1);
samples.Accumulate(3, 2);
samples.Accumulate(64, 3);
EXPECT_EQ(1, samples.GetCount(0));
EXPECT_EQ(2, samples.GetCount(2));
EXPECT_EQ(3, samples.GetCount(65));
// Extreme case.
EXPECT_DEATH_IF_SUPPORTED(samples.Accumulate(INT_MIN, 100), "");
EXPECT_DEATH_IF_SUPPORTED(samples.Accumulate(-1, 100), "");
EXPECT_DEATH_IF_SUPPORTED(samples.Accumulate(INT_MAX, 100), "");
// Custom buckets: [1, 5) [5, 10)
// Note, this is not a valid BucketRanges for Histogram because it does not
// have overflow buckets.
BucketRanges ranges2(3);
ranges2.set_range(0, 1);
ranges2.set_range(1, 5);
ranges2.set_range(2, 10);
SampleVector samples2(2, &ranges2);
// Normal case.
samples2.Accumulate(1, 1);
samples2.Accumulate(4, 1);
samples2.Accumulate(5, 2);
samples2.Accumulate(9, 2);
EXPECT_EQ(2, samples2.GetCount(1));
EXPECT_EQ(4, samples2.GetCount(5));
// Extreme case.
EXPECT_DEATH_IF_SUPPORTED(samples2.Accumulate(0, 100), "");
EXPECT_DEATH_IF_SUPPORTED(samples2.Accumulate(10, 100), "");
}
TEST_F(SampleVectorTest, AddSubtractBucketNotMatch) {
// Custom buckets 1: [1, 3) [3, 5)
BucketRanges ranges1(3);
ranges1.set_range(0, 1);
ranges1.set_range(1, 3);
ranges1.set_range(2, 5);
SampleVector samples1(1, &ranges1);
// Custom buckets 2: [0, 1) [1, 3) [3, 6) [6, 7)
BucketRanges ranges2(5);
ranges2.set_range(0, 0);
ranges2.set_range(1, 1);
ranges2.set_range(2, 3);
ranges2.set_range(3, 6);
ranges2.set_range(4, 7);
SampleVector samples2(2, &ranges2);
samples2.Accumulate(1, 100);
// Despite [1, 3) matching in both samples, we expect AddSubtractImpl to fail
// as it requires perfect alignment of buckets.
EXPECT_FALSE(samples1.Add(samples2));
// Extra bucket in the beginning. These should cause AddSubtractImpl to fail.
samples2.Accumulate(0, 100);
EXPECT_FALSE(samples1.Add(samples2));
EXPECT_FALSE(samples1.Subtract(samples2));
// Extra bucket in the end. These should cause AddSubtractImpl to fail.
samples2.Accumulate(0, -100);
samples2.Accumulate(6, 100);
EXPECT_FALSE(samples1.Add(samples2));
EXPECT_FALSE(samples1.Subtract(samples2));
// Bucket not match: [3, 5) VS [3, 6). These should cause AddSubtractImpl to
// fail.
samples2.Accumulate(6, -100);
samples2.Accumulate(3, 100);
EXPECT_FALSE(samples1.Add(samples2));
EXPECT_FALSE(samples1.Subtract(samples2));
}
TEST_F(SampleVectorTest, Iterate) {
BucketRanges ranges(5);
ranges.set_range(0, 0);
ranges.set_range(1, 1);
ranges.set_range(2, 2);
ranges.set_range(3, 3);
ranges.set_range(4, 4);
// Create iterator from SampleVector.
SampleVector samples(1, &ranges);
samples.Accumulate(0, 0); // Iterator will bypass this empty bucket.
samples.Accumulate(1, 1);
samples.Accumulate(2, 2);
samples.Accumulate(3, 3);
std::unique_ptr<SampleCountIterator> it = samples.Iterator();
int i;
size_t index;
HistogramBase::Sample min;
int64_t max;
HistogramBase::Count count;
for (i = 1; !it->Done(); i++, it->Next()) {
it->Get(&min, &max, &count);
EXPECT_EQ(i, min);
EXPECT_EQ(i + 1, max);
EXPECT_EQ(i, count);
EXPECT_TRUE(it->GetBucketIndex(&index));
EXPECT_EQ(static_cast<size_t>(i), index);
}
EXPECT_EQ(4, i);
}
TEST_F(SampleVectorTest, Iterator_InvalidSingleSample) {
// Create 3 buckets: [0, 1), [1, 2), [2, INT_MAX).
BucketRanges ranges(4);
ranges.set_range(0, 0);
ranges.set_range(1, 1);
ranges.set_range(2, 2);
ranges.set_range(3, HistogramBase::kSampleType_MAX);
// Create an invalid SingleSample.
HistogramSamples::AtomicSingleSample invalid_single_sample;
invalid_single_sample.Accumulate(/*bucket=*/4, /*count=*/1);
// Create a SampleVector and set its SingleSample to the invalid one.
SampleVector samples(&ranges);
*samples.SingleSampleForTesting() = invalid_single_sample;
// Create an iterator and verify that it is empty (the sample is ignored).
std::unique_ptr<SampleCountIterator> it = samples.Iterator();
ASSERT_TRUE(it->Done());
// Add some valid samples. SampleVector should now use a counts storage.
samples.Accumulate(/*value=*/0, /*count=*/1);
samples.Accumulate(/*value=*/1, /*count=*/1);
// Create an iterator. Verify that the new samples are returned, and that the
// invalid sample is not (it was discarded).
HistogramBase::Sample min;
int64_t max;
HistogramBase::Count count;
it = samples.Iterator();
ASSERT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(min, 0);
EXPECT_EQ(max, 1);
EXPECT_EQ(count, 1);
it->Next();
ASSERT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(min, 1);
EXPECT_EQ(max, 2);
EXPECT_EQ(count, 1);
it->Next();
EXPECT_TRUE(it->Done());
}
TEST_F(SampleVectorTest, ExtractingIterator_InvalidSingleSample) {
// Create 3 buckets: [0, 1), [1, 2), and [2, INT_MAX).
BucketRanges ranges(4);
ranges.set_range(0, 0);
ranges.set_range(1, 1);
ranges.set_range(2, 2);
ranges.set_range(3, HistogramBase::kSampleType_MAX);
// Create an invalid SingleSample.
HistogramSamples::AtomicSingleSample invalid_single_sample;
invalid_single_sample.Accumulate(/*bucket=*/4, /*count=*/1);
// Create a SampleVector and set its SingleSample to the invalid one.
SampleVector samples(&ranges);
*samples.SingleSampleForTesting() = invalid_single_sample;
// Create an extracting iterator and verify that it is empty (the sample is
// ignored).
std::unique_ptr<SampleCountIterator> it = samples.ExtractingIterator();
ASSERT_TRUE(it->Done());
// Verify that the invalid sample was extracted.
HistogramSamples::SingleSample current_single_sample =
samples.SingleSampleForTesting()->Load();
EXPECT_EQ(current_single_sample.bucket, 0);
EXPECT_EQ(current_single_sample.count, 0);
}
TEST_F(SampleVectorTest, IterateDoneDeath) {
BucketRanges ranges(5);
ranges.set_range(0, 0);
ranges.set_range(1, 1);
ranges.set_range(2, 2);
ranges.set_range(3, 3);
ranges.set_range(4, INT_MAX);
SampleVector samples(1, &ranges);
std::unique_ptr<SampleCountIterator> it = samples.Iterator();
EXPECT_TRUE(it->Done());
HistogramBase::Sample min;
int64_t max;
HistogramBase::Count count;
EXPECT_DCHECK_DEATH(it->Get(&min, &max, &count));
EXPECT_DCHECK_DEATH(it->Next());
samples.Accumulate(2, 100);
it = samples.Iterator();
EXPECT_FALSE(it->Done());
}
TEST_F(SampleVectorTest, SingleSample) {
// Custom buckets: [1, 5) [5, 10)
BucketRanges ranges(3);
ranges.set_range(0, 1);
ranges.set_range(1, 5);
ranges.set_range(2, 10);
SampleVector samples(&ranges);
// Ensure that a single value accumulates correctly.
EXPECT_FALSE(HasSamplesCounts(samples));
samples.Accumulate(3, 200);
EXPECT_EQ(200, samples.GetCount(3));
EXPECT_FALSE(HasSamplesCounts(samples));
samples.Accumulate(3, 400);
EXPECT_EQ(600, samples.GetCount(3));
EXPECT_FALSE(HasSamplesCounts(samples));
EXPECT_EQ(3 * 600, samples.sum());
EXPECT_EQ(600, samples.TotalCount());
EXPECT_EQ(600, samples.redundant_count());
// Ensure that the iterator returns only one value.
HistogramBase::Sample min;
int64_t max;
HistogramBase::Count count;
std::unique_ptr<SampleCountIterator> it = samples.Iterator();
ASSERT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(1, min);
EXPECT_EQ(5, max);
EXPECT_EQ(600, count);
it->Next();
EXPECT_TRUE(it->Done());
// Ensure that it can be merged to another single-sample vector.
SampleVector samples_copy(&ranges);
samples_copy.Add(samples);
EXPECT_FALSE(HasSamplesCounts(samples_copy));
EXPECT_EQ(3 * 600, samples_copy.sum());
EXPECT_EQ(600, samples_copy.TotalCount());
EXPECT_EQ(600, samples_copy.redundant_count());
// A different value should cause creation of the counts array.
samples.Accumulate(8, 100);
EXPECT_TRUE(HasSamplesCounts(samples));
EXPECT_EQ(600, samples.GetCount(3));
EXPECT_EQ(100, samples.GetCount(8));
EXPECT_EQ(3 * 600 + 8 * 100, samples.sum());
EXPECT_EQ(600 + 100, samples.TotalCount());
EXPECT_EQ(600 + 100, samples.redundant_count());
// The iterator should now return both values.
it = samples.Iterator();
ASSERT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(1, min);
EXPECT_EQ(5, max);
EXPECT_EQ(600, count);
it->Next();
ASSERT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(5, min);
EXPECT_EQ(10, max);
EXPECT_EQ(100, count);
it->Next();
EXPECT_TRUE(it->Done());
// Ensure that it can merged to a single-sample vector.
samples_copy.Add(samples);
EXPECT_TRUE(HasSamplesCounts(samples_copy));
EXPECT_EQ(3 * 1200 + 8 * 100, samples_copy.sum());
EXPECT_EQ(1200 + 100, samples_copy.TotalCount());
EXPECT_EQ(1200 + 100, samples_copy.redundant_count());
}
TEST_F(SampleVectorTest, PersistentSampleVector) {
LocalPersistentMemoryAllocator allocator(64 << 10, 0, "");
std::atomic<PersistentMemoryAllocator::Reference> samples_ref;
samples_ref.store(0, std::memory_order_relaxed);
HistogramSamples::Metadata samples_meta;
memset(&samples_meta, 0, sizeof(samples_meta));
// Custom buckets: [1, 5) [5, 10)
BucketRanges ranges(3);
ranges.set_range(0, 1);
ranges.set_range(1, 5);
ranges.set_range(2, 10);
// Persistent allocation.
const size_t counts_bytes =
sizeof(HistogramBase::AtomicCount) * ranges.bucket_count();
const DelayedPersistentAllocation allocation(&allocator, &samples_ref, 1,
counts_bytes, false);
PersistentSampleVector samples1(0, &ranges, &samples_meta, allocation);
EXPECT_FALSE(HasSamplesCounts(samples1));
samples1.Accumulate(3, 200);
EXPECT_EQ(200, samples1.GetCount(3));
EXPECT_FALSE(HasSamplesCounts(samples1));
EXPECT_EQ(0, samples1.GetCount(8));
EXPECT_FALSE(HasSamplesCounts(samples1));
PersistentSampleVector samples2(0, &ranges, &samples_meta, allocation);
EXPECT_EQ(200, samples2.GetCount(3));
EXPECT_FALSE(HasSamplesCounts(samples2));
HistogramBase::Sample min;
int64_t max;
HistogramBase::Count count;
std::unique_ptr<SampleCountIterator> it = samples2.Iterator();
ASSERT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(1, min);
EXPECT_EQ(5, max);
EXPECT_EQ(200, count);
it->Next();
EXPECT_TRUE(it->Done());
samples1.Accumulate(8, 100);
EXPECT_TRUE(HasSamplesCounts(samples1));
EXPECT_FALSE(HasSamplesCounts(samples2));
EXPECT_EQ(200, samples2.GetCount(3));
EXPECT_EQ(100, samples2.GetCount(8));
EXPECT_TRUE(HasSamplesCounts(samples2));
EXPECT_EQ(3 * 200 + 8 * 100, samples2.sum());
EXPECT_EQ(300, samples2.TotalCount());
EXPECT_EQ(300, samples2.redundant_count());
it = samples2.Iterator();
ASSERT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(1, min);
EXPECT_EQ(5, max);
EXPECT_EQ(200, count);
it->Next();
ASSERT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(5, min);
EXPECT_EQ(10, max);
EXPECT_EQ(100, count);
it->Next();
EXPECT_TRUE(it->Done());
PersistentSampleVector samples3(0, &ranges, &samples_meta, allocation);
EXPECT_TRUE(HasSamplesCounts(samples2));
EXPECT_EQ(200, samples3.GetCount(3));
EXPECT_EQ(100, samples3.GetCount(8));
EXPECT_EQ(3 * 200 + 8 * 100, samples3.sum());
EXPECT_EQ(300, samples3.TotalCount());
EXPECT_EQ(300, samples3.redundant_count());
it = samples3.Iterator();
ASSERT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(1, min);
EXPECT_EQ(5, max);
EXPECT_EQ(200, count);
it->Next();
ASSERT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(5, min);
EXPECT_EQ(10, max);
EXPECT_EQ(100, count);
it->Next();
EXPECT_TRUE(it->Done());
}
TEST_F(SampleVectorTest, PersistentSampleVectorTestWithOutsideAlloc) {
LocalPersistentMemoryAllocator allocator(64 << 10, 0, "");
std::atomic<PersistentMemoryAllocator::Reference> samples_ref;
samples_ref.store(0, std::memory_order_relaxed);
HistogramSamples::Metadata samples_meta;
memset(&samples_meta, 0, sizeof(samples_meta));
// Custom buckets: [1, 5) [5, 10)
BucketRanges ranges(3);
ranges.set_range(0, 1);
ranges.set_range(1, 5);
ranges.set_range(2, 10);
// Persistent allocation.
const size_t counts_bytes =
sizeof(HistogramBase::AtomicCount) * ranges.bucket_count();
const DelayedPersistentAllocation allocation(&allocator, &samples_ref, 1,
counts_bytes, false);
PersistentSampleVector samples1(0, &ranges, &samples_meta, allocation);
EXPECT_FALSE(HasSamplesCounts(samples1));
samples1.Accumulate(3, 200);
EXPECT_EQ(200, samples1.GetCount(3));
EXPECT_FALSE(HasSamplesCounts(samples1));
// Because the delayed allocation can be shared with other objects (the
// |offset| parameter allows concatinating multiple data blocks into the
// same allocation), it's possible that the allocation gets realized from
// the outside even though the data block being accessed is all zero.
allocation.Get<uint8_t>();
EXPECT_EQ(200, samples1.GetCount(3));
EXPECT_FALSE(HasSamplesCounts(samples1));
HistogramBase::Sample min;
int64_t max;
HistogramBase::Count count;
std::unique_ptr<SampleCountIterator> it = samples1.Iterator();
ASSERT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(1, min);
EXPECT_EQ(5, max);
EXPECT_EQ(200, count);
it->Next();
EXPECT_TRUE(it->Done());
// A duplicate samples object should still see the single-sample entry even
// when storage is available.
PersistentSampleVector samples2(0, &ranges, &samples_meta, allocation);
EXPECT_EQ(200, samples2.GetCount(3));
// New accumulations, in both directions, of the existing value should work.
samples1.Accumulate(3, 50);
EXPECT_EQ(250, samples1.GetCount(3));
EXPECT_EQ(250, samples2.GetCount(3));
samples2.Accumulate(3, 50);
EXPECT_EQ(300, samples1.GetCount(3));
EXPECT_EQ(300, samples2.GetCount(3));
it = samples1.Iterator();
ASSERT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(1, min);
EXPECT_EQ(5, max);
EXPECT_EQ(300, count);
it->Next();
EXPECT_TRUE(it->Done());
samples1.Accumulate(8, 100);
EXPECT_TRUE(HasSamplesCounts(samples1));
EXPECT_EQ(300, samples1.GetCount(3));
EXPECT_EQ(300, samples2.GetCount(3));
EXPECT_EQ(100, samples1.GetCount(8));
EXPECT_EQ(100, samples2.GetCount(8));
samples2.Accumulate(8, 100);
EXPECT_EQ(300, samples1.GetCount(3));
EXPECT_EQ(300, samples2.GetCount(3));
EXPECT_EQ(200, samples1.GetCount(8));
EXPECT_EQ(200, samples2.GetCount(8));
}
// Tests GetPeakBucketSize() returns accurate max bucket size.
TEST_F(SampleVectorTest, GetPeakBucketSize) {
// Custom buckets: [1, 5) [5, 10) [10, 20)
BucketRanges ranges(4);
ranges.set_range(0, 1);
ranges.set_range(1, 5);
ranges.set_range(2, 10);
ranges.set_range(3, 20);
SampleVector samples(1, &ranges);
samples.Accumulate(3, 1);
samples.Accumulate(6, 2);
samples.Accumulate(12, 3);
EXPECT_EQ(3, samples.GetPeakBucketSize());
}
} // namespace base