Apache Bench or AB is a great tool to help simulate traffic to your website or page(s).
The tool can be installed on your Linux Server or Linux VPS by logging into your server and installed via the following command.
yum -y install httpd-tools
Installing Apache Bench – HTTPD Tools
I already have this installed but you can see by the above output the install works.
Once installed, the tool is pretty straight forward in using.
- Set your target domain/url/page/file
- Set the load count (example 1000 page loads)
- Set the cuncurrent connections
- And Go!
In the following example I will use a simple test on a 1GB Linux VPS I have setup to test this.
[root@fabric ~]# ab -n 1000 -c 200 http://192.168.1.102/banners/320×50.jpg
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.1.102 (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: nginx/1.16.1
Server Hostname: 192.168.1.102
Server Port: 80
Document Path: /banners/320×50.jpg
Document Length: 34110 bytes
Concurrency Level: 200
Time taken for tests: 1.918 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 34347000 bytes
HTML transferred: 34110000 bytes
Requests per second: 521.46 #/sec
Time per request: 383.539 ms
Time per request: 1.918 [ms] (mean, across all concurrent requests)
Transfer rate: 17490.80 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 41 53 9.7 50 97
Processing: 124 246 134.7 219 1202
Waiting: 42 113 130.0 94 1098
Total: 165 299 132.2 273 1244
Percentage of the requests served within a certain time (ms)
100% 1244 (longest request)
As you can see by the above output the test linux vps was able to handle without any problems 521 Requests per second when there was a concurrency rate of 200 users.
Because ab supports concurrency, this has several advantages the main one is that it allows you to test how your code runs concurrently, this can help you identify any possible race conditions, or locking issues. Concurrent requests are also a more natural simulation of load than loops.
Suppose you wanted to test multiple url’s concurrently as well? You can do this by creating a shell script, with multiple ab calls. At the end of each line place an
& this makes the command run in the background, and lets the next command start execution. You will also want to redirect the output to a file for each url using
> filename For example:
#!/bin/sh ab -n 100 -c 10 http://192.168.1.102/banner/test.jpg > test1.txt & ab -n 100 -c 10 http://192.168.1.102/banner/test2.jpg > test1.txt &
This can be played with by changing the following line.
ab -n 1000 -c 200 http://192.168.1.102/banners/320×50.jpg ( This sends 200 users for a total of 1000 requests )
ab -n 10000 -c 50 http://192.168.1.102/banners/320×50.jpg ( This sends 50 users for a total of 10000 requests )
Here is some more options and examples of using the Apache Bench tool.
The tool Apache Bench is a very powerful tool that allows you to help simulate large or small amounts of traffic to your site. This is also a powerful tool to test your script load times, mysql connections, image server, load balancers and more!