Load Testing For API With Apache Benchmark On Windows
Tags: testing, apache, apitesting, ab
Published: 2023-03-18 18:43:31


I recently had the need to carry out load testing on some endpoints that I created for a project on my Windows OS. Setting this up was part of a herculean task because most tutorials were done on an Ubuntu machine.

## What is Load Testing

Load Testing is a type of performance testing that gives you insight into how your web application or HTTP server would react when more demand is placed on it.

For this article, we will focus on testing an HTTP server using Apache Benchmark on a Windows OS.

## Requirements

- Vs-code
- Apache Benchmark

## Installing Apache Benchmark
The latest version of Apache Benchmark for windows can be installed [here](https://www.apachelounge.com/download/#google_vignette)

![Latest Binary](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2pid86g3qg9ztye2r6hu.jpg)





![Folder structure after unzipping](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qijy2qi4i7vdlbdsuocy.JPG)


Move into the `Apache24/bin` directory and copy both the `ab` and `abs` executable files to your project directory (optional).


![ab and abs exec files](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bl93ulh3j49hrjegfqf9.JPG)


![Current project structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5tajmji0n6kyzs2dnzo.JPG)

## Making a GET request to our server

Run the following command in your terminal, in the directory where your `ab` and `abs` executable files are located:

```
./ab -c 10 -n 20 http://localhost:8080/ 
```

- `-c` Concurrency
This indicates the number of multiple requests to make at a time. For this test, we are sending 10 requests to our server concurrently at the same time.



- `-n` Request
This indicates the number of requests to perform.

More of this flags and there descriptions can be found in the [Apache Benchmark site](https://httpd.apache.org/docs/2.4/programs/ab.html#:~:text=ab%20is%20a%20tool%20for,installation%20is%20capable%20of%20serving.)

For this test, below is our expected response.

```
this is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:
Server Hostname:        localhost
Server Port:            8080

Document Path:          /
Document Length:        23 bytes

Concurrency Level:      10
Time taken for tests:   0.033 seconds
Complete requests:      20
Failed requests:        0
Total transferred:      19240 bytes
HTML transferred:       460 bytes
Requests per second:    606.58 [#/sec] (mean)
Time per request:       16.486 [ms] (mean)
Time per request:       1.649 [ms] (mean, across all concurrent requests)
Transfer rate:          569.85 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0       1
Processing:     3   12   3.9     13      18
Waiting:        2   10   4.0     10      17
Total:          3   13   3.8     13      18

Percentage of the requests served within a certain time (ms)
  50%     13
  66%     15
  75%     16
  80%     17
  90%     17
  95%     18
  98%     18
  99%     18
 100%     18 (longest request)
```

This provides a detailed insight into how our server performs when 10 concurrent requests are sent. It shows the amount of time it takes to process a certain percentage of our requests and provides in-depth analysis of our server's performance.

## Making a POST request to our server using Apache Benchmark

When making a POST request that requires a request body, we need to pass the data to our benchmark request. This is done by creating a `.txt` file that should contain our request body data. The file should be in the same location as our `ab` and `abs` executable files.

For this test, I created a `post.txt` file that contains a JSON object representing our request body.

The request body should be in one line with no extra end-of-line spaces.

**TIP:** To place data in one line on VS Code, select the whole content and use this command `CTRL + SHIFT + J`.


![File containing our request body data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y57ex7nce2boflcr1om5.JPG)

Proceed to run the following line of command in your terminal.

```
./ab -p post.txt -T application/json  -c 5 -n 5 http://localhost:8080/v1/product
```

- `-p` Post File
This indicates the file containing data to POST.

- `-t` Content Type
Content-type header to use for POST/PUT data, eg. application/x-www-form-urlencoded. Default is text/plain.

To test a server that requires an authentication header with Apache benchmark, the following command can be used:

```
ab -p post.txt -T application/json -H 'Authorization: Token abcdbdhbuhfrhv' -c 10 -n 200 http://localhost:8080/v1/product
```

- `-H` Custom header  -                
This appends extra headers to the request. The argument is typically in the form of a valid header line, containing a colon-separated field-value pair (i.e., "Accept-Encoding: zip/zop;8bit").


> Note: Apache Bench only uses one OS thread irrespective of the concurrency level (specified by the -c flag). Therefore, when benchmarking high-capacity servers, a single instance of Apache Bench can be a bottleneck. To completely saturate the target URL, use additional instances of Apache Bench in parallel (if your server has multiple processor cores).

Don't forget to like, share and let me know if you found this helpful.