介绍2款非常简单好用的压测工具 ab & wrk, 再也不用看测试的眼色了,自己搞起来~

想知道你的接口或服务器性能如何嘛?本文为你挑选了2款方便易用压测工具: abwrk.

ab

全称:ApacheBench,用于 web 性能压力测试,ab 命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问。

ab 命令对发出负载的计算机要求很低,不会占用很高CPU和内存,但却会给目标服务器造成巨大的负载。

安装

ab 是 apache 服务器的附属工具,当然如果不需要 apache 也可以独立安装。

ubuntu

apt install -y apache2-utils

centos

yum install -y httpd-tools

帮助文档

ab -h
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
    -n requests     Number of requests to perform
    -c concurrency  Number of multiple requests to make at a time
    -t timelimit    Seconds to max. to spend on benchmarking
                    This implies -n 50000
    -s timeout      Seconds to max. wait for each response
                    Default is 30 seconds
    -b windowsize   Size of TCP send/receive buffer, in bytes
    -B address      Address to bind to when making outgoing connections
    -p postfile     File containing data to POST. Remember also to set -T
    -u putfile      File containing data to PUT. Remember also to set -T
    -T content-type Content-type header to use for POST/PUT data, eg.
                    'application/x-www-form-urlencoded'
                    Default is 'text/plain'
    -v verbosity    How much troubleshooting info to print
    -w              Print out results in HTML tables
    -i              Use HEAD instead of GET
    -x attributes   String to insert as table attributes
    -y attributes   String to insert as tr attributes
    -z attributes   String to insert as td or th attributes
    -C attribute    Add cookie, eg. 'Apache=1234'. (repeatable)
    -H attribute    Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
                    Inserted after all normal header lines. (repeatable)
    -A attribute    Add Basic WWW Authentication, the attributes
                    are a colon separated username and password.
    -P attribute    Add Basic Proxy Authentication, the attributes
                    are a colon separated username and password.
    -X proxy:port   Proxyserver and port number to use
    -V              Print version number and exit
    -k              Use HTTP KeepAlive feature
    -d              Do not show percentiles served table.
    -S              Do not show confidence estimators and warnings.
    -q              Do not show progress when doing more than 150 requests
    -g filename     Output collected data to gnuplot format file.
    -e filename     Output CSV file with percentages served
    -r              Don't exit on socket receive errors.
    -h              Display usage information (this message)
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol
                    (SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

常用的参数:

  • -c 线程数(并发数)

  • -n 请求数,总共要发送多少请求

  • -p POST请求使用文件

  • -T 请求类型,Content-Type, 可以用 -H 替代

  • -H 请求头

实例

废话少说,上实例:

创建10个线程(模拟10个用户),累计向百度发送100个请求

ab -c 10 -n 100 https://www.baidu.com/

Server Software:        BWS/1.1
Server Hostname:        www.baidu.com
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128

Document Path:          /
Document Length:        227 bytes

Concurrency Level:      10
Time taken for tests:   0.711 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      111095 bytes
HTML transferred:       22700 bytes
Requests per second:    140.56 [#/sec] (mean)
Time per request:       71.144 [ms] (mean)
Time per request:       7.114 [ms] (mean, across all concurrent requests)
Transfer rate:          152.49 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       34   46   6.6     45      62
Processing:    12   16   3.5     16      41
Waiting:       12   16   3.4     15      41
Total:         48   62   8.4     62      86

Percentage of the requests served within a certain time (ms)
  50%     62
  66%     66
  75%     68
  80%     70
  90%     73
  95%     75
  98%     83
  99%     86
 100%     86 (longest request)

POST请求,需创建参数文件

data.txt
a=1&b=2
ab -c 10 -n 100 -p data.txt -T 'application/x-www-form-urlencoded' https://www.baidu.com/

data.json
{"a":1}
ab -c 10 -n 100 -p data.json -T 'application/json' https://www.baidu.com/

注: 线程数跟 ulimit 有关,centos 默认 ulimit=1024 最大不超过1024个线程,如需更多线程,可以调整 ulimit 值。

wrk

与 ab 的区别是 wrk 可以指定持续压测的时间(例如:持续打压30分钟)并且支持 lua 脚本的执行,支持多个URL的压测,更加具备扩展性和灵活性。

https://github.com/wg/wrk

https://github.com/wg/wrk/tree/master/scripts

安装

centos安装wrk

yum install git -y
git clone https://github.com/wg/wrk.git wrk
yum install unzip -y
yum install gcc -y
make
cp wrk /usr/local/bin/

帮助文档

wrk --help
Usage: wrk <options> <url>
  Options:
    -c, --connections <N>  Connections to keep open
    -d, --duration    <T>  Duration of test
    -t, --threads     <N>  Number of threads to use

    -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

  Numeric arguments may include a SI unit (1k, 1M, 1G)
  Time arguments may include a time unit (2s, 2m, 2h)

实例 

开2个线程,建立6个连接,持续请求百度5s

wrk -t 2 -c 6 -d 5s https://www.baidu.com
Running 5s test @ https://www.baidu.com
  2 threads and 6 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    15.97ms    3.58ms  36.06ms   86.53%
    Req/Sec   185.96     21.39   220.00     82.00%
  1862 requests in 5.03s, 27.90MB read
  Socket errors: connect 0, read 3, write 0, timeout 0
Requests/sec:    369.85
Transfer/sec:      5.54MB

原理示意图:

使用脚本发送表单请求

# post.lua
wrk.method = "POST"
wrk.body   = "login=sammy&password=test"
wrk.headers["Content-Type"] = "application/x-www-form-urlencoded"

wrk -t 2 -c 6 -d 5s -s post.lua https://www.baidu.com

推荐一篇技术文章给大家阅读:里面有一些原理性的介绍,可以辅助你构建更复杂的测试需求。

https://cloud.tencent.com/developer/article/1346649

好了,今天的分享就是这些,简单又好用的压测工具,你学会了吗?

学会了,记得点赞,收藏,评论互动。关注公&号:新质程序猿,可以找到我。

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值