zoukankan      html  css  js  c++  java
  • 高效的性能测试工具-wrk

    今天主要介绍一款高效的性能测试工具wrk。wrk的使用方式和apache bench这些工具也大体相似,通过命令行的方式即可发起。但是wrk比起apache bench更为高效,因为它支持多线程,更容易发挥多核CPU的能力,甚至可以压满CPU。wrk还支持Lua脚本来提供更多的参数定制、参数加密等需求,灵活度更高。

    安装

    wrk支持大部分UNIX系统,不支持windows系统。安装过程比较简单,从github克隆项目到本地,再在项目路径下make即可,在此就不详述,具体可查看github文档。

    基础使用

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

    以上命令行表示对本地80端口的index.html文件发起请求,压测时间持续30秒,并发12线程,保持400个HTTP连接请求。

    输出结果:

    Running 30s test @ http://127.0.0.1:80/index.html
      12 threads and 400 connections
      Thread Stats   Avg      Stdev     Max   +/- Stdev
        Latency    90.43ms  201.95ms   1.99s    88.34%
        Req/Sec     1.79k     1.80k   21.57k    89.17%
      577891 requests in 30.09s, 188.46MB read
      Socket errors: connect 0, read 0, write 0, timeout 37
      Non-2xx or 3xx responses: 577891
    Requests/sec:  19202.50
    Transfer/sec:      6.26MB
    结果展示了本次压测的请求平均延迟Latency、每秒每线程平均请求数Req/Sec、合计的每秒请求数Requests/sec和每秒吞吐量Transfer/sec等。这些数据即可反映出压测服务器的大概响应情况,以对服务器性能做初步判断。

    贴一下请求的命令参数:​​​​​​​

    -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.
    关于长连接和短连接:apache bench默认短连接,wrk默认长连接。wrk如需测试短连接,可添加 -H “Connection:Close” 来关闭长连接。

    Post请求

    wrk不仅能发起对Get请求,也能通过Lua脚本发起Post请求。如编写post.lua脚本:​​​​​​​

    wrk.method = "POST"wrk.headers["Content-Type"] = "application/x-www-form-urlencoded"wrk.body = "youbody&youset"

    在脚本中定义了Post请求的headers和body。执行wrk请求时加上—script指定该脚本:

    wrk -t4 -c2000 -d60s -T5s --script=post.lua --latency http://127.0.0.1:80/user/login

    如此,便可轻松实现Post请求的压测。

    后续

    wrk作为一种高效的性能测试工具,可以作为一种常用手段对待测url发起请求。本文只是初步介绍了引用Lua脚本执行Post请求,Lua脚本其实可以实现更为丰富的测试需求,下一次我们再详细看下Lua脚本如何为wrk添翼。

  • 相关阅读:
    [置顶] 从零开始学C++之STL(二):实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)
    HDU4607(求树中的最长链)
    java从文件中读取数据然后插入到数据库表中
    rcp(插件开发)插件B需要引用插件A中的jar包-如何处理依赖关系
    HDU 2084 数塔
    Object-c学习之路二(oc内存管理黄金法则1)
    android adb命令 unable to connect to 192.168.1.155:5555
    安装node.js / npm / express / KMC
    oracle断电重启之ORA-00600[4194]
    virtualbox虚拟机迁移出现"connot find device eth0"错误
  • 原文地址:https://www.cnblogs.com/eflypro/p/12420181.html
Copyright © 2011-2022 走看看