Mastering Apache Benchmark Commands and Parameters
The Apache Benchmark tool (ab) is used to perform simple load testing on an HTTP web server. It provides insights into how many requests per second the server can handle, the average time taken for requests, and other performance metrics.
Basic Syntax – Mastering Apache Benchmark
ab [options] [http[s]://]hostname[:port]/path
Mastering Apache Benchmark
Basic Options
- -n requests: Specifies the total number of requests to perform for the benchmarking session. By default, ab will execute one request.
ab -n 1000 http://example.com/


- -c concurrency: Specifies the number of multiple requests to perform at a time. The default is one request at a time.
ab -n 1000 -c 50 http://example.com/


- -t timelimit: Specifies a timelimit for the benchmarking session. The implied number of requests (-n) is then defined by the number of requests ab can perform in the given time.
ab -t 60 http://example.com/
- -s timeout: Specifies a timeout in seconds for each request. Default is 30 seconds.
ab -s 120 -n 1000 http://example.com/
- -q: Reduces output. When present, only the result summary is shown.
ab -q -n 1000 -c 50 http://example.com/


Mastering Apache Benchmark
HTTP Method Options
- -m method: Specifies the HTTP method to use in the requests. The default is GET.
ab -n 1000 -c 50 -m POST http://example.com/
- -p postfile: Indicates the file containing data to POST. Must be used with -T.
ab -n 1000 -c 50 -p postdata.txt -T application/x-www-form-urlencoded http://example.com/
- -u putfile: Indicates the file containing data to PUT. Must be used with -T.
ab -n 1000 -c 50 -u putdata.txt -T application/x-www-form-urlencoded http://example.com/
- -T content-type: Specifies the content-type header for POST/PUT data. Default is text/plain.
ab -n 1000 -c 50 -p postdata.txt -T application/json http://example.com/
Mastering Apache Benchmark
Additional Options
- -v verbosity: Sets the verbosity level. Acceptable values are 1, 2, or 4. Default is 1.
ab -v 2 -n 1000 -c 50 http://example.com/
- -C cookie-name=value: Adds a cookie to the request.
ab -n 1000 -c 50 -C "sessionid=123456" http://example.com/


- -H header: Adds an arbitrary header line. You can use this option multiple times to add multiple headers.
ab -n 1000 -c 50 -H "Authorization: Bearer token" http://example.com/
- -A auth-username:password: Adds basic authentication credentials.
ab -n 1000 -c 50 -A admin:password http://example.com/
- -P proxy-auth-username:password: Adds proxy authentication credentials.
ab -n 1000 -c 50 -P proxyuser:password http://example.com/
- -X proxy:port: Specifies a proxy server and port to send requests through.
ab -n 1000 -c 50 -X proxy.example.com:8080 http://example.com/
- -k: Enables HTTP KeepAlive, sending multiple requests over a single connection.
ab -n 1000 -c 50 -k http://example.com/
- -g gnuplot-file: Writes all measured values to a gnuplot file, which can be used for further analysis or graphing.
ab -n 1000 -c 50 -g output.gnuplot http://example.com/
- -e csv-file: Outputs results in CSV format.
ab -n 1000 -c 50 -e output.csv http://example.com/
Example Commands
Basic Load Test
Perform 1000 requests to the specified URL with a concurrency of 50.
ab -n 1000 -c 50 http://example.com/


POST Request with Data from File
Send 500 POST requests with data from a file.
ab -n 500 -c 10 -p postdata.txt -T application/x-www-form-urlencoded http://example.com/api
Benchmarking with HTTP Authentication
Perform 200 requests using basic authentication.
ab -n 200 -c 20 -A admin:password http://example.com/secure
Using a Proxy Server
Benchmark through a proxy server at proxy.example.com:8080.
ab -n 1000 -c 50 -X proxy.example.com:8080 http://example.com/
Output Results to a CSV File
Generate a CSV file with the results of 1000 requests.
ab -n 1000 -c 50 -e results.csv http://example.com/


Summary – Mastering Apache Benchmark
ab (Apache Benchmark) is a powerful and straightforward tool for performing load testing on HTTP servers. It offers various options to customize requests, simulate multiple users, and analyze server performance under load. Understanding and utilizing the available commands and parameters can help you conduct effective performance testing and identify potential bottlenecks in your web applications.
Useful Links
https://httpd.apache.org/docs/current/programs/ab.html
https://sanchitgurukul.com/tutorials-cat
Mastering Apache Benchmark (ab) for Effective Load Testing
This article provided insights on the topic. For latest updates and detailed guides, stay connected with Sanchit Gurukul.
