http压测wrk使用教程

栏目: 后端 · 前端 · 发布时间: 6年前

内容简介:WRK是一个现代的HTTP基准测试工具,能够在单个多核CPU上运行时产生大量的负载。它将多线程设计与可伸缩的事件通知系统(Base: epoll和kqueue)结合在一起。函数说明wrk.format这个函数的作用,根据参数和全局变量wrk生成一个http请求 函数签名:

WRK是一个现代的HTTP基准测试工具,能够在单个多核CPU上运行时产生大量的负载。它将多线程设计与可伸缩的事件通知系统(Base: epoll和kqueue)结合在一起。

http压测wrk使用教程
http-wrk

安装

git clone https://github.com/wg/wrk.git wrk
cd wrk
make
sudo cp wrk /usr/local/bin

压测

# 使用12个线程运行30秒, 400个http并发
wrk -t12 -c400 -d30s http://127.0.0.1:8080/index.html

参数

-c, --connections: 总的http并发数
 
-d, --duration:    持续压测时间, 比如: 2s, 2m, 2h
 
-t, --threads:     总线程数
 
-s, --script:      luajit脚本,使用方法往下看
 
-H, --header:      添加http header, 比如. "User-Agent: wrk"
 
    --latency:     在控制台打印出延迟统计情况
 
    --timeout:     http超时时间

lua脚本压测

使用post方法压力测试示例

 wrk -t20 -c1000 -d30s -s /path/post.lua  http://127.0.0.1:8080/test
 
# post.lua文件内容
wrk.method = "POST"
wrk.body = “protobuf-data”
wrk.headers["Content-Type"] = "application/x-protobuf"
wrk.headers["Content-Encoding"] = "gzip"

DDCC压力测试示例

request = function()
   uid = math.random(1, 10000000)
   path = "/test?uid=" .. uid
   return wrk.format(nil, path)
end

函数说明

wrk.format这个函数的作用,根据参数和全局变量wrk生成一个http请求 函数签名: function wrk.format(method, path, headers, body) method:http方法,比如GET/POST等 path: url上的路径(含函数) headers: http header body: http body

登录压测示例

token = nil
path  = "/authenticate"
 
request = function()
   return wrk.format("GET", path)
end
 
response = function(status, headers, body)
   if not token and status == 200 then
      token = headers["X-Token"]
      path  = "/resource"
      wrk.headers["X-Token"] = token
   end
end

发送json压测示例

request = function()
    local headers = { }
    headers['Content-Type'] = "application/json"
    body = {
        mobile={"1533899828"},
        params={code=math.random(1000,9999)}
    }
 
    local cjson = require("cjson")
    body_str = cjson.encode(body)
    return wrk.format('POST', nil, headers, body_str)
end

项目地址

https://github.com/wg/wrk
# 压测脚本示例
https://github.com/wg/wrk/tree/master/scripts

by:cpp.la


以上所述就是小编给大家介绍的《http压测wrk使用教程》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法与数据结构

算法与数据结构

张乃孝 / 高等教育出版社 / 2006-1 / 31.00元

《算法与数据结构:C语言描述(第2版)》以数据结构为主线,算法为辅线组织教学内容。全书共分10章:绪论、线性表、字符串、栈与队列、二叉树与树、集合与字典、高级字典结构、排序、图和算法分析与设计。《算法与数据结构:C语言描述(第2版)》体系完整,概念清楚,内容充实,取材适当。第一版被列入“面向21世纪课程教材”,2004年被评为“北京市高等教育精品教材”,第二版被列入普通高等教育“十一五”国家级规划......一起来看看 《算法与数据结构》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具