34 lines
1.2 KiB
Go
34 lines
1.2 KiB
Go
package telemetry
|
|
|
|
import (
|
|
"git.linuxiarz.pl/gru/EasyDNS-Web/internal/easydns"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSamplesAreMeasuredOnly(t *testing.T) {
|
|
h := New()
|
|
t0 := time.Unix(1000000, 0)
|
|
h.Record(t0, easydns.Stats{QueriesTotal: 100, Responses: map[string]uint64{"noerror": 90}, Cache: easydns.Cache{Hits: 50, Misses: 10}, LatencyP95S: 0.014})
|
|
sm, _, _ := h.Snapshot()
|
|
if len(sm) != 1 || sm[0].QueryRate != nil {
|
|
t.Fatal("must not fake initial rate")
|
|
}
|
|
h.Record(t0.Add(10*time.Second), easydns.Stats{QueriesTotal: 150, Responses: map[string]uint64{"noerror": 136}, Cache: easydns.Cache{Hits: 90, Misses: 20}, LatencyP95S: 0.010})
|
|
sm, _, _ = h.Snapshot()
|
|
if len(sm) != 2 || sm[1].QueryRate == nil || *sm[1].QueryRate != 5 {
|
|
t.Fatalf("bad query rate: %+v", sm)
|
|
}
|
|
if sm[1].CacheHitRate == nil || *sm[1].CacheHitRate != 80 {
|
|
t.Fatalf("bad cache ratio: %+v", sm[1].CacheHitRate)
|
|
}
|
|
if sm[1].P95MS != 10 {
|
|
t.Fatalf("bad latency: %v", sm[1].P95MS)
|
|
}
|
|
h.Record(t0.Add(20*time.Second), easydns.Stats{QueriesTotal: 0, Responses: map[string]uint64{}, Cache: easydns.Cache{Hits: 0, Misses: 0}})
|
|
sm, _, _ = h.Snapshot()
|
|
if sm[2].QueryRate != nil || sm[2].CacheHitRate != nil {
|
|
t.Fatal("counter reset must not produce negative/wrong rates")
|
|
}
|
|
}
|