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 org.openjdk.jmh.annotations.Benchmark; 21 import org.openjdk.jmh.annotations.BenchmarkMode; 22 import org.openjdk.jmh.annotations.Fork; 23 import org.openjdk.jmh.annotations.Measurement; 24 import org.openjdk.jmh.annotations.Mode; 25 import org.openjdk.jmh.annotations.Param; 26 import org.openjdk.jmh.annotations.Scope; 27 import org.openjdk.jmh.annotations.Setup; 28 import org.openjdk.jmh.annotations.State; 29 import org.openjdk.jmh.annotations.Warmup; 30 import org.openjdk.jmh.infra.Blackhole; 31 import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; 32 import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; 33 import software.amazon.awssdk.enhanced.dynamodb.Key; 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 EnhancedClientDeleteV1MapperComparisonBenchmark { 43 @Benchmark v2Delete(TestState s)44 public void v2Delete(TestState s) { 45 s.v2Table.deleteItem(s.key); 46 } 47 48 @Benchmark v1Delete(TestState s)49 public void v1Delete(TestState s) { 50 s.v1DdbMapper.delete(s.testItem.v1Key); 51 } 52 getV2Client(Blackhole bh)53 private static DynamoDbClient getV2Client(Blackhole bh) { 54 return new V2TestDynamoDbDeleteItemClient(bh); 55 } 56 getV1Client(Blackhole bh)57 private static AmazonDynamoDB getV1Client(Blackhole bh) { 58 return new V1TestDynamoDbDeleteItemClient(bh); 59 } 60 61 @State(Scope.Benchmark) 62 public static class TestState { 63 @Param({"TINY", "SMALL", "HUGE", "HUGE_FLAT"}) 64 public TestItem testItem; 65 66 private final Key key = Key.builder().partitionValue("key").build(); 67 68 private DynamoDbTable v2Table; 69 private DynamoDBMapper v1DdbMapper; 70 71 72 @Setup setup(Blackhole bh)73 public void setup(Blackhole bh) { 74 DynamoDbEnhancedClient v2DdbEnh = DynamoDbEnhancedClient.builder() 75 .dynamoDbClient(getV2Client(bh)) 76 .build(); 77 78 v2Table = v2DdbEnh.table(testItem.name(), testItem.schema); 79 80 v1DdbMapper = new DynamoDBMapper(getV1Client(bh)); 81 } 82 83 public enum TestItem { 84 TINY( 85 V2ItemFactory.TINY_BEAN_TABLE_SCHEMA, 86 new V1ItemFactory.V1TinyBean("hashKey") 87 ), 88 89 SMALL( 90 V2ItemFactory.SMALL_BEAN_TABLE_SCHEMA, 91 new V1ItemFactory.V1SmallBean("hashKey") 92 ), 93 94 HUGE( 95 V2ItemFactory.HUGE_BEAN_TABLE_SCHEMA, 96 new V1ItemFactory.V1HugeBean("hashKey") 97 98 ), 99 100 HUGE_FLAT( 101 V2ItemFactory.HUGE_BEAN_FLAT_TABLE_SCHEMA, 102 new V1ItemFactory.V1HugeBeanFlat("hashKey") 103 ), 104 ; 105 106 // V2 107 private TableSchema schema; 108 109 // V1 110 private Object v1Key; 111 TestItem(TableSchema<?> schema, Object v1Key)112 TestItem(TableSchema<?> schema, 113 Object v1Key) { 114 this.schema = schema; 115 116 this.v1Key = v1Key; 117 } 118 } 119 } 120 } 121