Skip to content

Commit

Permalink
Added support for pinging individual region (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
dastergon authored and rakyll committed Aug 13, 2019
1 parent 51e4464 commit 6d741c7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ifeq ($(origin version), undefined)
version := latest
endif
endif

release:
GOOS=windows GOARCH=amd64 go build -o ./bin/gcping_windows_amd64_$(version)
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Options:
By default 10; can't be negative.
-c Max number of requests to be made at any time.
By default 10; can't be negative or zero.
-r Report latency for an individual region.
-t Timeout. By default, no timeout.
Examples: "500ms", "1s", "1s500ms".
-top If true, only the top (non-global) region is printed.
Expand Down Expand Up @@ -50,6 +51,11 @@ $ gcping
21. [asia-south1] 573.022571ms
```

```
$ gcping -r us-east1
1. [us-east1] 502.068712ms
```

## Installation

* Linux 64-bit: https://storage.googleapis.com/gcping-release/gcping_linux_amd64_0.0.1
Expand Down
35 changes: 31 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,31 @@ var (
timeout time.Duration
csv bool
verbose bool
region string
// TODO(jbd): Add payload options such as body size.

client *http.Client // TODO(jbd): One client per worker?
inputs chan input
outputs chan output
)

// calcOutputSize calculates the actual output size.
func calcOutputSize() int {
outputSize := number * len(endpoints)
if region != "" {
outputSize = number
}
return outputSize
}

func main() {
flag.BoolVar(&top, "top", false, "")
flag.IntVar(&number, "n", 10, "")
flag.IntVar(&concurrency, "c", 10, "")
flag.DurationVar(&timeout, "t", time.Duration(0), "")
flag.BoolVar(&verbose, "v", false, "")
flag.BoolVar(&csv, "csv", false, "")
flag.StringVar(&region, "r", "", "")

flag.Usage = usage
flag.Parse()
Expand All @@ -82,16 +93,30 @@ func main() {
verbose = false // if output is CSV, no need for verbose output
}

if region != "" {
if _, found := endpoints[region]; !found {
fmt.Printf("region %q is not supported or does not exist\n", region)
os.Exit(1)
}
}

client = &http.Client{
Timeout: timeout,
}

go start()

inputs = make(chan input, concurrency)
outputs = make(chan output, number*len(endpoints))
outputSize := calcOutputSize()
outputs = make(chan output, outputSize)
for i := 0; i < number; i++ {
for r, e := range endpoints {
inputs <- input{region: r, endpoint: e}
if region != "" {
e, _ := endpoints[region]
inputs <- input{region: region, endpoint: e}
} else {
for r, e := range endpoints {
inputs <- input{region: r, endpoint: e}
}
}
}
close(inputs)
Expand All @@ -109,8 +134,9 @@ func start() {
}

func report() {
outputSize := calcOutputSize()
m := make(map[string]output)
for i := 0; i < number*len(endpoints); i++ {
for i := 0; i < outputSize; i++ {
o := <-outputs

a := m[o.region]
Expand Down Expand Up @@ -163,6 +189,7 @@ Options:
By default 10; can't be negative.
-c Max number of requests to be made at any time.
By default 10; can't be negative or zero.
-r Report latency for an individual region.
-t Timeout. By default, no timeout.
Examples: "500ms", "1s", "1s500ms".
-top If true, only the top (non-global) region is printed.
Expand Down

0 comments on commit 6d741c7

Please sign in to comment.