性能压测工具

1,717 阅读2分钟

一、性能压测指标

吞吐率(Request per second)qps

定义:单位时间内处理的请求数;单位时间内能处理的最大请求数,称为“最大吞吐率”。

计算方式:总请求数/处理完成这些请求所花费的时间


并发连接数(The number of concurrent connections)

定义:某个时刻服务器所接受的所有请求数


并发用户数(The number of concurrent user)

定义:某个时刻服务器所接受的所有用户请求,与连接数不同,一个用户可以产生多个会话连接


用户平均请求等待时间(Time per request)

计算方式:处理完所有请求数所花费时间/(总的请求数/并发用户数)


服务器平均请求等待时间

计算方式:处理完成所有请求数所花费时间/总的请求数


二、常用工具

hey

定义:使用Golang开发的并发请求的应用程序

github:github.com/rakyll/hey

安装:go get -u github.com/rakyll/hey

优点: 安装简单,支持GET、POST、请求体等扩展参数

缺点:

常见参数:

  • -n : 请求数,默认200;  -n 100 
  • -c : 并发数,默认50;  -c 100
  • -q:请求速率,QPS
  • -z:请求时间; -z 10s / -z  3m
  • -o:输出类型
  • -m:请求方法 GET、POST
  • -H:自定义请求头 -H "Accept: text/html" -H "Content-Type: application/xml" 
  • -t:设置超时花间,默认20s,设置0则不限制
  • -A:接受的请求头信息
  • -d:请求体
  • -D:从文件中获取请求体
  • -T:消息类型,默认“text/html”
  • -a: 认证信息
  • -x:http代理地址
  • -h2: 启用HTTP/2
  • -host: host头信息
  • -diable-compression:禁用压缩
  • -disable-keepalive:禁止持续连接
  • -disable-redirects:禁止跳转
  • -cpus:指定cpu内核数 默认8

使用案例

[root@~]$hey https://www.baidu.com

Summary:
  Total:	0.2532 secs
  Slowest:	0.1534 secs
  Fastest:	0.0112 secs
  Average:	0.0505 secs
  Requests/sec:	790.0060
  
  Total data:	45400 bytes
  Size/request:	227 bytes

Response time histogram:
  0.011 [1]	|■
  0.025 [62]	|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.040 [34]	|■■■■■■■■■■■■■■■■■■■■■■
  0.054 [38]	|■■■■■■■■■■■■■■■■■■■■■■■■■
  0.068 [14]	|■■■■■■■■■
  0.082 [1]	|■
  0.097 [10]	|■■■■■■
  0.111 [38]	|■■■■■■■■■■■■■■■■■■■■■■■■■
  0.125 [1]	|■
  0.139 [0]	|
  0.153 [1]	|■


Latency distribution:
  10% in 0.0153 secs
  25% in 0.0209 secs
  50% in 0.0408 secs
  75% in 0.0909 secs
  90% in 0.1057 secs
  95% in 0.1076 secs
  99% in 0.1110 secs

Details (average, fastest, slowest):
  DNS+dialup:	0.0153 secs, 0.0112 secs, 0.1534 secs
  DNS-lookup:	0.0016 secs, 0.0000 secs, 0.0071 secs
  req write:	0.0002 secs, 0.0000 secs, 0.0040 secs
  resp wait:	0.0271 secs, 0.0075 secs, 0.0665 secs
  resp read:	0.0001 secs, 0.0000 secs, 0.0040 secs

Status code distribution:
  [200]	200 responses


ab

定义:apache旗下的 http server 的性能评测工具

官网:httpd.apache.org/

常见参数:

  • -n : 请求数,默认200; -n 100
  • -c : 并发数,默认50; -c 100
  • -q:执行超过150个请求时不显示进度
  • -s:每个请求最大等待时间,默认30s
  • -b:TCP发送和接收大小
  • -B:进行传出连接时要绑定到的地址

优点: 使用简单

缺点:有请求描述符限制,无法设置请求方法

使用案例:

[root@PHP-6-20 ~]# ab -n 1000 -c 10 https://juejin.cn
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking juejin.im (be patient)
Completed 100 requests
Completed 200 requests
SSL read failed (5) - closing connection
Completed 300 requests
SSL read failed (5) - closing connection
Completed 400 requests
SSL read failed (5) - closing connection
SSL read failed (5) - closing connection
Completed 500 requests
Completed 600 requests
SSL read failed (5) - closing connection
SSL read failed (5) - closing connection
Completed 700 requests
Completed 800 requests
Completed 900 requests
SSL read failed (5) - closing connection
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx
Server Hostname:        juejin.im
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128

Document Path:          /
Document Length:        34662 bytes

Concurrency Level:      10
Time taken for tests:   80.256 seconds
Complete requests:      1000
Failed requests:        14
   (Connect: 0, Receive: 0, Length: 7, Exceptions: 7)
Write errors:           0
Total transferred:      34980287 bytes
HTML transferred:       34494800 bytes
Requests per second:    12.46 [#/sec] (mean)
Time per request:       802.563 [ms] (mean)
Time per request:       80.256 [ms] (mean, across all concurrent requests)
Transfer rate:          425.64 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  316 793.6     62   12393
Processing:    61  478 485.5    363    4326
Waiting:       14  163 196.5    108    3349
Total:         86  794 967.0    538   13442

Percentage of the requests served within a certain time (ms)
  50%    538
  66%    740
  75%    935
  80%   1128
  90%   1583
  95%   2174
  98%   3103
  99%   4153
 100%  13442 (longest request)


wrk

定义:一种现代HTTP基准测试工具,当在单个多核CPU上运行时,能够产生大量负载。它结合了多线程设计和可扩展的事件通知系统,例如epoll和kqueue。 可选的LuaJIT脚本可以执行HTTP请求生成,响应处理和自定义报告。

github:https://github.com/wg/wrk.git

安装:

git clone https://github.com/wg/wrk.git wrk
cd wrk
make
# 把生成的wrk移到一个PATH目录下面, 比如
sudo cp wrk /usr/local/bin

常见参数:

-c, --connections: 总的http并发数
-d, --duration:    持续压测时间, 比如: 2s, 2m, 2h
-t, --threads:     总线程数
-s, --script:      lua脚本
-H, --header:      添加http header, 比如. "User-Agent: wrk"
    --latency:     在控制台打印出延迟统计情况
    --timeout:     http超时时间
-V, --version:     打印版本信息

使用案例:

[root@~]$wrk -t12 -c400 -d10s https://www.baidu.com
Running 10s test @ https://www.baidu.com
  12 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   308.89ms  386.39ms   1.99s    87.89%
    Req/Sec    43.92     38.23   414.00     84.23%
  4262 requests in 10.14s, 63.78MB read
  Socket errors: connect 0, read 0, write 0, timeout 176
Requests/sec:    420.28
Transfer/sec:      6.29MB


三、参考