【问题标题】:Vertx WebClient reponds slowlyVertx WebClient 响应缓慢
【发布时间】:2021-08-17 00:03:44
【问题描述】:

我是 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();
  });
});
}
}

我想知道的是,是什么让我的响应时间变慢了?

【问题讨论】:

    标签: java rx-java2 vert.x vertx-verticle vertx-httpclient


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 我的实例的正确可配置池大小是多少?关于如何计算每个核心的任何想法?
    • 我不认为有一个简单的答案。有太多因素在起作用,因此适当的大小将取决于应用程序。您可以在 google 上搜索一些有关正确大小的连接池的资源(但是,其中大多数都谈论数据库连接,这通常与 http 连接有点不同),但找到正确大小的最可靠方法可能是通过性能测试。
    • 我也是这么想的。很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多