• 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.datamodeling.DynamoDBMapperConfig;
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.TableSchema;
35 import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
36 
37 @BenchmarkMode(Mode.Throughput)
38 @Warmup(iterations = 5)
39 @Measurement(iterations = 5)
40 @Fork(2)
41 @State(Scope.Benchmark)
42 public class EnhancedClientPutV1MapperComparisonBenchmark {
43     private static final V2ItemFactory V2_ITEM_FACTORY = new V2ItemFactory();
44     private static final V1ItemFactory V1_ITEM_FACTORY = new V1ItemFactory();
45     private static final DynamoDBMapperConfig MAPPER_CONFIG =
46         DynamoDBMapperConfig.builder()
47                             .withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.PUT)
48                             .build();
49 
50     @Benchmark
v2Put(TestState s)51     public void v2Put(TestState s) {
52         s.v2Table.putItem(s.testItem.v2Bean);
53     }
54 
55     @Benchmark
v1Put(TestState s)56     public void v1Put(TestState s) {
57         s.v1DdbMapper.save(s.testItem.v1Bean);
58     }
59 
getV2Client(Blackhole bh)60     private static DynamoDbClient getV2Client(Blackhole bh) {
61         return new V2TestDynamoDbPutItemClient(bh);
62     }
63 
getV1Client(Blackhole bh)64     private static AmazonDynamoDB getV1Client(Blackhole bh) {
65         return new V1TestDynamoDbPutItemClient(bh);
66     }
67 
68     @State(Scope.Benchmark)
69     public static class TestState {
70         @Param({"TINY", "SMALL", "HUGE", "HUGE_FLAT"})
71         public TestItem testItem;
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))
81                     .build();
82 
83             v2Table = v2DdbEnh.table(testItem.name(), testItem.schema);
84 
85             v1DdbMapper = new DynamoDBMapper(getV1Client(bh), MAPPER_CONFIG);
86         }
87 
88         public enum TestItem {
89             TINY(
90                     V2ItemFactory.TINY_BEAN_TABLE_SCHEMA,
91                     V2_ITEM_FACTORY.tinyBean(),
92 
93                     V1_ITEM_FACTORY.v1TinyBean()
94             ),
95 
96             SMALL(
97                     V2ItemFactory.SMALL_BEAN_TABLE_SCHEMA,
98                     V2_ITEM_FACTORY.smallBean(),
99 
100                     V1_ITEM_FACTORY.v1SmallBean()
101             ),
102 
103             HUGE(
104                     V2ItemFactory.HUGE_BEAN_TABLE_SCHEMA,
105                     V2_ITEM_FACTORY.hugeBean(),
106 
107                     V1_ITEM_FACTORY.v1hugeBean()
108             ),
109 
110             HUGE_FLAT(
111                     V2ItemFactory.HUGE_BEAN_FLAT_TABLE_SCHEMA,
112                     V2_ITEM_FACTORY.hugeBeanFlat(),
113 
114                     V1_ITEM_FACTORY.v1HugeBeanFlat()
115             ),
116             ;
117 
118             // V2
119             private TableSchema<?> schema;
120             private Object v2Bean;
121 
122             // V1
123             private Object v1Bean;
124 
TestItem(TableSchema<?> schema, Object v2Bean, Object v1Bean)125             TestItem(TableSchema<?> schema,
126                              Object v2Bean,
127 
128                              Object v1Bean) {
129                 this.schema = schema;
130                 this.v2Bean = v2Bean;
131 
132                 this.v1Bean = v1Bean;
133             }
134         }
135     }
136 }
137