HTTP_prot = {
“/gateway/services”,
“/gateway/services”,
}
–Interface type
HTTP_type = {
“POST”,
“POST “,
}
–parameters
p>
HTTP_body = {
‘{“phone”: “19012000335”}’,
‘{“phone”: “19012000335” ,”code”: “1569”}’,
– No translation inside double square brackets
}
——— –If there are multiple interfaces, the name, type, and parameters must be in the same order.
math.randomseed(os.time())
function changeData(tal,order)
local body = string. format()
end
local reqIndex =0
numLink = 0 –Record the number of connections
numHTTP = 1 –Record which interface
numData = “19012 “
lenthHTTP = 0
respError = 0 –variable, used to record the number of error requests
local threads = {}
–The test is executed once
function setup(thread)
— thread:set(“id”, counter)
table.insert(threads , thread)
reqIndex = reqIndex +1;
thread:set(“index”, reqIndex);
end< /p>
–execute once per thread
init = function()
local r = { }
local path = “” – Local variables (without local is a global variable)
local method = “GET” – The default interface type is GET
< p>
– header header
– wrk.headers[“Hash”]= “85280aa135bbd0108dd6aa424565a”
– wrk.headers[“Token” ]= “”
for i, v in ipairs(HTTP_prot) do – key from 1 Start non-zero
path = v – interface path
method = HTTP_type[i] – method get interface type
—–According to the interface type, judge which judgment to enter—–
if method == “POST” then –judge the interface type as “POST”
POST parameter json format
wrk.headers[“content-type”]= “application/json”
POST Token parameters
/p>
wrk.body = HTTP_body[i] –Get parameters
end
if method == “GET” and HTTP_body[i] ~= “” then –Judging that the interface type is “GET” and the parameter is not empty
path = v .. “?” ..HTTP_body[i]
end
— io.write(method, “—“, HTTP_body[i], “—-“, path, ”
“) – Print request method (1 thread will print once), parameters, Path (excluding domain name)
r[i] = wrk.format(method, path)
end
req = table.concat(r)
end
–Each request is executed once in Before the request starts
request = function()
return req
end
–Test Test results, each link return will be executed once
response = function(status, headers, body)
–judge whether the returned result is correct, if If the returned result is wrong, print the returned result and enter the LOG.txt file
if(not string.find(body,'”code”:0′)) then
respError = respError + 1
file = io.open(“LOG.txt”,”a”)
file:write(body..”
“)
file:flush()
end
end
— The final result of the test, which will be executed at the end of the test
< p>done = function(summary, latency, requests)
local x4 = 0
for index, thread in ipairs(threads) do
x4 = x4 + thread:get(“respError”)
end
local durations = summary.duration / 1000000 –execution time ,Unit-seconds
local errors = summary.errors.status –http status (status) does not start with 200, 300
local requests = summary.requests
local valid = requests-x4-number of valid requests = total number of requests-number of wrong requests
local conne ct = summary.errors.connect
local read1 = summary.errors.read
local write1 = summary.errors.write
local timeout = summary.errors .timeout
local errorRate = (x4/requests)*100
errorRate = string.format(“%.1f”,errorRate)
io.write(“+++++++++++++++++++++++++++++++++++++
“)
io.write(” Test duration: “..string.format(“%.2f”,durations)..”s”..”
“)
io.write(” Average response time: “..string.format(“%.2f”,latency.mean / 1000)..”ms”..”
“)
io.write (“Minimum response time: “..(latency.min / 1000)..”ms”..”
“)
io.write(” Maximum response time: “..(latency. max / 1000)..”ms”..”
“)
io.write(” Total number of requests: “..summary.requests..”
“)
< p> io.write(” Number of wrong requests: “..x4..”
“)
io.write(” Number of valid requests: “..valid..”
” )< /p>
io.write(” Error rate: “..errorRate..”%
“)
io.write(” Query rate per second: “..string.format( “%.2f”,valid / durations)..”
” )
io.write(“+++++++++++ ++++++++++++++++++++++++++
“)
end
< /p>