【轉】wrk 壓力測試的 lua指令碼
阿新 • • 發佈:2021-10-11
轉載: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 是全域性變數) localmethod = "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