/ LINUX

A Complete Guide to Apache Bench for Performance Testing

A Complete Guide to Apache Bench for Performance Testing

Apache Bench, or AB, is a powerful HTTP load-testing tool the Apache Software Foundation provides. With this tool, you can benchmark the performance of your web server or website. But first, let’s dive into understanding the usage of Apache AB with some examples.


Table of Contents:


Installation

Before we start, you need to install the Apache Bench ab command on your machine. You can install ab on Ubuntu using the following:

sudo apt-get install apache2-utils

MacOS users can install it using Homebrew:

brew install httpd

Basic Usage of Apache Bench

To test a website, you can use the following command structure:

ab -n number_of_requests -c concurrent_requests target_URL

Where:

  • -n: Apache Bench will send this number of requests to the target URL.
  • -c: The number of concurrent requests Apache AB will send to the target URL.

For example:

ab -n 100 -c 10 https://yourwebsite.com/

This command will send 100 requests to https://yourwebsite.com/ with concurrently processed ten requests.

Understanding Apache Bench Output

On successful completion of the performance testing, Apache Bench will provide an output with multiple details. Here’s a brief explanation of some of the key metrics:

  • Requests per second: Refers to the number of requests your web server can serve.
  • Time per request: The average time per request.
  • Transfer rate: The data transfer rate.

Testing With Different Data and Headers

Apache Bench allows you to test your POST APIs with different data and headers. Here’s how:

First, create a text file that contains the JSON data you want to POST.

{
    "username": "test",
    "password": "password"
}

Save this as post_data.txt.

Run the following command to execute the Apache Bench command along with the JSON file data:

ab -p post_data.txt -T application/json -H 'Authorization: Bearer your-token' -n 100 -c 10 http://yourwebsite.com/api/

Where:

  • -p: Defines the data you want to POST.
  • -T: The Content-Type flag in the request header.
  • -H: The additional headers flag in the request.

Conclusion

Apache Bench is a robust tool for benchmarking the performance of your server or website. By understanding its usage and interpreting the output, you can glean valuable insights into the performance and stability of your site under various loads. Experiment with different settings and parameters to fully explore the capabilities of Apache Bench.

faizan

Faizan Bashir

Principal Engineer | Architecting and building distributed applications in the Cloud | Adventurer

Read More