HTTP_prot = {
"/gateway/services",
"/gateway/services",
}
--Interface Type
HTTP_type = {
"POST",
"POST",
}
--parameter
HTTP_body = {
'{"phone": "19012000335"}',
'{"phone": "19012000335","code": "1569"}',
- double brackets which are not translated
}
----------- If there are multiple interfaces, name, type, 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 - recording of several interfaces
numData = "19012"
lenthHTTP = 0
respError = 0 - variable number of records in the error request
local threads = {}
- Testing started once
function setup(thread)
-- thread:set("id", counter)
table.insert(threads, thread)
reqIndex reqIndex = +1;
thread:set("index", reqIndex);
end
- Each thread execution time
init = function()
local r = {}
local path = "" - Local variables (without local global variables)
local method = "GET" - the default interface type is GET
- header header
-- wrk.headers["Hash"]= "85280aa135bbd0108dd6aa424565a"
-- wrk.headers["Token"]= ""
for i, v in ipairs (HTTP_prot) do - starting from a non-zero key
path = v - interface path
method = HTTP_type [i] - method obtained Interface Type
----- The interface type, into the determination that determines -------
if method == "POST" then - determines the interface type is "POST"
--POST parameter json format
wrk.headers["content-type"]= "application/json"
--POST Token parameter
wrk.body = HTTP_body [i] - acquisition parameters
end
if method == "GET" and HTTP_body [i] ~ = "" then - determines the interface type is "GET", and the parameter is not null
path = v .. "?" ..HTTP_body[i]
end
- io.write (method, "---", HTTP_body [i], "----", path, "\ n") - print request mode (a primary thread will print), the parameters, the path (excluding domain name)
r[i] = wrk.format(method, path)
end
req = table.concat(r)
end
- Each request is executed once before the start of the request
request = function()
return req
end
- test results, will be executed once for each link back
response = function(status, headers, body)
- return to judge the result is correct, the result is returned if an error is returned print result, enter the file LOG.txt
if(not string.find(body,'"code":0')) then
respError = respError + 1
file = io.open("LOG.txt","a")
file:write(body.."\n")
file:flush()
end
end
- The final test results performed at the end of the test
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 in - seconds
local errors = summary.errors.status --http status (state) 200, 300 is not the beginning of the
local requests = summary.requests - the total number of requests
local valid = requests - x4 - = total number of valid requests the number of requests - requests errors
local connect = 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("+++++++++++++++++++++++++++++++++++++\n")
io.write ( "test duration:" ..string.format ( ".% 2f", durations) .. "s" .. "\ n")
io.write ( "Average Response Time:" ..string.format ( ".% 2f", latency.mean / 1000) .. "ms" .. "\ n")
io.write ( "Minimum Response Time:" .. (latency.min / 1000) .. "ms" .. "\ n")
io.write ( "Maximum Response Time:" .. (latency.max / 1000) .. "ms" .. "\ n")
io.write ( "entire number of requests:" ..summary.requests .. "\ n")
io.write ( "Number Request Error:" ..x4 .. "\ n")
io.write ( "the effective number of requests:" ..valid .. "\ n")
io.write(" 错误率: "..errorRate.."%\n")
io.write ( "query rate per second:" ..string.format ( ".% 2f", valid / durations) .. "\ n")
io.write("+++++++++++++++++++++++++++++++++++++\n")
end