diff --git a/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/Main.kt b/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/Main.kt index 86f5da31b..eb793e62c 100644 --- a/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/Main.kt +++ b/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/Main.kt @@ -22,7 +22,8 @@ import com.expediagroup.sdk.performancetest.benchmark.AsyncPostMessageOperationB import com.expediagroup.sdk.performancetest.benchmark.DownloadOperationBenchmark import com.expediagroup.sdk.performancetest.benchmark.GetMessageOperationBenchmark import com.expediagroup.sdk.performancetest.benchmark.PostMessageOperationBenchmark -import com.expediagroup.sdk.performancetest.client.EanBasedClient +import com.expediagroup.sdk.performancetest.client.BasicAuthBasedClient +import com.expediagroup.sdk.performancetest.client.EanAuthBasedClient import com.expediagroup.sdk.performancetest.common.PerformanceTestClient import com.expediagroup.sdk.performancetest.common.benchmark.AsyncBenchmarked import com.expediagroup.sdk.performancetest.common.benchmark.Benchmark @@ -45,13 +46,23 @@ class Main { } } - private val clients: List = listOf( - EanBasedClient.builder() - .key("dummy-key") - .secret("dummy-secret") - .endpoint(System.getProperty("mockServerUrl", "http://localhost:8080")) - .build() - ) + private val clients: List = buildList { + add( + EanAuthBasedClient.builder() + .key("dummy-key") + .secret("dummy-secret") + .endpoint(System.getProperty("mockServerUrl", "http://localhost:8080")) + .build() + ) + + add( + BasicAuthBasedClient.builder() + .key("dummy-key") + .secret("dummy-secret") + .endpoint(System.getProperty("mockServerUrl", "http://localhost:8080")) + .build() + ) + } private val syncBenchmarks: List> = buildList { clients.forEach { client -> diff --git a/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/benchmark/DownloadOperationBenchmarks.kt b/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/benchmark/DownloadOperationBenchmarks.kt index 65783bba6..bd4ac53f0 100644 --- a/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/benchmark/DownloadOperationBenchmarks.kt +++ b/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/benchmark/DownloadOperationBenchmarks.kt @@ -32,7 +32,7 @@ class DownloadOperationBenchmark( repeat = 10, timeout = 1, timeoutUnit = TimeUnit.MINUTES, - description = "Sync File Download Operation Executed 10 times", + description = "Sync File Download Operation Executed 10 times using ${client.javaClass.simpleName} client", ) { override fun benchmark(): Response { return client.execute(DownloadFileOperation()) @@ -46,7 +46,7 @@ class AsyncDownloadOperationBenchmark( repeat = 1000, timeout = 1, timeoutUnit = TimeUnit.HOURS, - description = "Async File Download Operation Executed 1000 times", + description = "Async File Download Operation Executed 1000 times using ${client.javaClass.simpleName} client", ) { override fun benchmarkAsync(): CompletableFuture> { return client.executeAsync(DownloadFileOperation()) diff --git a/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/benchmark/GetMessageOperationBenchmark.kt b/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/benchmark/GetMessageOperationBenchmark.kt index b33971346..0df3417db 100644 --- a/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/benchmark/GetMessageOperationBenchmark.kt +++ b/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/benchmark/GetMessageOperationBenchmark.kt @@ -32,7 +32,7 @@ class GetMessageOperationBenchmark( repeat = 10, timeout = 1, timeoutUnit = TimeUnit.MINUTES, - description = "Sync Get Message Operation Executed 10 times" + description = "Sync Get Message Operation Executed 10 times using ${client.javaClass.simpleName} client" ) { override fun benchmark(): Response { return client.execute(GetMessageOperation()) @@ -46,7 +46,7 @@ class AsyncGetMessageOperationBenchmark( repeat = 1000, timeout = 1, timeoutUnit = TimeUnit.HOURS, - description = "Async Get Message Operation Executed 1000 times" + description = "Async Get Message Operation Executed 1000 times using ${client.javaClass.simpleName} client" ) { override fun benchmarkAsync(): CompletableFuture> { return client.executeAsync(GetMessageOperation()) diff --git a/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/benchmark/PostMessageOperationBenchmark.kt b/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/benchmark/PostMessageOperationBenchmark.kt index 3c8e8e5b6..3ee8fb89b 100644 --- a/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/benchmark/PostMessageOperationBenchmark.kt +++ b/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/benchmark/PostMessageOperationBenchmark.kt @@ -33,7 +33,7 @@ class PostMessageOperationBenchmark( repeat = 10, timeout = 1, timeoutUnit = TimeUnit.MINUTES, - description = "Sync Post Message Operation Executed 10 times" + description = "Sync Post Message Operation Executed 10 times using ${client.javaClass.simpleName} client" ) { override fun benchmark(): Response { return client.execute(PostMessageOperation(Message("Hello, World!".repeat(1e6.toInt())))) @@ -47,7 +47,7 @@ class AsyncPostMessageOperationBenchmark( repeat = 1000, timeout = 1, timeoutUnit = TimeUnit.HOURS, - description = "Async Post Message Operation Executed 1000 times" + description = "Async Post Message Operation Executed 1000 times using ${client.javaClass.simpleName} client" ) { override fun benchmarkAsync(): CompletableFuture> { return client.executeAsync(PostMessageOperation(Message("Hello, World!".repeat(1e6.toInt())))) diff --git a/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/client/BasicAuthBasedClient.kt b/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/client/BasicAuthBasedClient.kt new file mode 100644 index 000000000..2a3d97b3a --- /dev/null +++ b/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/client/BasicAuthBasedClient.kt @@ -0,0 +1,120 @@ +package com.expediagroup.sdk.performancetest.client + +import com.expediagroup.sdk.core.client.BaseXapClient +import com.expediagroup.sdk.core.configuration.XapClientConfiguration +import com.expediagroup.sdk.core.model.EmptyResponse +import com.expediagroup.sdk.core.model.Nothing +import com.expediagroup.sdk.core.model.Operation +import com.expediagroup.sdk.core.model.Response +import com.expediagroup.sdk.core.model.exception.handle +import com.expediagroup.sdk.performancetest.common.PerformanceTestClient +import com.expediagroup.sdk.performancetest.common.model.Message +import com.expediagroup.sdk.performancetest.common.operation.DownloadFileOperation +import com.expediagroup.sdk.performancetest.common.operation.GetMessageOperation +import com.expediagroup.sdk.performancetest.common.operation.PostMessageOperation +import io.ktor.client.call.* +import io.ktor.client.request.* +import io.ktor.client.statement.* +import io.ktor.http.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.future.future +import java.io.InputStream +import java.util.concurrent.CompletableFuture + +class BasicAuthBasedClient( + clientConfiguration: XapClientConfiguration +) : PerformanceTestClient, BaseXapClient( + namespace = "ean", + clientConfiguration = clientConfiguration +) { + companion object { + @JvmStatic + fun builder() = Builder() + } + + private suspend inline fun executeHttpRequest(operation: Operation): HttpResponse { + return httpClient.request { + method = HttpMethod.parse(operation.method) + url(operation.url) + + operation.params?.getHeaders()?.let { + headers.appendAll(it) + } + + operation.params?.getQueryParams()?.let { + url.parameters.appendAll(it) + } + + contentType(ContentType.Application.Json) + setBody(operation.requestBody) + } + } + + private inline fun executeAsyncWithEmptyResponse(operation: Operation): CompletableFuture { + return GlobalScope.future(Dispatchers.IO) { + try { + val response = executeHttpRequest(operation) + throwIfError(response, operation.operationId) + EmptyResponse(response.status.value, response.headers.entries()) + } catch (exception: Exception) { + exception.handle() + } + } + } + + private inline fun execute(operation: Operation): Response { + try { + return executeAsync(operation).get() + } catch (exception: Exception) { + exception.handle() + } + } + + private inline fun executeAsync(operation: Operation): CompletableFuture> { + return GlobalScope.future(Dispatchers.IO) { + try { + val response = executeHttpRequest(operation) + throwIfError(response, operation.operationId) + Response(response.status.value, response.body(), response.headers.entries()) + } catch (exception: Exception) { + exception.handle() + } + } + } + + override fun execute(operation: DownloadFileOperation): Response { + return execute(operation) + } + + override fun executeAsync(operation: DownloadFileOperation): CompletableFuture> { + return executeAsync(operation) + } + + override fun execute(operation: GetMessageOperation): Response { + return execute(operation) + } + + override fun executeAsync(operation: GetMessageOperation): CompletableFuture> { + return executeAsync(operation) + } + + override fun execute(operation: PostMessageOperation): Response { + return execute(operation) + } + + override fun executeAsync(operation: PostMessageOperation): CompletableFuture> { + return executeAsync(operation) + } + + override suspend fun throwServiceException(response: HttpResponse, operationId: String) { + throw Exception("Service exception occurred for operation $operationId with status code ${response.status.value}") + } + + class Builder : BaseXapClient.Builder() { + override fun build() = + BasicAuthBasedClient( + XapClientConfiguration(key, secret, endpoint, requestTimeout, connectionTimeout, socketTimeout, maskedLoggingHeaders, maskedLoggingBodyFields) + ) + } +} diff --git a/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/client/EanBasedClient.kt b/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/client/EanAuthBasedClient.kt similarity index 98% rename from performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/client/EanBasedClient.kt rename to performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/client/EanAuthBasedClient.kt index a2081aeff..782d991df 100644 --- a/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/client/EanBasedClient.kt +++ b/performance-test/src/main/kotlin/com/expediagroup/sdk/performancetest/client/EanAuthBasedClient.kt @@ -38,7 +38,7 @@ import kotlinx.coroutines.future.future import java.io.InputStream import java.util.concurrent.CompletableFuture -class EanBasedClient( +class EanAuthBasedClient( clientConfiguration: RapidClientConfiguration ) : PerformanceTestClient, BaseRapidClient( namespace = "ean", @@ -129,7 +129,7 @@ class EanBasedClient( class Builder : BaseRapidClient.Builder() { override fun build() = - EanBasedClient( + EanAuthBasedClient( RapidClientConfiguration(key, secret, endpoint, requestTimeout, connectionTimeout, socketTimeout, maskedLoggingHeaders, maskedLoggingBodyFields) ) }