Add getTotalCount() to Histogram
authorHasnain Lakhani <mhl@fb.com>
Tue, 19 May 2015 18:26:24 +0000 (11:26 -0700)
committerViswanath Sivakumar <viswanath@fb.com>
Wed, 20 May 2015 17:57:11 +0000 (10:57 -0700)
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

folly/stats/Histogram-defs.h
folly/stats/Histogram.h
folly/test/HistogramTest.cpp

index fe745c81e193dd03a7aa6719bd418eaa81805d0b..925ab28e4ad0142199edb80d728635f198e4717e 100644 (file)
@@ -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 CountFn>
 unsigned int HistogramBuckets<T, BucketType>::getPercentileBucketIdx(
index 86bf997454d705281d88e39b9243620f41534a01..c3bdf96db5f77d5c723a32f82c41fc4f46a2855f 100644 (file)
@@ -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 <typename CountFn>
+  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
    *
index 8ddb6a380ee243904e586adb7794e221226ac8be..fac9cc144e657c2c2e8409b7eb76c6f3d19b81d9 100644 (file)
@@ -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<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());
+}