forked from goadapp/goad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goad.go
212 lines (183 loc) · 4.97 KB
/
goad.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package goad
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"math"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/goadapp/goad/infrastructure"
"github.com/goadapp/goad/queue"
"github.com/goadapp/goad/version"
)
// TestConfig type
type TestConfig struct {
URL string
Concurrency uint
TotalRequests uint
RequestTimeout time.Duration
Regions []string
Method string
Body string
Headers []string
AwsProfile string
}
type invokeArgs struct {
File string `json:"file"`
Args []string `json:"args"`
}
const nano = 1000000000
var supportedRegions = []string{
"us-east-1",
"us-west-2",
"eu-west-1",
"ap-northeast-1",
"eu-central-1",
}
// Test type
type Test struct {
config *TestConfig
}
// NewTest returns a configured Test
func NewTest(config *TestConfig) (*Test, error) {
err := config.check()
if err != nil {
return nil, err
}
return &Test{config}, nil
}
// Start a test
func (t *Test) Start() <-chan queue.RegionsAggData {
awsConfig := aws.NewConfig().WithRegion(t.config.Regions[0])
if t.config.AwsProfile != "" {
creds := credentials.NewSharedCredentials("", t.config.AwsProfile)
if _, err := creds.Get(); err != nil {
log.Fatal(err)
}
awsConfig.WithCredentials(creds)
}
infra, err := infrastructure.New(t.config.Regions, awsConfig)
if err != nil {
log.Fatal(err)
}
t.invokeLambdas(awsConfig, infra.QueueURL())
results := make(chan queue.RegionsAggData)
go func() {
for result := range queue.Aggregate(awsConfig, infra.QueueURL(), t.config.TotalRequests) {
results <- result
}
infra.Clean()
close(results)
}()
return results
}
func (t *Test) invokeLambdas(awsConfig *aws.Config, sqsURL string) {
lambdas := numberOfLambdas(t.config.Concurrency, len(t.config.Regions))
for i := 0; i < lambdas; i++ {
region := t.config.Regions[i%len(t.config.Regions)]
requests, requestsRemainder := divide(t.config.TotalRequests, lambdas)
concurrency, _ := divide(t.config.Concurrency, lambdas)
if requestsRemainder > 0 && i == lambdas-1 {
requests += requestsRemainder
}
c := t.config
args := []string{
"-u",
fmt.Sprintf("%s", c.URL),
"-c",
fmt.Sprintf("%s", strconv.Itoa(int(concurrency))),
"-n",
fmt.Sprintf("%s", strconv.Itoa(int(requests))),
"-s",
fmt.Sprintf("%s", sqsURL),
"-q",
fmt.Sprintf("%s", c.Regions[0]),
"-t",
fmt.Sprintf("%s", c.RequestTimeout.String()),
"-f",
fmt.Sprintf("%s", reportingFrequency(lambdas).String()),
"-r",
fmt.Sprintf("%s", region),
"-m",
fmt.Sprintf("%s", c.Method),
"-b",
fmt.Sprintf("%s", c.Body),
}
for _, v := range t.config.Headers {
args = append(args, "-H", fmt.Sprintf("%s", v))
}
invokeargs := invokeArgs{
File: "./goad-lambda",
Args: args,
}
config := aws.NewConfig().WithRegion(region)
go t.invokeLambda(config, invokeargs)
}
}
func (t *Test) invokeLambda(awsConfig *aws.Config, args invokeArgs) {
svc := lambda.New(session.New(), awsConfig)
j, _ := json.Marshal(args)
svc.InvokeAsync(&lambda.InvokeAsyncInput{
FunctionName: aws.String("goad:" + version.LambdaVersion()),
InvokeArgs: bytes.NewReader(j),
})
}
func numberOfLambdas(concurrency uint, numRegions int) int {
if numRegions > int(concurrency) {
return int(concurrency)
}
if concurrency/200 > 350 { // > 70.000
return 500
} else if concurrency/100 > 100 { // 10.000 <> 70.000
return 300
} else if concurrency/10 > 100 { // 1.000 <> 10.000
return 100
}
if int(concurrency) < 10*numRegions {
return numRegions
}
return int(concurrency-1)/10 + 1
}
func divide(dividend uint, divisor int) (quotient, remainder uint) {
return dividend / uint(divisor), dividend % uint(divisor)
}
func reportingFrequency(numberOfLambdas int) time.Duration {
return time.Duration((math.Log2(float64(numberOfLambdas)) + 1)) * time.Second
}
func (c TestConfig) check() error {
concurrencyLimit := 25000 * uint(len(c.Regions))
if c.Concurrency < 1 || c.Concurrency > concurrencyLimit {
return fmt.Errorf("Invalid concurrency (use 1 - %d)", concurrencyLimit)
}
if c.TotalRequests < 1 || c.TotalRequests > 2000000 {
return errors.New("Invalid total requests (use 1 - 2000000)")
}
if c.RequestTimeout.Nanoseconds() < nano || c.RequestTimeout.Nanoseconds() > nano*100 {
return errors.New("Invalid timeout (1s - 100s)")
}
for _, region := range c.Regions {
supportedRegionFound := false
for _, supported := range supportedRegions {
if region == supported {
supportedRegionFound = true
}
}
if !supportedRegionFound {
return fmt.Errorf("Unsupported region: %s. Supported regions are: %s.", region, strings.Join(supportedRegions, ", "))
}
}
for _, v := range c.Headers {
header := strings.Split(v, ":")
if len(header) < 2 {
return fmt.Errorf("Header %s not valid. Make sure your header is of the form \"Header: value\"", v)
}
}
return nil
}