【转】wrk 压力测试的 lua脚本
转载:https://blog.csdn.net/qq_32373277/article/details/89453681
用 lua脚本同时GET,POST
压测
category.lua 脚本如下:
urimap = { "/v3/goods/detail", "/v3/goods/status", "/v3/goods/sku_detail", "/v3/coupons/lists", "/v3/pages/recommend", "/v3/carts", } methodmap = { "POST", "POST", "POST", "GET", "GET", "GET", } params = { [[{"id":2}]], [[{"spu_id":2,"type":1}]], [[{"id":2,"sku_id":7}]], -- 双中括号里面不转译 "page=1&size=100", "", "", } math.randomseed(os.time()) init = function() local r = {} local path = "" -- 局部变量(不加local 是全局变量) local method = "get" -- 默认get -- header 头 wrk.headers["Hash"]= "85280aa135bbd0108dd6aa424565a" wrk.headers["Token"]= "" for i, v in ipairs(urimap) do -- 键从1 开始 非 0 path = v -- 路径 method = methodmap[i] -- method if method == "POST" then wrk.headers["content-type"]= "application/json" --POST 参数json格式 wrk.body = params[i] end if method == "GET" and params[i] ~= "" then path = v .. "?" ..params[i] end io.write(method, "---", params[i], "----", path, "\n") -- 打印请求方式(1个线程会打印一次),参数,路径(不含域名) r[i] = wrk.format(method, path) end req = table.concat(r) end request = function() return req end response = function(status, headers, body) if status ~= 200 then print("status:", status) print("error:", body) wrk.thread:stop() else -- print("body:", body) end end done = function(summary, latency, requests) local durations=summary.duration / 1000000 -- 执行时间,单位是秒 local errors=summary.errors.status -- http status不是200,300开头的 local requests=summary.requests -- 总的请求数 local valid=requests-errors -- 有效请求数=总请求数-error请求数 io.write("Durations: "..string.format("%.2f",durations).."s".."\n") io.write("Requests: "..summary.requests.."\n") io.write("Avg RT: "..string.format("%.2f",latency.mean / 1000).."ms".."\n") io.write("Max RT: "..(latency.max / 1000).."ms".."\n") io.write("Min RT: "..(latency.min / 1000).."ms".."\n") io.write("Error requests: "..errors.."\n") io.write("Valid requests: "..valid.."\n") io.write("QPS: "..string.format("%.2f",valid / durations).."\n") io.write("--------------------------\n") end
脚本就这么完事了,开始压测吧!
wrk -t1 -c1000 -d30s -T10s -s category.lua --latency https://shop-api-crs.chi.com -t 开启多少线程 -c 连接数量 -T 超时时间(timeout),支持时间单位 (s, m, h) -s Lua脚本路径 --latency 压测结束完,打印统计信息 返回: Running 15s test @ https://shop-api-crs.chi.com 1 threads and 10 connections. Thread Stats Avg Stdev Max +/- Stdev Latency 124.50ms 173.08ms 1.11s 47.38% Req/Sec 252.53 111.40 363.00 79.86% Latency Distribution 50% 381.93ms 75% 0.00us 90% 0.00us 99% 0.00us 3674 requests in 15.08s, 2.51MB read --(15.08秒内共处理完成了 3674个请求,读取了2.51MB数据) Requests/sec: 243.56 -- (平均每秒处理完成请求) Transfer/sec: 170.29KB -- (平均每秒读取数据) Durations: 15.08s -- 执行时间,单位是秒 Requests: 3674 -- 总的请求数 Avg RT: 124.50ms --平均值 Max RT: 1110.714ms -- 最大值 Min RT: 78.324ms -- 最小值 Error requests: 0 -- Error请求数 Valid requests: 3674 -- 有效请求数 QPS: 243.56 --QPS
转载:https://blog.csdn.net/qq_32373277/article/details/89453681
转载:https://blog.csdn.net/qq_32373277/article/details/89453681