AWS SDK for Java 2.x では内部で使うHTTP Clientを変更できるようになっている。現在サポートされているのは次の4つ。
<!-- synchronous -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>apache-client</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>url-connection-client</artifactId>
</dependency>
<!-- asynchronous -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>netty-nio-client</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-crt-client</artifactId>
<version>2.16.79-PREVIEW</version>
</dependency>
system propertyでデフォルトクライアントを指定でき、クライアントごとに変えることもできる。
// JAVA_TOOL_OPTIONS: "-Dsoftware.amazon.awssdk.http.async.service.impl=software.amazon.awssdk.http.crt.AwsCrtSdkHttpService"
System.setProperty("software.amazon.awssdk.http.async.service.impl", "software.amazon.awssdk.http.crt.AwsCrtSdkHttpService");
S3Client s3 = S3Client
.builder()
.region(Region.US_EAST_1)
.httpClientBuilder(UrlConnectionHttpClient.builder())
.build();
S3AsyncClient s3Async = S3AsyncClient
.builder()
.region(Region.US_EAST_1)
.build();
いずれの実装も依存に入っていないと次のエラーになる。
Unable to load an HTTP implementation from any provider in the chain. You must declare a dependency on an appropriate HTTP implementation or pass in an SdkHttpClient explicitly to the client builder
Clientを変更してListObjectするlambdaを実行したところ、cold start時の初期化時間はapache-clientが最も短いが、 総実行時間はドキュメント通りaws-crt-clientが最速となった。 全体のコードはGitHubにある。
- apache-client
Duration: 22420.26 ms Billed Duration: 23714 ms Memory Size: 256 MB Max Memory Used: 148 MB Init Duration: 1293.02 ms
- url-connection-client
Duration: 18861.36 ms Billed Duration: 20207 ms Memory Size: 256 MB Max Memory Used: 144 MB Init Duration: 1345.43 ms
- netty-nio-client
Duration: 16948.49 ms Billed Duration: 24659 ms Memory Size: 256 MB Max Memory Used: 133 MB Init Duration: 7710.06 ms
- aws-crt-client
Duration: 12659.32 ms Billed Duration: 14692 ms Memory Size: 256 MB Max Memory Used: 143 MB Init Duration: 2032.31 ms