How to use the command wrk (with examples)
The wrk
command is an HTTP benchmarking tool that allows you to measure the performance of a web server by making requests and measuring the response time. It is commonly used to simulate high load scenarios and test the scalability of web applications.
Use case 1: Run a benchmark for 30 seconds, using 12 threads, and keeping 400 HTTP connections open
Code:
wrk -t12 -c400 -d30s "http://127.0.0.1:8080/index.html"
Motivation: This use case is useful for testing the performance and scalability of a web server under heavy load. By specifying 12 threads and 400 connections, we can simulate a high number of concurrent requests to the server.
Explanation:
-t12
: Specifies the number of threads to use. In this case, we are using 12 threads.-c400
: Sets the number of HTTP connections to keep open. Here, we are keeping 400 connections open.-d30s
: Specifies the duration of the benchmark in seconds. In this case, the benchmark will run for 30 seconds."http://127.0.0.1:8080/index.html"
: The URL to benchmark. Here, we are benchmarking theindex.html
page on the local server.
Example output:
Running 30s test @ http://127.0.0.1:8080/index.html
12 threads and 400 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 12.22ms 5.85ms 75.52ms 85.00%
Req/Sec 36.93k 11.74k 50.43k 84.67%
13355782 requests in 30.10s, 2.27GB read
Requests/sec: 443575.44
Transfer/sec: 76.02MB
This output provides information about the average latency, the number of requests per second, and the amount of data transferred during the benchmark.
Use case 2: Run a benchmark with a custom header
Code:
wrk -t2 -c5 -d5s -H "Host: example.com" "http://example.com/index.html"
Motivation: Sometimes, it is necessary to send custom headers in the request. This use case demonstrates how to add a custom header while benchmarking a web server.
Explanation:
-H "Host: example.com"
: Specifies the custom header to add to the request. In this case, we are adding theHost: example.com
header.- The remaining arguments (
-t2
,-c5
,-d5s
,"http://example.com/index.html"
) have the same meaning as in the previous use case.
Example output:
Running 5s test @ http://example.com/index.html
2 threads and 5 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 43.16ms 17.91ms 179.21ms 64.84%
Req/Sec 60.99 19.47 81.00 76.00%
610 requests in 5.17s, 60.32KB read
Requests/sec: 118.07
Transfer/sec: 11.66KB
The output displays information about the latency, requests per second, and the amount of data transferred during the benchmark.
Use case 3: Run a benchmark with a request timeout of 2 seconds
Code:
wrk -t2 -c5 -d5s --timeout 2s "http://example.com/index.html"
Motivation:
In some scenarios, it is important to define a timeout for the requests made during the benchmark. This use case illustrates how to set a request timeout using the wrk
command.
Explanation:
--timeout 2s
: Sets the request timeout to 2 seconds. If a request takes longer than 2 seconds to complete, it will be considered as a failed request.- The remaining arguments (
-t2
,-c5
,-d5s
,"http://example.com/index.html"
) have the same meaning as in the previous use cases.
Example output:
Running 5s test @ http://example.com/index.html
2 threads and 5 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 24.11ms 87.57ms 1.55s 97.56%
Req/Sec 801.60 868.41 3.00k 65.83%
3587 requests in 5.08s, 781.20KB read
Socket errors: connect 0, read 0, write 0, timeout 48
Requests/sec: 705.75
Transfer/sec: 154.78KB
The output provides statistics about the latency, requests per second, the amount of data transferred, and any socket errors that occurred during the benchmark.
Conclusion:
The wrk
command is a powerful tool for benchmarking the performance of web servers. It allows users to simulate high load scenarios, measure response times, and evaluate the scalability of web applications. By using the provided examples, you can effectively test the performance of your own web servers and identify any potential bottlenecks in your application.