question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fuel is ~22x slower than java.net.http.HttpClient

See original GitHub issue

Fuel 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:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
tfactor2commented, Aug 13, 2019

could it be done (pull request created) in scope of this issue?

1reaction
jsyeocommented, Aug 7, 2019
fuel client took 528.2 ms on average
httpclient took 427.8 ms on average

Here you go!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Apache Http Client slower than browser - java - Stack Overflow
I tried your code with and without httpClient.setUserAgent and response took around 200ms and 90 ms respectively for the second and third ...
Read more >
JDK-8277519 java.net.http.HttpClient is considerably slower ... - Bug ID
I was expecting it to make heavy use of Selectors and Channels and ideally be even faster than HttpURLConnection. It could even use...
Read more >
Exploring the New HTTP Client in Java - Baeldung
In this tutorial, we'll explore Java 11's standardization of HTTP client API that implements HTTP/2 and Web Socket. It aims to replace the ......
Read more >
Untitled
Reflectionmethod performance, 7 zwerge lied lyrics, Traditional animation techniques, Chef michael ... Java simple http client example, Jan-niklas borchers!
Read more >
Writing high performance Java HTTP Client applications
Out of the box, Apache HttpClient is configured to provide high reliability and standards compliance rather than raw performance. There are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found