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
|
package manager
import (
"bytes"
"context"
"fmt"
"strconv"
"sync"
"sync/atomic"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
s3testing "github.com/aws/aws-sdk-go-v2/feature/s3/manager/internal/testing"
"github.com/aws/aws-sdk-go-v2/internal/sdkio"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type testReader struct {
br *bytes.Reader
m sync.Mutex
}
func (r *testReader) Read(p []byte) (n int, err error) {
r.m.Lock()
defer r.m.Unlock()
return r.br.Read(p)
}
func TestUploadByteSlicePool(t *testing.T) {
cases := map[string]struct {
PartSize int64
FileSize int64
Concurrency int
ExAllocations uint64
}{
"single part, single concurrency": {
PartSize: sdkio.MebiByte * 5,
FileSize: sdkio.MebiByte * 5,
ExAllocations: 2,
Concurrency: 1,
},
"multi-part, single concurrency": {
PartSize: sdkio.MebiByte * 5,
FileSize: sdkio.MebiByte * 10,
ExAllocations: 2,
Concurrency: 1,
},
"multi-part, multiple concurrency": {
PartSize: sdkio.MebiByte * 5,
FileSize: sdkio.MebiByte * 20,
ExAllocations: 3,
Concurrency: 2,
},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
var p *recordedPartPool
unswap := swapByteSlicePool(func(sliceSize int64) byteSlicePool {
p = newRecordedPartPool(sliceSize)
return p
})
defer unswap()
client, _, _ := s3testing.NewUploadLoggingClient(nil)
uploader := NewUploader(client, func(u *Uploader) {
u.PartSize = tt.PartSize
u.Concurrency = tt.Concurrency
})
expected := s3testing.GetTestBytes(int(tt.FileSize))
_, err := uploader.Upload(context.Background(), &s3.PutObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("key"),
Body: &testReader{br: bytes.NewReader(expected)},
})
if err != nil {
t.Errorf("expected no error, but got %v", err)
}
if v := atomic.LoadInt64(&p.recordedOutstanding); v != 0 {
t.Fatalf("expected zero outsnatding pool parts, got %d", v)
}
gets, allocs := atomic.LoadUint64(&p.recordedGets), atomic.LoadUint64(&p.recordedAllocs)
t.Logf("total gets %v, total allocations %v", gets, allocs)
if e, a := tt.ExAllocations, allocs; a > e {
t.Errorf("expected %v allocations, got %v", e, a)
}
})
}
}
func TestUploadByteSlicePool_Failures(t *testing.T) {
const (
putObject = "PutObject"
createMultipartUpload = "CreateMultipartUpload"
uploadPart = "UploadPart"
completeMultipartUpload = "CompleteMultipartUpload"
)
cases := map[string]struct {
PartSize int64
FileSize int64
Operations []string
}{
"single part": {
PartSize: sdkio.MebiByte * 5,
FileSize: sdkio.MebiByte * 4,
Operations: []string{
putObject,
},
},
"multi-part": {
PartSize: sdkio.MebiByte * 5,
FileSize: sdkio.MebiByte * 10,
Operations: []string{
createMultipartUpload,
uploadPart,
completeMultipartUpload,
},
},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
for _, operation := range tt.Operations {
t.Run(operation, func(t *testing.T) {
var p *recordedPartPool
unswap := swapByteSlicePool(func(sliceSize int64) byteSlicePool {
p = newRecordedPartPool(sliceSize)
return p
})
defer unswap()
client, _, _ := s3testing.NewUploadLoggingClient(nil)
switch operation {
case putObject:
client.PutObjectFn = func(*s3testing.UploadLoggingClient, *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
return nil, fmt.Errorf("put object failure")
}
case createMultipartUpload:
client.CreateMultipartUploadFn = func(*s3testing.UploadLoggingClient, *s3.CreateMultipartUploadInput) (*s3.CreateMultipartUploadOutput, error) {
return nil, fmt.Errorf("create multipart upload failure")
}
case uploadPart:
client.UploadPartFn = func(*s3testing.UploadLoggingClient, *s3.UploadPartInput) (*s3.UploadPartOutput, error) {
return nil, fmt.Errorf("upload part failure")
}
case completeMultipartUpload:
client.CompleteMultipartUploadFn = func(*s3testing.UploadLoggingClient, *s3.CompleteMultipartUploadInput) (*s3.CompleteMultipartUploadOutput, error) {
return nil, fmt.Errorf("complete multipart upload failure")
}
}
uploader := NewUploader(client, func(u *Uploader) {
u.Concurrency = 1
u.PartSize = tt.PartSize
})
expected := s3testing.GetTestBytes(int(tt.FileSize))
_, err := uploader.Upload(context.Background(), &s3.PutObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("key"),
Body: &testReader{br: bytes.NewReader(expected)},
})
if err == nil {
t.Fatalf("expected error but got none")
}
if v := atomic.LoadInt64(&p.recordedOutstanding); v != 0 {
t.Fatalf("expected zero outsnatding pool parts, got %d", v)
}
})
}
})
}
}
func TestUploadByteSlicePoolConcurrentMultiPartSize(t *testing.T) {
var (
pools []*recordedPartPool
mtx sync.Mutex
)
unswap := swapByteSlicePool(func(sliceSize int64) byteSlicePool {
mtx.Lock()
defer mtx.Unlock()
b := newRecordedPartPool(sliceSize)
pools = append(pools, b)
return b
})
defer unswap()
client, _, _ := s3testing.NewUploadLoggingClient(nil)
uploader := NewUploader(client, func(u *Uploader) {
u.PartSize = 5 * sdkio.MebiByte
u.Concurrency = 2
})
var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(2)
go func() {
defer wg.Done()
expected := s3testing.GetTestBytes(int(15 * sdkio.MebiByte))
_, err := uploader.Upload(context.Background(), &s3.PutObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("key"),
Body: &testReader{br: bytes.NewReader(expected)},
})
if err != nil {
t.Errorf("expected no error, but got %v", err)
}
}()
go func() {
defer wg.Done()
expected := s3testing.GetTestBytes(int(15 * sdkio.MebiByte))
_, err := uploader.Upload(context.Background(), &s3.PutObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("key"),
Body: &testReader{br: bytes.NewReader(expected)},
}, func(u *Uploader) {
u.PartSize = 6 * sdkio.MebiByte
})
if err != nil {
t.Errorf("expected no error, but got %v", err)
}
}()
}
wg.Wait()
if e, a := 3, len(pools); e != a {
t.Errorf("expected %v, got %v", e, a)
}
for _, p := range pools {
if v := atomic.LoadInt64(&p.recordedOutstanding); v != 0 {
t.Fatalf("expected zero outsnatding pool parts, got %d", v)
}
t.Logf("total gets %v, total allocations %v",
atomic.LoadUint64(&p.recordedGets),
atomic.LoadUint64(&p.recordedAllocs))
}
}
func BenchmarkPools(b *testing.B) {
cases := []struct {
PartSize int64
FileSize int64
Concurrency int
ExAllocations uint64
}{
0: {
PartSize: sdkio.MebiByte * 5,
FileSize: sdkio.MebiByte * 5,
Concurrency: 1,
},
1: {
PartSize: sdkio.MebiByte * 5,
FileSize: sdkio.MebiByte * 10,
Concurrency: 1,
},
2: {
PartSize: sdkio.MebiByte * 5,
FileSize: sdkio.MebiByte * 20,
Concurrency: 2,
},
3: {
PartSize: sdkio.MebiByte * 5,
FileSize: sdkio.MebiByte * 250,
Concurrency: 10,
},
}
client, _, _ := s3testing.NewUploadLoggingClient(nil)
pools := map[string]func(sliceSize int64) byteSlicePool{
"sync.Pool": func(sliceSize int64) byteSlicePool {
return newSyncSlicePool(sliceSize)
},
"custom": func(sliceSize int64) byteSlicePool {
return newMaxSlicePool(sliceSize)
},
}
for name, poolFunc := range pools {
b.Run(name, func(b *testing.B) {
unswap := swapByteSlicePool(poolFunc)
defer unswap()
for i, c := range cases {
b.Run(strconv.Itoa(i), func(b *testing.B) {
uploader := NewUploader(client, func(u *Uploader) {
u.PartSize = c.PartSize
u.Concurrency = c.Concurrency
})
expected := s3testing.GetTestBytes(int(c.FileSize))
b.ResetTimer()
_, err := uploader.Upload(context.Background(), &s3.PutObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("key"),
Body: &testReader{br: bytes.NewReader(expected)},
})
if err != nil {
b.Fatalf("expected no error, but got %v", err)
}
})
}
})
}
}
|