Skip to content

Commit 3367e76

Browse files
committed
docstore/awsdynamodb: support the AWS v2 SDK
1 parent ad1e769 commit 3367e76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+41038
-8
lines changed

blob/s3blob/s3blob_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ type harness struct {
5858
func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
5959
t.Helper()
6060

61-
cfg, rt, done, _ := setup.NewAWSv2Config(ctx, t, region)
61+
cfg, rt, done, _ := setup.NewAWSv2Config(ctx, t, region, false)
6262
return &harness{client: s3.NewFromConfig(cfg), opts: nil, rt: rt, closer: done}, nil
6363
}
6464

6565
func newHarnessUsingLegacyList(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
6666
t.Helper()
6767

68-
cfg, rt, done, _ := setup.NewAWSv2Config(ctx, t, region)
68+
cfg, rt, done, _ := setup.NewAWSv2Config(ctx, t, region, false)
6969
return &harness{client: s3.NewFromConfig(cfg), opts: &Options{UseLegacyList: true}, rt: rt, closer: done}, nil
7070
}
7171

@@ -271,7 +271,7 @@ func TestOpenBucket(t *testing.T) {
271271
t.Run(test.description, func(t *testing.T) {
272272
var client *s3.Client
273273
if !test.nilClient {
274-
cfg, _, done, _ := setup.NewAWSv2Config(ctx, t, region)
274+
cfg, _, done, _ := setup.NewAWSv2Config(ctx, t, region, false)
275275
defer done()
276276
client = s3.NewFromConfig(cfg)
277277
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright 2019 The Go Cloud Development Kit Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package awsdynamodb
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"strconv"
21+
"testing"
22+
23+
awsv2cfg "github.com/aws/aws-sdk-go-v2/config"
24+
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression"
25+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
26+
dyn2Types "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
27+
)
28+
29+
var benchmarkTableName = collectionName3
30+
31+
func BenchmarkPutVSTransact(b *testing.B) {
32+
// This benchmark compares two ways to replace N items and retrieve their previous values.
33+
// The first way makes N calls to PutItem with ReturnValues set to ALL_OLD.
34+
// The second way calls BatchGetItem followed by TransactWriteItem.
35+
//
36+
// The results show that separate PutItems are faster for up to two items.
37+
cfg, err := awsv2cfg.LoadDefaultConfig(context.Background())
38+
if err != nil {
39+
b.Fatal("Error initializing aws session for benchmark: ", err)
40+
}
41+
db := dynamodb.NewFromConfig(cfg)
42+
43+
for nItems := 1; nItems <= 5; nItems++ {
44+
b.Run(fmt.Sprintf("%d-Items", nItems), func(b *testing.B) {
45+
var items []map[string]dyn2Types.AttributeValue
46+
for i := 0; i < nItems; i++ {
47+
items = append(items, map[string]dyn2Types.AttributeValue{
48+
"name": &dyn2Types.AttributeValueMemberS{Value: fmt.Sprintf("pt-vs-transact-%d", i)},
49+
"x": &dyn2Types.AttributeValueMemberN{Value: strconv.Itoa(i)},
50+
"rev": &dyn2Types.AttributeValueMemberN{Value: "1"},
51+
})
52+
}
53+
for _, item := range items {
54+
_, err := db.PutItem(context.Background(), &dynamodb.PutItemInput{
55+
TableName: &benchmarkTableName,
56+
Item: item,
57+
})
58+
if err != nil {
59+
b.Fatal(err)
60+
}
61+
}
62+
b.Run("PutItem", func(b *testing.B) {
63+
for n := 0; n < b.N; n++ {
64+
putItems(b, db, items)
65+
}
66+
})
67+
b.Run("TransactWrite", func(b *testing.B) {
68+
for n := 0; n < b.N; n++ {
69+
batchGetTransactWrite(b, db, items)
70+
}
71+
})
72+
})
73+
}
74+
}
75+
76+
func putItems(b *testing.B, db *dynamodb.Client, items []map[string]dyn2Types.AttributeValue) {
77+
b.Helper()
78+
79+
for i, item := range items {
80+
item["x"] = &dyn2Types.AttributeValueMemberN{Value: strconv.Itoa(i + 1)}
81+
in := &dynamodb.PutItemInput{
82+
TableName: &benchmarkTableName,
83+
Item: item,
84+
ReturnValues: dyn2Types.ReturnValueAllOld,
85+
}
86+
ce, err := expression.NewBuilder().
87+
WithCondition(expression.Name("rev").Equal(expression.Value(1))).
88+
Build()
89+
if err != nil {
90+
b.Fatal(err)
91+
}
92+
in.ExpressionAttributeNames = ce.Names()
93+
in.ExpressionAttributeValues = ce.Values()
94+
in.ConditionExpression = ce.Condition()
95+
out, err := db.PutItem(context.Background(), in)
96+
if err != nil {
97+
b.Fatal(err)
98+
}
99+
if got, want := len(out.Attributes), 3; got != want {
100+
b.Fatalf("got %d attributes, want %d", got, want)
101+
}
102+
}
103+
}
104+
105+
func batchGetTransactWrite(b *testing.B, db *dynamodb.Client, items []map[string]dyn2Types.AttributeValue) {
106+
b.Helper()
107+
108+
keys := make([]map[string]dyn2Types.AttributeValue, len(items))
109+
tws := make([]dyn2Types.TransactWriteItem, len(items))
110+
for i, item := range items {
111+
keys[i] = map[string]dyn2Types.AttributeValue{"name": items[i]["name"]}
112+
item["x"] = &dyn2Types.AttributeValueMemberN{Value: strconv.Itoa(i + 2)}
113+
put := &dyn2Types.Put{TableName: &benchmarkTableName, Item: items[i]}
114+
ce, err := expression.NewBuilder().
115+
WithCondition(expression.Name("rev").Equal(expression.Value(1))).
116+
Build()
117+
if err != nil {
118+
b.Fatal(err)
119+
}
120+
put.ExpressionAttributeNames = ce.Names()
121+
put.ExpressionAttributeValues = ce.Values()
122+
put.ConditionExpression = ce.Condition()
123+
tws[i] = dyn2Types.TransactWriteItem{Put: put}
124+
}
125+
_, err := db.BatchGetItem(context.Background(), &dynamodb.BatchGetItemInput{
126+
RequestItems: map[string]dyn2Types.KeysAndAttributes{
127+
benchmarkTableName: {Keys: keys},
128+
},
129+
})
130+
if err != nil {
131+
b.Fatal(err)
132+
}
133+
_, err = db.TransactWriteItems(context.Background(), &dynamodb.TransactWriteItemsInput{TransactItems: tws})
134+
if err != nil {
135+
b.Fatal(err)
136+
}
137+
}

0 commit comments

Comments
 (0)