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 java.util.Map; 19 import org.openjdk.jmh.annotations.Benchmark; 20 import org.openjdk.jmh.annotations.BenchmarkMode; 21 import org.openjdk.jmh.annotations.Fork; 22 import org.openjdk.jmh.annotations.Measurement; 23 import org.openjdk.jmh.annotations.Mode; 24 import org.openjdk.jmh.annotations.Param; 25 import org.openjdk.jmh.annotations.Scope; 26 import org.openjdk.jmh.annotations.Setup; 27 import org.openjdk.jmh.annotations.State; 28 import org.openjdk.jmh.annotations.Warmup; 29 import org.openjdk.jmh.infra.Blackhole; 30 import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; 31 import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; 32 import software.amazon.awssdk.benchmark.utils.MockHttpClient; 33 import software.amazon.awssdk.core.interceptor.Context; 34 import software.amazon.awssdk.core.interceptor.ExecutionAttributes; 35 import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; 36 import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; 37 import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; 38 import software.amazon.awssdk.enhanced.dynamodb.TableSchema; 39 import software.amazon.awssdk.services.dynamodb.DynamoDbClient; 40 import software.amazon.awssdk.services.dynamodb.model.AttributeValue; 41 42 @BenchmarkMode(Mode.Throughput) 43 @Warmup(iterations = 5) 44 @Measurement(iterations = 5) 45 @Fork(2) 46 @State(Scope.Benchmark) 47 public class EnhancedClientPutOverheadBenchmark { 48 @Benchmark lowLevelPut(TestState s)49 public void lowLevelPut(TestState s) { 50 s.ddb.putItem(r -> r.item(s.testItem.av)); 51 } 52 53 @Benchmark enhancedPut(TestState s)54 public void enhancedPut(TestState s) { 55 s.enhTable.putItem(s.testItem.bean); 56 } 57 58 @State(Scope.Benchmark) 59 public static class TestState { 60 @Param({"TINY", "SMALL", "HUGE", "HUGE_FLAT"}) 61 private TestItem testItem; 62 private DynamoDbClient ddb; 63 64 private DynamoDbTable enhTable; 65 66 @Setup setup(Blackhole bh)67 public void setup(Blackhole bh) { 68 ddb = DynamoDbClient.builder() 69 .credentialsProvider(StaticCredentialsProvider.create( 70 AwsBasicCredentials.create("akid", "skid"))) 71 .httpClient(new MockHttpClient("{}", "{}")) 72 .overrideConfiguration(c -> c.addExecutionInterceptor(new ExecutionInterceptor() { 73 @Override 74 public void afterUnmarshalling(Context.AfterUnmarshalling context, 75 ExecutionAttributes executionAttributes) { 76 bh.consume(context); 77 bh.consume(executionAttributes); 78 } 79 })) 80 .build(); 81 82 DynamoDbEnhancedClient ddbEnh = DynamoDbEnhancedClient.builder() 83 .dynamoDbClient(ddb) 84 .build(); 85 86 enhTable = ddbEnh.table(testItem.name(), testItem.tableSchema); 87 } 88 } 89 90 public enum TestItem { 91 TINY, 92 SMALL, 93 HUGE, 94 HUGE_FLAT 95 ; 96 97 private static final V2ItemFactory FACTORY = new V2ItemFactory(); 98 99 private Map<String, AttributeValue> av; 100 101 private TableSchema tableSchema; 102 private Object bean; 103 104 static { 105 TINY.av = FACTORY.tiny(); 106 TINY.tableSchema = V2ItemFactory.TINY_BEAN_TABLE_SCHEMA; 107 TINY.bean = FACTORY.tinyBean(); 108 109 SMALL.av = FACTORY.small(); 110 SMALL.tableSchema = V2ItemFactory.SMALL_BEAN_TABLE_SCHEMA; 111 SMALL.bean = FACTORY.smallBean(); 112 113 HUGE.av = FACTORY.huge(); 114 HUGE.tableSchema = V2ItemFactory.HUGE_BEAN_TABLE_SCHEMA; 115 HUGE.bean = FACTORY.hugeBean(); 116 117 HUGE_FLAT.av = FACTORY.hugeFlat(); 118 HUGE_FLAT.tableSchema = V2ItemFactory.HUGE_BEAN_FLAT_TABLE_SCHEMA; 119 HUGE_FLAT.bean = FACTORY.hugeBeanFlat(); 120 } 121 } 122 } 123