1、get請求
(1)、wrk -t 12 -c 100 -d 5s -T 30s --latency '接口的路徑'
(2)、wrk參數意義
t:需要模擬的線程數量(線程數不宜過多,核數的2到4倍足夠了。 多了反而因為線程切換過多造成效率降低)
d:時間 單位可以是s(秒),m(分鐘),測試的持續時間
c:需要模擬的連接數,
T:延時時間
stdev:標準偏差(標準差如果太大說明樣本本身離散程度比較高. 有可能系統性能波動很大.)
+/- Stdev: 正負一個標準差占比
Max:最大
Avg:平均
lanency:響應時間
Request/sec:每個線程每秒鐘的完成的請求(QPS(每秒請求數)通過這個參數看應用程序的吞吐量。)
(3)、
可以放在shell腳本里面集體運行
可以單個運行
2、post請求
例舉:用戶注冊,signinByPhone.lua
functiongetTableLength(tal)
localcount =0
for_inpairs(tal)docount = count +1end
returncount
end
functiongetRequestBody(tal)
localbody ="{"
localindex =0
locallength =getTableLength(tal)
forkey,valueinpairs(tal)do
index = index +1
body = body..'"'..key..'"'..":"..'"'..value..'"'
ifindex < lengththen
body = body..","
end
end
body = body.."}"
returnbody
end
localrandomNumbers={}
localidx =0
fori =200000,900000do
idx = idx +1
randomNumbers[idx]=i
end
math.randomseed(tostring(os.time()):reverse():sub(1,7))
localreqIndex =math.random(100000,300000)
wrk.method="POST"
wrk.headers["Content-Type"]="application/json"
setup =function(thread)
reqIndex = reqIndex +100000;
thread:set("index", reqIndex);
-- print(reqIndex)
end
request =function()
localobj = { }
localnumIndex = wrk.thread:get("index")
wrk.thread:set("index",numIndex +1)
localrandomNumber = randomNumbers[numIndex]
obj.phone="18500"..randomNumber
obj.password="qwerty123456"
obj.code="000000"
print(getRequestBody(obj))
returnwrk.format(nil,nil,nil,getRequestBody(obj))
end
例子:
手機注冊
function getTableLength(tal)
local count = 0
for _ in pairs(tal) do count = count + 1 end
return count
end
function getRequestBody(tal)
local body = "{"
local index = 0
local length = getTableLength(tal)
for key,value in pairs(tal) do
index = index + 1
body = body..'"'..key..'"'..":"..'"'..value..'"'
if index < length then
body = body..","
end
end
body = body.."}"
return body
end
local randomNumbers={}
local idx = 0
for i = 1000 , 4000? do
idx = idx + 1
randomNumbers[idx]=i
end
math.randomseed(tostring(os.time()):reverse():sub(1, 7))
local reqIndex = math.random(1,2000)
wrk.method="POST"
wrk.headers["Content-Type"]="application/json"
setup = function(thread)
reqIndex = reqIndex;
thread:set("index" , reqIndex);
print(reqIndex)
end
request = function()
local obj = { }
local numIndex = wrk.thread:get("index")
wrk.thread:set("index",numIndex + 1)
local randomNumber = randomNumbers[numIndex]
obj.UserName = "測試"..randomNumber
obj.phoneNumber = "1301111"..randomNumber
obj.password = "123456"
obj.ConfirmPassword = "123456"
obj.code = "000000"
obj.InvitationCode = "ba90"..randomNumber
local tbody = getRequestBody(obj)
print (tbody)
return wrk.format(nil,nil,nil,tbody)
end