Skip to content

Commit

Permalink
Add histogram.Mean() method (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ibrahim Jarif authored Sep 1, 2020
1 parent 834a9bc commit 9d26abc
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions z/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ func (histogram *HistogramData) Update(value int64) {
}
}

// Mean returns the mean value for the histogram.
func (histogram *HistogramData) Mean() float64 {
if histogram.Count == 0 {
return 0
}
return float64(histogram.Sum) / float64(histogram.Count)
}

// String converts the histogram data into human-readable string.
func (histogram *HistogramData) String() string {
if histogram == nil {
Expand All @@ -106,8 +114,7 @@ func (histogram *HistogramData) String() string {
b.WriteString(" -- Histogram: ")
b.WriteString(fmt.Sprintf("Min value: %d ", histogram.Min))
b.WriteString(fmt.Sprintf("Max value: %d ", histogram.Max))
b.WriteString(fmt.Sprintf("Mean: %.2f ",
float64(histogram.Sum)/float64(histogram.Count)))
b.WriteString(fmt.Sprintf("Mean: %.2f ", histogram.Mean()))

numBounds := len(histogram.Bounds)
for index, count := range histogram.CountPerBucket {
Expand Down

0 comments on commit 9d26abc

Please sign in to comment.