短连接服务器压测-wrk

背景

由于业务需要我们从原来的 长连接 转为 短连接,提高单服同时在线人数。

老压测

在服务器编写机器人,编写一部分客户端逻辑(这里如果客户端严格使用mvc 模式,其实可以把 view 层换为 服务器测试代码层,而一般不同层之间通过事件机制,触发调用,这样下来可以减少重复代码编写)测试。测试周期长,需要写大量代码。

新压测

通过 wrk 在 linux 上可以开多个连接,模拟发送请求,轻轻松松压测。如果是业务测试,可能麻烦一些,需要编写相关的 lua 脚本,我们使用 scripts/pipeline.lua 改写,创建连接,测试。

Lua 复制代码
wrk -v -t4000 -c4000 -d20m http://127.0.0.1:30001 --latency -s pipeline.lua

这样轻松创建 4000和连接并发处理

部分参数说明:

-c, --connections <N> Connections to keep open 打开连接数

-d, --duration <T> Duration of test 持续时间

-t, --threads <N> Number of threads to use 线程数

-s, --script <S> Load Lua script file 指定那个脚本

-H, --header <H> Add header to request

--latency Print latency statistics

--timeout <T> Socket/request timeout

-v, --version Print version details

脚本

参考 wrk/SCRIPTING文件,里面介绍了所有可以使用的 lua 函数,可以自定义改写

delay.lua

每次请求间隔

pipeline.lua

定义请求流水线

例子

Lua 复制代码
-- example script demonstrating HTTP pipelining

function fillParameters(path, code, bodyContent)
   -- 指定header内容
   local headers = {}
   headers["Content-Type"] = "text/plain"
   headers["Code"] = code
   headers["TestJson"] = "1"
   headers["Account"] = "TRA1"

   -- 协议类型
   local method = "POST"
   return wrk.format(method, path, headers, bodyContent)
end

init = function(args)

   local r = {
        -- 登录
        fillParameters("/Proto/C2G", "12002", '{ "RpcId": 1, "Account":"TRA1", "Password":"123"}'),
         -- 同步时间
        fillParameters("/Proto/C2G", "12008", '{ "RpcId":2}'),
        -- ...
      }

   req = table.concat(r)
end

request = function()
   return req
end

-- 每次请求间隔
function delay()
   return math.random(5000, 6000)
end

Note:

wrk 后跟 -s 引用脚本只能用一个,加多个 -s 执行不会报错,正常执行,但执行时只会使用其中一个lua脚本,如果要使用多个函数,可以将相关函数写在一个脚本中。

相关推荐
Mintimate43 分钟前
云服务器 Linux 手动 DD 安装第三方 Linux 发行版:原理与实战
linux·运维·服务器
RussellFans1 小时前
Linux 环境配置
linux·运维·服务器
网硕互联的小客服1 小时前
503 Service Unavailable:服务器暂时无法处理请求,可能是超载或维护中如何处理?
服务器·git·github
高冷的肌肉码喽2 小时前
Linux-进程间的通信
linux·运维·服务器
jekc8682 小时前
禅道18.2集成LDAP
linux·运维·服务器
Tender_光3 小时前
iptables实验
运维·服务器
szxinmai主板定制专家4 小时前
【飞腾AI加固服务器】全国产化飞腾+昇腾310+PCIe Switch的AI大模型服务器解决方案
运维·服务器·arm开发·人工智能·fpga开发
深科文库4 小时前
构建 MCP 服务器:第 3 部分 — 添加提示
服务器·python·chatgpt·langchain·prompt·aigc·agi
点击查询4 小时前
怎么把自己电脑设置成服务器?
运维·服务器
wanhengidc4 小时前
服务器中日志分析的作用都有哪些
运维·服务器