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 com.amazonaws.services.dynamodbv2.model.UpdateItemResult; 22 import org.openjdk.jmh.annotations.Benchmark; 23 import org.openjdk.jmh.annotations.BenchmarkMode; 24 import org.openjdk.jmh.annotations.Fork; 25 import org.openjdk.jmh.annotations.Measurement; 26 import org.openjdk.jmh.annotations.Mode; 27 import org.openjdk.jmh.annotations.Param; 28 import org.openjdk.jmh.annotations.Scope; 29 import org.openjdk.jmh.annotations.Setup; 30 import org.openjdk.jmh.annotations.State; 31 import org.openjdk.jmh.annotations.Warmup; 32 import org.openjdk.jmh.infra.Blackhole; 33 import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; 34 import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; 35 import software.amazon.awssdk.enhanced.dynamodb.TableSchema; 36 import software.amazon.awssdk.services.dynamodb.DynamoDbClient; 37 import software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse; 38 39 @BenchmarkMode(Mode.Throughput) 40 @Warmup(iterations = 5) 41 @Measurement(iterations = 5) 42 @Fork(2) 43 @State(Scope.Benchmark) 44 public class EnhancedClientUpdateV1MapperComparisonBenchmark { 45 private static final V2ItemFactory V2_ITEM_FACTORY = new V2ItemFactory(); 46 private static final V1ItemFactory V1_ITEM_FACTORY = new V1ItemFactory(); 47 private static final DynamoDBMapperConfig MAPPER_CONFIG = 48 DynamoDBMapperConfig.builder() 49 .withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.UPDATE) 50 .build(); 51 52 @Benchmark v2Update(TestState s)53 public void v2Update(TestState s) { 54 s.v2Table.updateItem(s.testItem.v2Bean); 55 } 56 57 @Benchmark v1Update(TestState s)58 public void v1Update(TestState s) { 59 s.v1DdbMapper.save(s.testItem.v1Bean); 60 } 61 getV2Client(Blackhole bh, UpdateItemResponse updateItemResponse)62 private static DynamoDbClient getV2Client(Blackhole bh, UpdateItemResponse updateItemResponse) { 63 return new V2TestDynamoDbUpdateItemClient(bh, updateItemResponse); 64 } 65 getV1Client(Blackhole bh, UpdateItemResult updateItemResult)66 private static AmazonDynamoDB getV1Client(Blackhole bh, UpdateItemResult updateItemResult) { 67 return new V1TestDynamoDbUpdateItemClient(bh, updateItemResult); 68 } 69 70 @State(Scope.Benchmark) 71 public static class TestState { 72 @Param({"TINY", "SMALL", "HUGE", "HUGE_FLAT"}) 73 public TestItem testItem; 74 75 private DynamoDbTable v2Table; 76 private DynamoDBMapper v1DdbMapper; 77 78 79 @Setup setup(Blackhole bh)80 public void setup(Blackhole bh) { 81 DynamoDbEnhancedClient v2DdbEnh = DynamoDbEnhancedClient.builder() 82 .dynamoDbClient(getV2Client(bh, testItem.v2UpdateItemResponse)) 83 .build(); 84 85 v2Table = v2DdbEnh.table(testItem.name(), testItem.schema); 86 87 v1DdbMapper = new DynamoDBMapper(getV1Client(bh, testItem.v1UpdateItemResult), MAPPER_CONFIG); 88 } 89 90 public enum TestItem { 91 TINY( 92 V2ItemFactory.TINY_BEAN_TABLE_SCHEMA, 93 V2_ITEM_FACTORY.tinyBean(), 94 UpdateItemResponse.builder().attributes(V2_ITEM_FACTORY.tiny()).build(), 95 96 V1_ITEM_FACTORY.v1TinyBean(), 97 new UpdateItemResult().withAttributes(V1_ITEM_FACTORY.tiny()) 98 ), 99 100 SMALL( 101 V2ItemFactory.SMALL_BEAN_TABLE_SCHEMA, 102 V2_ITEM_FACTORY.smallBean(), 103 UpdateItemResponse.builder().attributes(V2_ITEM_FACTORY.small()).build(), 104 105 V1_ITEM_FACTORY.v1SmallBean(), 106 new UpdateItemResult().withAttributes(V1_ITEM_FACTORY.small()) 107 ), 108 109 HUGE( 110 V2ItemFactory.HUGE_BEAN_TABLE_SCHEMA, 111 V2_ITEM_FACTORY.hugeBean(), 112 UpdateItemResponse.builder().attributes(V2_ITEM_FACTORY.huge()).build(), 113 114 V1_ITEM_FACTORY.v1hugeBean(), 115 new UpdateItemResult().withAttributes(V1_ITEM_FACTORY.huge()) 116 ), 117 118 HUGE_FLAT( 119 V2ItemFactory.HUGE_BEAN_FLAT_TABLE_SCHEMA, 120 V2_ITEM_FACTORY.hugeBeanFlat(), 121 UpdateItemResponse.builder().attributes(V2_ITEM_FACTORY.hugeFlat()).build(), 122 123 V1_ITEM_FACTORY.v1HugeBeanFlat(), 124 new UpdateItemResult().withAttributes(V1_ITEM_FACTORY.hugeFlat()) 125 ), 126 ; 127 128 // V2 129 private TableSchema schema; 130 private Object v2Bean; 131 private UpdateItemResponse v2UpdateItemResponse; 132 133 // V1 134 private Object v1Bean; 135 private UpdateItemResult v1UpdateItemResult; 136 TestItem(TableSchema<?> schema, Object v2Bean, UpdateItemResponse v2UpdateItemResponse, Object v1Bean, UpdateItemResult v1UpdateItemResult)137 TestItem(TableSchema<?> schema, 138 Object v2Bean, 139 UpdateItemResponse v2UpdateItemResponse, 140 141 Object v1Bean, 142 UpdateItemResult v1UpdateItemResult) { 143 this.schema = schema; 144 this.v2Bean = v2Bean; 145 this.v2UpdateItemResponse = v2UpdateItemResponse; 146 147 this.v1Bean = v1Bean; 148 this.v1UpdateItemResult = v1UpdateItemResult; 149 } 150 } 151 } 152 } 153