From: Tian Fang Date: Mon, 21 Apr 2014 02:23:13 +0000 (-0700) Subject: Support addValueAggregated() for histogram X-Git-Tag: v0.22.0~582 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=9c428ab9e1fd05eee817eb05085d727cf1b72378;p=folly.git 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 --- 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 */