Vertx WebClient 响应缓慢

Tan*_*yan 1 java vert.x vertx-httpclient rx-java2 vertx-verticle

我是 vertx 和 RxJava 的新手。我正在尝试实现一个简单的测试程序。但是,我无法理解这个程序的动态。为什么有些请求需要 10 秒以上才能响应?

以下是我的示例测试应用程序

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();
  });
});
}
}
Run Code Online (Sandbox Code Playgroud)

我想知道是什么导致我的响应时间变慢?

kst*_*rek 5

这里的问题似乎在于您使用的方式WebClient和/或您测量“响应”时间的方式(取决于您在这里想要实现的目标)。

与大多数 http 客户端一样, Vert.xWebClient在底层使用有限大小的连接池来发送请求。换句话说,调用.send(...)不一定立即启动 http 请求 - 相反,它可能会在某种队列中等待可用连接。您的测量包括这个潜在的等待时间。

您正在使用默认池大小,似乎是 5(至少在最新版本的 Vert.x 中 - 它是在此处定义的),并且几乎立即启动 200 个 http 请求。大多数时候您的请求都会等待可用的连接,这并不奇怪。

如果您想测试我是否正确,您可以尝试增加池大小:

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