How to use the command 'ab' (with examples)
The ‘ab’ command is the Apache HTTP server benchmarking tool, used to test the performance of HTTP servers by simulating a high number of concurrent requests. It is a useful tool for developers and system administrators to understand the capabilities and limitations of their web servers.
Use case 1: Execute 100 HTTP GET requests to a given URL
Code:
ab -n 100 url
Motivation: This use case allows us to send a specific number of HTTP GET requests to a given URL. By specifying the number of requests, we can evaluate the server’s performance under a predetermined load.
Explanation:
-n 100
: Specifies the number of requests to be made (100 in this case).url
: The URL to which the requests will be sent.
Example output:
Server Software: Apache
Server Hostname: example.com
Server Port: 80
Document Path: /
Document Length: 1234 bytes
Concurrency Level: 1
Time taken for tests: 0.267 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 123400 bytes
HTML transferred: 123400 bytes
Requests per second: 374.53 [#/sec] (mean)
Time per request: 2.674 [ms] (mean)
Transfer rate: 453.86 [Kbytes/sec] received
Connection Times (ms)
min avg max
Connect: 0 0 0
Processing: 1 2 4
Total: 1 2 4
Use case 2: Execute 100 HTTP GET requests, in concurrent batches of 10, to a URL
Code:
ab -n 100 -c 10 url
Motivation: This use case allows us to send a specific number of concurrent HTTP GET requests to a given URL. By specifying the concurrency level, we can evaluate the server’s performance under simultaneous requests.
Explanation:
-n 100
: Specifies the number of requests to be made (100 in this case).-c 10
: Specifies the concurrency level, indicating the number of concurrent requests to be made (10 in this case).url
: The URL to which the requests will be sent.
Example output:
Server Software: Apache
Server Hostname: example.com
Server Port: 80
Document Path: /
Document Length: 1234 bytes
Concurrency Level: 10
Time taken for tests: 2.014 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 123400 bytes
HTML transferred: 123400 bytes
Requests per second: 41.70 [#/sec] (mean)
Time per request: 201.363 [ms] (mean)
Transfer rate: 59.84 [Kbytes/sec] received
Connection Times (ms)
min avg max
Connect: 0 0 1
Processing: 14 66 101
Total: 14 66 101
Use case 3: Execute 100 HTTP POST requests to a URL, using a JSON payload from a file
Code:
ab -n 100 -T application/json -p path/to/file.json url
Motivation: This use case allows us to send a specific number of HTTP POST requests to a given URL, using a JSON payload from a file. By specifying the payload and its format, we can test the server’s handling of POST requests.
Explanation:
-n 100
: Specifies the number of requests to be made (100 in this case).-T application/json
: Specifies the content type of the payload as JSON.-p path/to/file.json
: Specifies the path to the JSON file containing the payload.url
: The URL to which the requests will be sent.
Example output:
Server Software: Apache
Server Hostname: example.com
Server Port: 80
Document Path: /
Document Length: 1234 bytes
Concurrency Level: 1
Time taken for tests: 0.723 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 124100 bytes
HTML transferred: 123400 bytes
Requests per second: 138.35 [#/sec] (mean)
Time per request: 7.226 [ms] (mean)
Transfer rate: 167.30 [Kbytes/sec] received
Connection Times (ms)
min avg max
Connect: 1 3 4
Processing: 2 4 8
Total: 2 7 13
Use case 4: Use HTTP keep-Alive, i.e. perform multiple requests within one HTTP session
Code:
ab -k url
Motivation: This use case allows us to execute multiple requests within a single HTTP session using the keep-alive feature. This can provide insights into the efficiency of server connection and resource utilization.
Explanation:
-k
: Enables HTTP keep-alive, allowing multiple requests to be made within a single session.url
: The URL to which the requests will be sent.
Example output:
Server Software: Apache
Server Hostname: example.com
Server Port: 80
Document Path: /
Document Length: 1234 bytes
Concurrency Level: 1
Time taken for tests: 0.705 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 124100 bytes
HTML transferred: 123400 bytes
Requests per second: 141.84 [#/sec] (mean)
Time per request: 7.051 [ms] (mean)
Transfer rate: 171.78 [Kbytes/sec] received
Connection Times (ms)
min avg max
Connect: 1 2 3
Processing: 3 5 12
Total: 4 7 15
Use case 5: Set the maximum number of seconds (timeout) to spend for benchmarking
Code:
ab -t 60 url
Motivation: This use case allows us to specify the maximum amount of time for benchmarking. By setting a timeout, we can limit the duration of the benchmarking process.
Explanation:
-t 60
: Sets the maximum number of seconds (60 in this case) for benchmarking.url
: The URL to which the requests will be sent.
Example output:
Server Software: Apache
Server Hostname: example.com
Server Port: 80
Document Path: /
Document Length: 1234 bytes
Concurrency Level: 1
Time taken for tests: 0.330 seconds
Complete requests: 52
Failed requests: 0
Total transferred: 64328 bytes
HTML transferred: 64168 bytes
Requests per second: 157.84 [#/sec] (mean)
Time per request: 6.355 [ms] (mean)
Transfer rate: 190.48 [Kbytes/sec] received
Connection Times (ms)
min avg max
Connect: 1 2 3
Processing: 2 4 8
Total: 3 6 11
Conclusion:
The ‘ab’ command is a powerful tool for testing and benchmarking the performance of HTTP servers. By using different options and parameters, it allows us to simulate various scenarios and evaluate the server’s capabilities. Whether it’s evaluating the server’s handling of GET or POST requests, testing concurrency levels, or setting a timeout, the ‘ab’ command provides valuable insights into server performance under different conditions.