Originally posted on another blog in april 2016

I've just come across a nifty little tool called wrkwith it it's really easy to load test your webapp and see how much throughput it can handle from your laptop.

Writing tests on GET endpoints is as easy as:

wrk -t10 -c100 -d30s http://example.com

That was easy, right? And you can find that on the github page. But what if you want something more involved, like posting something? Well as it turns put wrk has scripting support in lua. It's available in brew and apt and surely some other package managers, unfourtunately it doesn't seem to work on windows, see wrk issue # 195.

I had to test a service that I was writing and we needed to post some json to a rest endpoint. It's relatively easy to write a script that will generate a post request:

wrk.body   = [[{
    "user": 1,
    "action": "insert",
    "amount": 10.2
}]]

wrk.headers["Content-Type"] = "application/json"
wrk.headers["Accept"] = "application/json"}

To post this you must pass in the script by using the -s param, like this:

wrk -t10 -c100 -d30s -s post.lua http://example.com/accounts/23

There's a whole lot more scripting that you can do, In our case we're using a messaging service that would load balance the incoming messages depending on the url. So we had to tweak the url for each request. What I did was to create a function request that wrkwill call automatically.

counter = 0

request = function ()
    path = "/accounts/" .. counter
    counter = counter + 1
    if counter>2560 then
        counter = 1
    end
    return wrk.format(nil, path)
end

As you can see that path is now provided, this means that in your terminal you'll no longer pass the whole url, only the host.

wrk -t10 -c100 -d30s -s post.lua http://example.com

I hope this post has been of som use to you!'