/
statistic.go
102 lines (99 loc) · 2.6 KB
/
statistic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package kping
import (
"fmt"
"math"
"os"
)
// statistic ping result
func (p *kping) statistic() (statistics map[string]*Statistic) {
statistics = make(map[string]*Statistic, p.ipCount)
// number of rtt value
rttStatistic := make(map[string]int, 15)
for addr, statistic := range p.stats {
// fix sent & recv number
if statistic.SentNum > p.count {
statistic.SentNum = p.count
}
if statistic.RecvNum > p.count {
statistic.RecvNum = p.count
}
statistic.LostNum = float64(statistic.SentNum-statistic.RecvNum) / float64(statistic.SentNum) * 100
if len(statistic.RTTs) <= 0 {
rttStatistic["==0"]++
} else {
rttStatistic[">0"]++
if len(statistic.RTTs) >= 100 {
rttStatistic[">=100"]++
}
if len(statistic.RTTs) >= 90 && len(statistic.RTTs) < 100 {
rttStatistic[">=90"]++
}
if len(statistic.RTTs) >= 80 && len(statistic.RTTs) < 90 {
rttStatistic[">=80"]++
}
if len(statistic.RTTs) >= 70 && len(statistic.RTTs) < 80 {
rttStatistic[">=70"]++
}
if len(statistic.RTTs) >= 60 && len(statistic.RTTs) < 70 {
rttStatistic[">=60"]++
}
if len(statistic.RTTs) >= 50 && len(statistic.RTTs) < 60 {
rttStatistic[">=50"]++
}
if len(statistic.RTTs) >= 40 && len(statistic.RTTs) < 50 {
rttStatistic[">=40"]++
}
if len(statistic.RTTs) >= 30 && len(statistic.RTTs) < 40 {
rttStatistic[">=30"]++
}
if len(statistic.RTTs) >= 20 && len(statistic.RTTs) < 30 {
rttStatistic[">=20"]++
}
// first: calculate avg value
var sum float64
for _, rtt := range statistic.RTTs {
sum += rtt
}
avg := sum / float64(len(statistic.RTTs))
// second: delete bad value
rttsNew := make([]float64, 0, len(statistic.RTTs))
for _, rtt := range statistic.RTTs {
if rtt <= 5*avg && rtt < 500 {
rttsNew = append(rttsNew, rtt)
}
}
// third: calculate again
var min, max float64
if len(rttsNew) > 0 {
min = rttsNew[0]
max = rttsNew[0]
}
sum = 0
for _, rtt := range rttsNew {
if rtt < min {
min = rtt
}
if rtt > max {
max = rtt
}
sum += rtt
}
statistic.MaxRTT = max
statistic.MinRTT = min
if len(rttsNew) > 0 {
statistic.AvgRTT = sum / float64(len(rttsNew))
var sumsquares float64
for _, rtt := range rttsNew {
sumsquares += (rtt - statistic.AvgRTT) * (rtt - statistic.AvgRTT)
}
statistic.StdDevRTT = math.Sqrt(float64(sumsquares / float64(len(rttsNew))))
}
}
statistics[addr] = statistic
}
fmt.Fprintf(os.Stderr, "kping statistic: RTT numbers:\n")
for k, v := range rttStatistic {
fmt.Fprintf(os.Stderr, "\t%s: %d\n", k, v)
}
return statistics
}