Skip to content

Instantly share code, notes, and snippets.

@fluidsonic
Created October 12, 2020 10:46
Show Gist options
  • Save fluidsonic/510f0df9c77d70d72793f92cced04ab2 to your computer and use it in GitHub Desktop.
Save fluidsonic/510f0df9c77d70d72793f92cced04ab2 to your computer and use it in GitHub Desktop.
CIO fails under high load
import io.ktor.application.*
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import kotlinx.coroutines.*
suspend fun main() = withContext(Dispatchers.Default) {
val batchCount = 100
val batchSize = 100
val threadCount = batchSize
startServer()
delay(1_000)
HttpClient(CIO) {
engine {
threadsCount = threadCount
}
}.use { client ->
repeat(batchCount) {
coroutineScope {
repeat(batchSize) {
launch { client.get<HttpResponse>("http://localhost:8080").readText() }
}
}
println(it)
}
}
}
private fun startServer() {
embeddedServer(Netty, port = 8080) {
val data = " ".repeat(64_000)
routing {
get("/") {
call.respondText(data, ContentType.Text.Plain)
}
}
}.start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment