Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

Website Performance Benchmarking Using Apache Bench

profile picture
Author
Faleddo
Published
2024-01-23
Time to read
13 minutes reading time

Introduction

Apache Bench, commonly abbreviated as ab, is a single-threaded command-line tool provided by the Apache HTTP server project. It is designed to perform benchmarks on your Apache Hypertext Transfer Protocol (HTTP) server. By simulating multiple requests to a server, Apache Bench measures how well your server can handle the load. This is known as stress testing or load testing. It's akin to a fire drill for your website, ensuring that when real traffic hits, your server can handle the influx without breaking a sweat.

Importance of Benchmarking

Imagine driving a car without a speedometer, tachometer, or fuel gauge. You'd be in the dark about how fast you're going, how hard the engine is working, and how much gas you have left. Benchmarking your website is similar to using these instruments; it provides you with crucial performance readings. It helps you understand:

  • Capacity: How many visitors your site can handle before the user experience degrades.
  • Stability: How your system behaves under various levels of traffic, especially unexpected surges.
  • Bottlenecks: Which parts of your system (e.g., database, server software, network) are limiting performance.

Regular benchmarking is not just about keeping up with the numbers. It's about ensuring that your website remains accessible, reliable, and fast, even under the most demanding conditions. By doing so, you not only keep your current users happy but also scale confidently as your user base grows.

Step 1 - Installing Apache Bench

Before we can begin benchmarking, we need to ensure that Apache Bench (ab) is installed on your system. Apache Bench typically comes bundled with the Apache HTTP server, but it can be installed separately if you're not using Apache as your web server. Here's how you can install Apache Bench on various operating systems:

  • Debian-based distributions (Ubuntu, Debian)
    Apache Bench is part of the apache2-utils package. To install it, open your terminal and run the following command:

    sudo apt-get update
    sudo apt-get install apache2-utils
  • Red Hat-based distributions (Fedora, CentOS)
    On Red Hat-based systems, you can use yum or dnf (on Fedora) to install Apache Bench as follows:

    sudo yum install httpd-tools

    Or if you're using dnf:

    sudo dnf install httpd-tools
  • Arch Linux
    For Arch Linux users, Apache Bench can be installed using pacman:

    sudo pacman -S apache-tools
  • macOS

Apache Bench comes pre-installed on many macOS systems. To check if it's already installed, open your terminal and type ab. If it's installed, you'll see the usage information for the command. If it's not installed, or you want to update to the latest version, you can install it using Homebrew:

brew install homebrew/apache/ab
  • Windows
    Apache Bench is not natively available on Windows, but you can install it through several methods:

    Method Description
    XAMPP XAMPP is a free and open-source cross-platform web server solution stack package. When you install XAMPP, you also get Apache Bench. You can download XAMPP from the official website:
    https://www.apachefriends.org/index.html
    After installation, you'll find ab.exe in the C:\xampp\apache\bin directory.

    Apache Lounge
    Apache Lounge offers Windows builds of Apache and related utilities. You can download a version of Apache that includes Apache Bench from their website:
    https://www.apachelounge.com/download/
    Unzip the downloaded file and find ab.exe in the bin directory.

    WSL
    (Windows Subsystem for Linux)

    If you have WSL enabled on your Windows 10 or later system, you can install Apache Bench just as you would on a Linux system. Open your WSL terminal and follow the Linux installation instructions for your chosen Linux distribution.
    Once you have installed Apache Bench, you can verify the installation by running the following command in your terminal or command prompt:
    ab -V
    This command should output the version information for Apache Bench, confirming that it's successfully installed and ready to use. With Apache Bench installed, you're now ready to start benchmarking and optimizing your web server's performance.

Step 2 - Setting Up the Test Environment

Before you begin benchmarking your web server with Apache Bench, it's crucial to establish a controlled test environment. This ensures that your test results are as accurate and consistent as possible.

  • Selecting a Test Server

    • Isolation: Choose a server environment that is isolated from production traffic to prevent interference with real users and to get consistent results.
    • Replication: Ensure the test server mirrors the production environment as closely as possible in terms of hardware, software, and network configuration.
  • Network Considerations

    • Bandwidth: Verify that your network has sufficient bandwidth available. Network congestion can skew results.
    • Latency: Consider the latency between the test client (where Apache Bench will run) and the test server. High latency can affect the accuracy of connection times.
  • Resource Monitoring

    • Server Resources: Set up monitoring for server resources (CPU, memory, disk I/O, network I/O) to identify potential bottlenecks during the test.
    • Baseline Performance: Record the baseline performance of the server with no load. This will give you a point of comparison for your benchmarking results.

Step 3 - Identifying Benchmarking Goals

Clear goals will help guide your benchmarking process and enable you to interpret the results effectively. For examples:

  • Max Concurrent Users: Determine the maximum number of users your server can handle simultaneously without significant performance degradation.
  • Response Times: Establish acceptable response times for your web application under various load conditions.
  • Throughput: Decide on the throughput (requests per second) your server should support.
  • Peak Traffic: Plan to test for peak traffic scenarios to ensure your web server can handle high loads.

Step 4 - Understanding Benchmarking Metrics

Apache Bench provides metrics that quantify your web server's performance. Here's a breakdown of some key metrics you'll encounter:

  • Requests per Second (Throughput): The number of requests your server can handle each second. Higher values indicate better performance and the server's ability to handle more traffic.

  • Time per Request: The average time taken to process a single request. Lower values indicate faster response times and a more responsive server.

  • Concurrent Requests: The number of requests made to the server at the same time. This metric helps in understanding how well the server performs under simultaneous load.

  • Transfer Rate: The amount of data transferred per second during the test. Indicates the server's capacity to deliver content; important for bandwidth-intensive applications.

  • Success/Error Rates: The ratio of successful requests to error responses (e.g., 404, 500). High success rates are critical for user satisfaction and reliability.

Understanding these metrics will help you interpret the results from Apache Bench and determine whether your server meets the performance goals you've set. Keep in mind that these metrics should be considered together, as they collectively represent your server's performance profile.

Step 5 - Running a Simple Test

Open a terminal window to begin. Select a URL that you want to benchmark. This should be a page that is representative of typical usage. For our example, we'll use http://example.com. Enter the following command to perform a simple benchmarking test:

ab -n 100 -c 10 http://example.com/

This command will make 100 requests to http://example.com with a concurrency of 10 requests at a time. After executing the command, Apache Bench will run the test and provide a series of outputs upon completion. It may take a few seconds to a few minutes, depending on the number of requests and the server's response time.

Step 6 - Analyzing the Output

After the test concludes, Apache Bench will display the results. Here is an example of a truncated output:

Document Path:          /
Document Length:        108 bytes

Concurrency Level:      10
Time taken for tests:   12.905 seconds
Complete requests:      100
Failed requests:        0
Non-2xx responses:      100
Total transferred:      29500 bytes
HTML transferred:       10800 bytes
Requests per second:    7.75 [#/sec] (mean)
Time per request:       1290.527 [ms] (mean)
Time per request:       129.053 [ms] (mean, across all concurrent requests)
Transfer rate:          2.23 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      645  757 109.5    740    1401
Processing:   173  219 205.1    200    2246
Waiting:      172  218 205.1    196    2246
Total:        834  976 227.1    929    2939

Percentage of the requests served within a certain time (ms)
  50%    929
  66%    973
  75%    988
  80%    988
  90%   1022
  95%   1069
  98%   1622
  99%   2939
 100%   2939 (longest request)

The output from Apache Bench provides several key metrics that help understand the performance of the website under test. Here's a breakdown of the key metrics from the output above:

Metrics Description
Concurrency Level This is the number of multiple requests that the tool attempts to perform at a time. In this example, it's set to 10.
Time taken for tests This is the total time it took to perform all the requests, which is 12.905 seconds.
Complete requests This indicates the total number of HTTP requests that were successfully completed during the benchmarking test. This example has 100 complete requests, which means all planned requests were completed without premature termination.
Failed requests This shows the number of requests that failed due to errors such as network issues, timeouts, or server errors. The example has 0 failed requests, which is ideal.
Non-2xx responses This represents the number of successful requests that returned a status code outside of the 2xx range, which typically indicates success. In this case, it has 100 non-2xx responses, which suggests that while the requests were completed, they did not result in successful HTTP status codes (e.g., 200 OK). This could indicate client errors (4xx) or server errors (5xx).
Total transferred This is the cumulative number of bytes transferred during the test. The example test transferred 29500 bytes.
HTML transferred This is the total amount of HTML content transferred. In the example test, this was 10800 bytes of HTML.
Requests per second This is the average number of requests per second that the server was able to handle. In the example output, it's 7.75 requests per second. This is a measure of the throughput of the server.
Time per request (mean) This is the average time taken for each single request when considering the concurrency level. The mean time taken for each request is 1290.527 milliseconds (ms). This metric is useful for understanding the latency per individual request.
Time per request (mean,
across all concurrent requests)
This is the average time taken for each request to be processed, divided by the concurrency level. It is a representation of the latency from the perspective of the client. In this example, this is 129.053 ms, which is a more accurate reflection of the user experience during high traffic.
Transfer rate This is the rate at which data was transferred from the server to the client, measured in kilobytes per second (Kbytes/sec). The example test shows a transfer rate of 2.23 Kbytes/sec.

From these metrics, you can deduce that the server is responding fairly slowly (as indicated by the high average time per request), and every request resulted in a non-2xx status, which means there is likely an issue with the server configuration or the resource being accessed is causing an error. The low transfer rate could also indicate network or server performance issues. These areas would require further investigation to diagnose and resolve any issues.

Step 7 - Advanced Usage and Best Practices

  • Concurrent Testing
    Concurrent testing is essential to understand how your web server performs under pressure, simulating multiple users accessing your website simultaneously.

    1. Command Syntax: The -c flag in Apache Bench is used to set the concurrency level. The syntax looks like:
      ab -c <number_of_concurrent_users> http://example.com/
    2. Example: To simulate 100 users hitting your server at the same time, you would use:
      ab -c 100 http://example.com/
    3. Monitoring Server Resources: While the test runs, monitor server resources using tools like top on Linux to ensure the server is not overtaxed.
  • Prolonged Testing
    Prolonged testing involves running the benchmark over an extended period to identify potential memory leaks or performance degradation over time.

    1. Command Syntax: Use the -t flag to run the test for a specified number of seconds. This will run the test for 10 minutes, for example:
      ab -t 600 http://example.com/
    2. Example: Combine with concurrency to simulate a real-world scenario:
      ab -c 100 -t 600 http://example.com/
  • Testing with Different HTTP Methods

    Understanding HTTP Methods

    • GET: The default method used to retrieve data from the server.
    • POST: Used to submit data to be processed to a specified resource.

    How to test with POST Method

    1. Preparing Data for Submission: Create a file containing the data to be submitted.
    2. Command Syntax: Use the -p flag to specify your data file and the -T flag to set the content type.
    3. Example:
      ab -p postdata.txt -T application/x-www-form-urlencoded

Step 8 - Taking Action from Bench Result

After running Apache Bench, you can identify bottlenecks by looking for:

  • High time per request values: This might indicate slow processing or server resource constraints.
  • Low requests per second: This could suggest your server is not optimized to handle high traffic loads effectively.
  • A wide distribution in the percentage of requests served within certain times: If many requests take significantly longer to serve, this could point to inconsistent server performance.

So you can make decision to increase server performance. For example:

  • Server Configuration: Adjust configurations based on performance. For instance, you may need to tweak settings like KeepAlive or MaxClients.
  • Scaling: Determine if your server hardware can handle your peak load or if you need to scale up or out.
  • Optimization: Identify slow-performing scripts or database queries that need optimization.

Conclusion

Throughout this tutorial, you've learned how to install Apache Bench, conduct basic and advanced benchmarking tests, interpret the results, and make informed decisions to optimize your server's performance. Benchmarking is an iterative process. Encourage yourself to continuously monitor your website's performance and experiment with different configurations. Stay informed about new optimization techniques and tools to keep your skills sharp and your website running smoothly. Remember, a well-performing website offers a better user experience, which can lead to increased user satisfaction and retention. Keep pushing the boundaries of your website's performance!

License: MIT
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue
Try Hetzner Cloud

Get 20€ free credit!

Valid until: 31 December 2024 Valid for: 3 months and only for new customers
Get started
Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more