mirror of
https://github.com/wg/wrk
synced 2026-05-17 03:02:32 +08:00
add script setup() and thread methods
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
-- example script that demonstrates use of setup() to pass
|
||||
-- a random server address to each thread
|
||||
|
||||
local addrs = nil
|
||||
|
||||
function setup(thread)
|
||||
if not addrs then
|
||||
addrs = wrk.lookup(wrk.host, wrk.port or "http")
|
||||
for i = #addrs, 1, -1 do
|
||||
if not wrk.connect(addrs[i]) then
|
||||
table.remove(addrs, i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
thread.addr = addrs[math.random(#addrs)]
|
||||
end
|
||||
|
||||
function init(args)
|
||||
local msg = "thread addr: %s"
|
||||
print(msg:format(wrk.thread.addr))
|
||||
end
|
||||
@@ -0,0 +1,38 @@
|
||||
-- example script that demonstrates use of setup() to pass
|
||||
-- data to and from the threads
|
||||
|
||||
local counter = 1
|
||||
local threads = {}
|
||||
|
||||
function setup(thread)
|
||||
thread:set("id", counter)
|
||||
table.insert(threads, thread)
|
||||
counter = counter + 1
|
||||
end
|
||||
|
||||
function init(args)
|
||||
requests = 0
|
||||
responses = 0
|
||||
|
||||
local msg = "thread %d created"
|
||||
print(msg:format(id))
|
||||
end
|
||||
|
||||
function request()
|
||||
requests = requests + 1
|
||||
return wrk.request()
|
||||
end
|
||||
|
||||
function response(status, headers, body)
|
||||
responses = responses + 1
|
||||
end
|
||||
|
||||
function done(summary, latency, requests)
|
||||
for index, thread in ipairs(threads) do
|
||||
local id = thread:get("id")
|
||||
local requests = thread:get("requests")
|
||||
local responses = thread:get("responses")
|
||||
local msg = "thread %d made %d requests and got %d responses"
|
||||
print(msg:format(id, requests, responses))
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
-- example script that demonstrates use of thread:stop()
|
||||
|
||||
local counter = 1
|
||||
|
||||
function response()
|
||||
if counter == 100 then
|
||||
wrk.thread:stop()
|
||||
end
|
||||
counter = counter + 1
|
||||
end
|
||||
Reference in New Issue
Block a user