Commit 01b6297a authored by Adphi's avatar Adphi
Browse files

fix stats

parent 9ca71674
......@@ -34,11 +34,15 @@ func (u *destination) ping(pinger *ping.Pinger, timeout time.Duration) {
func (s *history) addResult(rtt time.Duration, err error) {
s.mtx.Lock()
if len(s.results) > 1 {
s.results = append([]time.Duration{rtt}, s.results[:len(s.results)-2]...)
} else if len(s.results) == 1 {
switch len(s.results) {
case 0:
case 1:
s.results[0] = rtt
default:
s.results = append([]time.Duration{rtt}, s.results[:len(s.results)-1]...)
}
// TODO : What if we reach max of int
if err == nil {
s.received++
} else {
......@@ -56,44 +60,42 @@ func (s *history) compute() (st Statistics) {
if s.received == 0 {
if s.lost > 0 {
st.PacketLoss = 100
st.Rtts = make([]time.Duration, 10)
st.Rtts = make([]time.Duration, len(s.results))
copy(st.Rtts, s.results)
}
return
}
collection := s.results[:]
st.PacketsSent = s.received + s.lost
size := len(s.results)
// we don't yet have filled the buffer
if s.received <= size {
collection = s.results[:s.received]
size = s.received
}
st.PacketLoss = float64(s.lost) / float64(s.received+s.lost) * 100
st.MinRtt, st.MaxRtt = collection[0], collection[0]
st.MinRtt, st.MaxRtt = s.results[0], s.results[0]
total := time.Duration(0)
for _, rtt := range collection {
if rtt < st.MinRtt {
count := 0
for _, rtt := range s.results {
if rtt < st.MinRtt && rtt != 0 {
st.MinRtt = rtt
}
if rtt > st.MaxRtt {
st.MaxRtt = rtt
}
if rtt != 0 {
count++
}
total += rtt
}
st.AvgRtt = time.Duration(float64(total) / float64(size))
if count == 0 {
count = 1
}
st.AvgRtt = time.Duration(float64(total) / float64(count))
stddevNum := float64(0)
for _, rtt := range collection {
for _, rtt := range s.results {
stddevNum += math.Pow(float64(rtt-st.AvgRtt), 2)
}
st.StdDevRtt = time.Duration(math.Sqrt(stddevNum / float64(size)))
st.Rtts = make([]time.Duration, 10)
st.StdDevRtt = time.Duration(math.Sqrt(stddevNum / float64(count)))
st.Rtts = make([]time.Duration, len(s.results))
copy(st.Rtts, s.results)
return
}
......@@ -40,18 +40,6 @@ func TestComputeStats(t *testing.T) {
received: 1,
best: ms, worst: ms, mean: ms, stddev: z,
},
{
title: "same as before, but sent>len(res)",
results: []time.Duration{ms},
received: 3,
best: ms, worst: ms, mean: ms, stddev: z,
},
{
title: "same as before, but sent<len(res)",
results: []time.Duration{ms, ms, 5 * ms},
received: 2,
best: ms, worst: ms, mean: ms, stddev: z,
},
{
title: "different numbers, manually calculated",
results: []time.Duration{ms, 2 * ms},
......@@ -110,3 +98,44 @@ func TestComputeStats(t *testing.T) {
assert.InDelta(tc.loss, subject.PacketLoss, 0.01, "test case #%d (%s): pktLoss", i, tc.title)
}
}
func Test_history_addResult(t *testing.T) {
h := &history{
results: make([]time.Duration, 10),
}
h.addResult(1, nil)
assert.Equal(t, []time.Duration{1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, h.results)
h.addResult(2, nil)
assert.Equal(t, []time.Duration{2, 1, 0, 0, 0, 0, 0, 0, 0, 0}, h.results)
h.addResult(3, nil)
assert.Equal(t, []time.Duration{3, 2, 1, 0, 0, 0, 0, 0, 0, 0}, h.results)
h.addResult(4, nil)
assert.Equal(t, []time.Duration{4, 3, 2, 1, 0, 0, 0, 0, 0, 0}, h.results)
h.addResult(5, nil)
assert.Equal(t, []time.Duration{5, 4, 3, 2, 1, 0, 0, 0, 0, 0}, h.results)
h.addResult(6, nil)
assert.Equal(t, []time.Duration{6, 5, 4, 3, 2, 1, 0, 0, 0, 0}, h.results)
h.addResult(7, nil)
assert.Equal(t, []time.Duration{7, 6, 5, 4, 3, 2, 1, 0, 0, 0}, h.results)
h.addResult(8, nil)
assert.Equal(t, []time.Duration{8, 7, 6, 5, 4, 3, 2, 1, 0, 0}, h.results)
h.addResult(9, nil)
assert.Equal(t, []time.Duration{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, h.results)
h.addResult(10, nil)
assert.Equal(t, []time.Duration{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, h.results)
h.addResult(11, nil)
assert.Equal(t, []time.Duration{11, 10, 9, 8, 7, 6, 5, 4, 3, 2}, h.results)
h.addResult(0, nil)
assert.Equal(t, []time.Duration{0, 11, 10, 9, 8, 7, 6, 5, 4, 3}, h.results)
}
......@@ -140,10 +140,10 @@ func TestPingerReset(t *testing.T) {
continue
}
if resetDone {
s := p.Statistics()
if !p.IsRunning() {
return
}
s := p.Statistics()
logrus.Debug(s)
l, ok := s[ip1]
......@@ -217,10 +217,10 @@ func TestTwoIPs(t *testing.T) {
select {
case <-c.C:
count++
s := p.Statistics()
if !p.IsRunning() {
return
}
s := p.Statistics()
assert.NotEmpty(t, s)
logrus.Debug(s)
......@@ -246,11 +246,11 @@ func TestPinger(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
p, err := NewPinger(ctx, "127.0.0.1", "255.0.0.255", "127.0.0.2")
p.SetLogger(logrus.StandardLogger())
assert.NoError(t, err)
if err != nil {
t.FailNow()
}
p.SetLogger(logrus.StandardLogger())
go func() {
time.Sleep(5 * time.Second)
......@@ -277,10 +277,10 @@ func TestPinger(t *testing.T) {
select {
case <-tk.C:
count++
s := p.Statistics()
if !p.IsRunning() {
return
}
s := p.Statistics()
logrus.Debugf("count: %d", count)
for _, v := range s {
logStats(v)
......@@ -335,13 +335,13 @@ func TestPingerStatistics(t *testing.T) {
for {
select {
case <-tk.C:
count++
s := p.Statistics()
if !p.IsRunning() {
return
}
count++
s := p.Statistics()
ls, ok := s["127.0.0.1"]
assert.True(t, ok)
require.True(t, ok)
logrus.Debugf("127.0.0.1 : %v", ls)
assert.Equal(t, 10, len(ls.Rtts))
for i, rtt := range ls.Rtts {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment