Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable 1s sleep #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Usage of ./stratum-ping:
stratum type: stratum1, stratum2 (default "stratum2")
-tls
use TLS
-dontsleep
don't wait 1s between pings
-u string
login (default "0x63a14c53f676f34847b5e6179c4f5f5a07f0b1ed")

Expand Down
37 changes: 21 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ package main
import (
"bufio"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"encoding/json"
"net"
"strconv"
"strings"
"time"
)

type StratumPinger struct {
login string
pass string
count int
ipv6 bool
host string
port string
addr *net.IPAddr
proto string
tls bool
login string
pass string
count int
ipv6 bool
host string
port string
addr *net.IPAddr
proto string
tls bool
dontsleep bool
}

func main() {
Expand All @@ -37,6 +38,7 @@ func main() {
argV6 := flag.Bool("6", false, "use ipv6")
argProto := flag.String("t", "stratum2", "stratum type: stratum1, stratum2")
argTLS := flag.Bool("tls", false, "use TLS")
argDontSleep := flag.Bool("dontsleep", false, "don't wait 1s between pings")

flag.Parse()

Expand Down Expand Up @@ -83,6 +85,7 @@ func main() {
ipv6: *argV6,
proto: *argProto,
tls: *argTLS,
dontsleep: *argDontSleep,
}

if err := pinger.Do(); err != nil {
Expand All @@ -107,8 +110,8 @@ func (p *StratumPinger) Do() error {

min := time.Duration(time.Hour)
max := time.Duration(0)
avg := time.Duration(0)
avgCount := 0
sum := time.Duration(0)
count := 0
success := 0
start := time.Now()

Expand All @@ -124,17 +127,19 @@ func (p *StratumPinger) Do() error {
if elapsed < min {
min = elapsed
}
avg += elapsed
avgCount++
sum += elapsed
count++
success++
}
time.Sleep(1 * time.Second)
if !p.dontsleep {
time.Sleep(1 * time.Second)
}
}
fmt.Printf("\n--- %s ping statistics ---\n", p.host)
loss := 100 - int64(float64(success) / float64(p.count) * 100.0)
fmt.Printf("%d packets transmitted, %d received, %d%% packet loss, time %s\n", p.count, success, loss, time.Since(start))
if success > 0 {
fmt.Printf("min/avg/max = %s, %s, %s\n", min.String(), (avg / time.Duration(avgCount)).String(), max.String())
fmt.Printf("min/avg/max/sum = %s, %s, %s, %s\n", min.String(), (sum / time.Duration(count)).String(), max.String(), sum.String())
}
return nil
}
Expand Down