• 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, " ") -- 打印请求方式(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.." ")

                  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("+++++++++++++++++++++++++++++++++++++ ")

           io.write(" 测试持续时间: "..string.format("%.2f",durations).."s".." ")

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

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

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

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

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

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

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

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

           io.write("+++++++++++++++++++++++++++++++++++++ ")       

    end

    测试,是为了发现错误而执行的过程。当你在一个程序中发现的BUG越多,就代表着——你没发现的BUG就越多。
  • 相关阅读:
    Amazon EBS的功能更新
    ORA-03113: end-of-file on communication channel
    云serverlinux又一次挂载指定文件夹(非扩充)
    Binder对象死亡通知机制
    cocos2d::ui::TextField 调用setAttachWithIME和setDetachWithIME都无效
    shell linux基本命令实例、笔记
    降智严重——nowcoder练习赛46&&codeforces #561 Div2
    2018-2-13-win10-uwp-隐藏实时可视化
    2018-2-13-win10-uwp-隐藏实时可视化
    2018-2-13-win10-UWP-ListView-模仿开始菜单
  • 原文地址:https://www.cnblogs.com/blog12357/p/11616795.html
Copyright © 2020-2023  润新知