2017-07-26 09:29:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
2019-01-27 14:48:43 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 09:29:10 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
protocolVersion uint16 = 0x00 // Protocol Version
|
|
|
|
blockSize int64 = 200 // size (KB) of each block of data copied to/from remote
|
|
|
|
throughputTestLength uint = 10 // length of time to conduct each throughput test
|
|
|
|
numPings int = 30 // number of pings to attempt
|
|
|
|
|
2017-07-28 05:29:16 +00:00
|
|
|
kbps = 1024.0
|
|
|
|
mbps = 1024.0 * kbps
|
2017-07-26 09:29:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type testResult struct {
|
|
|
|
Ping struct {
|
|
|
|
Min, Max, Avg, Dev float64
|
|
|
|
}
|
|
|
|
Send struct {
|
|
|
|
Min, Max, Avg float64
|
|
|
|
}
|
|
|
|
Receive struct {
|
|
|
|
Min, Max, Avg float64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestResult() *testResult {
|
|
|
|
r := &testResult{}
|
|
|
|
r.Ping.Min = math.Inf(1)
|
|
|
|
r.Ping.Max = math.Inf(-1)
|
|
|
|
r.Send.Min = math.Inf(1)
|
|
|
|
r.Send.Max = math.Inf(-1)
|
|
|
|
r.Receive.Min = math.Inf(1)
|
|
|
|
r.Receive.Max = math.Inf(-1)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t testResult) String() string {
|
|
|
|
return fmt.Sprintf("Ping(ms): min=%.2f max=%.2f avg=%.2f stddev=%.2f | Download(Mbps): min=%.2f max=%.2f avg=%.2f | Upload(Mbps): min=%.2f max=%.2f avg=%.2f",
|
|
|
|
t.Ping.Min,
|
|
|
|
t.Ping.Max,
|
|
|
|
t.Ping.Avg,
|
|
|
|
t.Ping.Dev,
|
2017-07-28 05:29:16 +00:00
|
|
|
t.Receive.Min/mbps,
|
|
|
|
t.Receive.Max/mbps,
|
|
|
|
t.Receive.Avg/mbps,
|
|
|
|
t.Send.Min/mbps,
|
|
|
|
t.Send.Max/mbps,
|
|
|
|
t.Send.Avg/mbps,
|
2017-07-26 09:29:10 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type sparkClient struct {
|
|
|
|
remote string
|
|
|
|
conn net.Conn
|
|
|
|
reader *bufio.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSparkClient(hostname string, port int) *sparkClient {
|
|
|
|
return &sparkClient{
|
|
|
|
remote: fmt.Sprintf("%s:%d", hostname, port),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sparkClient) dial() error {
|
|
|
|
c, err := net.Dial("tcp", s.remote)
|
|
|
|
if err != nil {
|
2019-01-27 14:48:43 +00:00
|
|
|
return errors.Wrap(err, "Unable to dial")
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
s.conn = c
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sparkClient) connect() error {
|
|
|
|
if err := s.dial(); err != nil {
|
2019-01-27 14:48:43 +00:00
|
|
|
return errors.Wrapf(err, "Unable to connect to sparkyfish-server %q", s.remote)
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.reader = bufio.NewReader(s.conn)
|
|
|
|
|
|
|
|
if err := s.writeCommand(fmt.Sprintf("HELO%d", protocolVersion)); err != nil {
|
2019-01-27 14:48:43 +00:00
|
|
|
return errors.Wrap(err, "Unable to send HELO command")
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.readGreeting()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sparkClient) writeCommand(command string) error {
|
|
|
|
if _, err := fmt.Fprintf(s.conn, "%s\r\n", command); err != nil {
|
2019-01-27 14:48:43 +00:00
|
|
|
return errors.Wrapf(err, "Unable to send command %q", command)
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sparkClient) readGreeting() error {
|
|
|
|
if helo, err := s.reader.ReadString('\n'); err != nil || strings.TrimSpace(helo) != "HELO" {
|
2019-01-27 14:48:43 +00:00
|
|
|
return errors.New("Unexpected response to greeting")
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cn, err := s.reader.ReadString('\n')
|
|
|
|
if err != nil {
|
2019-01-27 14:48:43 +00:00
|
|
|
return errors.Wrap(err, "Unable to read string")
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cn = strings.TrimSpace(cn)
|
|
|
|
if cn == "none" {
|
|
|
|
cn = s.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
loc, err := s.reader.ReadString('\n')
|
|
|
|
if err != nil {
|
2019-01-27 14:48:43 +00:00
|
|
|
return errors.Wrap(err, "Unable to read string")
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loc = strings.TrimSpace(loc)
|
|
|
|
|
2019-01-27 14:48:43 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"cn": cn,
|
|
|
|
"location": loc,
|
|
|
|
}).Debug("Connected to server")
|
2017-07-26 09:29:10 +00:00
|
|
|
return nil
|
|
|
|
}
|