Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions frameworks/ktor-tuned/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ COPY --from=build /app/build/libs/ktor-tuned-httparena.jar .
EXPOSE 8080 8081 8082 8443
ENTRYPOINT ["java", \
"-server", \
"-XX:+UseContainerSupport", \
"-XX:MaxRAMPercentage=50.0", \
"-XX:MaxDirectMemorySize=512m", \
"-XX:MaxMetaspaceSize=256m", \
"-XX:ReservedCodeCacheSize=128m", \
"-Xss512k", \
"-XX:+UseG1GC", \
"-XX:+UseNUMA", \
"-XX:+AlwaysPreTouch", \
Expand Down
2 changes: 2 additions & 0 deletions frameworks/ktor-tuned/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": false,
"spec": ["h1", "h2", "h3"],
"tests": [
"baseline", "async", "latency-1m", "latency-10k", "latency-500k-8cpu",
"limited-conn",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.html.*
Expand All @@ -18,13 +17,11 @@ 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.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
Expand All @@ -47,6 +44,8 @@ fun main() {
val server = embeddedServer(Netty, environment, {
shareWorkGroup = true
enableH2c = true
workerGroupSize = parallelism
callGroupSize = 0

// HTTP/1.1
connector {
Expand Down Expand Up @@ -224,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()
Expand Down Expand Up @@ -283,7 +282,7 @@ private fun Application.configureRouting(appData: ArenaApplicationDeps) {
get("/fortunes") {
val fortunes = mutableListOf<Fortune>()
try {
withContext(Dispatchers.IO) {
withContext(appData.ioDispatcher) {
transaction(appData.postgres, readOnly = true) {
FortuneTable.selectAll()
.map(FortuneTable::toFortune)
Expand Down Expand Up @@ -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<JsonElement>() // 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<ByteArray>()
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() }
}
}

}
}

Expand Down Expand Up @@ -359,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 }
Expand Down Expand Up @@ -389,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 }
Expand Down Expand Up @@ -420,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),
Expand Down Expand Up @@ -463,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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,6 +82,7 @@ object ArenaApplicationDepsFactory {
val dataset: List<DatasetItem> = 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 {
Expand Down Expand Up @@ -139,7 +142,8 @@ object ArenaApplicationDepsFactory {
crudCache,
dataset,
postgres,
keystore
keystore,
ioDispatcher,
)
}
}
Expand All @@ -157,4 +161,5 @@ class ArenaApplicationDeps(
val dataset: List<DatasetItem>,
val postgres: Database?,
val keyStore: KeyStore?,
val ioDispatcher: CoroutineDispatcher,
)
6 changes: 6 additions & 0 deletions frameworks/ktor/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ COPY --from=build /app/build/libs/ktor-httparena.jar .
EXPOSE 8080 8081 8082 8443
ENTRYPOINT ["java", \
"-server", \
"-XX:+UseContainerSupport", \
"-XX:MaxRAMPercentage=50.0", \
"-XX:MaxDirectMemorySize=512m", \
"-XX:MaxMetaspaceSize=256m", \
"-XX:ReservedCodeCacheSize=128m", \
"-Xss512k", \
"-XX:+UseG1GC", \
"-XX:+UseNUMA", \
"-XX:+AlwaysPreTouch", \
Expand Down
2 changes: 2 additions & 0 deletions frameworks/ktor/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": false,
"spec": ["h1", "h2", "h3"],
"tests": [
"baseline", "async", "latency-1m", "latency-10k", "latency-500k-8cpu",
"limited-conn",
Expand Down
70 changes: 64 additions & 6 deletions frameworks/ktor/src/main/kotlin/com/httparena/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -199,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()
Expand Down Expand Up @@ -258,7 +260,7 @@ private fun Application.configureRouting(appData: ArenaApplicationDeps) {
get("/fortunes") {
val fortunes = mutableListOf<Fortune>()
try {
withContext(Dispatchers.IO) {
withContext(appData.ioDispatcher) {
transaction(appData.postgres, readOnly = true) {
FortuneTable.selectAll()
.map(FortuneTable::toFortune)
Expand Down Expand Up @@ -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<JsonElement>() // 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<ByteArray>()
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() }
}
}

}
}

Expand All @@ -317,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 }
Expand Down Expand Up @@ -347,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 }
Expand Down Expand Up @@ -378,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),
Expand Down Expand Up @@ -421,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 }
Expand Down
7 changes: 6 additions & 1 deletion frameworks/ktor/src/main/kotlin/com/httparena/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,6 +82,7 @@ object ArenaApplicationDepsFactory {
val dataset: List<DatasetItem> = 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 {
Expand Down Expand Up @@ -139,7 +142,8 @@ object ArenaApplicationDepsFactory {
crudCache,
dataset,
postgres,
keystore
keystore,
ioDispatcher,
)
}
}
Expand All @@ -157,4 +161,5 @@ class ArenaApplicationDeps(
val dataset: List<DatasetItem>,
val postgres: Database?,
val keyStore: KeyStore?,
val ioDispatcher: CoroutineDispatcher,
)
Loading