Simple Load Testing for Web Servers Using wrk and Bash Automation
This guide provides a quick way to perform load testing on your web server using wrk
. The setup includes a Lua script for customizing requests and a Bash script to automate the test.
Lua Script for Custom Requests
Save this as script.lua
:
init = function(args)
math.randomseed(os.time() + wrk.thread:get('id') + tonumber(tostring({}):sub(8)))
end
local thread_counter = 0
setup = function(thread)
thread_counter = thread_counter + 1
thread:set('id', thread_counter)
end
request = function()
local random_number = math.random(10000000, 99999999)
local email = "testuser-" .. random_number .. "@test.com"
local body = string.format('{"email": "%s", "password": "testpassword"}', email)
wrk.method = "POST"
wrk.headers["Content-Type"] = "application/json"
wrk.headers["Authorization"] = "ApiKey 5a13aa909cae619c604b5635a6f7c397cfb5a3b9ef6f976baa5df050262b1744"
wrk.body = body
return wrk.format(nil, "/auth/register")
end
This script creates a dynamic request with random email values for load testing.
Bash Script for Automated Load Testing
Save this as load_test.sh
, adjusting the parameters as needed:
#!/bin/bash
BASE_URL="http://localhost:8080"
DURATION="10s"
THREADS=4
INITIAL_CONNECTIONS=50
MAX_CONNECTIONS=500
STEP=50
SCRIPT="script.lua"
OUTPUT_DIR="load_results"
mkdir -p $OUTPUT_DIR
for (( CONNS=$INITIAL_CONNECTIONS; CONNS<=$MAX_CONNECTIONS; CONNS+=$STEP ))
do
echo "Running test with $CONNS connections..."
wrk -t$THREADS -c$CONNS -d$DURATION -s $SCRIPT $BASE_URL > $OUTPUT_DIR/results_$CONNS.txt
echo "Results saved to load_results/results_$CONNS.txt"
done
This script runs wrk
in increments of connections and saves the results to separate files.
Explanation
The wrk
command will be executed multiple times by the load_test.sh
script, adjusting the number of connections (-c
) for each iteration. Here’s an example of what a single execution might look like when run from the script:
wrk -t4 -c50 -d10s -s script.lua http://localhost:8080
Breakdown of Parameters:
-t4
: Uses 4 threads.-c50
: Simulates 50 concurrent connections.-d10s
: Runs the test for 10 seconds.-s script.lua
: Uses the Lua scriptscript.lua
to define the request structure.http://localhost:8080
: The base URL of the server under test.
The script will increment the -c
parameter (e.g., 50, 100, 150, etc.) during subsequent executions and save the results of each test in the load_results
directory as results_X.txt
, where X
is the number of connections.
Example Output File Name:
For a test with 50 connections, the results will be saved as:
load_results/results_50.txt
This file will contain detailed statistics about latency, requests per second, and errors (if any).
Running the Test
- Make the scripts executable:
chmod +x script.lua load_test.sh
- Execute the load test:
./load_test.sh
The results will be saved in the load_results
directory.
Summary
This setup allows you to quickly test your server under varying loads. You can adjust parameters such as the duration, number of threads, and connection increments to match your requirements. The results can be used to analyze server performance.