menu Chancel's Blog
rss_feed lightbulb_outline

ab与siege测试网站并发量

类型:Software
创建时间:2021-06-16 10:32:55
更新时间:2021-06-16 10:32:55
类型:Software
更新时间:2021-06-16 10:32:55

对于Web服务程序来讲,衡量并发性能通常会用到2个概念

QPS(Queries Per Second)

  • 每秒请求数,一般情况下指1秒内处理完成多少个流量请求

TPS(Transactions Per Second)

  • 每秒事务数,与QPS相比,TPS一般指1秒内能处理完成多少个原子操作请求

对于并发测试,通常可以写异步请求代码来做测试,更方便的方法可以使用apache的ab工具来做测试

使用 ab 指令来确认系统是否有安装,如无,则可以尝试安装apache

sudo apt install apache

使用ab来测试并发性,-n表示请求总数,-c表示请求并发数

ab -n 1000 -c 100 http://www.baidu.com/

例子:使用10个并发数请求1000次 http://www.baidu.com/ 如下

❯ ab -n 1000 -c 10 http://www.baidu.com/
                                                                                       ~
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking v.hao123.baidu.com [through 10.0.0.155:3128] (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache
Server Hostname:        v.hao123.baidu.com
Server Port:            80

Document Path:          /dongman/
Document Length:        28430 bytes

# 并发数
Concurrency Level:      10
# 总请求时间
Time taken for tests:   32.629 seconds
# 完成的请求(包含失败)
Complete requests:      1000
# 失败的请求
Failed requests:        13
   (Connect: 0, Receive: 0, Length: 13, Exceptions: 0)
Non-2xx responses:      11
Total transferred:      28799265 bytes
HTML transferred:       28109801 bytes
# 每秒请求数(系统吞吐量)
Requests per second:    30.65 [#/sec] (mean)
# 请求平均等待时间(毫秒)
Time per request:       326.287 [ms] (mean)
# 服务器平均处理时间
Time per request:       32.629 [ms] (mean, across all concurrent requests)
Transfer rate:          861.95 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:     1  323 672.7    224    7378
Waiting:        1  205 588.6    119    7201
Total:          2  323 672.7    224    7378

Percentage of the requests served within a certain time (ms)
  50%    224
  66%    267
  75%    305
  80%    343
 # 90%的请求在447毫秒内返回
  90%    447
  95%    620
  98%    927
  99%   3514
 100%   7378 (longest request)

ab压力测试HTTPS通常会有不支持证书等问题,所以也可以使用siege来做压力测试

sudo apt install siege

使用siege来测试并发数,-c表示并发数,-r表示重复次数

siege -c 20 -r 10 https://www.baidu.com

举例如下

❯ siege -c 20 -r 10 https://www.baidu.com
{   
       "transactions":                  4217,
    "availability":                93.03,
    "elapsed_time":               185.07,
    "data_transferred":            55.28,
    "response_time":                0.35,
    "transaction_rate":            22.79,
    "throughput":                   0.30,
    "concurrency":                  7.90,
    "successful_transactions":          4250,
    "failed_transactions":               316,
    "longest_transaction":             14.54,
    "shortest_transaction":             0.07
}

其参数含义如下

参数 解释
transactions 请求总次数
availability 请求成功百分比
elapsed_time 请求所耗费总时间
data_transferred 请求总数据量大小
response_time 请求的响应时间
transaction_rate 请求的处理请求频率
throughput 系统吞吐量(网络传输速度)
concurrency 请求并发连接数
successful_transactions 请求成功次数
failed_transactions 请求失败次数
longest_transaction 请求最大耗时
shortest_transaction 请求最短耗时

[[replyMessage== null?"发表评论":"@" + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageList.data.items.length]])

[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[messageItem.m_environ.browser]] [[messageItem.m_environ.os]] [[messageItem.m_environ.device]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[subMessage.m_environ.browser]] [[subMessage.m_environ.os]] [[subMessage.m_environ.device]]