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使用教程》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Tales from Facebook

Tales from Facebook

Daniel Miller / Polity Press / 2011-4-1 / GBP 55.00

Facebook is now used by nearly 500 million people throughout the world, many of whom spend several hours a day on this site. Once the preserve of youth, the largest increase in usage today is amongst ......一起来看看 《Tales from Facebook》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试