wrk 压测工具

wrk 压测工具wrk 压测工具安装 gitclonehttp github com wg wrk gitcdwrkmake 常用参数类型 Usage wrk options url Options c connections N Connectionst d duration T Durationofte e g 2s 2m 2h T N url options

wrk 压测工具

安装

git clone https://github.com/wg/wrk.git cd wrk make 

常用参数类型

Usage: wrk <options> <url> Options: -c, --connections <N> Connections to keep open -d, --duration <T> Duration of test, e.g. 2s, 2m, 2h -t, --threads <N> Number of threads to use -s, --script <S> Load Lua script file -H, --header <H> Add header to request, e.g. "User-Agent: wrk" --latency Print latency statistics(延迟统计) --timeout <T> Socket/request timeout -v, --version Print version details Numeric arguments may include a SI unit (1k, 1M, 1G) Time arguments may include a time unit (2s, 2m, 2h) 

高级用法

wrk可以结合lua来做,通过wrk提供的几个lua函数来对请求进行修改,结果输出、设置延迟等操作。下面来看看wrk提供的几个lua函数:

lua声明周期

共有三个阶段,启动阶段,运行阶段,结束阶段。wrk支持在这三个阶段对压测进行个性化。

启动阶段
setup 函数
运行阶段
init 函数

setup方法中可操作该thread对象,获取信息、存储信息、甚至关闭该线程。

thread.addr - get or set the thread's server address thread:get(name) - get the value of a global in the thread's env thread:set(name, value) - set the value of a global in the thread's env thread:stop() - stop the thread 
delay函数

这个函数返回一个数值, 在这次请求执行完以后延迟多长时间执行下一个请求. 可以对应 thinking time 的场景.

request函数

通过这个函数可以每次请求之前修改本次请求的属性. 返回一个字符串. 这个函数要慎用, 会影响测试端性能.

request = function() wrk.method = "GET" wrk.headers["Host"] = "www.test.com" wrk.headers["x-header-trace"] = "all" return wrk.format("GET", wrk.path) end 
response函数

每次请求返回以后被调用. 可以根据响应内容做特殊处理, 比如遇到特殊响应停止执行测试, 或输出到控制台等等.

function response(status, headers, body) if status ~= 200 then print(body) wrk.thread:stop() end end 
结束阶段
done函数

在所有请求执行完以后调用, 一般用于自定义统计结果.

done = function(summary, latency, requests) io.write("------------------------------\n") for _, p in pairs({ 
    50, 90, 99, 99.999 }) do n = latency:percentile(p) io.write(string.format("%g%%,%d\n", p, n)) end end 
wrk官网提供的setup.lua实例
-- example script that demonstrates use of setup() to pass -- data to and from the threads local counter = 1 local threads = { 
   } function setup(thread) thread:set("id", counter) table.insert(threads, thread) counter = counter + 1 end function init(args) requests = 0 responses = 0 local msg = "thread %d created" print(msg:format(id)) end function request() requests = requests + 1 return wrk.request() end function response(status, headers, body) responses = responses + 1 end function done(summary, latency, requests) for index, thread in ipairs(threads) do local id = thread:get("id") local requests = thread:get("requests") local responses = thread:get("responses") local msg = "thread %d made %d requests and got %d responses" print(msg:format(id, requests, responses)) end end 

使用setup.lua:

[root@jerrik wrk]# wrk -t 4 -c 100 -d 20s --latency -s scripts/setup.lua https://www.baidu.com thread 1 created thread 2 created thread 3 created thread 4 created Running 20s test @ https://www.baidu.com 4 threads and 100 connections Thread Stats Avg Stdev Max +/- Stdev Latency 251.75ms 336.19ms 2.00s 86.89% Req/Sec 138.51 69.90 690.00 71.23% Latency Distribution 50% 215.74ms 75% 401.87ms 90% 664.84ms 99% 1.54s 11021 requests in 20.02s, 162.82MB read Socket errors: connect 0, read 3, write 0, timeout 50 Requests/sec: 550.62 Transfer/sec: 8.13MB thread 1 made 2945 requests and got 2919 responses thread 2 made 2831 requests and got 2807 responses thread 3 made 2772 requests and got 2747 responses thread 4 made 2573 requests and got 2548 responses [root@jerrik wrk]#  

将每个线程的请求数和响应数输出来了。其它更多使用可以参考github script目录下的lua脚本。

方法

wrk.fomat wrk.lookup wrk.connect

function wrk.format(method, path, headers, body) --根据参数和全局变量wrk,生成一个HTTP rquest string。 function wrk.lookup(host, service) --给定host和service(port/well known service name),返回所有可用的服务器地址信息。 function wrk.connect(addr) --测试与给定的服务器地址信息是否可以成功创建连接 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/232046.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CMQ是什么

    CMQ是什么CMQ 是腾讯云提供的一种高性能 MQ 和其他 MQ 一样 是消息传递的组件 至少一次可达

    2025年7月29日
    4
  • 清除vs2005、vs2008起始页最近打开项目

    清除vs2005、vs2008起始页最近打开项目

    2021年11月17日
    46
  • Workbench中直接调用ICEM CFD进行网格划分「建议收藏」

    Workbench中直接调用ICEM CFD进行网格划分「建议收藏」Workbench中直接调用ICEMCFD进行网格划分自从ANSYS12.0之后,ICEMCFD就从Workbench中被分离出去,作为一个独立的程序使用了。取而代之的是Meshing模块。在Meshing的属性节点菜单中右键点击Mesh,选择Insert>Method,插入方法。选择需要划分网格的几何体,点击apply。此时Geometry显示为1Body。设置Method为MultiZone,如果不设置成这个的话,找不到进入ICEMCFD的入口。如果要划分四面体,

    2022年5月9日
    159
  • 雷柏 V500PRO Win键失效「建议收藏」

    雷柏 V500PRO Win键失效「建议收藏」之前买了一个雷柏V500PRO机械键盘玩游戏,但是2020年国庆后再没有玩过。最近在使用键盘的过程中,发现Win键失效,然后开始找原因。网上很多是在任务管理器、注册表之类的,总感觉不属于我的问题范围。在博客的评论区看到一个关于机械键盘的评论,引起很多人的共鸣,感觉和我遇到的问题应该一样。接着再搜一下关于雷柏V500Win键失效,找到并解决。其他机械键盘,自己搜索。对于雷柏V500PRO,Win键控制快捷键:Fn+Win键。…

    2022年5月27日
    102
  • 数据库置疑处理方法有哪些_sql2008数据库置疑

    数据库置疑处理方法有哪些_sql2008数据库置疑/*1.新建一个与置疑数据库同名的数据库(一定要保证文件名是相同的,包括数据文件名和日志文件名)2.停掉sqlserver3.删除新数据库的日志文件4.用置疑数据库的数据文件(.mdf)覆盖掉这个新建的同名数据库的数据库文件5.再重启sqlserver6.执行以下语句*/   use master go execsp_con

    2022年8月20日
    6
  • verilog序列生成器最少移位寄存器实现[通俗易懂]

    verilog序列生成器最少移位寄存器实现[通俗易懂]verilog序列生成器最少移位寄存器实现序列生成器序列生成器实现方式移位寄存器版(输入序列版)最少移位寄存器版VCS仿真![仿真](https://img-blog.csdnimg.cn/42e1e4f55f4443faa70b38adad7c5db1.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBASmFydmlz56CB5ZGY,size_20,color_FFFFFF,t_7

    2022年7月16日
    17

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号