http压测wrk使用教程

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

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

查看所有标签

猜你喜欢:

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

深入Linux内核架构

深入Linux内核架构

Wolfgang Mauerer / 郭旭 / 人民邮电出版社 / 201005 / 149.00元

众所周知,Linux操作系统的源代码复杂、文档少,对程序员的要求高,要想看懂这些代码并不是一件容易事。本书结合内核版本2.6.24源代码中最关键的部分,深入讨论Linux内核的概念、结构和实现。具体包括进程管理和调度、虚拟内存、进程间通信、设备驱动程序、虚拟文件系统、网络、时间管理、数据同步等方面的内容。本书引导你阅读内核源代码,熟悉Linux所有的内在工作机理,充分展现Linux系统的魅力。 ......一起来看看 《深入Linux内核架构》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

SHA 加密
SHA 加密

SHA 加密工具