背景
由于业务需要我们从原来的 长连接 转为 短连接,提高单服同时在线人数。
老压测
在服务器编写机器人,编写一部分客户端逻辑(这里如果客户端严格使用mvc 模式,其实可以把 view 层换为 服务器测试代码层,而一般不同层之间通过事件机制,触发调用,这样下来可以减少重复代码编写)测试。测试周期长,需要写大量代码。
新压测
通过 wrk 在 linux 上可以开多个连接,模拟发送请求,轻轻松松压测。如果是业务测试,可能麻烦一些,需要编写相关的 lua 脚本,我们使用 scripts/pipeline.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
定义请求流水线
例子
-- 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脚本,如果要使用多个函数,可以将相关函数写在一个脚本中。