Skip to content

Commit

Permalink
cmd/multiping: group statistical data in a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
dmke committed Feb 23, 2018
1 parent dfb14eb commit 31872c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
37 changes: 24 additions & 13 deletions cmd/multiping/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ type destination struct {
*history
}

type stat struct {
pktSent int
pktLoss float64
last time.Duration
best time.Duration
worst time.Duration
mean time.Duration
stddev time.Duration
lastError string
}

func (u *destination) ping(pinger *ping.Pinger) {
u.addResult(pinger.PingRTT(u.remote))
}
Expand All @@ -40,48 +51,48 @@ func (s *history) addResult(rtt time.Duration, err error) {
s.mtx.Unlock()
}

func (s *history) compute() (pktSent int, pktLoss float64, last, best, worst, mean, stddev time.Duration) {
func (s *history) compute() (st stat) {
s.mtx.RLock()
defer s.mtx.RUnlock()

if s.received == 0 {
if s.lost > 0 {
pktLoss = 1.0
st.pktLoss = 1.0
}
return
}

collection := s.results[:]
pktSent = s.received + s.lost
st.pktSent = s.received + s.lost
size := len(s.results)

if s.received < size {
collection = s.results[:s.received]
size = s.received
}

last = collection[s.received%size]
pktLoss = float64(s.lost) / float64(size)
best, worst = collection[0], collection[0]
st.last = collection[s.received%size]
st.pktLoss = float64(s.lost) / float64(size)
st.best, st.worst = collection[0], collection[0]

total := time.Duration(0)
for _, rtt := range collection {
if rtt < best {
best = rtt
if rtt < st.best {
st.best = rtt
}
if rtt > worst {
worst = rtt
if rtt > st.worst {
st.worst = rtt
}
total += rtt
}

mean = time.Duration(float64(total) / float64(s.received))
st.mean = time.Duration(float64(total) / float64(s.received))

stddevNum := float64(0)
for _, rtt := range collection {
stddevNum += math.Pow(float64(rtt-mean), 2)
stddevNum += math.Pow(float64(rtt-st.mean), 2)
}
stddev = time.Duration(math.Sqrt(stddevNum / float64(mean)))
st.stddev = time.Duration(math.Sqrt(stddevNum / float64(st.mean)))

return
}
16 changes: 8 additions & 8 deletions cmd/multiping/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ func (ui *userInterface) update(interval time.Duration) {
time.Sleep(interval)
for {
for i, u := range ui.destinations {
sent, loss, last, best, worst, mean, stddev := u.compute()
stats := u.compute()
r := i + 2

ui.table.GetCell(r, 2).SetText(strconv.Itoa(sent))
ui.table.GetCell(r, 3).SetText(fmt.Sprintf("%0.2f%%", loss))
ui.table.GetCell(r, 4).SetText(ts(last))
ui.table.GetCell(r, 5).SetText(ts(best))
ui.table.GetCell(r, 6).SetText(ts(worst))
ui.table.GetCell(r, 7).SetText(ts(mean))
ui.table.GetCell(r, 8).SetText(stddev.String())
ui.table.GetCell(r, 2).SetText(strconv.Itoa(stats.sent))
ui.table.GetCell(r, 3).SetText(fmt.Sprintf("%0.2f%%", stats.loss))
ui.table.GetCell(r, 4).SetText(ts(stats.last))
ui.table.GetCell(r, 5).SetText(ts(stats.best))
ui.table.GetCell(r, 6).SetText(ts(stats.worst))
ui.table.GetCell(r, 7).SetText(ts(stats.mean))
ui.table.GetCell(r, 8).SetText(stats.stddev.String())

if u.lastErr != nil {
ui.table.GetCell(r, 8).SetText(fmt.Sprintf("%v", u.lastErr))
Expand Down

0 comments on commit 31872c4

Please sign in to comment.