2018-02-23 12:56:08 +03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func BenchmarkHistogram(b *testing.B) {
|
|
|
|
h := NewHistogram(NewUniformSample(100))
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
h.Update(int64(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetOrRegisterHistogram(t *testing.T) {
|
|
|
|
r := NewRegistry()
|
|
|
|
s := NewUniformSample(100)
|
|
|
|
NewRegisteredHistogram("foo", r, s).Update(47)
|
2019-11-22 18:04:35 +03:00
|
|
|
if h := GetOrRegisterHistogram("foo", r, s); h.Count() != 1 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Fatal(h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistogram10000(t *testing.T) {
|
|
|
|
h := NewHistogram(NewUniformSample(100000))
|
|
|
|
for i := 1; i <= 10000; i++ {
|
|
|
|
h.Update(int64(i))
|
|
|
|
}
|
|
|
|
testHistogram10000(t, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistogramEmpty(t *testing.T) {
|
|
|
|
h := NewHistogram(NewUniformSample(100))
|
2019-11-22 18:04:35 +03:00
|
|
|
if count := h.Count(); count != 0 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("h.Count(): 0 != %v\n", count)
|
|
|
|
}
|
2019-11-22 18:04:35 +03:00
|
|
|
if min := h.Min(); min != 0 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("h.Min(): 0 != %v\n", min)
|
|
|
|
}
|
2019-11-22 18:04:35 +03:00
|
|
|
if max := h.Max(); max != 0 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("h.Max(): 0 != %v\n", max)
|
|
|
|
}
|
2019-11-22 18:04:35 +03:00
|
|
|
if mean := h.Mean(); mean != 0.0 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("h.Mean(): 0.0 != %v\n", mean)
|
|
|
|
}
|
2019-11-22 18:04:35 +03:00
|
|
|
if stdDev := h.StdDev(); stdDev != 0.0 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("h.StdDev(): 0.0 != %v\n", stdDev)
|
|
|
|
}
|
|
|
|
ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
|
2019-11-22 18:04:35 +03:00
|
|
|
if ps[0] != 0.0 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("median: 0.0 != %v\n", ps[0])
|
|
|
|
}
|
2019-11-22 18:04:35 +03:00
|
|
|
if ps[1] != 0.0 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("75th percentile: 0.0 != %v\n", ps[1])
|
|
|
|
}
|
2019-11-22 18:04:35 +03:00
|
|
|
if ps[2] != 0.0 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("99th percentile: 0.0 != %v\n", ps[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistogramSnapshot(t *testing.T) {
|
|
|
|
h := NewHistogram(NewUniformSample(100000))
|
|
|
|
for i := 1; i <= 10000; i++ {
|
|
|
|
h.Update(int64(i))
|
|
|
|
}
|
|
|
|
snapshot := h.Snapshot()
|
|
|
|
h.Update(0)
|
|
|
|
testHistogram10000(t, snapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testHistogram10000(t *testing.T, h Histogram) {
|
2019-11-22 18:04:35 +03:00
|
|
|
if count := h.Count(); count != 10000 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("h.Count(): 10000 != %v\n", count)
|
|
|
|
}
|
2019-11-22 18:04:35 +03:00
|
|
|
if min := h.Min(); min != 1 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("h.Min(): 1 != %v\n", min)
|
|
|
|
}
|
2019-11-22 18:04:35 +03:00
|
|
|
if max := h.Max(); max != 10000 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("h.Max(): 10000 != %v\n", max)
|
|
|
|
}
|
2019-11-22 18:04:35 +03:00
|
|
|
if mean := h.Mean(); mean != 5000.5 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("h.Mean(): 5000.5 != %v\n", mean)
|
|
|
|
}
|
2019-11-22 18:04:35 +03:00
|
|
|
if stdDev := h.StdDev(); stdDev != 2886.751331514372 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("h.StdDev(): 2886.751331514372 != %v\n", stdDev)
|
|
|
|
}
|
|
|
|
ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
|
2019-11-22 18:04:35 +03:00
|
|
|
if ps[0] != 5000.5 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("median: 5000.5 != %v\n", ps[0])
|
|
|
|
}
|
2019-11-22 18:04:35 +03:00
|
|
|
if ps[1] != 7500.75 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("75th percentile: 7500.75 != %v\n", ps[1])
|
|
|
|
}
|
2019-11-22 18:04:35 +03:00
|
|
|
if ps[2] != 9900.99 {
|
2018-02-23 12:56:08 +03:00
|
|
|
t.Errorf("99th percentile: 9900.99 != %v\n", ps[2])
|
|
|
|
}
|
|
|
|
}
|