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
}
}
+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 CountFn>
unsigned int HistogramBuckets<T, BucketType>::getPercentileBucketIdx(
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.
*
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
*
EXPECT_EQ(1, h.getBucketByIndex(2).count);
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());
+}