Simple Load-Testing with Apache Benchmark

Last Updated:  December 26, 2020 | Published: February 17, 2019

Testing your application's performance during development as a single user is easy but often misleading. With writing unit- and integration-tests you make sure your main use cases and algorithms work as expected and cover the requirements of the business. Testing the performance of your application under the expected load is often neglected and after deploying to production and serving hundreds/thousands of users concurrently serious performance issues get detected. To avoid this scenario I want to show you a simple way to load test your applications with Apache Benchmark.

Introduction to Apache Benchmark

Apache Benchmark (ab) is a CLI-tool which comes with the Apache HTTP server (HTTPD). To follow the example in the blog post, make sure you have installed the Apache HTTP Server on your machine and the ab binary is available on your PATH:

For testing purposes, I've created a simple Spring Boot based application with a REST interface which I want to test with Apache Benchmark. The setup is quite simple as I am using an H2 database to store a thousand entries which are randomly loaded with a small simulated delay:

This is of course not a real-world example but the following tips can be applied to any REST based application.

Getting insights with the Java VisualVm

To have a visual overview about what's going inside the Java application, I'm using Java VisualVM which was is part of the JDK until Java 8 or available for download. If you are using Java 8 and have the JDK installed, you can start the Visual VM with:

If you have downloaded Java VisualVM manually, you just have to run the executable.

When starting Java VisualVM you can see all running Java applications on your machine and can select one by double-clicking on it in the left-side menu:

Next, select the tab Monitor to get a visual overview of the CPU usage, heap, amount of loaded classes and threads:

In addition, you can also sample the CPU usage and memory consumption more detailed within the tab Sampler and see which method in your application is taking most of the CPU time:

Perform a load test with Apache Benchmark

To see how your application behaves under load, launch the application and a terminal window. With Apache Benchmark you can specify the number of requests (-n argument) to fire and how concurrently (-c argument – indicating the number of “virtual users”) they are. In our simple example, I'm triggering 5.000 HTTP GET calls with 15 parallel users:

Apache Benchmark is of course not limited to trigger simple GET-calls, you can simulate any HTTP method, pass HTTP headers, specify the content-type and pass an HTTP body along with the call:

After the requests finished, you'll get a detailed console output with statistics and average execution times about your load-test and during the execution of the HTTP calls your can monitor your application with Java VisualVM in realtime:

This solution can be easily automated during nightly builds e.g. on Jenkins to have a better indicator of how the application behaves under production-load.

You can find the sample Spring Boot project as always on GitHub. For a more sophisticated solution to test internal implementations, take a look at Java Microbenchmark Harness (JMH).

Have fun testing your application under heady-load,