Vertx WebClient reponds slowly

1.1k views Asked by At

I am new to vertx and RxJava. I am trying to implement a simple test program. However, I am not able to understand the dynamics of this program. Why do some requests take more than 10 seconds to respond?

Below is my sample Test application

public class Test {

public static void main(String[] args) {

Vertx vertx = Vertx.vertx();
WebClient webClient = WebClient.create(vertx);

Observable < Object > google = hitURL("www.google.com", webClient);
Observable < Object > yahoo = hitURL("www.yahoo.com", webClient);

for (int i = 0; i < 100; i++) {
  google.repeat(100).subscribe(timeTaken -> {
    if ((Long) timeTaken > 10000) {
      System.out.println(timeTaken);
    }
  }, error -> {
    System.out.println(error.getMessage());
  });
  yahoo.repeat(100).subscribe(timeTaken -> {
    if ((Long) timeTaken > 10000) {
      System.out.println(timeTaken);
    }
  }, error -> {
    System.out.println(error.getMessage());
  });
}
}

public static Observable < Object > hitURL(String url, WebClient webClient) {
return Observable.create(emitter -> {
  Long l1 = System.currentTimeMillis();
  webClient.get(80, url, "").send(ar -> {
    if (ar.succeeded()) {
      Long elapsedTime = (System.currentTimeMillis() - l1);
      emitter.onNext(elapsedTime);
    } else {
      emitter.onError(ar.cause());
    }
    emitter.onComplete();
  });
});
}
}

What I want to know is, what is making my response time slow?

1

There are 1 answers

3
kstrek On

The problem here seems to be in the way you are using WebClient and/or the way you are measuring "response" times (depending on what you are trying to achieve here).

Vert.x's WebClient, like most http clients, under the hood uses limited-size connection pool to send the requests. In other words, calling .send(...) does not necessarily start the http request immediately - instead, it might wait in some sort of queue for an available connection. Your measurements include this potential waiting time.

You are using the default pool size, which seems to be 5 (at least in the latest version of Vert.x - it's defined here), and almost immediately starting 200 http requests. It's not surprising that most of the time your requests wait for the available connection.

You might try increasing the pool size if you want to test if I'm right:

WebClient webClient = WebClient.create(vertx, new WebClientOptions().setMaxPoolSize(...));