Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(histogram): adding percentile in histogram #241

Merged
merged 2 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions z/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,41 @@ func (histogram *HistogramData) String() string {
b.WriteString(" --\n")
return b.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
if pval <= 0 {
if i == len(histogram.Bounds) {
break
}
return histogram.Bounds[i]
}
}
// default return should be the max range
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
}
80 changes: 80 additions & 0 deletions z/histogram_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package z

import (
"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)
for i := range bounds {
if i == 0 {
bounds[0] = 32
continue
}
if i == size {
bounds[i] = 514
break
}
bounds[i] = bounds[i-1] + 4
}

h := NewHistogramData(bounds)
for v := 16; v <= 1024; v = v + 4 {
for i := 0; i < 1000; i++ {
h.Update(int64(v))
}
}

require.Equal(t, h.Percentile(0.0), 32.0)
}

func TestPercentile99(t *testing.T) {
size := int(math.Ceil((float64(514) - float64(32)) / float64(4)))
bounds := make([]float64, size+1)
for i := range bounds {
if i == 0 {
bounds[0] = 32
continue
}
if i == size {
bounds[i] = 514
break
}
bounds[i] = bounds[i-1] + 4
}
h := NewHistogramData(bounds)
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), 512.0)
}

func TestPercentile100(t *testing.T) {
size := int(math.Ceil((float64(514) - float64(32)) / float64(4)))
bounds := make([]float64, size+1)
for i := range bounds {
if i == 0 {
bounds[0] = 32
continue
}
if i == size {
bounds[i] = 514
break
}
bounds[i] = bounds[i-1] + 4
}
h := NewHistogramData(bounds)
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)
}