Ktor Client for load testing


I developed a small tool for load testing of a microservice via REST. The tool is based on Ktor Client. At the moment, for each simulated user and for each test iteration a HttpClient is instantiated and closed at the end of the test iteration. At the moment I am using the Apache Engine.

I noticed that a lot of threads are created - with simulated 500 concurrent users there are up to 3000 threads. Most of the threads come from Ktor and Apache HttpClient. I think it is too much.

My question is: should I reuse the HttpClients, i.e. use them across the CUs? What would be the right pattern for such application?

I tried to use the CIO engine, but it failed: at a higher number of CUs the program crashes. The exception looks something like this:

Caused by: java.net.BindException: Address already in use: no further information
at java.base/sun.nio.ch.Net.pollConnect(Native Method)
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:660)
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:875)
at io.ktor.network.sockets.SocketImpl.connect$ktor_network(SocketImpl.kt:37)
at io.ktor.network.sockets.SocketImpl$connect$1.invokeSuspend(SocketImpl.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
… 4 common frames omitted

With thanks in advance, Vladimir

Sure, this could be Ktor/Apache bug/design issue, but it doesn’t matter. I don’t think Ktor is suitable for load testing.
It is better to use specialized tools. There are a lot of them, my personal choice is wrk2.

1 Like