git clone --depth=1 https://github.com/wg/wrk cd wrk make ln -sf /root/tools/wrk/wrk /usr/local/bin/wrk
查看下版本以及命令帮助信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ wrk -v wrk 4.1.0 [epoll] Copyright (C) 2012 Will Glozer 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 --latency Print latency statistics # 加上此选项可输出响应时间的分布情况 --timeout <T> Socket/request timeout # wrk 默认超时时间是1秒,用此参数设置超时时间 -v, --version Print version details Numeric arguments may include a SI unit (1k, 1M, 1G) Time arguments may include a time unit (2s, 2m, 2h)
functionresponse(status, headers, body) ifstatus ~= 200then print(body) wrk.thread:stop() end end
done 在所有请求执行完以后调用, 一般用于自定义统计结果.
1 2 3 4 5 6 7
done = function(summary, latency, requests) io.write("------------------------------\n") for _, p inpairs({ 50, 90, 99, 99.999 }) do n = latency:percentile(p) io.write(string.format("%g%%,%d\n", p, n)) end end
local counter = 1 local threads = {} functionsetup(thread) thread:set("id", counter) table.insert(threads, thread) counter = counter + 1 end functioninit(args) requests = 0 responses = 0 local msg = "thread %d created" print(msg:format(id)) end functionrequest() requests = requests + 1 return wrk.request() end functionresponse(status, headers, body) responses = responses + 1 end functiondone(summary, latency, requests) for index, thread inipairs(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
counter = 1 math.randomseed(os.time()) math.random(); math.random(); math.random() functionfile_exists(file) local f = io.open(file, "rb") if f then f:close() end return f ~= nil end functionshuffle(paths) local j, k local n = #paths for i = 1, n do j, k = math.random(n), math.random(n) paths[j], paths[k] = paths[k], paths[j] end return paths end functionnon_empty_lines_from(file) ifnot file_exists(file) thenreturn {} end lines = {} for line inio.lines(file) do ifnot (line == '') then lines[#lines + 1] = line end end return shuffle(lines) end paths = non_empty_lines_from("paths.txt") if #paths <= 0then print("multiplepaths: No paths found. You have to create a file paths.txt with one path per line") os.exit() end print("multiplepaths: Found " .. #paths .. " paths") request = function() path = paths[counter] counter = counter + 1 if counter > #paths then counter = 1 end return wrk.format(nil, path) end