Created
March 14, 2024 12:05
-
-
Save bfreuden/342898907b19074f71307a45d9b957e1 to your computer and use it in GitHub Desktop.
Vert.x WebClient benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package io.vertx.example; | |
| import io.vertx.core.AbstractVerticle; | |
| import io.vertx.core.Promise; | |
| import io.vertx.core.Vertx; | |
| import io.vertx.core.http.*; | |
| import io.vertx.ext.web.client.HttpResponse; | |
| import io.vertx.ext.web.client.WebClient; | |
| import io.vertx.ext.web.client.WebClientOptions; | |
| import io.vertx.ext.web.codec.BodyCodec; | |
| import java.util.concurrent.CountDownLatch; | |
| public class WebClientBenchmark extends AbstractVerticle { | |
| private WebClient webClient; | |
| final int nbWarmupRequestsRequests = 10000; | |
| final int nbBenchmarkRequestsRequests = 100000; | |
| int nbRequests; | |
| int counter; | |
| int successes = 0; | |
| int failures = 0; | |
| private Promise<Void> donePromise = Promise.promise(); | |
| private long begin; | |
| @Override | |
| public void start(Promise<Void> startPromise) { | |
| webClient = WebClient.create(vertx, new WebClientOptions().setDefaultHost("127.0.0.1").setDefaultPort(3000).setProtocolVersion(HttpVersion.HTTP_2)); | |
| donePromise.future().onComplete(ar -> startPromise.complete()); | |
| Promise<Void> warmupDonePromise = Promise.promise(); | |
| warmupDonePromise.future().onComplete(ar -> { | |
| begin = System.currentTimeMillis(); | |
| counter = nbBenchmarkRequestsRequests; | |
| nbRequests = nbBenchmarkRequestsRequests; | |
| successes = 0; | |
| failures = 0; | |
| runBenchmark(donePromise); | |
| }); | |
| begin = System.currentTimeMillis(); | |
| counter = nbWarmupRequestsRequests; | |
| nbRequests = nbWarmupRequestsRequests; | |
| successes = 0; | |
| failures = 0; | |
| runBenchmark(warmupDonePromise); | |
| } | |
| public void runBenchmark(Promise<Void> donePromise) { | |
| webClient.get("/").as(BodyCodec.none()).send().onComplete(ar -> { | |
| counter--; | |
| if (ar.succeeded()) { | |
| HttpResponse<Void> res = ar.result(); | |
| if (res.statusCode() == 200) | |
| successes++; | |
| else | |
| failures++; | |
| } else { | |
| failures++; | |
| } | |
| if (counter != 0) | |
| runBenchmark(donePromise); | |
| else { | |
| long end = System.currentTimeMillis(); | |
| System.out.println("Requests: " +nbRequests); | |
| System.out.println("Successes: " +successes); | |
| System.out.println("Failures: " +failures); | |
| System.out.println("Elapsed (ms): " +(end - begin)); | |
| System.out.println("Request/sec: " +1000* nbRequests /(end - begin)); | |
| donePromise.complete(); | |
| } | |
| }); | |
| } | |
| // | |
| public static void main(String[] args) { | |
| try { | |
| Vertx vertx = Vertx.vertx(); | |
| CountDownLatch countDownLatch = new CountDownLatch(1); | |
| vertx.deployVerticle(WebClientBenchmark.class.getName()).onComplete(ar -> { | |
| countDownLatch.countDown(); | |
| if (ar.failed()) | |
| ar.cause().printStackTrace(); | |
| }); | |
| countDownLatch.await(); | |
| System.out.println("done"); | |
| vertx.close(); | |
| } catch (Throwable t) { | |
| t.printStackTrace(); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment