Apache Bench is an open-source command-line application for load-testing HTTP servers. Despite the rather limited functionality compared to the commercial software, it’s absolutely sufficient for simple performance benchmarks. Apache Bench can be used to test any HTTP web server, not only the Apache2 server.
Installation
Apache Bench is a part of apache2-utils package. Execute the following command to install the package:
sudo apt install apache2-utils
If you have Apache2 HTTP server installed on your machine, probably you also already have Apache Bench. Check it by executing ab -V (capital V) in your console. If the command returns a version of the application, that means Apache Bench is installed. In this case, you don’t have to do anything more.
Syntax and parameters
The syntax of the command is following:
ab <OPTIONS> <HOST>/<PATH>
A full list of options is available here. I will only present the most commonly used. Personally, I’ve never had a need to use other ones.
- -n – number of requests (default is 1),
- -c – number of concurrent requests,
- -l – if you use this option, Apache Bench will not report length errors. This option is created for dynamic websites and almost all websites are dynamic nowadays. I always use it,
- -t – maximum time for a benchmark (in seconds),
- -C – sets a cookie (syntax -C cookieName=cookieValue). Useful when you’re testing an endpoint that requires authentication,
An example command looks like this:
ab -n 100 -c 10 -l https://www.example.com/
If you want to test a main page, don’t forget to add a slash (/) after the host. Otherwise, Apache Bench throws “Invalid URL” error.
Interpreting the result
Let’s take a look at the results of Google page. We run the command with 100 requests (10 concurrent) with the following command:
ab -n 100 -c 10 -l https://www.google.com/
The last 2 sections are the most interesting for us (Connection Times and Percentage of the requests served within a certain time).
- Connections Times – gives us statistics (min, max, mean and median request time) of all requests sent during the test. Results are also divided into stages of request lifecycle.
- Percentage of the requests served within a certain time – shows percentiles of sent requests. As shown on the screen 50% of requests were served in 267 milliseconds or less and 99% within 364 milliseconds or less. I usually focus on the 95th percentile to exclude extreme results.
Conclusion
I usually use Apache Bench when optimizing web applications locally. That’s the perfect case to use that software because of its simple syntax and quick outcome. For proper performance testing and performance monitoring, we have many better tools.