Skip to content

Instantly share code, notes, and snippets.

@rpgreen
Last active December 10, 2018 19:33
Show Gist options
  • Select an option

  • Save rpgreen/c08c255f2c546387b5b5 to your computer and use it in GitHub Desktop.

Select an option

Save rpgreen/c08c255f2c546387b5b5 to your computer and use it in GitHub Desktop.
A simple script to measure API Gateway endpoint latency. Requires Apache HttpComponents
public void testPerf() throws IOException {
// final HttpGet request = new HttpGet("http://petstore-demo-endpoint.execute-api.com/petstore/pets");
// final HttpGet request = new HttpGet("http://11ro68yamg.execute-api.us-east-1.amazonaws.com/petstore/pets"); //cf->petstore
final HttpGet request = new HttpGet("https://11ro68yamg.execute-api.us-east-1.amazonaws.com/prod");
final int numRequests = 10000;
long totalLatency = 0;
final int[] histogram = new int[100];
final List<Integer> latencies = new ArrayList<>();
for (int i = 0; i < numRequests; i++) {
final CloseableHttpClient client = HttpClients.createDefault();
final Stopwatch sw = Stopwatch.createStarted();
final CloseableHttpResponse resp = client.execute(request);
sw.stop();
long millis = sw.elapsed(TimeUnit.MILLISECONDS);
totalLatency += millis;
if (millis > 600) {
LOG.warn(String.format("Request took %dms" + ", request ID: %s", millis, resp.getFirstHeader("X-Amzn-Requestid")));
}
histogram[((int) (millis / 100))]++;
latencies.add((int) millis);
}
Collections.sort(latencies);
final Integer p50 = latencies.get((50 * (numRequests) / 100) - 1);
final Integer p90 = latencies.get((90 * (numRequests) / 100) - 1);
final Integer p99 = latencies.get((99 * (numRequests) / 100) - 1);
final Integer p100 = latencies.get((100 * (numRequests) / 100) - 1);
LOG.info(String.format("Num requests: %d Avg latency: %fms", numRequests, totalLatency / (double) numRequests));
LOG.info(String.format("P50: %dms P90: %dms P99: %dms P100: %dms", 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