wrk http压测工具介绍

1.介绍
wrk是一种现代HTTP基准测试工具,当在单个多核CPU上运行时,能够产生大量负载。它结合了多线程设计和可伸缩事件通知系统,例如epoll和kqueue。

2.安装

# 下载源码
git clone https://github.com/wg/wrk

#编译(可执行文件wrk在当前目录下)
make -j4
wrk http压测工具介绍

#建立软连接
ln -s 编译目录/wrk /usr/local/bin/wrk

3.示例demo

Usage: wrk <options> <url>                            
  Options:
# 脚本开启的HTTP连接数                                          
    -c, --connections <N>  Connections to keep open
# 测试脚本执行的时长   
    -d, --duration      <T>  Duration of test   
# 测试脚本使用的线程数        
    -t, --threads        <N>  Number of threads to use 
# 加载Lua脚本文件                          
    -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 
[root@localhost wrk]# ./wrk -t4 -c100 -d60s --latency http://www.cnblogs.com/imyalost
Running 1m test @ http://www.cnblogs.com/imyalost
  4 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   196.87ms  318.88ms   1.90s    86.33%
    Req/Sec   316.86    220.75     3.19k    75.19%
  Latency Distribution
     50%    5.73ms
     75%  259.62ms
     90%  615.77ms
     99%    1.47s 
  73434 requests in 1.00m, 11.06MB read
  Socket errors: connect 0, read 2, write 0, timeout 267
Requests/sec:   1222.07
Transfer/sec:    188.51KB
结果解析:

4 threads and 100 connections :4个线程,发起100个http连接请求;

Thread Stats Avg Stdev Max +/- Stdev :测试结果统计(精简版jmeter的聚合报告),分别是:平均值、标准偏差、
最大值、偏差比(值越高表示测试结果离散程度越高,性能波动较大); Latency :响应时间分布(即百分比响应时间范围); Req/Sec :每秒完成的请求数; Latency Distribution :如上面的示例结果,分别代表50/75/90/99%的响应时间在多少ms以内; 73434 requests in 1.00m, 11.06MB read :本次测试共计在1min内发起73434个请求,总计读取11.06MB的数据; Socket errors: connect 0, read 2, write 0, timeout 267 :本次测试中,连接失败0个,读取错误2个,超时267个; Requests/sec :所有线程平均每秒钟完成1222.07个请求; Transfer/sec :平均每秒读取188.51KB数据(吞吐量);

4.Lua脚本示例

wrk http压测工具介绍

package.cpath = '/usr/local/lualib/?.so'

local cjson = require("cjson.safe")

function init()
   
end

function request()
    -- http报文头设置
    --wrk.headers["content-type"]= "application/json"
    --wrk.headers["Connection"] = "Keep-Alive"
    
    -- http报文路由设置
    local path = "/nlu/v2/dispatch?productId=tryProductId&recordId=UNITTEST_RECORDID_11&requestId=UNITTEST_REQUESTID_11"
    
    -- 报文头设置
    local headers = {}
    headers["content-type"]= "application/json"
    headers["Connection"] = "Keep-Alive"
    
    -- http报文体设置
    local body = {
        request = {
            input = "天气怎么样",
        },
        context = {
            skills = {}
        },
        version="1.0"
    }
    
    table.insert(body.context.skills, {id = "2019122100000021", version = "1"})
    
    local tmp = cjson.encode(body)
    
    local msg = wrk.format("POST", path, headers, tmp)
    
    -- 返回请求报文(不可以是nil)
    return msg
end

function response(status, headers, body)
    print("status:",status)
    print("headers:",headers)
    print("body:",body)
end

 

上一篇:求众数-摩尔投票法


下一篇:【算法】力扣第 277 场周赛