diff --git a/cmd/icmperf/main.go b/cmd/icmperf/main.go index 365fc30..e961ccb 100644 --- a/cmd/icmperf/main.go +++ b/cmd/icmperf/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "net" + "time" "github.com/alecthomas/kong" @@ -10,13 +11,39 @@ import ( "github.com/b4nst/icmperf/pkg/pinger" ) +type result struct { + stats *pinger.Statistics + err error +} + func main() { cli := cli.CLI{} ktx := kong.Parse(&cli) pinger := pinger.NewPinger(cli.Size, cli.Duration) - stats, err := pinger.Eval((*net.IPAddr)(&cli.Target)) - ktx.FatalIfErrorf(err) + fmt.Printf("Evaluating %s (etc %s)...\n", cli.Target.IP, cli.Duration) + + r := make(chan result) + go func() { + stats, err := pinger.Eval((*net.IPAddr)(&cli.Target)) + r <- result{stats, err} + }() + start := time.Now() + ticker := time.NewTicker(1 * time.Second) - fmt.Println(stats) + for { + select { + case t := <-ticker.C: + etc := cli.Duration - t.Sub(start) + if etc < 0 { + etc = 0 + } + stats := pinger.Stats() + fmt.Printf("%s (etc %s)...\n", stats, etc.Round(time.Second)) + case res := <-r: + ktx.FatalIfErrorf(res.err) + fmt.Println(res.stats) + return + } + } } diff --git a/pkg/pinger/statistics.go b/pkg/pinger/statistics.go index fe2e474..25aefb4 100644 --- a/pkg/pinger/statistics.go +++ b/pkg/pinger/statistics.go @@ -33,5 +33,5 @@ func (s *Statistics) Loss() float64 { } func (s *Statistics) String() string { - return fmt.Sprintf("%s sent, %s received in %s at %s/sec\n", humanize.Bytes(uint64(s.ByteSent())), humanize.Bytes(uint64(s.ByteReceived())), s.Duration, humanize.Bytes(uint64(s.Bitrate()))) + return fmt.Sprintf("%s sent, %s received in %s at %s/sec", humanize.Bytes(uint64(s.ByteSent())), humanize.Bytes(uint64(s.ByteReceived())), s.Duration, humanize.Bytes(uint64(s.Bitrate()))) }