Skip to content

Commit

Permalink
Allow skipping ICMP pings
Browse files Browse the repository at this point in the history
  • Loading branch information
maddie committed Jan 15, 2021
1 parent 5cf383b commit c9903d3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions defs/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
OptionIPv6Alt = "6"
OptionNoDownload = "no-download"
OptionNoUpload = "no-upload"
OptionNoICMP = "no-icmp"
OptionConcurrent = "concurrent"
OptionBytes = "bytes"
OptionMebiBytes = "mebibytes"
Expand Down
10 changes: 5 additions & 5 deletions defs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type Server struct {
SponsorName string `json:"sponsorName"`
SponsorURL string `json:"sponsorURL"`

icmpFailed bool `json:"-"`
TLog TelemetryLog `json:"-"`
NoICMP bool `json:"-"`
TLog TelemetryLog `json:"-"`
}

// IsUp checks the speed test backend is up by accessing the ping URL
Expand Down Expand Up @@ -71,8 +71,8 @@ func (s *Server) ICMPPingAndJitter(count int, srcIp, network string) (float64, f
s.TLog.Logf("ICMP ping took %s", time.Now().Sub(t).String())
}()

if s.icmpFailed {
log.Debug("ICMP ping failed already, using HTTP ping")
if s.NoICMP {
log.Debugf("Skipping ICMP for server %s, will use HTTP ping", s.Name)
return s.PingAndJitter(count + 2)
}

Expand Down Expand Up @@ -116,7 +116,7 @@ func (s *Server) ICMPPingAndJitter(count int, srcIp, network string) (float64, f
}

if len(stats.Rtts) == 0 {
s.icmpFailed = true
s.NoICMP = true
log.Debugf("No ICMP pings returned for server %s (%s), trying TCP ping", s.Name, u.Hostname())
return s.PingAndJitter(count + 2)
}
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func main() {
Name: defs.OptionNoUpload,
Usage: "Do not perform upload test",
},
&cli.BoolFlag{
Name: defs.OptionNoICMP,
Usage: "Do not use ICMP ping. ICMP doesn't work well under Linux\n" +
"at this moment, so you might want to disable it",
},
&cli.IntFlag{
Name: defs.OptionConcurrent,
Usage: "Concurrent HTTP requests being made",
Expand Down
3 changes: 3 additions & 0 deletions speedtest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
pb.Start()
}

// skip ICMP if option given
currentServer.NoICMP = c.Bool(defs.OptionNoICMP)

p, jitter, err := currentServer.ICMPPingAndJitter(pingCount, c.String(defs.OptionSource), network)
if err != nil {
log.Errorf("Failed to get ping and jitter: %s", err)
Expand Down
7 changes: 5 additions & 2 deletions speedtest/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func SpeedTest(c *cli.Context) error {

// spawn 10 concurrent pingers
for i := 0; i < 10; i++ {
go pingWorker(jobs, results, &wg, c.String(defs.OptionSource), network)
go pingWorker(jobs, results, &wg, c.String(defs.OptionSource), network, c.Bool(defs.OptionNoICMP))
}

// send ping jobs to workers
Expand Down Expand Up @@ -303,7 +303,7 @@ func SpeedTest(c *cli.Context) error {
}
}

func pingWorker(jobs <-chan PingJob, results chan<- PingResult, wg *sync.WaitGroup, srcIp, network string) {
func pingWorker(jobs <-chan PingJob, results chan<- PingResult, wg *sync.WaitGroup, srcIp, network string, noICMP bool) {
for {
job := <-jobs
server := job.Server
Expand All @@ -317,6 +317,9 @@ func pingWorker(jobs <-chan PingJob, results chan<- PingResult, wg *sync.WaitGro

// check the server is up by accessing the ping URL and checking its returned value == empty and status code == 200
if server.IsUp() {
// skip ICMP if option given
server.NoICMP = noICMP

// if server is up, get ping
ping, _, err := server.ICMPPingAndJitter(1, srcIp, network)
if err != nil {
Expand Down

0 comments on commit c9903d3

Please sign in to comment.