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