From 44aa1b4aab4eff975325c390fbde884164a20256 Mon Sep 17 00:00:00 2001 From: Will Date: Sat, 7 Dec 2013 14:09:44 +0900 Subject: [PATCH] add a few example scripts --- Makefile | 2 +- README | 3 ++- scripts/auth.lua | 18 ++++++++++++++++++ scripts/counter.lua | 14 ++++++++++++++ scripts/post.lua | 6 ++++++ scripts/report.lua | 10 ++++++++++ {scripts => src}/wrk.lua | 0 7 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 scripts/auth.lua create mode 100644 scripts/counter.lua create mode 100644 scripts/post.lua create mode 100644 scripts/report.lua rename {scripts => src}/wrk.lua (100%) diff --git a/Makefile b/Makefile index b05982c..d1175e2 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ $(OBJ): config.h Makefile $(LDIR)/libluajit.a | $(ODIR) $(ODIR): @mkdir -p $@ -$(ODIR)/bytecode.o: scripts/wrk.lua +$(ODIR)/bytecode.o: src/wrk.lua @echo LUAJIT $< @$(SHELL) -c 'cd $(LDIR) && ./luajit -b $(CURDIR)/$< $(CURDIR)/$@' diff --git a/README b/README index f12c70b..f4c865f 100644 --- a/README +++ b/README @@ -5,7 +5,8 @@ wrk - a HTTP benchmarking tool design with scalable event notification systems such as epoll and kqueue. An optional LuaJIT script can perform HTTP request generation, response - processing, and custom reporting. + processing, and custom reporting. Several example scripts are located in + scripts/ Basic Usage diff --git a/scripts/auth.lua b/scripts/auth.lua new file mode 100644 index 0000000..f20b209 --- /dev/null +++ b/scripts/auth.lua @@ -0,0 +1,18 @@ +-- example script that demonstrates response handling and +-- retrieving an authentication token to set on all future +-- requests + +token = nil +path = "/authenticate" + +request = function() + return wrk.format("GET", path) +end + +response = function(status, headers, body) + if not token and status == 200 then + token = headers["X-Token"] + path = "/resource" + wrk.headers["X-Token"] = token + end +end diff --git a/scripts/counter.lua b/scripts/counter.lua new file mode 100644 index 0000000..5e79ce1 --- /dev/null +++ b/scripts/counter.lua @@ -0,0 +1,14 @@ +-- example dynamic request script which demonstrates changing +-- the request path and a header for each request +------------------------------------------------------------- +-- NOTE: each wrk thread has an independent Lua scripting +-- context and thus there will be one counter per thread + +counter = 0 + +request = function() + path = "/" .. counter + wrk.headers["X-Counter"] = counter + counter = counter + 1 + return wrk.format(nil, path) +end diff --git a/scripts/post.lua b/scripts/post.lua new file mode 100644 index 0000000..a9e1d53 --- /dev/null +++ b/scripts/post.lua @@ -0,0 +1,6 @@ +-- example HTTP POST script which demonstrates setting the +-- HTTP method, body, and adding a header + +wrk.method = "POST" +wrk.body = "foo=bar&baz=quux" +wrk.headers["Content-Type"] = "application/x-www-form-urlencoded" diff --git a/scripts/report.lua b/scripts/report.lua new file mode 100644 index 0000000..bf0d8c8 --- /dev/null +++ b/scripts/report.lua @@ -0,0 +1,10 @@ +-- example reporting script which demonstrates a custom +-- done() function that prints latency percentiles as CSV + +done = function(summary, latency, requests) + io.write("------------------------------\n") + for _, p in pairs({ 50, 90, 99, 99.999 }) do + n = latency:percentile(p) + io.write(string.format("%g%%,%d\n", p, n)) + end +end diff --git a/scripts/wrk.lua b/src/wrk.lua similarity index 100% rename from scripts/wrk.lua rename to src/wrk.lua