wrk
wrk is a command-line HTTP benchmarking tool. It is particularly useful for evaluating the performance of web servers and APIs under high load. wrk uses a multi-threaded, event-driven architecture to generate significant load with minimal resource usage, making it an excellent tool for both quick spot checks and more in-depth performance analysis.
Key Features
- Multi-threaded: Uses multiple threads to simulate concurrent users.
- Event-driven: Leverages an event-driven architecture for efficiency.
- Lua scripting: Allows extending functionality with Lua scripts for custom request generation and response analysis.
- Simple command-line interface: Easy to use with a minimal set of options.
- Low resource consumption: Can generate high load with relatively low CPU and memory usage.
Use Cases
- Web Server Performance Testing: Evaluating the performance of web servers like Nginx, Apache, or Node.js.
- API Benchmarking: Measuring the performance and scalability of RESTful APIs.
- Load Testing: Simulating a large number of concurrent users to assess system behavior under load.
- Spot Checks: Quickly assessing the impact of code changes or configuration tweaks on performance.
- Regression Testing: Ensuring that new releases do not degrade performance.
Installation
Linux (Debian/Ubuntu)
sudo apt-get update
sudo apt-get install -y build-essential libssl-dev
git clone https://github.com/wg/wrk.git
cd wrk
make
sudo cp wrk /usr/local/bin/
macOS
Using Homebrew:
brew install wrk
Building from Source
If pre-built packages are not available, you can build wrk from source:
- Clone the repository:
git clone https://github.com/wg/wrk.git - Navigate to the
wrkdirectory:cd wrk - Compile:
make - Install (optional):
sudo cp wrk /usr/local/bin/ # Copy the executable to a directory in your PATH
Basic Usage
The basic syntax for running a test is:
wrk <options> <target_url>
Commonly Used Options
-t <number>: Number of threads to use.-c <number>: Number of connections to keep open.-d <duration>: Duration of the test (e.g.,10s,1m,2h).-s <script>: Lua script to use.-H <header>: Add a header to the HTTP request.-v: Verbose output.
Example
To send requests to http://localhost:8080/ using 12 threads and keeping 400 connections open for 10 seconds, run:
wrk -t12 -c400 -d10s http://localhost:8080/
Lua Scripting
wrk allows you to extend its functionality using Lua scripts. You can customize request generation and response analysis.
Sample Lua Script
-- setup function (called once before the test starts)
function setup(thread)
-- print("Setup function called from thread " .. thread:number())
end
-- Request function (called for each request)
request = function()
return wrk.format(
"GET", -- method
"/", -- path
{ ["Content-Type"] = "text/plain" }, -- headers
"Hello, World!" -- body
)
end
-- Response function (called for each response)
response = function(status, headers, body)
if status ~= 200 then
print("Unexpected status code: " .. status)
end
end
-- Done function (called once after the test ends)
function done(summary, latency, requests)
-- print("Done function for " .. summary.threads)
end
Save this script to a file (e.g., test.lua) and run wrk with the -s option:
wrk -t12 -c400 -d10s -s test.lua http://localhost:8080/
Lua API
wrk.format(method, path, headers, body): Formats an HTTP request.wrk.body: The response body.wrk.headers: The response headers.wrk.status: The HTTP status code.wrk.latency: The request latency.wrk.threads: The number of threads.
Interpreting Results
wrk provides the following metrics:
- Requests/sec: The number of requests completed per second.
- Transfer/sec: The amount of data transferred per second.
- Latency: The time taken to complete requests, including mean, stdev, max, and percentiles.