-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathmain.go
125 lines (106 loc) · 3.16 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2019 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Program gcping pings GCP regions and reports about the latency.
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"time"
"github.com/GoogleCloudPlatform/gcping/internal/config"
)
var (
top bool
number int // number of requests for each region
concurrency int
timeout time.Duration
csv bool
csvCum bool
verbose bool
region string
endpointsURL string
// TODO(jbd): Add payload options such as body size.
client *http.Client // TODO(jbd): One client per worker?
)
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.BoolVar(&csvCum, "csv-cum", false, "")
flag.StringVar(®ion, "r", "", "")
flag.StringVar(&endpointsURL, "url", "https://global.gcping.com/api/endpoints", "")
flag.Usage = usage
flag.Parse()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Fetch and cache endpoint map in memory for the duration of the
// process.
endpoints, err := config.EndpointsFromServer(ctx, endpointsURL)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if number < 0 || concurrency <= 0 {
usage()
}
if csv {
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,
}
w := &worker{}
go w.start()
switch {
case region != "":
w.reportRegion(endpoints, region)
case top:
w.reportTop(endpoints)
case csvCum:
w.reportCSV(endpoints)
default:
w.reportAll(endpoints)
}
}
func usage() {
fmt.Println(usageText)
os.Exit(0)
}
var usageText = `gcping [options...]
Options:
-n Number of requests to be made to each region.
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.
-csv-cum If true, cumulative value is printed in CSV; disables default report.
-url URL of endpoint list. Default is https://global.gcping.com/api/endpoints
-csv CSV output; disables verbose output.
-v Verbose output.
Need a website version? See gcping.com
`