First HttpClient creation and first http request are slow

2,532 views
Skip to first unread message

Erwan

unread,
Jul 17, 2017, 6:10:19 AM7/17/17
to vert.x
Hi,

I experienced some slowness when making HTTP request with Vertx.
The first HTTP Client creation takes around 150-200 ms. Subsequent HTTP client creation don't take time at all.
And then the first HTTP request (to a server on  a VM on the same machine), takes about 300ms, while subsequent requests are below 100ms.

This slowness was introduced in Vertx. 3.3.0 it seems. With vertx 3.2.1, things go faster.

Test and results:
Here's a very simple client that creates a new HTTP client and makes a GET request (it does it 3 times).
public class SimpleHttpClientTest {

public static void main(String[] args) throws Exception {
Vertx vertx = Vertx.vertx();
System.out.println("Try #1");
createHttpClientAndMakeRequest(vertx);
Thread.sleep(1000);
System.out.println("Try #2");
createHttpClientAndMakeRequest(vertx);
Thread.sleep(1000);
System.out.println("Try #3");
createHttpClientAndMakeRequest(vertx);
}
public static void createHttpClientAndMakeRequest(Vertx vertx) {
HttpClientOptions options = new HttpClientOptions().setKeepAlive(false);
long t0 = System.currentTimeMillis();
HttpClient client = vertx.createHttpClient(options);
System.out.println("HttpClient created in " + (System.currentTimeMillis() - t0));
long t1 = System.currentTimeMillis();
io.vertx.core.http.HttpClientRequest request = client.getAbs("http://my_local_server");
request.handler(response -> {
System.out.println("Got response in " + (System.currentTimeMillis() - t1));
});
request.end();
}
}

Output with Vertx 3.3.0 (similar with 3.4.1)
Try #1
HttpClient created in 173
Got response in 298
Try #2
HttpClient created in 0
Got response in 92
Try #3
HttpClient created in 0
Got response in 42

Output with Vertx 3.2.1
Try #1
HttpClient created in 11
Got response in 168
Try #2
HttpClient created in 0
Got response in 49
Try #3
HttpClient created in 0
Got response in 67

Investigation:
It seems that the slowness in creating the first HttpClient comes from the following block of code in SSLHelper.java
static {
    ArrayList<String> suite = new ArrayList<>();
    try {
      SSLContext context = SSLContext.getInstance("TLS");  // takes around 35ms for me
      context.init(null, null, null);   // takes around 30 ms for me
      SSLEngine engine = context.createSSLEngine(); // takes around 100ms for me
      Collections.addAll(suite, engine.getEnabledCipherSuites());
    } catch (Throwable e) {
      suite = null;
    }
    DEFAULT_JDK_CIPHER_SUITE = suite != null ? Collections.unmodifiableList(suite) : null;
  }
This code is executed even if the HttpClient doesn't use ssl.

Questions:
  • Is it just me, or are you guys having a similar experience?
  • Is it expected behavior that the first HttpClient creation and the first HttpRequest are much slower that the subsequent ones?
  • Is any tuning necessary for improving speed for simple HTTP requests in Vertx?
    • As shown in the test results, even subsequent http requests take 92 and 42ms. While testing with a python script, the same request was consistently executed under 20ms (even the first one).
Thanks!
Erwan

Julien Viet

unread,
Jul 17, 2017, 10:31:12 AM7/17/17
to vert.x
Hi,

you should create one HttpClient and share between requests instead of creating one every time.

Julien

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/d85c94dc-4541-4fbb-bf1c-7d4cb213c8d0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Erwan

unread,
Jul 17, 2017, 11:44:53 AM7/17/17
to vert.x
Hi Julien,

Yes, I usually do create a single HttpClient and re-use it. 
The reason why I created multiple HttpClients in my example is to show that only the first one was slow to be created.
Is this "slow" creation normal? And what about the slow first request?

Maybe it's generally not an issue in a Vertx Application if there's a bit of warm up time. I just wanted to check if that was a normal behavior.

Thanks,
Erwan

Tim Fox

unread,
Jul 17, 2017, 1:09:17 PM7/17/17
to vert.x


On Monday, 17 July 2017 16:44:53 UTC+1, Erwan wrote:
Hi Julien,

Yes, I usually do create a single HttpClient and re-use it. 
The reason why I created multiple HttpClients in my example is to show that only the first one was slow to be created.
Is this "slow" creation normal? And what about the slow first request?

Maybe it's generally not an issue in a Vertx Application if there's a bit of warm up time. I just wanted to check if that was a normal behavior.


Allowing for warm-up is normal in any Java application. Java can be slower to start-up than, say, Python for various reasons (JIT, loading classes etc), but steady state performance is often better than scripting languages. For this reason Java is rarely used for scripting, more commonly used for long lived server applications where startup time is less important.

Erwan

unread,
Jul 17, 2017, 1:32:30 PM7/17/17
to vert.x
Ok, got it.
Thanks!
Erwan
Reply all
Reply to author
Forward
0 new messages