-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
353 lines (329 loc) · 9.93 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"bytes"
"context"
"crypto/tls"
"flag"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
"reflect"
"runtime"
"sync"
)
func initializeS3Client(aEndpoint *string, aRegion *string) *s3.Client {
customResolver := aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
if aEndpoint != nil || aRegion != nil {
return aws.Endpoint{
PartitionID: "aws",
URL: *aEndpoint,
SigningRegion: *aRegion,
}, nil
}
// returning EndpointNotFoundError will allow the service to fallback to it's default resolution
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})
// Load the Shared AWS Configuration (~/.aws/config)
config, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolver(customResolver))
if err != nil {
log.Fatalf("\n%v", err)
}
// disable SSL cert checking. This is DANGEROUS, and only valid for custom endpoint benchmark. Never use in prod
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient := &http.Client{Transport: tr}
client := s3.NewFromConfig(config, func(o *s3.Options) {
o.UsePathStyle = true
o.UseAccelerate = false
o.HTTPClient = httpClient
})
return client
}
func createRandomFile(client *s3.Client, bucket string, chunkSize int, key *string) string {
if key == nil {
u, err := uuid.NewRandom()
if err != nil {
log.Fatal("\nCould not generate an UUID. Maybe lacking entropy?")
}
k := u.String()
key = &k
}
b := make([]byte, chunkSize)
r := bytes.NewReader(b)
log.Debugf("PUT object %s", *key)
_, err := client.PutObject(context.TODO(),
&s3.PutObjectInput{
Bucket: &bucket,
Key: key,
Body: r,
})
if err != nil {
log.Fatalf("\n%v", err)
}
return *key
}
func listAfterDelete(client *s3.Client, bucket string, iterations int, chunkSize int, errors chan int) {
count := 0
total := 0
for i := 0; i < iterations; i++ {
// create file
key := createRandomFile(client, bucket, chunkSize, nil)
// cleanup
log.Debugf("DELETE object %s", key)
_, err := client.DeleteObject(context.TODO(),
&s3.DeleteObjectInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
log.Fatalf("\nCould not DELETE object %s :: %v", key, err)
}
log.Debugf("LIST objects %s", key)
output, err := client.ListObjectsV2(context.TODO(),
&s3.ListObjectsV2Input{Bucket: &bucket})
if err != nil {
log.Fatalf("\nCould not list bucket %s :: %v", bucket, err)
}
found := false
for _, object := range output.Contents {
if aws.ToString(object.Key) == key {
found = true
break
}
}
if found {
count++
log.Debugf("Got a listAfterDelete error, expected %s file is still listed", key)
}
total++
}
log.Debugf("listAfterDelete %d/%d failed", count, total)
errors <- count
}
func listAfterCreate(client *s3.Client, bucket string, iterations int, chunkSize int, errors chan int) {
count := 0
total := 0
for i := 0; i < iterations; i++ {
// create file
key := createRandomFile(client, bucket, chunkSize, nil)
log.Debugf("LIST objects %s", key)
output, err := client.ListObjectsV2(context.TODO(),
&s3.ListObjectsV2Input{Bucket: &bucket})
if err != nil {
log.Fatalf("Could not list bucket %s :: %v", bucket, err)
}
found := false
for _, object := range output.Contents {
if aws.ToString(object.Key) == key {
found = true
break
}
}
if !found {
count++
log.Debugf("Got a listAfterCreate error, expected %s file not listed", key)
}
// cleanup
log.Debugf("DELETE object %s", key)
_, err = client.DeleteObject(context.TODO(),
&s3.DeleteObjectInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
log.Fatalf("Could not DELETE object %s :: %v", key, err)
}
total++
}
log.Debugf("listAfterCreate %d/%d failed", count, total)
errors <- count
}
func readAfterOverwrite(client *s3.Client, bucket string, iterations int, chunkSize int, errors chan int) {
count := 0
total := 0
for i := 0; i < iterations; i++ {
// create file
key := createRandomFile(client, bucket, chunkSize, nil)
// overwrite it
_ = createRandomFile(client, bucket, chunkSize+1, &key)
// read it
log.Debugf("GET object %s", key)
obj, err := client.GetObject(context.TODO(),
&s3.GetObjectInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
log.Fatalf("Could not GET object %s :: %v", key, err)
}
b, err := ioutil.ReadAll(obj.Body)
if len(b) != chunkSize+1 {
log.Debugf("Got a readAfterOverwrite error, expected %d bytes, got %d instead", chunkSize+1, len(b))
count += 1
}
// cleanup
log.Debugf("DELETE object %s", key)
_, err = client.DeleteObject(context.TODO(),
&s3.DeleteObjectInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
log.Fatalf("Could not DELETE object %s :: %v", key, err)
}
total++
}
errors <- count
}
func readAfterDelete(client *s3.Client, bucket string, iterations int, chunkSize int, errors chan int) {
count := 0
total := 0
for i := 0; i < iterations; i++ {
key := createRandomFile(client, bucket, chunkSize, nil)
log.Debugf("DELETE object %s", key)
_, err := client.DeleteObject(context.TODO(),
&s3.DeleteObjectInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
log.Fatalf("Could not DELETE object %s :: %v", key, err)
}
log.Debugf("GET object %s", key)
_, err = client.GetObject(context.TODO(),
&s3.GetObjectInput{
Bucket: &bucket,
Key: &key,
})
if err == nil {
count++
}
total++
}
log.Debugf("readAfterDelete %d/%d failed", count, total)
errors <- count
}
func readAfterCreate(client *s3.Client, bucket string, iterations int, chunkSize int, errors chan int) {
count := 0
total := 0
for i := 0; i < iterations; i++ {
key := createRandomFile(client, bucket, chunkSize, nil)
log.Debugf("GET object %s", key)
_, err := client.GetObject(context.TODO(),
&s3.GetObjectInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
count++
}
log.Debugf("DELETE object %s", key)
_, err = client.DeleteObject(context.TODO(),
&s3.DeleteObjectInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
log.Fatalf("Could not DELETE object %s :: %v", key, err)
}
total++
}
log.Debugf("readAfterCreate %d/%d failed", count, total)
errors <- count
}
func getFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
func runTest(client *s3.Client, bucket string, fn func(client *s3.Client, bucket string, iterations int, chunkSize int, errors chan int), iterations int, threads int, chunkSize int) int {
var wg sync.WaitGroup
wg.Add(threads)
c := make(chan int, threads)
errCount := 0
for i := 0; i < threads; i++ {
go func() {
defer wg.Done()
fn(client, bucket, iterations, chunkSize, c)
}()
}
wg.Wait()
for i := 0; i < threads; i++ {
errCount += <-c
}
errPct := float32(errCount) / (float32(iterations) * float32(threads)) * 100.0
if errCount > 0 {
fmt.Printf("%30s | %6d | %6d | \033[31m%.4f\033[0m\n", getFunctionName(fn), iterations*threads, errCount, errPct)
} else {
fmt.Printf("%30s | %6d | %6d | \033[32m%.4f\033[0m\n", getFunctionName(fn), iterations*threads, errCount, errPct)
}
return errCount
}
func cleanUp(client *s3.Client, bucket string) {
log.Debug("Cleaning repo")
output, err := client.ListObjectsV2(context.TODO(),
&s3.ListObjectsV2Input{Bucket: &bucket})
if err != nil {
log.Fatalf("Could not list bucket %s :: %v", bucket, err)
}
for _, object := range output.Contents {
key := aws.ToString(object.Key)
_, err = client.DeleteObject(context.TODO(),
&s3.DeleteObjectInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
log.Debug(err)
log.Fatal("Could not cleanup repository")
}
}
}
func main() {
log.SetLevel(log.InfoLevel)
iterationsFlag := flag.Int("iterations", 5, "Number of iteration per thread per test.")
threadsFlag := flag.Int("threads", 5, "Number threads per test.")
chunkSizeFlag := flag.Int("chunk-size", 1, "Size in bytes of created files")
endpointFlag := flag.String("endpoint", "https://s3.us-east-1.amazonaws.com", "S3 endpoint to use")
regionFlag := flag.String("region", "us-east-1", "S3 endpoint to use")
cleanFlag := flag.Bool("clean", false, "Clean bucket")
bucketFlag := flag.String("bucket", "s3-consistency", "Bucket to use for test")
flag.Parse()
client := initializeS3Client(endpointFlag, regionFlag)
bucketName := *bucketFlag
headOutput, err := client.HeadBucket(context.TODO(), &s3.HeadBucketInput{
Bucket: &bucketName,
})
if err != nil {
if headOutput == nil {
_, err := client.CreateBucket(context.TODO(), &s3.CreateBucketInput{Bucket: &bucketName})
if err != nil {
log.Fatalf("\nCould not create bucket :: %v", err)
}
}
}
iterations := *iterationsFlag
threads := *threadsFlag
chunkSize := *chunkSizeFlag
fmt.Printf("--------------------------------- \033[1;33mSETUP\033[0m ---------------------------------\n\n")
if *cleanFlag {
fmt.Printf("Cleaning up repo...\n")
cleanUp(client, bucketName)
os.Exit(0)
}
fmt.Printf("--------------------------------- \033[1;32mRESULTS\033[0m ---------------------------------\n\n")
fmt.Printf("\033[1m%d\033[0m iterations per thread with \033[1m%d\033[0m thread(s)\n", iterations, threads)
fmt.Printf("\033[1m%d bytes\033[0m chunk\n", chunkSize)
fmt.Printf("%30s | %10s | %6s | %8s\n", "Test", "Iterations", "Errors", "% Errors")
runTest(client, bucketName, readAfterDelete, iterations, threads, chunkSize)
runTest(client, bucketName, readAfterCreate, iterations, threads, chunkSize)
runTest(client, bucketName, readAfterOverwrite, iterations, threads, chunkSize)
runTest(client, bucketName, listAfterCreate, iterations, threads, chunkSize)
runTest(client, bucketName, listAfterDelete, iterations, threads, chunkSize)
fmt.Printf("\n------------------------------\n")
}