Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
histogram tests (#20)
Browse files Browse the repository at this point in the history
* histogram tests

* nil

* test

* Update Gopkg.toml

* Update Gopkg.toml

* Update Gopkg.toml
  • Loading branch information
djia-vm-wf authored and vikramraman committed Nov 15, 2019
1 parent dc8ddeb commit 81af74d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[[constraint]]
name = "github.com/wavefronthq/wavefront-sdk-go"
version = "0.9.3"
revision = "3efe8a103d9f8f22e557916c23fc8825cf3309c2"

[prune]
go-tests = true
Expand Down
77 changes: 56 additions & 21 deletions reporting/histogram_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,69 @@
package reporting

import (
"math"
"testing"
"time"

metrics "github.com/rcrowley/go-metrics"
"github.com/stretchr/testify/assert"
)

var pow10, inc100, inc1000, emptyHistogram = NewHistogram(), NewHistogram(), NewHistogram(), NewHistogram()

func createPow10Histogram() {
pow10_ := [9]int64{0, 1, 10, 10, 100, 1000, 10000, 10000, 100000}
for _, num := range pow10_ {
pow10.Update(num)
}
}

func setup() {
createPow10Histogram()
for i := 1; i <= 100; i++ {
inc100.Update(int64(i))
}
for i := 1; i <= 1000; i++ {
inc1000.Update(int64(i))
}
time.Sleep(1 * time.Minute) //flush to priorTimedBin
}

func TestHistogramCal(t *testing.T) {
setup()
//Count
assert.Equal(t, int64(9), pow10.Count())
assert.Equal(t, int64(9), pow10.Snapshot().Count())
assert.Equal(t, int64(0), emptyHistogram.Count())
//Max
assert.Equal(t, int64(100000), pow10.Max())
assert.Equal(t, int64(100000), pow10.Snapshot().Max())
assert.Equal(t, int64(math.NaN()), emptyHistogram.Max())
//Min
assert.Equal(t, int64(1), inc100.Min())
assert.Equal(t, int64(1), inc100.Snapshot().Min())
assert.Equal(t, int64(math.NaN()), emptyHistogram.Min())
//Mean
assert.Equal(t, float64(13457.888888888889), pow10.Mean())
assert.Equal(t, float64(13457.888888888889), pow10.Snapshot().Mean())
assert.True(t, math.IsNaN(emptyHistogram.Mean()))
//Sum
assert.Equal(t, int64(121121), pow10.Sum())
assert.Equal(t, int64(121121), pow10.Snapshot().Sum())
assert.Equal(t, int64(0), emptyHistogram.Sum())
//StdDev
stddev := pow10.StdDev()
assert.Equal(t, float64(30859.857493890177), stddev)
assert.Equal(t, float64(0), emptyHistogram.StdDev())
//Percentiles
snapshot := inc100.Snapshot()
assert.Equal(t, 25.25, snapshot.Percentile(0.25))
assert.Equal(t, 75.75, snapshot.Percentile(0.75))
assert.Equal(t, 98.98, snapshot.Percentile(0.98))
assert.Equal(t, 99.99, snapshot.Percentile(0.99))
assert.Equal(t, 999.999, inc1000.Snapshot().Percentile(0.999))
}

func TestWFHistogramAPI(t *testing.T) {
h := NewHistogram()

Expand All @@ -25,24 +81,3 @@ func TestWFHistogramAPI(t *testing.T) {
t.Fatalf("the histogram is not 'histogram.Histogram'")
}
}

var pow10 = NewHistogram()

func setup() {
pow10.Update(0)
pow10.Update(1)
pow10.Update(10)
pow10.Update(10)
pow10.Update(100)
pow10.Update(1000)
pow10.Update(10000)
pow10.Update(10000)
pow10.Update(100000)
}

func TestHistogram_StdDev(t *testing.T) {
setup()
time.Sleep(1 * time.Minute) //flush to priorTimedBin
stddev := pow10.StdDev()
assert.Equal(t, float64(30859.857493890177), stddev)
}

0 comments on commit 81af74d

Please sign in to comment.