github.com/aavshr/aws-sdk-go@v1.41.3/aws/types_test.go (about) 1 package aws 2 3 import ( 4 "bytes" 5 "math/rand" 6 "testing" 7 ) 8 9 func TestWriteAtBuffer(t *testing.T) { 10 b := &WriteAtBuffer{} 11 12 n, err := b.WriteAt([]byte{1}, 0) 13 if err != nil { 14 t.Errorf("expected no error, but received %v", err) 15 } 16 if e, a := 1, n; e != a { 17 t.Errorf("expected %d, but received %d", e, a) 18 } 19 20 n, err = b.WriteAt([]byte{1, 1, 1}, 5) 21 if err != nil { 22 t.Errorf("expected no error, but received %v", err) 23 } 24 if e, a := 3, n; e != a { 25 t.Errorf("expected %d, but received %d", e, a) 26 } 27 28 n, err = b.WriteAt([]byte{2}, 1) 29 if err != nil { 30 t.Errorf("expected no error, but received %v", err) 31 } 32 if e, a := 1, n; e != a { 33 t.Errorf("expected %d, but received %d", e, a) 34 } 35 36 n, err = b.WriteAt([]byte{3}, 2) 37 if err != nil { 38 t.Errorf("expected no error, but received %v", err) 39 } 40 if e, a := 1, n; e != a { 41 t.Errorf("expected %d, but received %d", e, a) 42 } 43 44 if !bytes.Equal([]byte{1, 2, 3, 0, 0, 1, 1, 1}, b.Bytes()) { 45 t.Errorf("expected %v, but received %v", []byte{1, 2, 3, 0, 0, 1, 1, 1}, b.Bytes()) 46 } 47 } 48 49 func BenchmarkWriteAtBuffer(b *testing.B) { 50 buf := &WriteAtBuffer{} 51 r := rand.New(rand.NewSource(1)) 52 53 b.ResetTimer() 54 for i := 0; i < b.N; i++ { 55 to := r.Intn(10) * 4096 56 bs := make([]byte, to) 57 buf.WriteAt(bs, r.Int63n(10)*4096) 58 } 59 } 60 61 func BenchmarkWriteAtBufferOrderedWrites(b *testing.B) { 62 // test the performance of a WriteAtBuffer when written in an 63 // ordered fashion. This is similar to the behavior of the 64 // s3.Downloader, since downloads the first chunk of the file, then 65 // the second, and so on. 66 // 67 // This test simulates a 150MB file being written in 30 ordered 5MB chunks. 68 chunk := int64(5e6) 69 max := chunk * 30 70 // we'll write the same 5MB chunk every time 71 tmp := make([]byte, chunk) 72 for i := 0; i < b.N; i++ { 73 buf := &WriteAtBuffer{} 74 for i := int64(0); i < max; i += chunk { 75 buf.WriteAt(tmp, i) 76 } 77 } 78 } 79 80 func BenchmarkWriteAtBufferParallel(b *testing.B) { 81 buf := &WriteAtBuffer{} 82 r := rand.New(rand.NewSource(1)) 83 84 b.ResetTimer() 85 b.RunParallel(func(pb *testing.PB) { 86 for pb.Next() { 87 to := r.Intn(10) * 4096 88 bs := make([]byte, to) 89 buf.WriteAt(bs, r.Int63n(10)*4096) 90 } 91 }) 92 }