WRK的使用-lua脚本POST请求静态数据

HTTP_prot = {

       "/gateway/services",

    "/gateway/services",

    

}

 

--接口类型

HTTP_type = {

    "POST",

       "POST",

 

}

 

 

 

--参数

HTTP_body = {

 

       ‘{"phone": "19012000335"}‘,

    ‘{"phone": "19012000335","code": "1569"}‘,

       -- 双中括号里面不转译

}

-----------如果有多个接口,名称、类型、参数必须按照相同的顺序。

math.randomseed(os.time())

 

function changeData(tal,order)

       local body = string.format()

 

 

 

 

end

 

local reqIndex =0

numLink = 0   --记录连接数

numHTTP = 1   --记录第几个接口

numData = "19012"

lenthHTTP = 0

respError = 0   --变量,用于记录错误请求的数量

local threads = {}

--测试开始 执行一次

function setup(thread)

--    thread:set("id",counter)

    table.insert(threads,thread)

     reqIndex = reqIndex +1;     

       thread:set("index",reqIndex);

 

end

 

--每个线程执行一次

init = function()

 

    local r = {}

    local path = ""   -- 局部变量(不加local 是全局变量)

    local method = "GET" -- 默认接口类型是GET

      

    -- header 头

    -- wrk.headers["Hash"]= "85280aa135bbd0108dd6aa424565a"

    -- wrk.headers["Token"]= ""

  

    for i,v in ipairs(HTTP_prot) do -- 键从1 开始 非 0

        path = v    -- 接口路径

        method = HTTP_type[i]  -- method 获得接口类型

 

       -----根据接口类型,判断进入那个判断-------

        if method == "POST" then  --判断接口类型为“POST”

                     --POST 参数json格式

            wrk.headers["content-type"]= "application/json"

                     --POST Token参数

 

                 wrk.body = HTTP_body[i] --获得参数

        end

        if method == "GET" and  HTTP_body[i] ~= "" then --判断接口类型为“GET”,并且参数不为空

                     path = v .. "?" ..HTTP_body[i]

        end

 --       io.write(method,"---",HTTP_body[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) 

 

       --判断返回结果是否正确,如果返回结果错误则打印返回结果,进入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 

 

-- 测试最终结果,在测试结束执行

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    --执行时间,单位-秒

       local errors = summary.errors.status                --http status(状态)不是200,300开头的

       local requests = summary.requests                         --总的请求数

       local valid = requests - x4                               --有效请求数 = 总的请求数 - 错误请求数

       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(" 测试持续时间: "..string.format("%.2f",durations).."s".."\n")

       io.write(" 平均响应时间: "..string.format("%.2f",latency.mean / 1000).."ms".."\n")

       io.write(" 最小响应时间: "..(latency.min / 1000).."ms".."\n")

       io.write(" 最大响应时间: "..(latency.max / 1000).."ms".."\n")

       io.write(" 全部请求数量: "..summary.requests.."\n")

       io.write(" 错误请求数量: "..x4.."\n")

       io.write(" 有效请求数量: "..valid.."\n"  )

       io.write(" 错误率: "..errorRate.."%\n")

       io.write(" 每秒查询率: "..string.format("%.2f",valid / durations).."\n"  )

       io.write("+++++++++++++++++++++++++++++++++++++\n")       

 

end

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...