From 9c428ab9e1fd05eee817eb05085d727cf1b72378 Mon Sep 17 00:00:00 2001 From: Tian Fang Date: Sun, 20 Apr 2014 19:23:13 -0700 Subject: [PATCH] Support addValueAggregated() for histogram 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 | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/folly/stats/Histogram.h b/folly/stats/Histogram.h index bbdc9b31..55c5c7b2 100644 --- a/folly/stats/Histogram.h +++ b/folly/stats/Histogram.h @@ -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 */ -- 2.34.1