forked from igneous-systems/s3bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3bench.go
257 lines (228 loc) · 7.84 KB
/
s3bench.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
package main
import (
"bytes"
"crypto/rand"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"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/s3"
)
const (
opRead = "Read"
opWrite = "Write"
)
var bufferBytes []byte
func main() {
endpoint := flag.String("endpoint", "", "S3 endpoint(s) comma separated - http://IP:PORT,http://IP:PORT")
accessKey := flag.String("accessKey", "", "the S3 access key")
accessSecret := flag.String("accessSecret", "", "the S3 access secret")
bucketName := flag.String("bucket", "bucketname", "the bucket for which to run the test")
objectNamePrefix := flag.String("objectNamePrefix", "loadgen_test_", "prefix of the object name that will be used")
objectSize := flag.Int64("objectSize", 80*1024*1024, "size of individual requests in bytes (must be smaller than main memory)")
numClients := flag.Int("numClients", 40, "number of concurrent clients")
numSamples := flag.Int("numSamples", 200, "total number of requests to send")
flag.Parse()
if *numClients > *numSamples || *numSamples < 1 {
fmt.Printf("numClients(%d) needs to be less than numSamples(%d) and greater than 0\n", *numClients, *numSamples)
os.Exit(1)
}
if *endpoint == "" {
fmt.Println("You need to specify endpoint(s)")
flag.PrintDefaults()
os.Exit(1)
}
// Setup and print summary of the accepted parameters
params := Params{
requests: make(chan Req),
responses: make(chan Resp),
numSamples: *numSamples,
numClients: uint(*numClients),
objectSize: *objectSize,
objectNamePrefix: *objectNamePrefix,
bucketName: *bucketName,
endpoints: strings.Split(*endpoint, ","),
}
fmt.Println(params)
fmt.Println()
// Generate the data from which we will do the writting
fmt.Printf("Generating in-memory sample data... ")
timeGenData := time.Now()
bufferBytes = make([]byte, *objectSize, *objectSize)
buffer := bytes.NewBuffer(bufferBytes)
_, err := io.CopyN(buffer, rand.Reader, *objectSize)
if err != nil {
fmt.Printf("Could not allocate a buffer")
os.Exit(1)
}
fmt.Printf("Done (%s)\n", time.Since(timeGenData))
fmt.Println()
// Start the load clients and run a write test followed by a read test
params.StartClients(&aws.Config{
Credentials: credentials.NewStaticCredentials(*accessKey, *accessSecret, ""),
Region: aws.String("igneous-test"),
S3ForcePathStyle: aws.Bool(true),
})
fmt.Printf("Running %s test...\n", opWrite)
writeResult := params.Run(opWrite)
fmt.Println()
fmt.Printf("Running %s test...\n", opRead)
readResult := params.Run(opRead)
fmt.Println()
// Repeating the parameters of the test followed by the results
fmt.Println(params)
fmt.Println()
fmt.Println(writeResult)
fmt.Println()
fmt.Println(readResult)
}
func (params *Params) Run(op string) Result {
startTime := time.Now()
// Start submitting load requests
go params.submitLoad(op)
// Collect and aggregate stats for completed requests
result := Result{opDurations: make([]float64, 0, params.numSamples), operation: op}
for i := 0; i < params.numSamples; i++ {
resp := <-params.responses
errorString := ""
if resp.err != nil {
result.numErrors++
errorString = fmt.Sprintf(", error: %s", resp.err)
} else {
result.bytesTransmitted = result.bytesTransmitted + params.objectSize
result.opDurations = append(result.opDurations, resp.duration.Seconds())
}
fmt.Printf("%v operation completed in %0.2fs (%d/%d) - %0.2fMB/s%s\n",
op, resp.duration.Seconds(), i+1, params.numSamples,
(float64(result.bytesTransmitted)/(1024*1024))/time.Since(startTime).Seconds(),
errorString)
}
result.totalDuration = time.Since(startTime)
sort.Float64s(result.opDurations)
return result
}
// Create an individual load request and submit it to the client queue
func (params *Params) submitLoad(op string) {
bucket := aws.String(params.bucketName)
for i := 0; i < params.numSamples; i++ {
key := aws.String(fmt.Sprintf("%s%d", params.objectNamePrefix, i))
if op == opWrite {
params.requests <- &s3.PutObjectInput{
Bucket: bucket,
Key: key,
Body: bytes.NewReader(bufferBytes),
}
} else if op == opRead {
params.requests <- &s3.GetObjectInput{
Bucket: bucket,
Key: key,
}
} else {
panic("Developer error")
}
}
}
func (params *Params) StartClients(cfg *aws.Config) {
for i := 0; i < int(params.numClients); i++ {
cfg.Endpoint = aws.String(params.endpoints[i%len(params.endpoints)])
go params.startClient(cfg)
}
}
// Run an individual load request
func (params *Params) startClient(cfg *aws.Config) {
svc := s3.New(session.New(), cfg)
for request := range params.requests {
putStartTime := time.Now()
var err error
numBytes := params.objectSize
switch r := request.(type) {
case *s3.PutObjectInput:
req, _ := svc.PutObjectRequest(r)
// Disable payload checksum calculation (very expensive)
req.HTTPRequest.Header.Add("X-Amz-Content-Sha256", "UNSIGNED-PAYLOAD")
err = req.Send()
case *s3.GetObjectInput:
req, resp := svc.GetObjectRequest(r)
err = req.Send()
numBytes = 0
if err == nil {
numBytes, err = io.Copy(ioutil.Discard, resp.Body)
}
if numBytes != params.objectSize {
err = fmt.Errorf("expected object length %d, actual %d", params.objectSize, numBytes)
}
default:
panic("Developer error")
}
params.responses <- Resp{err, time.Since(putStartTime), numBytes}
}
}
// Specifies the parameters for a given test
type Params struct {
operation string
requests chan Req
responses chan Resp
numSamples int
numClients uint
objectSize int64
objectNamePrefix string
bucketName string
endpoints []string
}
func (params Params) String() string {
output := fmt.Sprintln("Test parameters")
output += fmt.Sprintf("endpoint(s): %s\n", params.endpoints)
output += fmt.Sprintf("bucket: %s\n", params.bucketName)
output += fmt.Sprintf("objectNamePrefix: %s\n", params.objectNamePrefix)
output += fmt.Sprintf("objectSize: %0.4f MB\n", float64(params.objectSize)/(1024*1024))
output += fmt.Sprintf("numClients: %d\n", params.numClients)
output += fmt.Sprintf("numSamples: %d\n", params.numSamples)
return output
}
// Contains the summary for a given test result
type Result struct {
operation string
bytesTransmitted int64
numErrors int
opDurations []float64
totalDuration time.Duration
}
func (r Result) String() string {
report := fmt.Sprintf("Results Summary for %s Operation(s)\n", r.operation)
report += fmt.Sprintf("Total Transferred: %0.3f MB\n", float64(r.bytesTransmitted)/(1024*1024))
report += fmt.Sprintf("Total Throughput: %0.2f MB/s\n", (float64(r.bytesTransmitted)/(1024*1024))/r.totalDuration.Seconds())
report += fmt.Sprintf("Total Duration: %0.3f s\n", r.totalDuration.Seconds())
report += fmt.Sprintf("Number of Errors: %d\n", r.numErrors)
if len(r.opDurations) > 0 {
report += fmt.Sprintln("------------------------------------")
report += fmt.Sprintf("Put times Max: %0.3f s\n", r.percentile(100))
report += fmt.Sprintf("Put times 99th %%ile: %0.3f s\n", r.percentile(99))
report += fmt.Sprintf("Put times 90th %%ile: %0.3f s\n", r.percentile(90))
report += fmt.Sprintf("Put times 75th %%ile: %0.3f s\n", r.percentile(75))
report += fmt.Sprintf("Put times 50th %%ile: %0.3f s\n", r.percentile(50))
report += fmt.Sprintf("Put times 25th %%ile: %0.3f s\n", r.percentile(25))
report += fmt.Sprintf("Put times Min: %0.3f s\n", r.percentile(0))
}
return report
}
func (r Result) percentile(i int) float64 {
if i >= 100 {
i = len(r.opDurations) - 1
} else if i > 0 && i < 100 {
i = int(float64(i) / 100 * float64(len(r.opDurations)))
}
return r.opDurations[i]
}
type Req interface{}
type Resp struct {
err error
duration time.Duration
numBytes int64
}