Created
June 22, 2018 10:28
-
-
Save mrudnytskyi/374291cc0c9188ce3e8feaccdfa7c7c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sample; | |
import com.amazonaws.services.lambda.runtime.Context; | |
import com.amazonaws.services.lambda.runtime.LambdaLogger; | |
import com.amazonaws.services.lambda.runtime.RequestHandler; | |
import software.amazon.awssdk.auth.credentials.AwsCredentials; | |
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | |
import software.amazon.awssdk.core.client.builder.ClientAsyncHttpConfiguration; | |
import software.amazon.awssdk.regions.Region; | |
import software.amazon.awssdk.http.nio.netty.NettySdkHttpClientFactory; | |
import software.amazon.awssdk.services.dynamodb.DynamoDBAsyncClient; | |
import software.amazon.awssdk.services.dynamodb.model.*; | |
import java.time.Duration; | |
import java.time.Instant; | |
import java.util.*; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.stream.Stream; | |
class Parameters { | |
public String access = "XXX"; | |
public String secret = "YYY"; | |
public String tableName = "LambdaTestTable"; | |
public String region = "us-east-1"; | |
public Integer itemsCount = 10000; | |
public Integer maxConnections = 8192; | |
public Integer maxAcq = 700000; | |
public String attributeName = "id"; | |
public Duration acquisionTimeout = Duration.ofMinutes(3); | |
} | |
public class HighLoadSample implements RequestHandler<Object, Long> { | |
private static List<PutItemRequest> createFixture(Parameters parameters, LambdaLogger logger) { | |
logger.log("Creating fixture with count " + parameters.itemsCount + " at " + Instant.now() + System.lineSeparator()); | |
List<PutItemRequest> items = new ArrayList<>(parameters.itemsCount); | |
for (int i = 0; i < parameters.itemsCount; i++) { | |
String value = String.valueOf(UUID.randomUUID().toString()); | |
Map<String, AttributeValue> item = Collections.singletonMap(parameters.attributeName, AttributeValue.builder().s(value).build()); | |
items.add(PutItemRequest.builder().tableName(parameters.tableName).item(item).build()); | |
} | |
return items; | |
} | |
private static void populate(DynamoDBAsyncClient client, List<PutItemRequest> fixture) { | |
Stream<CompletableFuture<PutItemResponse>> stream = fixture.parallelStream().map(client::putItem); | |
CompletableFuture<Void> futures = CompletableFuture.allOf(stream.toArray(CompletableFuture[]::new)); | |
futures.join(); | |
} | |
@Override | |
public Long handleRequest(Object o, Context context) { | |
Parameters parameters = new Parameters(); | |
NettySdkHttpClientFactory httpClientFactory = NettySdkHttpClientFactory | |
.builder() | |
.maxConnectionsPerEndpoint(parameters.maxConnections) | |
.connectionAcquisitionTimeout(parameters.acquisionTimeout) | |
.maxPendingAcquires(parameters.maxAcq) | |
.build(); | |
ClientAsyncHttpConfiguration asyncHttpConf = ClientAsyncHttpConfiguration.builder().httpClientFactory(httpClientFactory).build(); | |
DynamoDBAsyncClient client = DynamoDBAsyncClient.builder() | |
.region(Region.of(parameters.region)) | |
.asyncHttpConfiguration(asyncHttpConf) | |
.credentialsProvider(StaticCredentialsProvider.create(AwsCredentials.create(parameters.access, parameters.secret))) | |
.build(); | |
LambdaLogger logger = context.getLogger(); | |
List<PutItemRequest> fixture = createFixture(parameters, logger); | |
logger.log("Fixture created!" + Instant.now() + System.lineSeparator()); | |
Long before = System.nanoTime(); | |
populate(client, fixture); | |
Long after = System.nanoTime(); | |
Long spent = after - before; | |
logger.log("Populated " + spent + " nanos. Time " + Instant.now() + System.lineSeparator()); | |
return spent; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var aws = require('aws-sdk'); | |
var uuid = require('uuid'); | |
var ddb = new aws.DynamoDB(); | |
function putDatum() { | |
var params = { | |
"TableName": "LambdaTestTable", | |
"Item": { | |
"id" : { | |
"S": uuid.v1() | |
} | |
} | |
}; | |
console.log("Writing to " + params.TableName + " in region " + ddb.config.region); | |
ddb.putItem(params).promise(); | |
} | |
exports.handler = function(event, context, cb) { | |
console.log('Starting Lambda...'); | |
var promiseStack = []; | |
for(var i = 0; i < 10000; i++) { | |
promiseStack.push(putDatum()); | |
} | |
Promise.all(promiseStack).then(function(values) { | |
console.log("Table populated!"); | |
cb(null, values.length); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.sample</groupId> | |
<artifactId>DynamoDb2Sampler</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-assembly-plugin</artifactId> | |
<version>2.4.1</version> | |
<configuration> | |
<descriptorRefs> | |
<descriptorRef>jar-with-dependencies</descriptorRef> | |
</descriptorRefs> | |
</configuration> | |
<executions> | |
<execution> | |
<id>make-assembly</id> | |
<phase>package</phase> | |
<goals> | |
<goal>single</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
<properties> | |
<awsVersion>2.0.0-preview-10</awsVersion> | |
<lambda-core>1.2.0</lambda-core> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>software.amazon.awssdk</groupId> | |
<artifactId>dynamodb</artifactId> | |
<version>${awsVersion}</version> | |
</dependency> | |
<dependency> | |
<groupId>com.amazonaws</groupId> | |
<artifactId>aws-lambda-java-core</artifactId> | |
<version>${lambda-core}</version> | |
</dependency> | |
<dependency> | |
<groupId>software.amazon.awssdk</groupId> | |
<artifactId>netty-nio-client</artifactId> | |
<version>${awsVersion}</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment