Commit 2a919488 authored by Hasnain Lakhani's avatar Hasnain Lakhani Committed by Viswanath Sivakumar

Add getTotalCount() to Histogram

Summary:
Add a getTotalCount() method to the Histogram class so that callers
can read out the number of values that were put into the histogram

Test Plan: Added a new test.

Reviewed By: simpkins@fb.com

Subscribers: net-systems@, evanmao, folly-diffs@, yfeldblum, chalfant

FB internal diff: D2078239

Tasks: 7097793

Signature: t1:2078239:1431739799:5de3a02df67ff535e06b4a1547690440cf594824
parent 3d381a35
...@@ -61,6 +61,17 @@ unsigned int HistogramBuckets<T, BucketType>::getBucketIdx( ...@@ -61,6 +61,17 @@ unsigned int HistogramBuckets<T, BucketType>::getBucketIdx(
} }
} }
template <typename T, typename BucketType>
template <typename CountFn>
const uint64_t HistogramBuckets<T, BucketType>::computeTotalCount(
CountFn countFromBucket) const {
uint64_t count = 0;
for (unsigned int n = 0; n < buckets_.size(); ++n) {
count += countFromBucket(const_cast<const BucketType&>(buckets_[n]));
}
return count;
}
template <typename T, typename BucketType> template <typename T, typename BucketType>
template <typename CountFn> template <typename CountFn>
unsigned int HistogramBuckets<T, BucketType>::getPercentileBucketIdx( unsigned int HistogramBuckets<T, BucketType>::getPercentileBucketIdx(
......
...@@ -142,6 +142,18 @@ class HistogramBuckets { ...@@ -142,6 +142,18 @@ class HistogramBuckets {
return min_ + (idx * bucketSize_); return min_ + (idx * bucketSize_);
} }
/**
* Computes the total number of values stored across all buckets.
*
* Runs in O(numBuckets)
*
* @param countFn A function that takes a const BucketType&, and returns the
* number of values in that bucket
* @return Returns the total number of values stored across all buckets
*/
template <typename CountFn>
const uint64_t computeTotalCount(CountFn countFromBucket) const;
/** /**
* Determine which bucket the specified percentile falls into. * Determine which bucket the specified percentile falls into.
* *
...@@ -376,6 +388,16 @@ class Histogram { ...@@ -376,6 +388,16 @@ class Histogram {
return buckets_.getBucketMax(idx); return buckets_.getBucketMax(idx);
} }
/**
* Computes the total number of values stored across all buckets.
*
* Runs in O(numBuckets)
*/
const uint64_t computeTotalCount() const {
CountFromBucket countFn;
return buckets_.computeTotalCount(countFn);
}
/* /*
* Get the bucket that the specified percentile falls into * Get the bucket that the specified percentile falls into
* *
......
...@@ -203,3 +203,22 @@ TEST(Histogram, TestDoubleWidthTooBig) { ...@@ -203,3 +203,22 @@ TEST(Histogram, TestDoubleWidthTooBig) {
EXPECT_EQ(1, h.getBucketByIndex(2).count); EXPECT_EQ(1, h.getBucketByIndex(2).count);
EXPECT_EQ(3.0, h.getPercentileEstimate(0.5)); EXPECT_EQ(3.0, h.getPercentileEstimate(0.5));
} }
// Test that we get counts right
TEST(Histogram, Counts) {
Histogram<int32_t> h(1, 0, 10);
EXPECT_EQ(12, h.getNumBuckets());
EXPECT_EQ(0, h.computeTotalCount());
// Add one to each bucket, make sure the counts match
for (int32_t i = 0; i < 10; i++) {
h.addValue(i);
EXPECT_EQ(i+1, h.computeTotalCount());
}
// Add a lot to one bucket, make sure the counts still make sense
for (int32_t i = 0; i < 100; i++) {
h.addValue(0);
}
EXPECT_EQ(110, h.computeTotalCount());
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment