Skip to content

Commit

Permalink
adding clear function to histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-bansal committed Feb 1, 2021
1 parent 1b848e7 commit a507938
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
22 changes: 19 additions & 3 deletions z/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,14 @@ func (histogram *HistogramData) String() string {
// Percentile returns the percentile value for the histogram.
// value of p should be between [0.0-1.0]
func (histogram *HistogramData) Percentile(p float64) float64 {
if histogram == nil {
return 0
}

if histogram.Count == 0 {
// if no data return the minimum range
return histogram.Bounds[0]
}

pval := int64(float64(histogram.Count) * p)
for i, v := range histogram.CountPerBucket {
pval = pval - v
Expand All @@ -169,5 +172,18 @@ func (histogram *HistogramData) Percentile(p float64) float64 {
}
}
// default return should be the max range
return histogram.Bounds[len(histogram.Bounds) - 1]
}
return histogram.Bounds[len(histogram.Bounds)-1]
}

// Clear reset the histogram. Helpful in situations where we need to reset the metrics
func (histogram *HistogramData) Clear() {
if histogram == nil {
return
}

histogram.Count = 0
histogram.CountPerBucket = make([]int64, len(histogram.Bounds)+1)
histogram.Sum = 0
histogram.Max = 0
histogram.Min = math.MaxInt64
}
24 changes: 12 additions & 12 deletions z/histogram_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package z

import (
"github.com/stretchr/testify/require"
"math"
"testing"

"github.com/stretchr/testify/require"
)

func TestPercentile00(t *testing.T) {
size := int(math.Ceil((float64(514) - float64(32)) / float64(4)))
bounds := make([]float64, size + 1)
bounds := make([]float64, size+1)
for i := range bounds {
if i == 0 {
bounds[0] = 32
Expand All @@ -22,8 +23,8 @@ func TestPercentile00(t *testing.T) {
}

h := NewHistogramData(bounds)
for v := 16; v <= 1024; v= v+4 {
for i:=0; i < 1000; i++ {
for v := 16; v <= 1024; v = v + 4 {
for i := 0; i < 1000; i++ {
h.Update(int64(v))
}
}
Expand All @@ -33,7 +34,7 @@ func TestPercentile00(t *testing.T) {

func TestPercentile99(t *testing.T) {
size := int(math.Ceil((float64(514) - float64(32)) / float64(4)))
bounds := make([]float64, size + 1)
bounds := make([]float64, size+1)
for i := range bounds {
if i == 0 {
bounds[0] = 32
Expand All @@ -46,18 +47,18 @@ func TestPercentile99(t *testing.T) {
bounds[i] = bounds[i-1] + 4
}
h := NewHistogramData(bounds)
for v := 16; v <= 1024; v= v+4 {
for i:=0; i < 1000; i++ {
for v := 16; v <= 512; v = v + 4 {
for i := 0; i < 1000; i++ {
h.Update(int64(v))
}
}

require.Equal(t, h.Percentile(0.99), 514.0)
require.Equal(t, h.Percentile(0.99), 512.0)
}

func TestPercentile100(t *testing.T) {
size := int(math.Ceil((float64(514) - float64(32)) / float64(4)))
bounds := make([]float64, size + 1)
bounds := make([]float64, size+1)
for i := range bounds {
if i == 0 {
bounds[0] = 32
Expand All @@ -70,11 +71,10 @@ func TestPercentile100(t *testing.T) {
bounds[i] = bounds[i-1] + 4
}
h := NewHistogramData(bounds)
for v := 16; v <= 1024; v= v+4 {
for i:=0; i < 1000; i++ {
for v := 16; v <= 1024; v = v + 4 {
for i := 0; i < 1000; i++ {
h.Update(int64(v))
}
}
require.Equal(t, h.Percentile(1.0), 514.0)
}

0 comments on commit a507938

Please sign in to comment.