如何在 WebClient Vert.x 中立即发送请求?

How to send request immediately in WebClient Vert.x?

我想对我的 Restful API 服务器进行基准测试,所以我向它发送了大量请求以测量吞吐量和延迟。我使用 Vert.x WebClient 创建客户端。

首先,我创建 BenchmarkVerticle extends from AbstractVerticle 并在 start 方法中创建一个 WebClient

public class BenchmarkVerticle extends AbstractVerticle {
    @Override
    public void start(Future<Void> future) {
        WebClient client = WebClient.create(vertx);

        while (true) {
             // warm up in 10s and benchmark in 20s by sending requests
             client.post(Server.port, "localhost", "/api")
                   .send(ar -> { 
                        // do something after receiving response
                   });      
        }
    }
}

然后我在 main

中部署 BenchmarkVerticle
public static void main(String[] args) {
    VertxOptions options = new VertxOptions();
    options.setBlockedThreadCheckInterval(1000*60*60); // make BlockedThreadChecker not show
    options.setWorkerPoolSize(40);
    Vertx vertx = Vertx.vertx(options);

    DeploymentOptions doptions = new DeploymentOptions()
            .setWorker(true);

    vertx.deployVerticle(new BenchmarkVerticle(), doptions);
}

我发现只有在 start 方法完成时才会发送请求。我认为每个请求都放在一个队列中,以便在方法 start 完成后执行。它会影响我的基准测试结果。我尝试通过 setMultiThreaded(true)DeploymentOptions 对象中使用多线程来使其并发发送,但它说:

java.lang.IllegalStateException: Cannot use HttpClient in a multi-threaded worker verticle

那么如何在WebClient中让请求立即发送呢? 注意:英语不是我的母语。如果你觉得难懂我再详细解释一下。

我不确定我是否理解设置。 BenchmarkVerticle 似乎没用,除非它有更多你没有在 post 中透露的逻辑。

  • 如果BenchmarkVerticle负责处理请求
    • 从此 class
    • 中删除 WebClient 引用
    • 一定要在start()
    • 正文末尾调用future.complete()
  • ...否则只需删除它

然后...

在具有main()方法的class中

  • 在此处创建WebClient
  • 然后使用允许您提供 completionHandler 的重载版本 deployVerticle 部署测试所需的任何 Verticle:

deployVerticle(String name, DeploymentOptions options, Handler> completionHandler)

完成处理程序在您的 Verticle 最终部署时被调用。在此处理程序中添加您的请求 + 响应处理。