-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_samples.go
197 lines (167 loc) · 4.95 KB
/
get_samples.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"lib/policy_client"
"math/rand"
"net/http"
"os"
"policy-server/models"
"strconv"
"time"
"code.cloudfoundry.org/lager"
)
func main() {
if err := mainWithError(); err != nil {
os.Stderr.Write([]byte(err.Error() + "\n"))
os.Exit(1)
}
}
func mainWithError() error {
args := os.Args
if len(args) < 1 {
return fmt.Errorf("usage: lots of args")
}
oauthToken := args[1]
numExistingASGs, err := strconv.Atoi(args[2])
if err != nil {
return fmt.Errorf("parsing num existing asgs: %s", err)
}
baseURL := args[3]
proxyAppGUID := args[4]
proxyAppRoute := args[5]
statsFile := args[6]
nSamples, err := strconv.Atoi(args[7])
if err != nil {
return fmt.Errorf("parsing num samples: %s", err)
}
backendAppGUID := args[8]
backendAppRoute := args[9]
logger := lager.NewLogger("test")
logger.RegisterSink(lager.NewWriterSink(os.Stderr, lager.INFO))
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // insecure!!!
},
},
}
policyClient := policy_client.NewExternal(logger, httpClient, baseURL)
destIP, err := getIPAddr(backendAppRoute)
if err != nil {
return fmt.Errorf("get ip addr for app: %s", err)
}
t := &TestThingy{
logger: logger,
policyClient: policyClient,
token: oauthToken,
sourceAppGUID: proxyAppGUID,
destAppGUID: backendAppGUID,
proxyAppRoute: proxyAppRoute,
}
for i := 0; i < nSamples; i++ {
duration, err := t.measureLatencyFor1More(destIP)
if err != nil {
return fmt.Errorf("measuring latency: %s", err)
}
logger.Info("measured-latency", lager.Data{"duration in seconds": duration.Seconds()})
err = appendStat(statsFile, numExistingASGs, duration)
if err != nil {
return fmt.Errorf("writing stats: %s", err)
}
jitter := time.Duration(100+rand.Intn(3000)) * time.Millisecond
time.Sleep(jitter)
}
return nil
}
type TestThingy struct {
logger lager.Logger
policyClient policy_client.ExternalPolicyClient
token string
sourceAppGUID, destAppGUID string
proxyAppRoute string
}
func appendStat(statsFile string, numExisting int, duration time.Duration) error {
f, err := os.OpenFile(statsFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return fmt.Errorf("open file: %s", err)
}
defer f.Close()
newLine := fmt.Sprintf("%d\t%f\n", numExisting, duration.Seconds())
if _, err = f.WriteString(newLine); err != nil {
return fmt.Errorf("write string: %s", err)
}
return nil
}
func getIPAddr(appRoute string) (string, error) {
resp, err := http.Get(appRoute)
if err != nil {
return "", fmt.Errorf("get: %s", err)
}
defer resp.Body.Close()
var proxyInfo struct {
ListenAddresses []string `json:"ListenAddresses"`
}
err = json.NewDecoder(resp.Body).Decode(&proxyInfo)
if err != nil {
return "", fmt.Errorf("decode json: %s", err)
}
if len(proxyInfo.ListenAddresses) < 2 {
return "", fmt.Errorf("unexpectedly short list of addresses: %+v", proxyInfo.ListenAddresses)
}
return proxyInfo.ListenAddresses[1], nil
}
func (t *TestThingy) measureLatencyFor1More(destIP string) (time.Duration, error) {
_, err := t.tryCheckReachable(destIP, 100, false)
if err != nil {
return 0, fmt.Errorf("waiting for pre-state to settle: %s", err)
}
oneMorePolicy := []models.Policy{
models.Policy{
Source: models.Source{
ID: t.sourceAppGUID,
},
Destination: models.Destination{
ID: t.destAppGUID,
Protocol: "tcp",
Port: 8080,
},
},
}
err = t.policyClient.AddPolicies(t.token, oneMorePolicy)
if err != nil {
return 0, fmt.Errorf("add policies: %s", err)
}
defer t.policyClient.DeletePolicies(t.token, oneMorePolicy)
return t.tryCheckReachable(destIP, 100, true)
}
func (t *TestThingy) tryCheckReachable(destIP string, numAttempts int, desiredReachability bool) (time.Duration, error) {
t.logger.Info("waiting-for-reachability", lager.Data{"desired-state": desiredReachability})
startTime := time.Now()
for attempt := 0; attempt < numAttempts; attempt++ {
reachable := t.checkReachable(destIP, desiredReachability)
if reachable {
t.logger.Info("achieved-reachability", lager.Data{"desired-state": desiredReachability})
return time.Since(startTime), nil
}
time.Sleep(100 * time.Millisecond)
}
return 0, fmt.Errorf("app never acheived the desired reachability state of %v", desiredReachability)
}
func (t *TestThingy) checkReachable(destIP string, desiredReachable bool) bool {
resp, err := http.Get(fmt.Sprintf("%s/proxy/%s:8080", t.proxyAppRoute, destIP))
defer resp.Body.Close()
if err != nil {
return false
}
body, _ := ioutil.ReadAll(resp.Body)
if desiredReachable {
t.logger.Info("desired-reachable-and-got", lager.Data{"status-code": resp.StatusCode, "body": string(body)})
return (resp.StatusCode == 200)
} else {
t.logger.Info("desired-reachable-and-got", lager.Data{"status-code": resp.StatusCode, "body": string(body)})
return resp.StatusCode == 500
}
}