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.DynamoDBQueryExpression; 21 import com.amazonaws.services.dynamodbv2.model.QueryResult; 22 import java.util.Arrays; 23 import org.openjdk.jmh.annotations.Benchmark; 24 import org.openjdk.jmh.annotations.BenchmarkMode; 25 import org.openjdk.jmh.annotations.Fork; 26 import org.openjdk.jmh.annotations.Measurement; 27 import org.openjdk.jmh.annotations.Mode; 28 import org.openjdk.jmh.annotations.Param; 29 import org.openjdk.jmh.annotations.Scope; 30 import org.openjdk.jmh.annotations.Setup; 31 import org.openjdk.jmh.annotations.State; 32 import org.openjdk.jmh.annotations.Warmup; 33 import org.openjdk.jmh.infra.Blackhole; 34 import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; 35 import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; 36 import software.amazon.awssdk.enhanced.dynamodb.Key; 37 import software.amazon.awssdk.enhanced.dynamodb.TableSchema; 38 import software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional; 39 import software.amazon.awssdk.services.dynamodb.DynamoDbClient; 40 import software.amazon.awssdk.services.dynamodb.model.QueryResponse; 41 42 @BenchmarkMode(Mode.Throughput) 43 @Warmup(iterations = 5) 44 @Measurement(iterations = 5) 45 @Fork(2) 46 @State(Scope.Benchmark) 47 public class EnhancedClientQueryV1MapperComparisonBenchmark { 48 private static final V2ItemFactory V2_ITEM_FACTORY = new V2ItemFactory(); 49 private static final V1ItemFactory V1_ITEM_FACTORY = new V1ItemFactory(); 50 51 @Benchmark v2Query(TestState s)52 public Object v2Query(TestState s) { 53 return s.v2Table.query(QueryConditional.keyEqualTo(s.key)).iterator().next(); 54 } 55 56 @Benchmark v1Query(TestState s)57 public Object v1Query(TestState s) { 58 return s.v1DdbMapper.query(s.testItem.getV1BeanClass(), s.testItem.v1QueryExpression).iterator().next(); 59 } 60 getV2Client(Blackhole bh, QueryResponse queryResponse)61 private static DynamoDbClient getV2Client(Blackhole bh, QueryResponse queryResponse) { 62 return new V2TestDynamoDbQueryClient(bh, queryResponse); 63 } 64 getV1Client(Blackhole bh, QueryResult queryResult)65 private static AmazonDynamoDB getV1Client(Blackhole bh, QueryResult queryResult) { 66 return new V1TestDynamoDbQueryClient(bh, queryResult); 67 } 68 69 @State(Scope.Benchmark) 70 public static class TestState { 71 @Param({"TINY", "SMALL", "HUGE", "HUGE_FLAT"}) 72 public TestItem testItem; 73 74 private DynamoDbTable<?> v2Table; 75 private DynamoDBMapper v1DdbMapper; 76 77 private final Key key = Key.builder().partitionValue("key").build(); 78 79 @Setup setup(Blackhole bh)80 public void setup(Blackhole bh) { 81 DynamoDbEnhancedClient v2DdbEnh = DynamoDbEnhancedClient.builder() 82 .dynamoDbClient(getV2Client(bh, testItem.v2Response)) 83 .build(); 84 85 v2Table = v2DdbEnh.table(testItem.name(), testItem.schema); 86 87 v1DdbMapper = new DynamoDBMapper(getV1Client(bh, testItem.v1Response)); 88 } 89 90 public enum TestItem { 91 TINY( 92 V2ItemFactory.TINY_BEAN_TABLE_SCHEMA, 93 QueryResponse.builder() 94 .items(Arrays.asList(V2_ITEM_FACTORY.tiny(), 95 V2_ITEM_FACTORY.tiny(), 96 V2_ITEM_FACTORY.tiny())) 97 .build(), 98 99 V1ItemFactory.V1TinyBean.class, 100 new DynamoDBQueryExpression().withHashKeyValues(new V1ItemFactory.V1TinyBean("hashKey")), 101 new QueryResult().withItems( 102 Arrays.asList(V1_ITEM_FACTORY.tiny(), V1_ITEM_FACTORY.tiny(), V1_ITEM_FACTORY.tiny())) 103 ), 104 SMALL( 105 V2ItemFactory.SMALL_BEAN_TABLE_SCHEMA, 106 QueryResponse.builder() 107 .items(Arrays.asList(V2_ITEM_FACTORY.small(), 108 V2_ITEM_FACTORY.small(), 109 V2_ITEM_FACTORY.small())) 110 .build(), 111 112 V1ItemFactory.V1SmallBean.class, 113 new DynamoDBQueryExpression().withHashKeyValues(new V1ItemFactory.V1SmallBean("hashKey")), 114 new QueryResult().withItems( 115 Arrays.asList(V1_ITEM_FACTORY.small(), V1_ITEM_FACTORY.small(), V1_ITEM_FACTORY.small())) 116 ), 117 118 HUGE( 119 V2ItemFactory.HUGE_BEAN_TABLE_SCHEMA, 120 QueryResponse.builder() 121 .items(Arrays.asList(V2_ITEM_FACTORY.huge(), 122 V2_ITEM_FACTORY.huge(), 123 V2_ITEM_FACTORY.huge())) 124 .build(), 125 126 V1ItemFactory.V1HugeBean.class, 127 new DynamoDBQueryExpression().withHashKeyValues(new V1ItemFactory.V1HugeBean("hashKey")), 128 new QueryResult().withItems( 129 Arrays.asList(V1_ITEM_FACTORY.huge(), V1_ITEM_FACTORY.huge(), V1_ITEM_FACTORY.huge())) 130 ), 131 132 HUGE_FLAT( 133 V2ItemFactory.HUGE_BEAN_FLAT_TABLE_SCHEMA, 134 QueryResponse.builder() 135 .items(Arrays.asList(V2_ITEM_FACTORY.hugeFlat(), 136 V2_ITEM_FACTORY.hugeFlat(), 137 V2_ITEM_FACTORY.hugeFlat())) 138 .build(), 139 140 V1ItemFactory.V1HugeBeanFlat.class, 141 new DynamoDBQueryExpression().withHashKeyValues(new V1ItemFactory.V1HugeBeanFlat("hashKey")), 142 new QueryResult().withItems( 143 Arrays.asList(V1_ITEM_FACTORY.hugeFlat(), V1_ITEM_FACTORY.hugeFlat(), V1_ITEM_FACTORY.hugeFlat())) 144 ), 145 ; 146 147 // V2 148 private TableSchema<?> schema; 149 private QueryResponse v2Response; 150 151 // V1 152 private Class<?> v1BeanClass; 153 private DynamoDBQueryExpression v1QueryExpression; 154 private QueryResult v1Response; 155 TestItem(TableSchema<?> schema, QueryResponse v2Response, Class<?> v1BeanClass, DynamoDBQueryExpression v1QueryExpression, QueryResult v1Response)156 TestItem(TableSchema<?> schema, 157 QueryResponse v2Response, 158 159 Class<?> v1BeanClass, 160 DynamoDBQueryExpression v1QueryExpression, 161 QueryResult v1Response) { 162 this.schema = schema; 163 this.v2Response = v2Response; 164 165 this.v1BeanClass = v1BeanClass; 166 this.v1QueryExpression = v1QueryExpression; 167 this.v1Response = v1Response; 168 } 169 getV1BeanClass()170 public Class<?> getV1BeanClass() { 171 return v1BeanClass; 172 } 173 } 174 } 175 } 176