Skip to content

Commit

Permalink
feat: add user feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
b4nst committed Sep 9, 2024
1 parent 6a20426 commit 9e94ed2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
33 changes: 30 additions & 3 deletions cmd/icmperf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,47 @@ package main
import (
"fmt"
"net"
"time"

"github.com/alecthomas/kong"

"github.com/b4nst/icmperf/pkg/cli"
"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
}
}
}
2 changes: 1 addition & 1 deletion pkg/pinger/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())))
}

0 comments on commit 9e94ed2

Please sign in to comment.