From: Hasnain Lakhani Date: Tue, 19 May 2015 18:26:24 +0000 (-0700) Subject: Add getTotalCount() to Histogram X-Git-Tag: v0.39.0~5 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=2a919488e71de244afa7297520894ccbff2e0692;p=folly.git 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 --- diff --git a/folly/stats/Histogram-defs.h b/folly/stats/Histogram-defs.h index fe745c81..925ab28e 100644 --- a/folly/stats/Histogram-defs.h +++ b/folly/stats/Histogram-defs.h @@ -61,6 +61,17 @@ unsigned int HistogramBuckets::getBucketIdx( } } +template +template +const uint64_t HistogramBuckets::computeTotalCount( + CountFn countFromBucket) const { + uint64_t count = 0; + for (unsigned int n = 0; n < buckets_.size(); ++n) { + count += countFromBucket(const_cast(buckets_[n])); + } + return count; +} + template template unsigned int HistogramBuckets::getPercentileBucketIdx( diff --git a/folly/stats/Histogram.h b/folly/stats/Histogram.h index 86bf9974..c3bdf96d 100644 --- a/folly/stats/Histogram.h +++ b/folly/stats/Histogram.h @@ -142,6 +142,18 @@ class HistogramBuckets { 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 + const uint64_t computeTotalCount(CountFn countFromBucket) const; + /** * Determine which bucket the specified percentile falls into. * @@ -376,6 +388,16 @@ class Histogram { 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 * diff --git a/folly/test/HistogramTest.cpp b/folly/test/HistogramTest.cpp index 8ddb6a38..fac9cc14 100644 --- a/folly/test/HistogramTest.cpp +++ b/folly/test/HistogramTest.cpp @@ -203,3 +203,22 @@ TEST(Histogram, TestDoubleWidthTooBig) { 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 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()); +}