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
|
//go:build go1.13 && integration && perftest
// +build go1.13,integration,perftest
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"runtime/trace"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/awstesting"
"github.com/aws/aws-sdk-go/awstesting/integration"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
var config Config
func main() {
parseCommandLine()
log.SetOutput(os.Stderr)
config.Profiler.Start()
defer config.Profiler.Stop()
var err error
key := config.Key
size := config.Size
if len(key) == 0 {
uploadPartSize := getUploadPartSize(size, config.UploadPartSize, config.SDK.PartSize)
log.Printf("uploading %s file to s3://%s\n", integration.SizeToName(int(config.Size)), config.Bucket)
key, err = setupDownloadTest(config.Bucket, config.Size, uploadPartSize)
if err != nil {
log.Fatalf("failed to setup download testing: %v", err)
}
defer func() {
log.Printf("cleaning up s3://%s/%s\n", config.Bucket, key)
if err = teardownDownloadTest(config.Bucket, key); err != nil {
log.Fatalf("failed to teardwn test artifacts: %v", err)
}
}()
} else {
size, err = getObjectSize(config.Bucket, key)
if err != nil {
log.Fatalf("failed to get object size: %v", err)
}
}
traces := make(chan *RequestTrace, config.SDK.Concurrency)
requestTracer := downloadRequestTracer(traces)
downloader := newDownloader(config.Client, config.SDK, requestTracer)
metricReportDone := startTraceReceiver(traces)
log.Println("starting download...")
start := time.Now()
_, err = downloader.Download(&awstesting.DiscardAt{}, &s3.GetObjectInput{
Bucket: &config.Bucket,
Key: &key,
})
if err != nil {
log.Fatalf("failed to download object, %v", err)
}
close(traces)
dur := time.Since(start)
log.Printf("Download finished, Size: %d, Dur: %s, Throughput: %.5f GB/s",
size, dur, (float64(size)/(float64(dur)/float64(time.Second)))/float64(1e9),
)
<-metricReportDone
}
func parseCommandLine() {
config.SetupFlags("", flag.CommandLine)
if err := flag.CommandLine.Parse(os.Args[1:]); err != nil {
flag.CommandLine.PrintDefaults()
log.Fatalf("failed to parse CLI commands")
}
if err := config.Validate(); err != nil {
flag.CommandLine.PrintDefaults()
log.Fatalf("invalid arguments: %v", err)
}
}
func setupDownloadTest(bucket string, fileSize, partSize int64) (key string, err error) {
er := &awstesting.EndlessReader{}
lr := io.LimitReader(er, fileSize)
key = integration.UniqueID()
sess := session.Must(session.NewSession(&aws.Config{
S3DisableContentMD5Validation: aws.Bool(true),
S3Disable100Continue: aws.Bool(true),
}))
uploader := s3manager.NewUploader(sess, func(u *s3manager.Uploader) {
u.PartSize = partSize
u.Concurrency = runtime.NumCPU() * 2
u.RequestOptions = append(u.RequestOptions, func(r *request.Request) {
if r.Operation.Name != "UploadPart" && r.Operation.Name != "PutObject" {
return
}
r.HTTPRequest.Header.Set("X-Amz-Content-Sha256", "UNSIGNED-PAYLOAD")
})
})
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: &bucket,
Body: lr,
Key: &key,
})
if err != nil {
err = fmt.Errorf("failed to upload test object to s3: %v", err)
}
return
}
func teardownDownloadTest(bucket, key string) error {
sess := session.Must(session.NewSession())
svc := s3.New(sess)
_, err := svc.DeleteObject(&s3.DeleteObjectInput{Bucket: &bucket, Key: &key})
return err
}
func startTraceReceiver(traces <-chan *RequestTrace) <-chan struct{} {
metricReportDone := make(chan struct{})
go func() {
defer close(metricReportDone)
metrics := map[string]*RequestTrace{}
for trace := range traces {
curTrace, ok := metrics[trace.Operation]
if !ok {
curTrace = trace
} else {
curTrace.attempts = append(curTrace.attempts, trace.attempts...)
if len(trace.errs) != 0 {
curTrace.errs = append(curTrace.errs, trace.errs...)
}
curTrace.finish = trace.finish
}
metrics[trace.Operation] = curTrace
}
for _, name := range []string{
"GetObject",
} {
if trace, ok := metrics[name]; ok {
printAttempts(name, trace, config.LogVerbose)
}
}
}()
return metricReportDone
}
func printAttempts(op string, trace *RequestTrace, verbose bool) {
if !verbose {
return
}
log.Printf("%s: latency:%s requests:%d errors:%d",
op,
trace.finish.Sub(trace.start),
len(trace.attempts),
len(trace.errs),
)
for _, a := range trace.attempts {
log.Printf(" * %s", a)
}
if err := trace.Err(); err != nil {
log.Printf("Operation Errors: %v", err)
}
log.Println()
}
func downloadRequestTracer(traces chan<- *RequestTrace) request.Option {
tracerOption := func(r *request.Request) {
id := "op"
if v, ok := r.Params.(*s3.GetObjectInput); ok {
if v.Range != nil {
id = *v.Range
}
}
tracer := NewRequestTrace(r.Context(), r.Operation.Name, id)
r.SetContext(tracer)
r.Handlers.Send.PushFront(tracer.OnSendAttempt)
r.Handlers.CompleteAttempt.PushBack(tracer.OnCompleteAttempt)
r.Handlers.Complete.PushBack(tracer.OnComplete)
r.Handlers.Complete.PushBack(func(rr *request.Request) {
traces <- tracer
})
}
return tracerOption
}
func newDownloader(clientConfig ClientConfig, sdkConfig SDKConfig, options ...request.Option) *s3manager.Downloader {
client := NewClient(clientConfig)
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{HTTPClient: client},
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
log.Fatalf("failed to load session, %v", err)
}
downloader := s3manager.NewDownloader(sess, func(d *s3manager.Downloader) {
d.PartSize = sdkConfig.PartSize
d.Concurrency = sdkConfig.Concurrency
d.BufferProvider = sdkConfig.BufferProvider
d.RequestOptions = append(d.RequestOptions, options...)
})
return downloader
}
func getObjectSize(bucket, key string) (int64, error) {
sess := session.Must(session.NewSession())
svc := s3.New(sess)
resp, err := svc.HeadObject(&s3.HeadObjectInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
return 0, err
}
return *resp.ContentLength, nil
}
type Profiler struct {
outputDir string
enableCPU bool
enableTrace bool
cpuFile *os.File
traceFile *os.File
}
func (p *Profiler) SetupFlags(prefix string, flagSet *flag.FlagSet) {
prefix += "profiler."
flagSet.StringVar(&p.outputDir, prefix+"output-dir", os.TempDir(), "output directory to write profiling data")
flagSet.BoolVar(&p.enableCPU, prefix+"cpu", false, "enable CPU profiling")
flagSet.BoolVar(&p.enableTrace, prefix+"trace", false, "enable tracing")
}
func (p *Profiler) Start() {
var err error
uuid := integration.UniqueID()
if p.enableCPU {
p.cpuFile, err = p.createFile(uuid, "cpu")
if err != nil {
panic(fmt.Sprintf("failed to create cpu profile file: %v", err))
}
err = pprof.StartCPUProfile(p.cpuFile)
if err != nil {
panic(fmt.Sprintf("failed to start cpu profile: %v", err))
}
}
if p.enableTrace {
p.traceFile, err = p.createFile(uuid, "trace")
if err != nil {
panic(fmt.Sprintf("failed to create trace file: %v", err))
}
err = trace.Start(p.traceFile)
if err != nil {
panic(fmt.Sprintf("failed to tracing: %v", err))
}
}
}
func (p *Profiler) logAndCloseFile(profile string, file *os.File) {
info, err := file.Stat()
if err != nil {
log.Printf("failed to stat %s profile: %v", profile, err)
} else {
log.Printf("writing %s profile to: %v", profile, filepath.Join(p.outputDir, info.Name()))
}
file.Close()
}
func (p *Profiler) Stop() {
if p.enableCPU {
pprof.StopCPUProfile()
p.logAndCloseFile("cpu", p.cpuFile)
}
if p.enableTrace {
trace.Stop()
p.logAndCloseFile("trace", p.traceFile)
}
}
func (p *Profiler) createFile(prefix, name string) (*os.File, error) {
return os.OpenFile(filepath.Join(p.outputDir, prefix+"."+name+".profile"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
}
func getUploadPartSize(fileSize, uploadPartSize, downloadPartSize int64) int64 {
partSize := uploadPartSize
if partSize == 0 {
partSize = downloadPartSize
}
if fileSize/partSize > s3manager.MaxUploadParts {
partSize = (fileSize / s3manager.MaxUploadParts) + 1
}
return partSize
}
|