-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathutils.go
176 lines (157 loc) · 4.65 KB
/
utils.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package awsping
import (
"fmt"
"io"
"math/rand"
"os"
"sort"
"strconv"
"strings"
"sync"
"time"
)
var (
// Version describes application version
Version = "2.0.0"
github = "https://github.com/ekalinin/awsping"
useragent = fmt.Sprintf("AwsPing/%s (+%s)", Version, github)
)
const (
// ShowOnlyRegions describes a type of output when only region's name and code printed out
ShowOnlyRegions = -1
)
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// Duration2ms converts time.Duration to ms (float64)
func Duration2ms(d time.Duration) float64 {
return float64(d.Nanoseconds()) / 1000 / 1000
}
// mkRandomString returns random string
func mkRandomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
// LatencyOutput prints data into console
type LatencyOutput struct {
Level int
Repeats int
w io.Writer
}
// NewOutput creates a new LatencyOutput instance
func NewOutput(level, repeats int) *LatencyOutput {
return &LatencyOutput{
Level: level,
Repeats: repeats,
w: os.Stdout,
}
}
func (lo *LatencyOutput) show(regions *AWSRegions) {
for _, r := range *regions {
fmt.Fprintf(lo.w, "%-15s %-s\n", r.Code, r.Name)
}
}
func (lo *LatencyOutput) show0(regions *AWSRegions) {
for _, r := range *regions {
fmt.Fprintf(lo.w, "%-25s %20s\n", r.Name, r.GetLatencyStr())
}
}
func (lo *LatencyOutput) show1(regions *AWSRegions) {
outFmt := "%5v %-15s %-30s %20s\n"
fmt.Fprintf(lo.w, outFmt, "", "Code", "Region", "Latency")
for i, r := range *regions {
fmt.Fprintf(lo.w, outFmt, i, r.Code, r.Name, r.GetLatencyStr())
}
}
func (lo *LatencyOutput) show2(regions *AWSRegions) {
// format
outFmt := "%5v %-15s %-25s"
outFmt += strings.Repeat(" %15s", lo.Repeats) + " %15s\n"
// header
outStr := []interface{}{"", "Code", "Region"}
for i := 0; i < lo.Repeats; i++ {
outStr = append(outStr, "Try #"+strconv.Itoa(i+1))
}
outStr = append(outStr, "Avg Latency")
// show header
fmt.Fprintf(lo.w, outFmt, outStr...)
// each region stats
for i, r := range *regions {
outData := []interface{}{strconv.Itoa(i), r.Code, r.Name}
for n := 0; n < lo.Repeats; n++ {
outData = append(outData, fmt.Sprintf("%.2f ms",
Duration2ms(r.Latencies[n])))
}
outData = append(outData, fmt.Sprintf("%.2f ms", r.GetLatency()))
fmt.Fprintf(lo.w, outFmt, outData...)
}
}
// Show print data
func (lo *LatencyOutput) Show(regions *AWSRegions) {
switch lo.Level {
case ShowOnlyRegions:
lo.show(regions)
case 0:
lo.show0(regions)
case 1:
lo.show1(regions)
case 2:
lo.show2(regions)
}
}
// GetRegions returns a list of regions
func GetRegions() AWSRegions {
return AWSRegions{
NewRegion("Africa (Cape Town)", "af-south-1"),
NewRegion("Asia Pacific (Hong Kong)", "ap-east-1"),
NewRegion("Asia Pacific (Tokyo)", "ap-northeast-1"),
NewRegion("Asia Pacific (Seoul)", "ap-northeast-2"),
NewRegion("Asia Pacific (Osaka)", "ap-northeast-3"),
NewRegion("Asia Pacific (Mumbai)", "ap-south-1"),
NewRegion("Asia Pacific (Hyderabad)", "ap-south-2"),
NewRegion("Asia Pacific (Singapore)", "ap-southeast-1"),
NewRegion("Asia Pacific (Sydney)", "ap-southeast-2"),
NewRegion("Asia Pacific (Jakarta)", "ap-southeast-3"),
NewRegion("Asia Pacific (Melbourne)", "ap-southeast-4"),
NewRegion("Canada (Central)", "ca-central-1"),
NewRegion("Europe (Frankfurt)", "eu-central-1"),
NewRegion("Europe (Zurich)", "eu-central-2"),
NewRegion("Europe (Stockholm)", "eu-north-1"),
NewRegion("Europe (Milan)", "eu-south-1"),
NewRegion("Europe (Spain)", "eu-south-2"),
NewRegion("Europe (Ireland)", "eu-west-1"),
NewRegion("Europe (London)", "eu-west-2"),
NewRegion("Europe (Paris)", "eu-west-3"),
NewRegion("Middle East (UAE)", "me-central-1"),
NewRegion("Middle East (Bahrain)", "me-south-1"),
NewRegion("South America (Sao Paulo)", "sa-east-1"),
NewRegion("US East (N. Virginia)", "us-east-1"),
NewRegion("US East (Ohio)", "us-east-2"),
NewRegion("US West (N. California)", "us-west-1"),
NewRegion("US West (Oregon)", "us-west-2"),
NewRegion("Israel (Tel Aviv)", "il-central-1"),
}
}
// CalcLatency returns list of aws regions sorted by Latency
func CalcLatency(regions AWSRegions, repeats int, useHTTP bool, useHTTPS bool, service string) {
regions.SetService(service)
switch {
case useHTTP:
regions.SetCheckType(CheckTypeHTTP)
case useHTTPS:
regions.SetCheckType(CheckTypeHTTPS)
default:
regions.SetCheckType(CheckTypeTCP)
}
regions.SetDefaultTarget()
var wg sync.WaitGroup
for n := 1; n <= repeats; n++ {
wg.Add(len(regions))
for i := range regions {
go regions[i].CheckLatency(&wg)
}
wg.Wait()
}
sort.Sort(regions)
}