25
25
#include < thread>
26
26
#include < utility>
27
27
28
+ static std::vector<std::pair<uint64_t , uint64_t >> getValuesOnePerBucket (
29
+ HdrHistogram& histo) {
30
+ std::vector<std::pair<uint64_t , uint64_t >> values;
31
+ auto iter = histo.makeLinearIterator (/* valueUnitsPerBucket */ 1 );
32
+ while (auto pair = iter.getNextValueAndCount ()) {
33
+ values.push_back (*pair);
34
+ }
35
+ return values;
36
+ }
37
+
28
38
/*
29
39
* Unit tests for the HdrHistogram
30
40
*/
@@ -76,13 +86,11 @@ TEST(HdrHistogramTest, linearIteratorTest) {
76
86
histogram.addValue (ii);
77
87
}
78
88
79
- // Need to create the iterator after we have added the data
80
- HdrHistogram::Iterator iter{
81
- histogram.makeLinearIterator (/* valueUnitsPerBucket */ 1 )};
82
89
uint64_t valueCount = 0 ;
83
- while (auto result = iter.getNextValueAndCount ()) {
84
- EXPECT_EQ (valueCount, result->first );
85
- ++valueCount;
90
+ auto values = getValuesOnePerBucket (histogram);
91
+ EXPECT_EQ (256 , values.size ());
92
+ for (auto & result : values) {
93
+ EXPECT_EQ (valueCount++, result.first );
86
94
}
87
95
}
88
96
@@ -159,11 +167,12 @@ TEST(HdrHistogramTest, addValueAndCountTest) {
159
167
HdrHistogram histogram{0 , 255 , 3 };
160
168
161
169
histogram.addValueAndCount (0 , 100 );
162
- // Need to create the iterator after we have added the data
163
- HdrHistogram::Iterator iter{histogram.makeLinearIterator (1 )};
164
- while (auto result = iter.getNextValueAndCount ()) {
165
- EXPECT_EQ (0 , result->first );
166
- EXPECT_EQ (100 , result->second );
170
+
171
+ auto values = getValuesOnePerBucket (histogram);
172
+ EXPECT_EQ (1 , values.size ());
173
+ for (auto & result : values) {
174
+ EXPECT_EQ (0 , result.first );
175
+ EXPECT_EQ (100 , result.second );
167
176
}
168
177
}
169
178
@@ -284,14 +293,12 @@ TEST(HdrHistogramTest, addValueParallel) {
284
293
ASSERT_EQ (maxVal - 1 , histogram.getMaxValue ());
285
294
ASSERT_EQ (0 , histogram.getMinValue ());
286
295
287
- HdrHistogram::Iterator iter{
288
- histogram.makeLinearIterator (/* valueUnitsPerBucket */ 1 )};
289
296
uint64_t valueCount = 0 ;
290
- // Assert that the right number of values were added to the histogram
291
- while ( auto result = iter. getNextValueAndCount ()) {
292
- ASSERT_EQ (valueCount, result-> first );
293
- ASSERT_EQ (threads. size () * numOfAddIterations , result-> second );
294
- ++valueCount ;
297
+ auto values = getValuesOnePerBucket ( histogram);
298
+ EXPECT_EQ (maxVal, values. size ());
299
+ for ( auto & result : values) {
300
+ ASSERT_EQ (valueCount++ , result. first );
301
+ ASSERT_EQ (threads. size () * numOfAddIterations, result. second ) ;
295
302
}
296
303
}
297
304
@@ -306,68 +313,64 @@ TEST(HdrHistogramTest, percentileWhenEmptyTest) {
306
313
307
314
// Test the aggregation operator method
308
315
TEST (HdrHistogramTest, aggregationTest) {
309
- HdrHistogram histogramOne{0 , 15 , 3 };
310
- HdrHistogram histogramTwo{0 , 15 , 3 };
316
+ const uint16_t numberOfValues = 15 ;
317
+ HdrHistogram histogramOne{0 , numberOfValues, 3 };
318
+ HdrHistogram histogramTwo{0 , numberOfValues, 3 };
311
319
312
- for (int i = 0 ; i < 15 ; i++) {
320
+ for (int i = 0 ; i < numberOfValues ; i++) {
313
321
histogramOne.addValue (i);
314
322
histogramTwo.addValue (i);
315
323
}
316
324
// Do aggregation
317
325
histogramOne += histogramTwo;
318
326
319
- HdrHistogram::Iterator iterOne{
320
- histogramOne.makeLinearIterator (/* valueUnitsPerBucket */ 1 )};
321
- HdrHistogram::Iterator iterTwo{
322
- histogramTwo.makeLinearIterator (/* valueUnitsPerBucket */ 1 )};
327
+ auto histoOneValues = getValuesOnePerBucket (histogramOne);
328
+ EXPECT_EQ (numberOfValues, histoOneValues.size ());
329
+
330
+ auto histoTwoValues = getValuesOnePerBucket (histogramTwo);
331
+ EXPECT_EQ (numberOfValues, histoTwoValues.size ());
332
+
333
+ EXPECT_NE (histoOneValues, histoTwoValues);
334
+
323
335
uint64_t valueCount = 0 ;
324
- for (int i = 0 ; i < 15 ; i++) {
325
- auto resultOne = iterOne.getNextValueAndCount ();
326
- auto resultTwo = iterTwo.getNextValueAndCount ();
336
+ for (int i = 0 ; i < numberOfValues; i++) {
327
337
// check values are the same for both histograms
328
- EXPECT_EQ (valueCount, resultTwo-> first );
329
- EXPECT_EQ (valueCount, resultOne-> first );
338
+ EXPECT_EQ (valueCount, histoTwoValues[i]. first );
339
+ EXPECT_EQ (valueCount, histoOneValues[i]. first );
330
340
// check that the counts for each value is twice as much as
331
341
// in a bucket in histogram one as it is in histogram two
332
- EXPECT_EQ (resultOne-> second , resultTwo-> second * 2 );
342
+ EXPECT_EQ (histoOneValues[i]. second , histoTwoValues[i]. second * 2 );
333
343
++valueCount;
334
344
}
335
345
336
346
// Check the totals of each histogram
337
- EXPECT_EQ (30 , histogramOne.getValueCount ());
338
- EXPECT_EQ (15 , histogramTwo.getValueCount ());
347
+ EXPECT_EQ (numberOfValues * 2 , histogramOne.getValueCount ());
348
+ EXPECT_EQ (numberOfValues , histogramTwo.getValueCount ());
339
349
}
340
350
341
351
// Test the aggregation operator method
342
352
TEST (HdrHistogramTest, aggregationTestEmptyLhr) {
353
+ const uint16_t numberOfValues = 200 ;
343
354
HdrHistogram histogramOne{0 , 15 , 3 };
344
- HdrHistogram histogramTwo{0 , 200 , 3 };
355
+ HdrHistogram histogramTwo{0 , numberOfValues , 3 };
345
356
346
- for (int i = 0 ; i < 200 ; i++) {
357
+ for (int i = 0 ; i < numberOfValues ; i++) {
347
358
histogramTwo.addValue (i);
348
359
}
349
360
// Do aggregation
350
361
histogramOne += histogramTwo;
351
362
352
- HdrHistogram::Iterator iterOne{
353
- histogramOne.makeLinearIterator (/* valueUnitsPerBucket */ 1 )};
354
- HdrHistogram::Iterator iterTwo{
355
- histogramTwo.makeLinearIterator (/* valueUnitsPerBucket */ 1 )};
363
+ auto histoOneValues = getValuesOnePerBucket (histogramOne);
364
+ EXPECT_EQ (numberOfValues, histoOneValues.size ());
356
365
357
- // Max value of LHS should be updated too 200 thus counts should be the
358
- // same for every value in both histograms
359
- for (int i = 0 ; i < 200 ; i++) {
360
- auto resultOne = iterOne.getNextValueAndCount ();
361
- auto resultTwo = iterTwo.getNextValueAndCount ();
362
- // check values are the same for both histograms
363
- EXPECT_EQ (resultOne->first , resultTwo->first );
364
- // check that the counts for each value are the same
365
- EXPECT_EQ (resultOne->second , resultTwo->second );
366
- }
366
+ auto histoTwoValues = getValuesOnePerBucket (histogramTwo);
367
+ EXPECT_EQ (numberOfValues, histoTwoValues.size ());
368
+
369
+ EXPECT_EQ (histoTwoValues, histoOneValues);
367
370
368
371
// Check the totals of each histogram
369
- EXPECT_EQ (200 , histogramOne.getValueCount ());
370
- EXPECT_EQ (200 , histogramTwo.getValueCount ());
372
+ EXPECT_EQ (numberOfValues , histogramOne.getValueCount ());
373
+ EXPECT_EQ (numberOfValues , histogramTwo.getValueCount ());
371
374
}
372
375
373
376
// Test the aggregation operator method
@@ -381,15 +384,13 @@ TEST(HdrHistogramTest, aggregationTestEmptyRhs) {
381
384
// Do aggregation
382
385
histogramOne += histogramTwo;
383
386
384
- HdrHistogram::Iterator iter{
385
- histogramOne.makeLinearIterator (/* valueUnitsPerBucket */ 1 )};
386
-
387
- uint64_t valueCount = 0 ;
388
387
// make sure the histogram has expanded in size for all 200 values
389
- while (auto result = iter.getNextValueAndCount ()) {
390
- EXPECT_EQ (valueCount, result->first );
391
- EXPECT_EQ (1 , result->second );
392
- ++valueCount;
388
+ auto values = getValuesOnePerBucket (histogramOne);
389
+ EXPECT_EQ (200 , values.size ());
390
+ uint64_t valueCount = 0 ;
391
+ for (auto & result : values) {
392
+ EXPECT_EQ (valueCount++, result.first );
393
+ EXPECT_EQ (1 , result.second );
393
394
}
394
395
395
396
// Check the totals of each histogram
@@ -412,11 +413,13 @@ TEST(HdrHistogramTest, int32MaxSizeTest) {
412
413
EXPECT_EQ (0 , histogram.getValueAtPercentile (100.0 ));
413
414
EXPECT_EQ (0 , histogram.getMinValue ());
414
415
415
- HdrHistogram::Iterator iter{histogram.getHistogramsIterator ()};
416
- auto res = iter.getNextBucketLowHighAndCount ();
417
- EXPECT_TRUE (res);
418
- // The 3rd field [2] of the returned tuple is the count
419
- EXPECT_EQ (limit, std::get<2 >(*res));
416
+ { // iter read lock scope
417
+ HdrHistogram::Iterator iter{histogram.getHistogramsIterator ()};
418
+ auto res = iter.getNextBucketLowHighAndCount ();
419
+ EXPECT_TRUE (res);
420
+ // The 3rd field [2] of the returned tuple is the count
421
+ EXPECT_EQ (limit, std::get<2 >(*res));
422
+ }
420
423
421
424
// Add 1 more count (previously this would overflow the total_count field
422
425
// in the iterator)
@@ -428,11 +431,13 @@ TEST(HdrHistogramTest, int32MaxSizeTest) {
428
431
EXPECT_EQ (0 , histogram.getValueAtPercentile (100.0 ));
429
432
EXPECT_EQ (0 , histogram.getMinValue ());
430
433
431
- HdrHistogram::Iterator iter2{histogram.getHistogramsIterator ()};
432
- auto res2 = iter2.getNextBucketLowHighAndCount ();
433
- EXPECT_TRUE (res2);
434
- // The 3rd field [2] of the returned tuple is the count
435
- EXPECT_EQ (limit, std::get<2 >(*res2));
434
+ { // iter2 read lock scope
435
+ HdrHistogram::Iterator iter2{histogram.getHistogramsIterator ()};
436
+ auto res2 = iter2.getNextBucketLowHighAndCount ();
437
+ EXPECT_TRUE (res2);
438
+ // The 3rd field [2] of the returned tuple is the count
439
+ EXPECT_EQ (limit, std::get<2 >(*res2));
440
+ }
436
441
}
437
442
438
443
TEST (HdrHistogramTest, int64MaxSizeTest) {
0 commit comments