From 8c5120e70434d4a848a4f1dfbeacecde478f348e Mon Sep 17 00:00:00 2001 From: Bruce Hamilton Date: Mon, 16 Nov 2026 16:24:23 +0100 Subject: [PATCH 1/5] Ktor compliance and resilience tests --- frameworks/ktor-tuned/meta.json | 2 + .../main/kotlin/com/httparena/Application.kt | 69 +++++++++++++++++-- frameworks/ktor/meta.json | 2 + .../main/kotlin/com/httparena/Application.kt | 58 ++++++++++++++++ 4 files changed, 124 insertions(+), 7 deletions(-) diff --git a/frameworks/ktor-tuned/meta.json b/frameworks/ktor-tuned/meta.json index 480b18e00b..be74832a77 100644 --- a/frameworks/ktor-tuned/meta.json +++ b/frameworks/ktor-tuned/meta.json @@ -13,6 +13,8 @@ "description": "JetBrains Ktor 3.x on Netty with Ghost Serializer, JDK 25.", "repo": "https://github.com/ktorio/ktor", "enabled": true, + "resilience": true, + "spec": ["h1", "h2", "h3"], "tests": [ "baseline", "async", "latency-1m", "latency-10k", "latency-500k-8cpu", "limited-conn", diff --git a/frameworks/ktor-tuned/src/main/kotlin/com/httparena/Application.kt b/frameworks/ktor-tuned/src/main/kotlin/com/httparena/Application.kt index c02d97d004..d0089bee3a 100644 --- a/frameworks/ktor-tuned/src/main/kotlin/com/httparena/Application.kt +++ b/frameworks/ktor-tuned/src/main/kotlin/com/httparena/Application.kt @@ -1,30 +1,29 @@ package com.httparena +import com.ghost.serialization.Ghost import com.httparena.DbResponse.Companion.toResponse import io.ktor.http.* import io.ktor.http.content.* -import com.ghost.serialization.Ghost -import io.ktor.http.content.ByteArrayContent -import io.ktor.server.request.receive +import io.ktor.serialization.kotlinx.json.json import io.ktor.server.application.* import io.ktor.server.engine.* import io.ktor.server.html.* import io.ktor.server.http.content.* import io.ktor.server.netty.* import io.ktor.server.plugins.compression.* -import io.ktor.server.plugins.contentnegotiation.* +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* import io.ktor.server.websocket.* import io.ktor.utils.io.* -import io.netty.channel.ChannelOption -import io.netty.channel.WriteBufferWaterMark -import io.netty.handler.flush.FlushConsolidationHandler import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.delay +import kotlinx.coroutines.ensureActive import kotlinx.coroutines.withContext import kotlinx.html.* +import kotlinx.serialization.json.JsonElement import org.jetbrains.exposed.v1.core.SortOrder import org.jetbrains.exposed.v1.core.between import org.jetbrains.exposed.v1.core.eq @@ -327,6 +326,62 @@ private fun Application.configureRouting(appData: ArenaApplicationDeps) { call.respond(TextContent(delayParam, ContentType.Text.Plain)) } + /** + * Parses the JSON body and responds with 200. + * https://http-arena.com/docs/test-profiles/resilience/ + * + * Ghost doesn't have a generic element so we use `kotlinx.serialization` instead here. + */ + route("/parse") { + install(ContentNegotiation) { + json(appData.json) + } + post { + try { + call.receive() // parses the payload + call.respond(HttpStatusCode.OK) + } catch (_: Exception) { + ensureActive() + call.respond(HttpStatusCode.BadRequest) + } + } + } + + /** + * Endpoints used in the compliance testing. + * https://http-arena.com/docs/test-profiles/compliance/implementation/ + */ + route("/spec") { + val specBody = ByteArrayContent("spec ok".toByteArray(), ContentType.Text.Plain) + + get { call.respond(specBody) } + head { call.respond(specBody) } + options { + call.receiveChannel().discard() + call.response.headers.append(HttpHeaders.Allow, "GET, HEAD, POST, OPTIONS") + call.respond(HttpStatusCode.OK) + } + post { + val body = call.receive() + call.respond(ByteArrayContent(body, ContentType.Application.OctetStream)) + } + + suspend fun ApplicationCall.respondHeaders() { + receiveChannel().discard() + val text = buildString { + request.headers.forEach { name, values -> + values.forEach { append(name).append(": ").append(it).append('\n') } + } + } + respond(TextContent(text, ContentType.Text.Plain)) + } + + route("/headers") { + get { call.respondHeaders() } + post { call.respondHeaders() } + } + } + } } diff --git a/frameworks/ktor/meta.json b/frameworks/ktor/meta.json index de4af28f02..b78eb58647 100644 --- a/frameworks/ktor/meta.json +++ b/frameworks/ktor/meta.json @@ -13,6 +13,8 @@ "description": "JetBrains Ktor 3.x on Netty with kotlinx.serialization, JDK 25.", "repo": "https://github.com/ktorio/ktor", "enabled": true, + "resilience": true, + "spec": ["h1", "h2", "h3"], "tests": [ "baseline", "async", "latency-1m", "latency-10k", "latency-500k-8cpu", "limited-conn", diff --git a/frameworks/ktor/src/main/kotlin/com/httparena/Application.kt b/frameworks/ktor/src/main/kotlin/com/httparena/Application.kt index ab262d6c12..c09f69667d 100644 --- a/frameworks/ktor/src/main/kotlin/com/httparena/Application.kt +++ b/frameworks/ktor/src/main/kotlin/com/httparena/Application.kt @@ -18,8 +18,10 @@ import io.ktor.server.websocket.* import io.ktor.utils.io.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay +import kotlinx.coroutines.ensureActive import kotlinx.coroutines.withContext import kotlinx.html.* +import kotlinx.serialization.json.JsonElement import org.jetbrains.exposed.v1.core.SortOrder import org.jetbrains.exposed.v1.core.between import org.jetbrains.exposed.v1.core.eq @@ -302,6 +304,62 @@ private fun Application.configureRouting(appData: ArenaApplicationDeps) { call.respond(TextContent(delayParam, ContentType.Text.Plain)) } + /** + * Parses the JSON body and responds with 200. + * https://http-arena.com/docs/test-profiles/resilience/ + * + * Ghost doesn't have a generic element so we use `kotlinx.serialization` instead here. + */ + route("/parse") { + install(ContentNegotiation) { + json(appData.json) + } + post { + try { + call.receive() // parses the payload + call.respond(HttpStatusCode.OK) + } catch (_: Exception) { + ensureActive() + call.respond(HttpStatusCode.BadRequest) + } + } + } + + /** + * Endpoints used in the compliance testing. + * https://http-arena.com/docs/test-profiles/compliance/implementation/ + */ + route("/spec") { + val specBody = ByteArrayContent("spec ok".toByteArray(), ContentType.Text.Plain) + + get { call.respond(specBody) } + head { call.respond(specBody) } + options { + call.receiveChannel().discard() + call.response.headers.append(HttpHeaders.Allow, "GET, HEAD, POST, OPTIONS") + call.respond(HttpStatusCode.OK) + } + post { + val body = call.receive() + call.respond(ByteArrayContent(body, ContentType.Application.OctetStream)) + } + + suspend fun ApplicationCall.respondHeaders() { + receiveChannel().discard() + val text = buildString { + request.headers.forEach { name, values -> + values.forEach { append(name).append(": ").append(it).append('\n') } + } + } + respond(TextContent(text, ContentType.Text.Plain)) + } + + route("/headers") { + get { call.respondHeaders() } + post { call.respondHeaders() } + } + } + } } From fccd1fe17a42cb39e7690c8f0327601b9fcbcbd9 Mon Sep 17 00:00:00 2001 From: Bruce Hamilton Date: Tue, 17 Nov 2026 08:06:51 +0100 Subject: [PATCH 2/5] Increase IO dispatcher threads in Ktor for blocking I/O --- .../main/kotlin/com/httparena/Application.kt | 20 +++++++++---------- .../src/main/kotlin/com/httparena/Utils.kt | 7 ++++++- .../main/kotlin/com/httparena/Application.kt | 12 +++++------ .../src/main/kotlin/com/httparena/Utils.kt | 7 ++++++- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/frameworks/ktor-tuned/src/main/kotlin/com/httparena/Application.kt b/frameworks/ktor-tuned/src/main/kotlin/com/httparena/Application.kt index d0089bee3a..162e8814cf 100644 --- a/frameworks/ktor-tuned/src/main/kotlin/com/httparena/Application.kt +++ b/frameworks/ktor-tuned/src/main/kotlin/com/httparena/Application.kt @@ -4,21 +4,19 @@ import com.ghost.serialization.Ghost import com.httparena.DbResponse.Companion.toResponse import io.ktor.http.* import io.ktor.http.content.* -import io.ktor.serialization.kotlinx.json.json +import io.ktor.serialization.kotlinx.json.* import io.ktor.server.application.* import io.ktor.server.engine.* import io.ktor.server.html.* import io.ktor.server.http.content.* import io.ktor.server.netty.* import io.ktor.server.plugins.compression.* -import io.ktor.server.plugins.contentnegotiation.ContentNegotiation +import io.ktor.server.plugins.contentnegotiation.* import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* import io.ktor.server.websocket.* import io.ktor.utils.io.* -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.delay import kotlinx.coroutines.ensureActive import kotlinx.coroutines.withContext @@ -46,6 +44,8 @@ fun main() { val server = embeddedServer(Netty, environment, { shareWorkGroup = true enableH2c = true + workerGroupSize = parallelism + callGroupSize = 0 // HTTP/1.1 connector { @@ -223,7 +223,7 @@ private fun Application.configureRouting(appData: ArenaApplicationDeps) { val max = call.request.queryParameters["max"]?.toIntOrNull() ?: 50 val limit = (call.request.queryParameters["limit"]?.toIntOrNull() ?: 50).coerceIn(1, 50) try { - val items = withContext(Dispatchers.IO) { + val items = withContext(appData.ioDispatcher) { transaction(appData.postgres, readOnly = true) { with(ItemTable) { selectAll() @@ -282,7 +282,7 @@ private fun Application.configureRouting(appData: ArenaApplicationDeps) { get("/fortunes") { val fortunes = mutableListOf() try { - withContext(Dispatchers.IO) { + withContext(appData.ioDispatcher) { transaction(appData.postgres, readOnly = true) { FortuneTable.selectAll() .map(FortuneTable::toFortune) @@ -414,7 +414,7 @@ fun Route.crudEndpoints(appData: ArenaApplicationDeps, log: Logger = LoggerFacto val offset = (page - 1).toLong() * limit try { - val items = withContext(Dispatchers.IO) { + val items = withContext(appData.ioDispatcher) { transaction(appData.postgres, readOnly = true) { ItemTable.selectAll() .where { ItemTable.category eq categoryParam } @@ -444,7 +444,7 @@ fun Route.crudEndpoints(appData: ArenaApplicationDeps, log: Logger = LoggerFacto } try { - val row = withContext(Dispatchers.IO) { + val row = withContext(appData.ioDispatcher) { transaction(appData.postgres, readOnly = true) { ItemTable.selectAll() .where { ItemTable.id eq id } @@ -475,7 +475,7 @@ fun Route.crudEndpoints(appData: ArenaApplicationDeps, log: Logger = LoggerFacto return@post } try { - withContext(Dispatchers.IO) { + withContext(appData.ioDispatcher) { transaction(appData.postgres) { ItemTable.upsert( keys = arrayOf(ItemTable.id), @@ -518,7 +518,7 @@ fun Route.crudEndpoints(appData: ArenaApplicationDeps, log: Logger = LoggerFacto return@put } try { - val updated = withContext(Dispatchers.IO) { + val updated = withContext(appData.ioDispatcher) { transaction(appData.postgres) { val rows = ItemTable.update({ ItemTable.id eq id }) { stmt -> req.name?.let { v -> stmt[ItemTable.name] = v } diff --git a/frameworks/ktor-tuned/src/main/kotlin/com/httparena/Utils.kt b/frameworks/ktor-tuned/src/main/kotlin/com/httparena/Utils.kt index e7e400779a..18ebd85192 100644 --- a/frameworks/ktor-tuned/src/main/kotlin/com/httparena/Utils.kt +++ b/frameworks/ktor-tuned/src/main/kotlin/com/httparena/Utils.kt @@ -3,6 +3,8 @@ package com.httparena import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource import io.ktor.utils.io.core.discard +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers import kotlinx.io.Buffer import kotlinx.io.RawSink import kotlinx.serialization.json.Json @@ -80,6 +82,7 @@ object ArenaApplicationDepsFactory { val dataset: List = datasetFile.takeIf { it.exists() }?.let { json.decodeFromString(it.readText()) } ?: emptyList() + val ioDispatcher = Dispatchers.IO.limitedParallelism(minOf(64, cpuCores * 2)) val postgres: Database? = System.getenv("DATABASE_URL")?.let { dbUrl -> runCatching { @@ -139,7 +142,8 @@ object ArenaApplicationDepsFactory { crudCache, dataset, postgres, - keystore + keystore, + ioDispatcher, ) } } @@ -157,4 +161,5 @@ class ArenaApplicationDeps( val dataset: List, val postgres: Database?, val keyStore: KeyStore?, + val ioDispatcher: CoroutineDispatcher, ) diff --git a/frameworks/ktor/src/main/kotlin/com/httparena/Application.kt b/frameworks/ktor/src/main/kotlin/com/httparena/Application.kt index c09f69667d..eca2258170 100644 --- a/frameworks/ktor/src/main/kotlin/com/httparena/Application.kt +++ b/frameworks/ktor/src/main/kotlin/com/httparena/Application.kt @@ -201,7 +201,7 @@ private fun Application.configureRouting(appData: ArenaApplicationDeps) { val max = call.request.queryParameters["max"]?.toIntOrNull() ?: 50 val limit = (call.request.queryParameters["limit"]?.toIntOrNull() ?: 50).coerceIn(1, 50) try { - val items = withContext(Dispatchers.IO) { + val items = withContext(appData.ioDispatcher) { transaction(appData.postgres, readOnly = true) { with(ItemTable) { selectAll() @@ -260,7 +260,7 @@ private fun Application.configureRouting(appData: ArenaApplicationDeps) { get("/fortunes") { val fortunes = mutableListOf() try { - withContext(Dispatchers.IO) { + withContext(appData.ioDispatcher) { transaction(appData.postgres, readOnly = true) { FortuneTable.selectAll() .map(FortuneTable::toFortune) @@ -375,7 +375,7 @@ fun Route.crudEndpoints(appData: ArenaApplicationDeps, log: Logger = LoggerFacto val offset = (page - 1).toLong() * limit try { - val items = withContext(Dispatchers.IO) { + val items = withContext(appData.ioDispatcher) { transaction(appData.postgres, readOnly = true) { ItemTable.selectAll() .where { ItemTable.category eq categoryParam } @@ -405,7 +405,7 @@ fun Route.crudEndpoints(appData: ArenaApplicationDeps, log: Logger = LoggerFacto } try { - val row = withContext(Dispatchers.IO) { + val row = withContext(appData.ioDispatcher) { transaction(appData.postgres, readOnly = true) { ItemTable.selectAll() .where { ItemTable.id eq id } @@ -436,7 +436,7 @@ fun Route.crudEndpoints(appData: ArenaApplicationDeps, log: Logger = LoggerFacto return@post } try { - withContext(Dispatchers.IO) { + withContext(appData.ioDispatcher) { transaction(appData.postgres) { ItemTable.upsert( keys = arrayOf(ItemTable.id), @@ -479,7 +479,7 @@ fun Route.crudEndpoints(appData: ArenaApplicationDeps, log: Logger = LoggerFacto return@put } try { - val updated = withContext(Dispatchers.IO) { + val updated = withContext(appData.ioDispatcher) { transaction(appData.postgres) { val rows = ItemTable.update({ ItemTable.id eq id }) { stmt -> req.name?.let { v -> stmt[ItemTable.name] = v } diff --git a/frameworks/ktor/src/main/kotlin/com/httparena/Utils.kt b/frameworks/ktor/src/main/kotlin/com/httparena/Utils.kt index e7e400779a..18ebd85192 100644 --- a/frameworks/ktor/src/main/kotlin/com/httparena/Utils.kt +++ b/frameworks/ktor/src/main/kotlin/com/httparena/Utils.kt @@ -3,6 +3,8 @@ package com.httparena import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource import io.ktor.utils.io.core.discard +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers import kotlinx.io.Buffer import kotlinx.io.RawSink import kotlinx.serialization.json.Json @@ -80,6 +82,7 @@ object ArenaApplicationDepsFactory { val dataset: List = datasetFile.takeIf { it.exists() }?.let { json.decodeFromString(it.readText()) } ?: emptyList() + val ioDispatcher = Dispatchers.IO.limitedParallelism(minOf(64, cpuCores * 2)) val postgres: Database? = System.getenv("DATABASE_URL")?.let { dbUrl -> runCatching { @@ -139,7 +142,8 @@ object ArenaApplicationDepsFactory { crudCache, dataset, postgres, - keystore + keystore, + ioDispatcher, ) } } @@ -157,4 +161,5 @@ class ArenaApplicationDeps( val dataset: List, val postgres: Database?, val keyStore: KeyStore?, + val ioDispatcher: CoroutineDispatcher, ) From 39371f0595a36411d2dac66753cad7830ccc0a9e Mon Sep 17 00:00:00 2001 From: Bruce Hamilton Date: Tue, 17 Nov 2026 13:01:18 +0100 Subject: [PATCH 3/5] Make Ktor play nice with container memory limitations --- frameworks/ktor-tuned/Dockerfile | 2 ++ frameworks/ktor/Dockerfile | 2 ++ 2 files changed, 4 insertions(+) diff --git a/frameworks/ktor-tuned/Dockerfile b/frameworks/ktor-tuned/Dockerfile index 2d87b97caf..f4eb4e5883 100644 --- a/frameworks/ktor-tuned/Dockerfile +++ b/frameworks/ktor-tuned/Dockerfile @@ -12,6 +12,8 @@ COPY --from=build /app/build/libs/ktor-tuned-httparena.jar . EXPOSE 8080 8081 8082 8443 ENTRYPOINT ["java", \ "-server", \ + "-XX:+UseContainerSupport", \ + "-XX:MaxRAMPercentage=75.0", \ "-XX:+UseG1GC", \ "-XX:+UseNUMA", \ "-XX:+AlwaysPreTouch", \ diff --git a/frameworks/ktor/Dockerfile b/frameworks/ktor/Dockerfile index d50890f823..ba23ccb03d 100644 --- a/frameworks/ktor/Dockerfile +++ b/frameworks/ktor/Dockerfile @@ -12,6 +12,8 @@ COPY --from=build /app/build/libs/ktor-httparena.jar . EXPOSE 8080 8081 8082 8443 ENTRYPOINT ["java", \ "-server", \ + "-XX:+UseContainerSupport", \ + "-XX:MaxRAMPercentage=75.0", \ "-XX:+UseG1GC", \ "-XX:+UseNUMA", \ "-XX:+AlwaysPreTouch", \ From 1546d32c629a0b28312b9fd04b8ce823331fcb32 Mon Sep 17 00:00:00 2001 From: Bruce Hamilton Date: Tue, 17 Nov 2026 13:42:37 +0100 Subject: [PATCH 4/5] Tweak JVM memory args to be a little more strict --- frameworks/ktor-tuned/Dockerfile | 6 +++++- frameworks/ktor/Dockerfile | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/frameworks/ktor-tuned/Dockerfile b/frameworks/ktor-tuned/Dockerfile index f4eb4e5883..641ceb0b51 100644 --- a/frameworks/ktor-tuned/Dockerfile +++ b/frameworks/ktor-tuned/Dockerfile @@ -13,7 +13,11 @@ EXPOSE 8080 8081 8082 8443 ENTRYPOINT ["java", \ "-server", \ "-XX:+UseContainerSupport", \ - "-XX:MaxRAMPercentage=75.0", \ + "-XX:MaxRAMPercentage=50.0", \ + "-XX:MaxDirectMemorySize=512m", \ + "-XX:MaxMetaspaceSize=256m", \ + "-XX:ReservedCodeCacheSize=128m", \ + "-Xss512k", \ "-XX:+UseG1GC", \ "-XX:+UseNUMA", \ "-XX:+AlwaysPreTouch", \ diff --git a/frameworks/ktor/Dockerfile b/frameworks/ktor/Dockerfile index ba23ccb03d..bafeb70821 100644 --- a/frameworks/ktor/Dockerfile +++ b/frameworks/ktor/Dockerfile @@ -13,7 +13,11 @@ EXPOSE 8080 8081 8082 8443 ENTRYPOINT ["java", \ "-server", \ "-XX:+UseContainerSupport", \ - "-XX:MaxRAMPercentage=75.0", \ + "-XX:MaxRAMPercentage=50.0", \ + "-XX:MaxDirectMemorySize=512m", \ + "-XX:MaxMetaspaceSize=256m", \ + "-XX:ReservedCodeCacheSize=128m", \ + "-Xss512k", \ "-XX:+UseG1GC", \ "-XX:+UseNUMA", \ "-XX:+AlwaysPreTouch", \ From 477cb0cfa7f0307c98e3295463fc8a07a63d2b1a Mon Sep 17 00:00:00 2001 From: Bruce Hamilton Date: Tue, 17 Nov 2026 14:35:09 +0100 Subject: [PATCH 5/5] Disable resilience for Ktor --- frameworks/ktor-tuned/meta.json | 2 +- frameworks/ktor/meta.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/ktor-tuned/meta.json b/frameworks/ktor-tuned/meta.json index be74832a77..e410806e95 100644 --- a/frameworks/ktor-tuned/meta.json +++ b/frameworks/ktor-tuned/meta.json @@ -13,7 +13,7 @@ "description": "JetBrains Ktor 3.x on Netty with Ghost Serializer, JDK 25.", "repo": "https://github.com/ktorio/ktor", "enabled": true, - "resilience": true, + "resilience": false, "spec": ["h1", "h2", "h3"], "tests": [ "baseline", "async", "latency-1m", "latency-10k", "latency-500k-8cpu", diff --git a/frameworks/ktor/meta.json b/frameworks/ktor/meta.json index b78eb58647..6f08284083 100644 --- a/frameworks/ktor/meta.json +++ b/frameworks/ktor/meta.json @@ -13,7 +13,7 @@ "description": "JetBrains Ktor 3.x on Netty with kotlinx.serialization, JDK 25.", "repo": "https://github.com/ktorio/ktor", "enabled": true, - "resilience": true, + "resilience": false, "spec": ["h1", "h2", "h3"], "tests": [ "baseline", "async", "latency-1m", "latency-10k", "latency-500k-8cpu",