zoukankan      html  css  js  c++  java
  • http压力测试工具wrk

    wrk是一款简单的HTTP压测工具,托管在Github上 -> https://github.com/wg/wrk.

    wrk 的一个很好的特性就是能用很少的线程压出很大的并发量. 原因是它使用了一些操作系统特定的高性能 io 机制, 比如 select, epoll, kqueue 等. 其实它是复用了 redis 的 ae 异步事件驱动框架. 确切的说 ae 事件驱动框架并不是 redis 发明的, 它来至于 Tcl的解释器 jim, 这个小巧高效的框架, 因为被 redis 采用而更多的被大家所熟知.

    安装:

    git clone https://github.com/wg/wrk.git  
    cd wrk
    make

    如果编译过程中出错:

    src/wrk.h:11:25: fatal error: openssl/ssl.h: No such file or directory  
    #include <openssl/ssl.h>

    则需要安装openssl,使用sudo apt-get install libssl-dev或 sudo yum install openssl-devel安装即可,最后编辑etc/profile配置环境变量。

     

    测试:

    wrk -t12 -c100 -d30s http://www.baidu.com

    这段脚本的输出是:

    [root@jerrik /]# wrk -t12 -c100 -d30s http://www.baidu.com  
    Running 30s test @ http://www.baidu.com
    12 threads and 100 connections
    Thread Stats Avg Stdev Max +/- Stdev
    Latency 211.76ms 304.92ms 1.97s 88.17%
    Req/Sec 72.93 68.72 797.00 90.97%
    23725 requests in 30.05s, 347.47MB read
    Socket errors: connect 0, read 48, write 0, timeout 50
    Requests/sec: 789.57
    Transfer/sec: 11.56MB
    [root@jerrik /]#

    一般线程数不宜过多. 核数的2到4倍足够了. 多了反而因为线程切换过多造成效率降低. 因为 wrk 不是使用每个连接一个线程的模型, 而是通过异步网络 io 提升并发量. 所以网络通信不会阻塞线程执行. 这也是 wrk 可以用很少的线程模拟大量网路连接的原因. 而现在很多性能工具并没有采用这种方式, 而是采用提高线程数来实现高并发. 所以并发量一旦设的很高, 测试机自身压力就很大. 测试效果反而下降.

    参数解释:

    • 12 threads and 100 connections:
      总共是12个线程,100个连接(不是一个线程对应一个连接)
    • latencyReq/Sec:
      代表单个线程的统计数据,latency代表延迟时间,Req/Sec代表单个线程每秒完成的请求数,他们都具有平均值, 标准偏差, 最大值, 正负一个标准差占比。一般我们来说我们主要关注平均值和最大值. 标准差如果太大说明样本本身离散程度比较高. 有可能系统性能波动很大.
    • 23725 requests in 30.05s, 347.47MB read
      在30秒之内总共有23725个请求,总共读取347.47MB的数据
    • Socket errors: connect 0, read 48, write 0, timeout 50
      总共有48个读错误,50个超时.
    • Requests/sec和Transfer/sec
      所有线程平均每秒钟完成了789.57个请求,每秒钟读取11.56MB数据量

    如果想看看响应时间的分布,可以增加--latency:

    wrk -t12 -c100 -d30s --latency http://www.baidu.com

    结果为:

    [root@jerrik ~]# wrk -t12 -c100 -d30s --latency http://www.baidu.com 
    Running 30s test @ http://www.baidu.com
    12 threads and 100 connections
    Thread Stats Avg Stdev Max +/- Stdev
    Latency 204.30ms 287.90ms 1.97s 88.61%
    Req/Sec 71.43 67.59 810.00 89.77%
    Latency Distribution
    50% 14.76ms
    75% 296.79ms
    90% 545.03ms
    99% 1.40s
    23676 requests in 30.03s, 346.84MB read
    Socket errors: connect 0, read 42, write 0, timeout 46
    Requests/sec: 788.29
    Transfer/sec: 11.55MB

    说明有50%的请求在14.76ms之内,90%在545.03ms之内。

    高级用法:

    wrk可以结合lua来做,通过wrk提供的几个lua函数来对请求进行修改,结果输出、设置延迟等操作。下面来看看wrk提供的几个lua函数:

    • setup 函数
      这个函数在目标 IP 地址已经解析完, 并且所有 thread 已经生成, 但是还没有开始时被调用. 每个线程执行一次这个函数.
      可以通过thread:get(name), thread:set(name, value)设置线程级别的变量.

    • init 函数
      每次请求发送之前被调用.
      可以接受 wrk 命令行的额外参数. 通过 -- 指定.

    • delay函数
      这个函数返回一个数值, 在这次请求执行完以后延迟多长时间执行下一个请求. 可以对应 thinking time 的场景.

    • request函数
      通过这个函数可以每次请求之前修改本次请求的属性. 返回一个字符串. 这个函数要慎用, 会影响测试端性能.

    • response函数
      每次请求返回以后被调用. 可以根据响应内容做特殊处理, 比如遇到特殊响应停止执行测试, 或输出到控制台等等.

    function response(status, headers, body)  
    if status ~= 200 then
    print(body)
    wrk.thread:stop()
    end
    end
    • done函数
      在所有请求执行完以后调用, 一般用于自定义统计结果.
    done = function(summary, latency, requests)  
    io.write("------------------------------ ")
    for _, p in pairs({ 50, 90, 99, 99.999 }) do
    n = latency:percentile(p)
    io.write(string.format("%g%%,%d ", p, n))
    end
    end

    wrk官网提供的setup.lua实例:

    -- example script that demonstrates use of setup() to pass
    -- data to and from the threads

    local counter = 1
    local threads = {}

    function setup(thread)
    thread:set("id", counter)
    table.insert(threads, thread)
    counter = counter + 1
    end

    function init(args)
    requests = 0
    responses = 0

    local msg = "thread %d created"
    print(msg:format(id))
    end

    function request()
    requests = requests + 1
    return wrk.request()
    end

    function response(status, headers, body)
    responses = responses + 1
    end

    function done(summary, latency, requests)
    for index, thread in ipairs(threads) do
    local id = thread:get("id")
    local requests = thread:get("requests")
    local responses = thread:get("responses")
    local msg = "thread %d made %d requests and got %d responses"
    print(msg:format(id, requests, responses))
    end
    end

    使用setup.lua:

    [root@jerrik wrk]# wrk -t 4 -c 100 -d 20s --latency -s scripts/setup.lua https://www.baidu.com
    thread 1 created
    thread 2 created
    thread 3 created
    thread 4 created
    Running 20s test @ https://www.baidu.com
    4 threads and 100 connections
    Thread Stats Avg Stdev Max +/- Stdev
    Latency 251.75ms 336.19ms 2.00s 86.89%
    Req/Sec 138.51 69.90 690.00 71.23%
    Latency Distribution
    50% 215.74ms
    75% 401.87ms
    90% 664.84ms
    99% 1.54s
    11021 requests in 20.02s, 162.82MB read
    Socket errors: connect 0, read 3, write 0, timeout 50
    Requests/sec: 550.62
    Transfer/sec: 8.13MB
    thread 1 made 2945 requests and got 2919 responses
    thread 2 made 2831 requests and got 2807 responses
    thread 3 made 2772 requests and got 2747 responses
    thread 4 made 2573 requests and got 2548 responses
    [root@jerrik wrk]#

    将每个线程的请求数和响应数输出来了。其它更多使用可以参考github script目录下的lua脚本。

    总结:

    wrk作为http压测还是非常简便的,但是要想应对更多复杂场景,就需要多熟悉lua的使用,深入了解wrk提供的那几个函数。其它http压测工具,jmeter,apache ab,siege也可以了解一下。

    转载:https://www.jianshu.com/p/ac185e01cc30

  • 相关阅读:
    企业级监控平台开发之nagios二次开发(七)
    ndoutils2.2.0(ndo2db)中文乱码问题解决
    CloudStack系统部署系列教程-KVM
    NDO to PNP( ndoutils to PNP4Nagios)
    nagios二次开发(六)---nagiosql原理及主要文件的介绍
    [转]配置sonar、jenkins进行持续审查
    Error: Cannot open main configuration file '//start' for reading! 解决办法
    Maven学习第4期---Maven简单使用
    Java多线程学习(四)---控制线程
    Java多线程学习(三)---线程的生命周期
  • 原文地址:https://www.cnblogs.com/leadership/p/11536243.html
Copyright © 2011-2022 走看看