|
| 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