Linux Utilities : Load Testing Web Service using Apache AB Testing Tool

Linux fun image

Unit testing or Feature testing for bugs and load testing for performance check are two important factors that one must consider before deployment of any web service. Load testing enables you to gauge your setup capacity in terms of handling concurrent requests, response time when load is heavy. Hence it also enable you to know at what point you need to scale your system.

Apache Benchmarking tool (ab) is one such tool available for Linux to carry out load testing with configurable parameters like number of requests, concurrency, headers, cookies etc. We shall discuss about its usage, understand its output in this article.


INSTALLING AND USING APACHE AB.


Installing Apache Benchmarking

On debian based system (Ubuntu)

sudo apt-get update
sudo apt-get install apache2

For fedora based system

sudo dnf install apache2

Running apache benchmark

For this tutorial, I have sample web service install on my local web server on port 8081. For your self learning you can also try out other website urls like http://google.com etc.

A basic usable apache benchmark command would look like.

ab -n 1000 -c 100 localhost:8081/users

Output:

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:
Server Hostname:        localhost
Server Port:            8081

Document Path:          /users
Document Length:        12 bytes

Concurrency Level:      100
Time taken for tests:   0.172 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      113000 bytes
HTML transferred:       12000 bytes
Requests per second:    5815.31 [#/sec] (mean)
Time per request:       17.196 [ms] (mean)
Time per request:       0.172 [ms] (mean, across all concurrent requests)
Transfer rate:          641.73 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.1      0       6
Processing:     2   16   4.6     14      29
Waiting:        2   16   4.6     14      29
Total:          8   16   4.4     15      29

Percentage of the requests served within a certain time (ms)
  50%     15
  66%     15
  75%     15
  80%     18
  90%     27
  95%     28
  98%     29
  99%     29
 100%     29 (longest request)
								 

It lists out every detail for the requests like average request time. Total completed requests, failed requests, time division in terms of connecting, processing, waiting etc. The fields are pretty self explanatory. Lets dive into various configuration parameters for the ab testing.

-n and -c are for total number of request and concurrency value.
Note: n(total number of requests) should be equal or larger than c (concurrency). Example :
Test 1000 requests with concurrency of 100. Means test in total of 1000 request with making 100 calls simultaneously.
ab -n 1000 -c 100 localhost:8081/users

-t timelimit Seconds to max. to spend on benchmarking.
For Example perform 100000 with concurrency of 100 requests with time limit of 1 sec.

ab -n 100000 -c 100 -t 1 localhost:8081/users

This will perform the test for only 1 sec. If the requests are still remaining they will not be fired and also will not be marked as failed. Only the result related to served requests within 1 sec will be shown. As you can see in screenshot, it was only able to complete and hence shows results for only those requests.

Ab testing timelimit image

-C send cookies in the form of key value pair as ‘key=value’

ab -n 100000 -c 100 -t 1 -C 'auth_token=agt4rDTH44' localhost:8081/users

Ab testing timelimit image

-q : quite progress when doing test for more than 150 requests. By default, the ab testing will show progress. In case you are testing with huge number of requests (n), you may want to hide the progress.

ab -n 100000 -c 100 -t 1 -C 'token=agt4rDTH44' -q localhost:8081/users

Ab testing timelimit image

-l : Accept variable document length. (use this for dynamic pages) when doing test for more than 150 requests. By default, the ab testing will show progress. In case you are testing with huge number of requests (n), you may want to hide the progress.
For dynamic pages the response value may vary. By default if the response length of request varies than the previous one, it would mark as fail. In case where the response pay load is of variable field, (for example the fetching latest commentary of cricket.) you may want to use -l parameter.

ab -n 100000 -c 100 -t 1 -C 'token=agt4rDTH44' -l localhost:8081/commentary


-w : Print the result in html table form. This one is interesting. In case you want to add the benchmarking as report into your webpage, you can get auto generate table of test result.
For dynamic pages the response value may vary. By default if the response length of request varies than the previous one, it would mark as fail. In case where the response pay load is of variable field, (for example the fetching latest commentary of cricket.) you may want to use -l parameter.

ab -n 100000 -c 100 -t 1 -C 'token=agt4rDTH44' -q -w localhost:8081/usersabtesting

Ab testing timelimit image

-g filename Gnuplot data file. To plot and visualize graph of benchmark result, you can use -g filename parameter and plot using gnuplot.

ab -n 100000 -c 100 -g /tmp/data.txt -C 'token=agt4rDTH44' "localhost:8081/users"

Ab testing timelimit image

The graph for the above load testing is as follows:

Ab testing timelimit image
NOTE: You need to use gnuplot utility to generate this kind of graph. To quick configure gnuplot for apache benchmark you may want to refer to the site : GunPlot

Conclusion

Apache benchmark is a flexible load testing utility that helps you to carry out and the performance testing of your service or web app. It helps you to understand the time split for the requests being served and figure out the capacity of your setup. Explore more with man page or just type command ‘ab’ on terminal and it will print out self explanatory command line parameters. Feel free to comment below in case of any suggestions or questions.
That's all for now. Do leave suggestions, comments or questions in the comment section below.