monitor: Remove Distribution() method and test.

Follow up to #939. Fixes #1586.
This commit is contained in:
Hector Sanjuan 2022-02-28 17:47:34 +01:00
parent 9e35b7bc9b
commit 49825d1df7
3 changed files with 0 additions and 151 deletions

View File

@ -175,25 +175,6 @@ func (mtrs *Store) PeerLatest(name string, pid peer.ID) *api.Metric {
return m
}
// Distribution returns the distribution of a particular metrics
// for a particular peer.
func (mtrs *Store) Distribution(name string, pid peer.ID) []float64 {
mtrs.mux.RLock()
defer mtrs.mux.RUnlock()
byPeer, ok := mtrs.byName[name]
if !ok {
return nil
}
window, ok := byPeer[pid]
if !ok {
return nil
}
return window.Distribution()
}
// MetricNames returns all the known metric names
func (mtrs *Store) MetricNames() []string {
mtrs.mux.RLock()

View File

@ -91,20 +91,3 @@ func (mw *Window) All() []*api.Metric {
return values
}
// Distribution returns the deltas between all the current
// values contained in the current window. This will
// only return values if the api.Metric.Type() is "ping",
// which are used for accural failure detection.
func (mw *Window) Distribution() []float64 {
ms := mw.All()
dist := make([]float64, 0, len(ms)-1)
// the last value can't be used to calculate a delta
for i, v := range ms[:len(ms)-1] {
// All() provides an order slice, where ms[i] is younger than ms[i+1]
delta := v.ReceivedAt - ms[i+1].ReceivedAt
dist = append(dist, float64(delta))
}
return dist
}

View File

@ -2,7 +2,6 @@ package metrics
import (
"fmt"
"strconv"
"testing"
"time"
@ -83,20 +82,6 @@ func TestWindow_Race(t *testing.T) {
}
}()
// go routine to query distribution at regular interval
distributionTicker := time.NewTicker(100 * time.Millisecond)
go func() {
<-start
for {
select {
case <-distributionTicker.C:
log <- fmt.Sprintf("dist: %v", w.Distribution())
case <-done:
return
}
}
}()
go func() {
<-start
<-done
@ -342,103 +327,3 @@ func BenchmarkWindow_All(b *testing.B) {
}
})
}
func TestWindow_Distribution(t *testing.T) {
var tests = []struct {
name string
heartbeats []float64
want []float64
}{
{
"even 1 sec distribution",
[]float64{1, 1, 1, 1},
[]float64{1, 1, 1, 1},
},
{
"increasing latency distribution",
[]float64{1, 1, 2, 2, 3, 3, 4},
[]float64{4, 3, 3, 2, 2, 1, 1},
},
{
"random latency distribution",
[]float64{4, 1, 3, 9, 7, 8, 11, 18},
[]float64{18, 11, 8, 7, 9, 3, 1, 4},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mw := NewWindow(len(tt.heartbeats) + 1)
for i, v := range tt.heartbeats {
mw.Add(makeMetric(strconv.Itoa(int(v * 10))))
// time.Sleep on the 1s of milliseconds level is
// susceptible to scheduler variance. Hence we
// multiple the input by 10 and this combined with
// truncating the result to just seconds, we should
// get stable distribution of timings between
// window.Adds.
time.Sleep(time.Duration(v*10) * time.Millisecond)
if i == len(tt.heartbeats)-1 {
mw.Add(makeMetric("last"))
}
}
got := mw.Distribution()
if len(got) != len(tt.want) {
t.Errorf("want len: %v, got len: %v", len(tt.want), len(got))
}
var gotseconds []float64
for _, v := range got {
// truncate nanoseconds to seconds for testing purposes
// also truncate decimal places by converting to int and then back
gotseconds = append(gotseconds, float64(int64(v/10000000)))
}
for i, s := range gotseconds {
if s != tt.want[i] {
t.Fatalf("want: %v, got: %v", tt.want, gotseconds)
break
}
}
})
}
}
func BenchmarkWindow_Distribution(b *testing.B) {
b.Run("window size 10", func(b *testing.B) {
mw := NewWindow(10)
for i := 0; i < 10; i++ {
mw.Add(makeMetric("1"))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
mw.Distribution()
}
})
b.Run("window size 25", func(b *testing.B) {
mw := NewWindow(25)
for i := 0; i < 25; i++ {
mw.Add(makeMetric("1"))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
mw.Distribution()
}
})
b.Run("window size 1000", func(b *testing.B) {
mw := NewWindow(1000)
for i := 0; i < 1000; i++ {
mw.Add(makeMetric("1"))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
mw.Distribution()
}
})
}