Skip to content

Instantly share code, notes, and snippets.

@josecoelho
Last active February 24, 2023 18:34
Show Gist options
  • Save josecoelho/a288aa6d8d387253e40185038e7261d0 to your computer and use it in GitHub Desktop.
Save josecoelho/a288aa6d8d387253e40185038e7261d0 to your computer and use it in GitHub Desktop.
Load testing using WRK2 + LUA + Redis

Lua 5.1 installed already

Installing luarocks

# yum install lua-devel
# wget http://luarocks.org/releases/luarocks-2.3.3.tar.gz
# tar -xzvf luarocks-2.2.1.tar.gz
# cd luarocks-2.2.1
# ./configure
# make bootstrap

Add luarocks to your .bash_profile eval $(luarocks path --bin)

Installing redis -lua $ luarocks install redis-lua --local

Installing wrk2

$ yum install openssl-devel
$ wget https://github.com/giltene/wrk2/archive/master.zip
$  unzip master.zip
$  cd wrk2-master/
$  make
[user@server wrk2-master]$ ./wrk
Usage: wrk <options> <url>
  Options:
    -c, --connections <N>  Connections to keep open
    -d, --duration    <T>  Duration of test
    -t, --threads     <N>  Number of threads to use

    -s, --script      <S>  Load Lua script file
    -H, --header      <H>  Add header to request
    -L  --latency          Print latency statistics
    -U  --u_latency        Print uncorrected latency statistics
        --timeout     <T>  Socket/request timeout
    -B, --batch_latency    Measure latency of whole
                           batches of pipelined ops
                           (as opposed to each op)
    -v, --version          Print version details
    -R, --rate        <T>  work rate (throughput)
                           in requests/sec (total)
                           [Required Parameter]


  Numeric arguments may include a SI unit (1k, 1M, 1G)
  Time arguments may include a time unit (2s, 2m, 2h)
wrk.method = "POST"
wrk.body = "{\"field\":\"value\"}"
wrk.headers["Content-Type"] = "application/json"
local redis = require "redis"
local client = redis.connect("127.0.0.1", 6379)
local redis_key = "lua:test"
init = function()
-- connect to redis
for i=10,1,-1 do client:sadd(redis_key, i) end
end
request = function()
-- build request using redis data
--
local value = client:srandmember(redis_key)
local path = string.format("/?value=%s", value)
print(path)
return wrk.format("GET", path)
end
  1. Clone and install wrk2 git clone git@github.com:giltene/wrk2.git cd wrk2; make;

  2. Install lua 5.1 and redis-lua brew install lua51 Reload your term and them: luarocks-5.1 install redis-lua

  3. Create your script lua (check file wrk-example-of-data-from-redis.lua)

  4. run wrk wrk2/wrk -s wrk-example-of-data-from-redis.lua -R10 -d1s http://localhost:9292

  5. Result Example

$ wrk2/wrk -s wrk-update-email.lua -R10 -d1s http://localhost:9292
/?value=7
/?value=7
/?value=4
/?value=2
/?value=10
/?value=5
Running 1s test @ http://localhost:9292
  2 threads and 10 connections
/?value=1
/?value=4
/?value=9
/?value=2
/?value=8
/?value=4
/?value=1
/?value=4
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    22.63ms    9.59ms  43.26ms   70.00%
    Req/Sec        nan       nan   0.00      0.00%
  12 requests in 1.01s, 2.29KB read
Requests/sec:     11.86
Transfer/sec:      2.26KB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment