简体   繁体   中英

How to send request immediately in WebClient Vert.x?

I want to benchmark my Restful API Server, so I send a ton of request to it to measure throughput and latency. I use Vert.x WebClient to create a client.

First, I create BenchmarkVerticle extends from AbstractVerticle and create a WebClient in start method.

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
                   });      
        }
    }
}

And then I deploy BenchmarkVerticle in main

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);
}

I find that request are only sent when start method finished. I think each request is put in a queue to execute after method start complete. It affects to my benchmark result. I try using multithreading by setMultiThreaded(true) in DeploymentOptions object to make it send concurrently but it says:

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

So how to make request send immediately in WebClient? NOTE: English is not my native language. I will expain more detailed if you feel hard to understand.

i'm not sure i understand the setup. BenchmarkVerticle seems useless, unless it has more logic that you haven't revealed to in your post.

  • if BenchmarkVerticle is responsible for handing requests
    • remove the WebClient references from this class
    • be sure to call future.complete() at the end of the body of start()
  • ...otherwise just remove it

then...

in the class that has the main() method

  • create the WebClient here
  • then deploy whatever Verticle(s) you need for your testing using the overloaded version of deployVerticle that allows your to supply a completionHandler:

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

the completion handler is called when your Verticle(s) are finally deployed. add your request + response handling inside this handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM