Mastering wrk: Install, Basic Use, and Advanced Lua Scripting for HTTP Load Testing
This guide walks you through installing the wrk load‑testing tool on Unix‑like systems, shows its basic command‑line usage with example output, and explains how to extend tests with Lua scripts for POST requests, dynamic parameters, request delays, authentication, and HTTP pipelining.
Installation
wrk runs on most Unix‑like operating systems (Windows is not supported) and requires LuaJIT and OpenSSL. Install it by cloning the source from GitHub and running
makein the project directory. The resulting
wrkbinary can be copied to a directory in
/usr/local/binfor easy access.
<code>git clone https://github.com/wg/wrk
make</code>If you prefer to use system‑installed LuaJIT and OpenSSL, compile with
WITH_LUAJITand
WITH_OPENSSLoptions, e.g.:
<code>make WITH_LUAJIT=/usr WITH_OPENSSL=/usr</code>Basic Usage
Running
wrkwithout arguments displays the help message. A typical test command looks like:
<code>wrk -t8 -c200 -d30s --latency "http://www.bing.com"</code>The output includes thread statistics, latency distribution, total requests, and throughput. For the example above, 8 threads and 200 connections were used for a 30‑second test against Bing, yielding about 59,658 requests per second.
Lua Script Customization
wrk allows Lua scripts to customize the load test in three phases: setup (executed once per thread before the test starts), init/request/delay/response (executed during the test), and done (executed once after the test finishes). Each thread gets its own Lua environment.
Setup Phase
<code>function setup(thread)
-- optional per‑thread initialization
end</code>Running Phase
<code>function init(args) end
function delay() end
function request()
return wrk.format(nil, "/path")
end
function response(status, headers, body) end</code>Done Phase
<code>function done(summary, latency, requests)
-- generate custom report
end</code>The global
wrktable provides fields such as
scheme,
host,
port,
method,
path,
headers, and
body, which can be modified to affect all requests.
Example Scripts
POST request: Set
wrk.method = "POST", define
wrk.body, and add a
Content‑Typeheader.
Dynamic URL parameter: In
request(), generate a random
uidand build the path
"/test?uid=" .. uid.
Fixed delay: Return
10from
delay()to pause 10 ms before each request.
Token‑based authentication: Use an initial
GET /authenticaterequest, capture the
X‑Tokenheader, then switch to
/resourcewith the token added to subsequent request headers.
HTTP pipelining: In
init(), concatenate multiple formatted requests (e.g.,
/?foo,
/?bar,
/?baz) into a single string returned by
request().
These examples demonstrate how wrk’s Lua API can be leveraged to perform more realistic and complex load‑testing scenarios.
Conclusion
wrk is a lightweight, high‑performance HTTP benchmarking tool that, combined with Lua scripting, offers great flexibility for simulating real‑world traffic patterns.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.