ab 压测工具的用法简介

699 阅读1分钟

ab 是 Apache 出品的一个 HTTP 服务器性能测试工具。使用它可以测试每秒请求数(Requests per second),吞吐率(Transfer rate)等参数。

它的用法如下:

$ ab -c 100 -n 10000 localhost:3000/

注意:ab 只能对接口进行压测,不能域名进行压测。因此,至少要有一个 / 后缀,表示根目录

常见参数

  • -c concurrency 设定并发数,默认并发数是 1
  • -n requests 设定压测的请求总数
  • -t timelimit 设定压测的时长,单位是秒
  • -p POST-file 设定 POST 文件路径,注意设定匹配的 -T 参数
  • -T content-type 设定 POST/PUT 的数据格式,比如 application/x-www-form-urlencoded。默认为 text/plain
  • -V 查看 ab 的版本信息

输出

ab 的输出结果有很多数据,其中最重要的是 Request per secondTransfer rate,通过它俩可以评判服务器应用的抗压能力。

$ ab -c100 -n10000 localhost:3000/

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 localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:
Server Hostname:        localhost
Server Port:            3000

Document Path:          /
Document Length:        12 bytes

Concurrency Level:      100
Time taken for tests:   1.589 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      870000 bytes
HTML transferred:       120000 bytes
+ Requests per second:    6295.21 [#/sec] (mean)
Time per request:       15.885 [ms] (mean)
Time per request:       0.159 [ms] (mean, across all concurrent requests)
+ Transfer rate:          534.85 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.6      1       5
Processing:     2   15  15.6     12     164
Waiting:        2    9  11.3      7     161
Total:          5   16  15.6     13     165

Percentage of the requests served within a certain time (ms)
  50%     13
  66%     15
  75%     17
  80%     18
  90%     22
  95%     25
  98%     42
  99%    152
 100%    165 (longest request)

参考文档

  1. ab - Apache HTTP server benchmarking tool - Apache HTTP Server Version 2.4