fuel is ~22x slower than java.net.http.HttpClient
See original GitHub issueFuel seems to be very slow when sending a sending a request in its body. This is even more pronounced when the payload gets bigger (> 1MB). When benchmarked against the java.net.http.HttpClient
, I noticed that fuel is 24 times slower than the http client that came with Java 11. This is the code I used to do a simple benchmark.
import com.github.kittinunf.fuel.httpPost
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.nio.file.Files
import java.nio.file.Paths
fun main() {
val payload = Files.readString(Paths.get("large.json"))
val url = "http://localhost:4567/lol"
// fuel client
val t = dumbBenchmark {
url.httpPost().body(payload).response()
}
println("fuel client took $t ms on average")
// java.net.http.HttpClient
val client = HttpClient.newHttpClient()
val req = HttpRequest.newBuilder()
.uri(URI(url))
.POST(HttpRequest.BodyPublishers.ofString(payload))
.setHeader("Content-Type", "application/json")
.build()
val t2 = dumbBenchmark {
client.send(req, HttpResponse.BodyHandlers.ofString())
}
println("httpclient took $t2 ms on average")
}
// Runs the lambda five times, time each invocation, and return the average
fun dumbBenchmark(f: () -> Unit): Double = (1..5).map {
val start = System.currentTimeMillis()
f.invoke()
val end = System.currentTimeMillis()
end - start
}.average()
Here I am running a local sinatra server at localhost:4567
, the server simply returns 200 OK for any kind of input. When I send a payload of ~2.2MB I get these results:
fuel client took 7488.2 ms on average
httpclient took 330.2 ms on average
I suspect this is due to the bufferring of the ProgressOutputStream
in HttpClient.kt
. I find it odd that even on a request that do not need progress reporting, fuel would still buffer the output stream. We should probably not buffer when a progress callback is not given to the request.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5
Top GitHub Comments
could it be done (pull request created) in scope of this issue?
Here you go!