diff --git a/cmd/multiping/destination.go b/cmd/multiping/destination.go index c860a56..e792f1f 100644 --- a/cmd/multiping/destination.go +++ b/cmd/multiping/destination.go @@ -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)) } @@ -40,19 +51,19 @@ 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 { @@ -60,28 +71,28 @@ func (s *history) compute() (pktSent int, pktLoss float64, last, best, worst, me 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 } diff --git a/cmd/multiping/ui.go b/cmd/multiping/ui.go index 731c43a..3a7d44a 100644 --- a/cmd/multiping/ui.go +++ b/cmd/multiping/ui.go @@ -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))