Files

59 lines
2.5 KiB
Java

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Closed-loop load generator. usage: java LoadDriver.java URL warmup measured threads rounds
* Prints one line per measured round: requests per second, p50 and p99 latency in microseconds.
* It shares the two vCPUs of the sandbox with the app under test and the OTLP sink, so the
* numbers are relative to each other, not absolute.
*/
public class LoadDriver {
public static void main(String[] a) throws Exception {
String url = a[0];
int warmup = Integer.parseInt(a[1]), measured = Integer.parseInt(a[2]);
int threads = Integer.parseInt(a[3]), rounds = Integer.parseInt(a[4]);
HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build();
HttpRequest req = HttpRequest.newBuilder(URI.create(url)).GET().build();
run(client, req, warmup, threads);
for (int r = 1; r <= rounds; r++) {
long t0 = System.nanoTime();
long[] lat = run(client, req, measured, threads);
double secs = (System.nanoTime() - t0) / 1e9;
Arrays.sort(lat);
System.out.printf("round %d: %6.0f req/s p50 %5d us p99 %6d us%n", r, lat.length / secs,
lat[lat.length / 2] / 1000, lat[(int) (lat.length * 0.99)] / 1000);
}
}
static long[] run(HttpClient c, HttpRequest req, int total, int threads) throws Exception {
ExecutorService ex = Executors.newFixedThreadPool(threads);
List<Callable<long[]>> jobs = new ArrayList<>();
int per = total / threads;
for (int t = 0; t < threads; t++) {
jobs.add(() -> {
long[] l = new long[per];
for (int i = 0; i < per; i++) {
long s = System.nanoTime();
HttpResponse<Void> r = c.send(req, HttpResponse.BodyHandlers.discarding());
if (r.statusCode() != 200) throw new IllegalStateException("status " + r.statusCode());
l[i] = System.nanoTime() - s;
}
return l;
});
}
long[] all = new long[per * threads];
int k = 0;
for (var f : ex.invokeAll(jobs)) for (long v : f.get()) all[k++] = v;
ex.shutdown();
return all;
}
}