Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple script to measure API Gateway endpoint latency. Requires Apache HttpComponents
@Test
public void testPerf() throws IOException {
final HttpGet request = new HttpGet("https://[API-ID].execute-api.[REGION].amazonaws.com/[STAGE]/[PATH]");
final int numRequests = 10000;
final int[] histogram = new int[100];
final List<Integer> latencies = new ArrayList<>();
final CloseableHttpClient client = HttpClients.createDefault();
for (int i = 0; i < numRequests; i++) {
long start = System.currentTimeMillis();
final CloseableHttpResponse resp = client.execute(request);
resp.close();
long millis = System.currentTimeMillis() - start;
if (millis > 800) {
LOG.warn(String.format("Request took %dms" + ", request ID: %s, CF request ID: %s", millis,
resp.getFirstHeader("X-Amzn-Requestid"), resp.getFirstHeader("X-Amz-Cf-Id")));
}
histogram[((int) (millis / 100))]++;
latencies.add((int) millis);
}
Collections.sort(latencies);
final int p50 = latencies.get((50 * (numRequests) / 100) - 1);
final int p90 = latencies.get((90 * (numRequests) / 100) - 1);
final int p99 = latencies.get((99 * (numRequests) / 100) - 1);
final int p100 = latencies.get((100 * (numRequests) / 100) - 1);
LOG.info(String.format("Num requests: %d P50: %dms P90: %dms P99: %dms P100: %dms", numRequests, p50, p90, p99, p100));
LOG.info(String.format("Distribution: "));
for (int i = 0; i < histogram.length; i++) {
LOG.info(i*100 + "ms - " + (i+1)*100 + "ms: " + histogram[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.