Created
March 14, 2024 12:06
-
-
Save bfreuden/2655785718026ecdf8c479f78ad7c33f to your computer and use it in GitHub Desktop.
Vert.x HttpClient 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 java.util.concurrent.CountDownLatch; | |
| public class HttpClientBenchmark extends AbstractVerticle { | |
| private HttpClient httpClient; | |
| 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) { | |
| httpClient = vertx.createHttpClient(new HttpClientOptions().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) { | |
| httpClient.request(HttpMethod.GET, "/").onComplete(ar -> { | |
| counter--; | |
| if (ar.succeeded()) { | |
| HttpClientRequest req = ar.result(); | |
| req.send().onComplete(ar2 -> { | |
| if (ar2.succeeded()) { | |
| HttpClientResponse res = ar2.result(); | |
| res.handler(buffer -> {}); | |
| if (res.statusCode() == 200) | |
| successes++; | |
| else | |
| failures++; | |
| } 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(HttpClientBenchmark.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