• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.benchmark.enhanced.dynamodb;
17 
18 import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
19 import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
20 import com.amazonaws.services.dynamodbv2.model.GetItemResult;
21 import org.openjdk.jmh.annotations.Benchmark;
22 import org.openjdk.jmh.annotations.BenchmarkMode;
23 import org.openjdk.jmh.annotations.Fork;
24 import org.openjdk.jmh.annotations.Measurement;
25 import org.openjdk.jmh.annotations.Mode;
26 import org.openjdk.jmh.annotations.Param;
27 import org.openjdk.jmh.annotations.Scope;
28 import org.openjdk.jmh.annotations.Setup;
29 import org.openjdk.jmh.annotations.State;
30 import org.openjdk.jmh.annotations.Warmup;
31 import org.openjdk.jmh.infra.Blackhole;
32 import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
33 import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
34 import software.amazon.awssdk.enhanced.dynamodb.Key;
35 import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
36 import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
37 import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
38 
39 @BenchmarkMode(Mode.Throughput)
40 @Warmup(iterations = 5)
41 @Measurement(iterations = 5)
42 @Fork(2)
43 @State(Scope.Benchmark)
44 public class EnhancedClientGetV1MapperComparisonBenchmark {
45     private static final V2ItemFactory V2_ITEM_FACTORY = new V2ItemFactory();
46     private static final V1ItemFactory V1_ITEM_FACTORY = new V1ItemFactory();
47 
48     @Benchmark
v2Get(TestState s)49     public Object v2Get(TestState s) {
50         return s.v2Table.getItem(s.key);
51     }
52 
53     @Benchmark
v1Get(TestState s)54     public Object v1Get(TestState s) {
55         return s.v1DdbMapper.load(s.testItem.v1Key);
56     }
57 
getV2Client(Blackhole bh, GetItemResponse getItemResponse)58     private static DynamoDbClient getV2Client(Blackhole bh, GetItemResponse getItemResponse) {
59         return new V2TestDynamoDbGetItemClient(bh, getItemResponse);
60     }
61 
getV1Client(Blackhole bh, GetItemResult getItemResult)62     private static AmazonDynamoDB getV1Client(Blackhole bh, GetItemResult getItemResult) {
63         return new V1TestDynamoDbGetItemClient(bh, getItemResult);
64     }
65 
66     @State(Scope.Benchmark)
67     public static class TestState {
68         @Param({"TINY", "SMALL", "HUGE", "HUGE_FLAT"})
69         public TestItem testItem;
70 
71         private final Key key = Key.builder().partitionValue("key").build();
72 
73         private DynamoDbTable<?> v2Table;
74         private DynamoDBMapper v1DdbMapper;
75 
76 
77         @Setup
setup(Blackhole bh)78         public void setup(Blackhole bh) {
79             DynamoDbEnhancedClient v2DdbEnh = DynamoDbEnhancedClient.builder()
80                     .dynamoDbClient(getV2Client(bh, testItem.v2Response))
81                     .build();
82 
83             v2Table = v2DdbEnh.table(testItem.name(), testItem.schema);
84 
85             v1DdbMapper = new DynamoDBMapper(getV1Client(bh, testItem.v1Response));
86         }
87 
88         public enum TestItem {
89             TINY(
90                     V2ItemFactory.TINY_BEAN_TABLE_SCHEMA,
91                     GetItemResponse.builder().item(V2_ITEM_FACTORY.tiny()).build(),
92 
93                     new V1ItemFactory.V1TinyBean("hashKey"),
94                     new GetItemResult().withItem(V1_ITEM_FACTORY.tiny())
95             ),
96 
97             SMALL(
98                     V2ItemFactory.SMALL_BEAN_TABLE_SCHEMA,
99                     GetItemResponse.builder().item(V2_ITEM_FACTORY.small()).build(),
100 
101                     new V1ItemFactory.V1SmallBean("hashKey"),
102                     new GetItemResult().withItem(V1_ITEM_FACTORY.small())
103             ),
104 
105             HUGE(
106                     V2ItemFactory.HUGE_BEAN_TABLE_SCHEMA,
107                     GetItemResponse.builder().item(V2_ITEM_FACTORY.huge()).build(),
108 
109                     new V1ItemFactory.V1HugeBean("hashKey"),
110                     new GetItemResult().withItem(V1_ITEM_FACTORY.huge())
111             ),
112 
113             HUGE_FLAT(
114                     V2ItemFactory.HUGE_BEAN_FLAT_TABLE_SCHEMA,
115                     GetItemResponse.builder().item(V2_ITEM_FACTORY.hugeFlat()).build(),
116 
117                     new V1ItemFactory.V1HugeBeanFlat("hashKey"),
118                     new GetItemResult().withItem(V1_ITEM_FACTORY.hugeFlat())
119             ),
120             ;
121 
122             // V2
123             private TableSchema<?> schema;
124             private GetItemResponse v2Response;
125 
126             // V1
127             private Object v1Key;
128             private GetItemResult v1Response;
129 
TestItem(TableSchema<?> schema, GetItemResponse v2Response, Object v1Key, GetItemResult v1Response)130             TestItem(TableSchema<?> schema,
131                              GetItemResponse v2Response,
132 
133                              Object v1Key,
134                              GetItemResult v1Response) {
135                 this.schema = schema;
136                 this.v2Response = v2Response;
137 
138                 this.v1Key = v1Key;
139                 this.v1Response = v1Response;
140             }
141         }
142     }
143 }
144