Support addValueAggregated() for histogram
authorTian Fang <tfang@fb.com>
Mon, 21 Apr 2014 02:23:13 +0000 (19:23 -0700)
committerDave Watson <davejwatson@fb.com>
Tue, 20 May 2014 19:53:52 +0000 (12:53 -0700)
Summary:
Support addValueAggregated() for histogram to add multiple samples for
one bucket at one time.

Test Plan: Have a client to call the new API and test in production.

Reviewed By: simpkins@fb.com

FB internal diff: D1286528

folly/stats/Histogram.h

index bbdc9b3141e28e4ce2501921b3bf1920dde1d92a..55c5c7b2d8c06ef59a020eb3169eec0b7a5ac93e 100644 (file)
@@ -238,6 +238,14 @@ class Histogram {
     bucket.count += 1;
   }
 
+  /* Add multiple same data points to the histogram */
+  void addRepeatedValue(ValueType value, uint64_t nSamples) {
+    Bucket& bucket = buckets_.getByValue(value);
+    // TODO: It would be nice to handle overflow here.
+    bucket.sum += value * nSamples;
+    bucket.count += nSamples;
+  }
+
   /*
    * Remove a data point to the histogram
    *
@@ -248,8 +256,26 @@ class Histogram {
   void removeValue(ValueType value) {
     Bucket& bucket = buckets_.getByValue(value);
     // TODO: It would be nice to handle overflow here.
-    bucket.sum -= value;
-    bucket.count -= 1;
+    if (bucket.count > 0) {
+      bucket.sum -= value;
+      bucket.count -= 1;
+    } else {
+      bucket.sum = ValueType();
+      bucket.count = 0;
+    }
+  }
+
+  /* Remove multiple same data points from the histogram */
+  void removeRepeatedValue(ValueType value, uint64_t nSamples) {
+    Bucket& bucket = buckets_.getByValue(value);
+    // TODO: It would be nice to handle overflow here.
+    if (bucket.count >= nSamples) {
+      bucket.sum -= value * nSamples;
+      bucket.count -= nSamples;
+    } else {
+      bucket.sum = ValueType();
+      bucket.count = 0;
+    }
   }
 
   /* Remove all data points from the histogram */