|
4 | 4 | package benchmark |
5 | 5 |
|
6 | 6 | import ( |
7 | | - "bytes" |
8 | 7 | "context" |
9 | 8 | "fmt" |
10 | 9 | "reflect" |
11 | 10 | "runtime/metrics" |
12 | | - "strconv" |
13 | 11 | "time" |
14 | 12 |
|
15 | 13 | dbesdkitemencryptortypes "github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/awscryptographydbencryptionsdkdynamodbitemencryptorsmithygeneratedtypes" |
16 | | - "github.com/aws/aws-sdk-go-v2/aws" |
17 | | - "github.com/aws/aws-sdk-go-v2/service/dynamodb" |
18 | 14 | "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" |
19 | 15 | ) |
20 | 16 |
|
21 | 17 | // === Helper Functions === |
22 | 18 |
|
23 | 19 | // runBatchPutGetCycle performs a BatchWriteItem-BatchGetItem cycle with 25 items and measures performance |
24 | | -func (b *DBESDKBenchmark) runBatchPutGetCycle(data []byte) (float64, float64, error) { |
25 | | - ctx := context.Background() |
26 | | - tableName := b.Config.TableName |
27 | | - |
28 | | - // Create 25 write requests with same data, different sort_key |
29 | | - var items []map[string]types.AttributeValue |
30 | | - |
31 | | - for i := 0; i < 25; i++ { |
32 | | - item := map[string]types.AttributeValue{ |
33 | | - "partition_key": &types.AttributeValueMemberS{Value: "benchmark-test"}, |
34 | | - "sort_key": &types.AttributeValueMemberN{Value: strconv.Itoa(i)}, |
35 | | - "attribute1": &types.AttributeValueMemberM{Value: map[string]types.AttributeValue{ |
36 | | - "data": &types.AttributeValueMemberB{Value: data}, |
37 | | - }}, |
38 | | - "attribute2": &types.AttributeValueMemberS{Value: "sign me!"}, |
39 | | - ":attribute3": &types.AttributeValueMemberS{Value: "ignore me!"}, |
40 | | - } |
41 | | - items = append(items, item) |
42 | | - } |
43 | | - |
44 | | - var writeRequests []types.WriteRequest |
45 | | - for _, item := range items { |
46 | | - writeRequests = append(writeRequests, types.WriteRequest{ |
47 | | - PutRequest: &types.PutRequest{Item: item}, |
48 | | - }) |
49 | | - } |
50 | | - |
51 | | - // BatchWriteItem |
52 | | - batchWriteStart := time.Now() |
53 | | - _, err := b.DbesdkClient.BatchWriteItem(ctx, &dynamodb.BatchWriteItemInput{ |
54 | | - RequestItems: map[string][]types.WriteRequest{tableName: writeRequests}, |
55 | | - }) |
56 | | - if err != nil { |
57 | | - return 0, 0, fmt.Errorf("BatchWriteItem failed: %w", err) |
58 | | - } |
59 | | - batchWriteDuration := time.Since(batchWriteStart).Seconds() * 1000 |
60 | | - |
61 | | - // Create 25 keys for BatchGetItem |
62 | | - var keys []map[string]types.AttributeValue |
63 | | - for i := 0; i < 25; i++ { |
64 | | - keys = append(keys, map[string]types.AttributeValue{ |
65 | | - "partition_key": &types.AttributeValueMemberS{Value: "benchmark-test"}, |
66 | | - "sort_key": &types.AttributeValueMemberN{Value: strconv.Itoa(i)}, |
67 | | - }) |
68 | | - } |
69 | | - |
70 | | - // BatchGetItem |
71 | | - batchGetStart := time.Now() |
72 | | - result, err := b.DbesdkClient.BatchGetItem(ctx, &dynamodb.BatchGetItemInput{ |
73 | | - RequestItems: map[string]types.KeysAndAttributes{ |
74 | | - tableName: {Keys: keys, ConsistentRead: aws.Bool(true)}, |
75 | | - }, |
76 | | - }) |
77 | | - if err != nil { |
78 | | - return 0, 0, fmt.Errorf("BatchGetItem failed: %w", err) |
79 | | - } |
80 | | - batchGetDuration := time.Since(batchGetStart).Seconds() * 1000 |
81 | | - |
82 | | - // Verify 25 items retrieved with correct data size |
83 | | - returnedItems := result.Responses[tableName] |
84 | | - if len(returnedItems) != 25 { |
85 | | - return 0, 0, fmt.Errorf("expected 25 items, got %d", len(returnedItems)) |
86 | | - } |
87 | | - |
88 | | - // Verify each returned item |
89 | | - for i, item := range returnedItems { |
90 | | - if _, ok := item["attribute1"]; !ok { |
91 | | - return 0, 0, fmt.Errorf("item %d missing attribute1", i) |
92 | | - } |
93 | | - |
94 | | - // Verify attribute1 |
95 | | - if attr1, ok := item["attribute1"].(*types.AttributeValueMemberM); ok { |
96 | | - if dataAttr, ok := attr1.Value["data"].(*types.AttributeValueMemberB); ok { |
97 | | - if !bytes.Equal(dataAttr.Value, data) { |
98 | | - return 0, 0, fmt.Errorf("item %d data mismatch", i) |
99 | | - } |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - // Verify attribute2 value |
104 | | - if attr2, ok := item["attribute2"].(*types.AttributeValueMemberS); ok { |
105 | | - if attr2.Value != "sign me!" { |
106 | | - return 0, 0, fmt.Errorf("item %d attribute2 mismatch: got %s", i, attr2.Value) |
107 | | - } |
108 | | - } else { |
109 | | - return 0, 0, fmt.Errorf("item %d attribute2 wrong type", i) |
110 | | - } |
111 | | - |
112 | | - // Verify :attribute3 value |
113 | | - if attr3, ok := item[":attribute3"].(*types.AttributeValueMemberS); ok { |
114 | | - if attr3.Value != "ignore me!" { |
115 | | - return 0, 0, fmt.Errorf("item %d :attribute3 mismatch: got %s", i, attr3.Value) |
116 | | - } |
117 | | - } else { |
118 | | - return 0, 0, fmt.Errorf("item %d :attribute3 wrong type", i) |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - return batchWriteDuration, batchGetDuration, nil |
123 | | -} |
124 | | - |
125 | | -// runBatchPutGetCycle performs a BatchWriteItem-BatchGetItem cycle with 25 items and measures performance |
| 20 | +// func (b *DBESDKBenchmark) runBatchPutGetCycle(data []byte) (float64, float64, error) { |
| 21 | +// ctx := context.Background() |
| 22 | +// tableName := b.Config.TableName |
| 23 | + |
| 24 | +// // Create 25 write requests with same data, different sort_key |
| 25 | +// var items []map[string]types.AttributeValue |
| 26 | + |
| 27 | +// for i := 0; i < 25; i++ { |
| 28 | +// item := map[string]types.AttributeValue{ |
| 29 | +// "partition_key": &types.AttributeValueMemberS{Value: "benchmark-test"}, |
| 30 | +// "sort_key": &types.AttributeValueMemberN{Value: strconv.Itoa(i)}, |
| 31 | +// "attribute1": &types.AttributeValueMemberM{Value: map[string]types.AttributeValue{ |
| 32 | +// "data": &types.AttributeValueMemberB{Value: data}, |
| 33 | +// }}, |
| 34 | +// "attribute2": &types.AttributeValueMemberS{Value: "sign me!"}, |
| 35 | +// ":attribute3": &types.AttributeValueMemberS{Value: "ignore me!"}, |
| 36 | +// } |
| 37 | +// items = append(items, item) |
| 38 | +// } |
| 39 | + |
| 40 | +// var writeRequests []types.WriteRequest |
| 41 | +// for _, item := range items { |
| 42 | +// writeRequests = append(writeRequests, types.WriteRequest{ |
| 43 | +// PutRequest: &types.PutRequest{Item: item}, |
| 44 | +// }) |
| 45 | +// } |
| 46 | + |
| 47 | +// // BatchWriteItem |
| 48 | +// batchWriteStart := time.Now() |
| 49 | +// _, err := b.DbesdkClient.BatchWriteItem(ctx, &dynamodb.BatchWriteItemInput{ |
| 50 | +// RequestItems: map[string][]types.WriteRequest{tableName: writeRequests}, |
| 51 | +// }) |
| 52 | +// if err != nil { |
| 53 | +// return 0, 0, fmt.Errorf("BatchWriteItem failed: %w", err) |
| 54 | +// } |
| 55 | +// batchWriteDuration := time.Since(batchWriteStart).Seconds() * 1000 |
| 56 | + |
| 57 | +// // Create 25 keys for BatchGetItem |
| 58 | +// var keys []map[string]types.AttributeValue |
| 59 | +// for i := 0; i < 25; i++ { |
| 60 | +// keys = append(keys, map[string]types.AttributeValue{ |
| 61 | +// "partition_key": &types.AttributeValueMemberS{Value: "benchmark-test"}, |
| 62 | +// "sort_key": &types.AttributeValueMemberN{Value: strconv.Itoa(i)}, |
| 63 | +// }) |
| 64 | +// } |
| 65 | + |
| 66 | +// // BatchGetItem |
| 67 | +// batchGetStart := time.Now() |
| 68 | +// result, err := b.DbesdkClient.BatchGetItem(ctx, &dynamodb.BatchGetItemInput{ |
| 69 | +// RequestItems: map[string]types.KeysAndAttributes{ |
| 70 | +// tableName: {Keys: keys, ConsistentRead: aws.Bool(true)}, |
| 71 | +// }, |
| 72 | +// }) |
| 73 | +// if err != nil { |
| 74 | +// return 0, 0, fmt.Errorf("BatchGetItem failed: %w", err) |
| 75 | +// } |
| 76 | +// batchGetDuration := time.Since(batchGetStart).Seconds() * 1000 |
| 77 | + |
| 78 | +// // Verify 25 items retrieved with correct data size |
| 79 | +// returnedItems := result.Responses[tableName] |
| 80 | +// if len(returnedItems) != 25 { |
| 81 | +// return 0, 0, fmt.Errorf("expected 25 items, got %d", len(returnedItems)) |
| 82 | +// } |
| 83 | + |
| 84 | +// // Verify each returned item |
| 85 | +// for i, item := range returnedItems { |
| 86 | +// if _, ok := item["attribute1"]; !ok { |
| 87 | +// return 0, 0, fmt.Errorf("item %d missing attribute1", i) |
| 88 | +// } |
| 89 | + |
| 90 | +// // Verify attribute1 |
| 91 | +// if attr1, ok := item["attribute1"].(*types.AttributeValueMemberM); ok { |
| 92 | +// if dataAttr, ok := attr1.Value["data"].(*types.AttributeValueMemberB); ok { |
| 93 | +// if !bytes.Equal(dataAttr.Value, data) { |
| 94 | +// return 0, 0, fmt.Errorf("item %d data mismatch", i) |
| 95 | +// } |
| 96 | +// } |
| 97 | +// } |
| 98 | + |
| 99 | +// // Verify attribute2 value |
| 100 | +// if attr2, ok := item["attribute2"].(*types.AttributeValueMemberS); ok { |
| 101 | +// if attr2.Value != "sign me!" { |
| 102 | +// return 0, 0, fmt.Errorf("item %d attribute2 mismatch: got %s", i, attr2.Value) |
| 103 | +// } |
| 104 | +// } else { |
| 105 | +// return 0, 0, fmt.Errorf("item %d attribute2 wrong type", i) |
| 106 | +// } |
| 107 | + |
| 108 | +// // Verify :attribute3 value |
| 109 | +// if attr3, ok := item[":attribute3"].(*types.AttributeValueMemberS); ok { |
| 110 | +// if attr3.Value != "ignore me!" { |
| 111 | +// return 0, 0, fmt.Errorf("item %d :attribute3 mismatch: got %s", i, attr3.Value) |
| 112 | +// } |
| 113 | +// } else { |
| 114 | +// return 0, 0, fmt.Errorf("item %d :attribute3 wrong type", i) |
| 115 | +// } |
| 116 | +// } |
| 117 | + |
| 118 | +// return batchWriteDuration, batchGetDuration, nil |
| 119 | +// } |
| 120 | + |
| 121 | +// runItemEncryptorCycle performs a item encryptor encrypt and decrypt cycle with 25 items and measures performance |
126 | 122 | func (b *DBESDKBenchmark) runItemEncryptorCycle(data []byte) (float64, float64, error) { |
127 | 123 | item := map[string]types.AttributeValue{ |
128 | 124 | "partition_key": &types.AttributeValueMemberS{Value: "benchmark-test"}, |
|
0 commit comments