2016-06-01 02:31:50 +08:00
|
|
|
package peerstore
|
2015-10-01 06:42:55 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-04-21 01:20:46 +08:00
|
|
|
"math"
|
2015-10-01 06:42:55 +08:00
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-09-02 19:10:55 +08:00
|
|
|
"github.com/libp2p/go-libp2p-peer/test"
|
2015-10-01 06:42:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLatencyEWMAFun(t *testing.T) {
|
|
|
|
t.Skip("run it for fun")
|
|
|
|
|
2016-06-01 02:31:50 +08:00
|
|
|
m := NewMetrics()
|
2015-10-01 06:42:55 +08:00
|
|
|
id, err := testutil.RandPeerID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mu := 100.0
|
|
|
|
sig := 10.0
|
|
|
|
next := func() time.Duration {
|
|
|
|
mu = (rand.NormFloat64() * sig) + mu
|
|
|
|
return time.Duration(mu)
|
|
|
|
}
|
|
|
|
|
|
|
|
print := func() {
|
|
|
|
fmt.Printf("%3.f %3.f --> %d\n", sig, mu, m.LatencyEWMA(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-time.After(200 * time.Millisecond):
|
|
|
|
m.RecordLatency(id, next())
|
|
|
|
print()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 01:20:46 +08:00
|
|
|
|
|
|
|
func TestLatencyEWMA(t *testing.T) {
|
2016-06-01 02:31:50 +08:00
|
|
|
m := NewMetrics()
|
2016-04-21 01:20:46 +08:00
|
|
|
id, err := testutil.RandPeerID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := 100.0
|
|
|
|
mu := exp
|
|
|
|
sig := 10.0
|
|
|
|
next := func() time.Duration {
|
2016-04-21 02:52:03 +08:00
|
|
|
mu := (rand.NormFloat64() * sig) + mu
|
2016-04-21 01:20:46 +08:00
|
|
|
return time.Duration(mu)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
select {
|
|
|
|
case <-time.After(200 * time.Millisecond):
|
|
|
|
m.RecordLatency(id, next())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lat := m.LatencyEWMA(id)
|
|
|
|
if math.Abs(exp-float64(lat)) > sig {
|
|
|
|
t.Fatal("latency outside of expected range: ", exp, lat, sig)
|
|
|
|
}
|
|
|
|
}
|