wrk 项目地址:

https://github.com/wg/wrk

安装:

  1. git clone https://github.com/wg/wrk.git
  2. make

make之后,会在项目路径下生成可执行文件wrk,随后就可以用其进行HTTP压测了。可以把这个可执行文件拷贝到某个已在path中的路径,比如/usr/local/bin,这样就可以在任何路径直接使用wrk了。

使用:

wrk -t12 -c400 -d30s http://127.0.0.1:8080/index.html

说明:
-c, --connections: total number of HTTP connections to keep open with
                   each thread handling N = connections/threads 连接数

-d, --duration:    duration of the test, e.g. 2s, 2m, 2h 请求持续时间

-t, --threads:     total number of threads to use 线程数

-s, --script:      LuaJIT script, see SCRIPTING

-H, --header:      HTTP header to add to request, e.g. "User-Agent: wrk"

    --latency:     print detailed latency statistics

    --timeout:     record a timeout if a response is not received within
                   this amount of time.

post请求

发送post请求时需要编写一个lua文件

http_post.lua:

wrk.method = "POST"
wrk.body = '{"pushtoken":"fl9Hs_5Vs","params":{"dpi":320,"device_category":"phone"}}'
wrk.headers["Content-Type"] = "application/json"

post请求:

./wrk -t1 -c400 -d200s --script=http_post.lua http://www.baidu.com

结果分析:

wrk -t8 -c200 -d30s --latency  "http://www.bing.com"

Running 30s test @ http://www.bing.com (压测时间30s)
  8 threads and 200 connections (共8个测试线程,200个连接)
  Thread Stats   Avg      Stdev     Max   +/- Stdev
              (平均值) (标准差)(最大值)(正负一个标准差所占比例)
    Latency    46.67ms  215.38ms   1.67s    95.59%
    (延迟)
    Req/Sec     7.91k     1.15k   10.26k    70.77%
    (处理中的请求数)
  Latency Distribution (延迟分布)
     50%    2.93ms
     75%    3.78ms
     90%    4.73ms
     99%    1.35s (99分位的延迟)
  1790465 requests in 30.01s, 684.08MB read (30.01秒内共处理完成了1790465个请求,读取了684.08MB数据)
Requests/sec:  59658.29 (平均每秒处理完成59658.29个请求)
Transfer/sec:     22.79MB (平均每秒读取数据22.79MB)

注意:

wrk 使用的是 HTTP/1.1,缺省开启的是长连接,而 ab 使用的是 HTTP/1.0,缺省开启的是短链接。

用 wrk 测试短链接:

wrk -H "Connection: Close" -c 100 -d 10 http://domain/path

也就是说通过参数「H」传递一个自定义的 Connection 请求头来关闭长链接。