forked from schweikert/fping-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
measurement.go
103 lines (92 loc) · 1.63 KB
/
measurement.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
103
package main
import (
"fmt"
"sort"
"strconv"
"strings"
)
type Measurements struct {
rtt []float64
lost []bool
}
func ParseMeasurements(text string) (Measurements, error) {
valuesText := strings.Split(text, " ")
m := Measurements{
rtt: make([]float64, len(valuesText)),
lost: make([]bool, len(valuesText)),
}
for i, valText := range valuesText {
if valText == "-" {
m.lost[i] = true
} else {
m.lost[i] = false
rtt, err := strconv.ParseFloat(valText, 32)
if err != nil {
return m, err
}
m.rtt[i] = float64(rtt) / 1000.0
}
}
sort.Sort(m)
return m, nil
}
func (m Measurements) Len() int {
return len(m.rtt)
}
func (m Measurements) Less(i, j int) bool {
if m.lost[i] {
return false
}
if m.lost[j] {
return true
}
return m.rtt[i] < m.rtt[j]
}
func (m Measurements) Swap(i, j int) {
m.rtt[i], m.rtt[j] = m.rtt[j], m.rtt[i]
m.lost[i], m.lost[j] = m.lost[j], m.lost[i]
}
func (m Measurements) String() string {
var str strings.Builder
for i := range m.rtt {
if i != 0 {
str.WriteString(" ")
}
if m.lost[i] {
str.WriteString("-")
} else {
str.WriteString(fmt.Sprintf("%.2f", m.rtt[i]))
}
}
return str.String()
}
func (m Measurements) GetSentCount() int {
return len(m.lost)
}
func (m Measurements) GetLostCount() int {
var count int
for _, val := range m.lost {
if val {
count++
}
}
return count
}
func (m Measurements) GetRTTSum() float64 {
var sum float64
for i, val := range m.rtt {
if !m.lost[i] {
sum += val
}
}
return sum
}
func (m Measurements) GetRTTCount() int {
var count int
for _, val := range m.lost {
if !val {
count++
}
}
return count
}