性能测试工具对比

Apache Benchmark vs wrk

Posted by John Mactavish on March 16, 2022

接口的性能测试是高性能 Web 服务开发的重要一步,业界已有一些成熟的测试工具, 本文将就其中的一些进行介绍和对比。

Apache Benchmark

Apache Benchmark 简称 ab,是知名的性能测试工具。它可以通过 Linux 的包管理器轻松下载, 也可在 Windows 环境下安装。ab 的使用实例如下:

ab -n 10000 -c 100 http://localhost:8080/hello

参数 n 指示请求总数,参数 c(指 concurrency) 指示模拟的并发用户数。 实例中请求总数为 10k,并发用户数为 100,则每个用户依次发送请求 10k/100=100 次。 ab 参数最后一定要提供待测的 url。

一般还推荐加上参数 k(指 keep-alive) 以让 ab 复用 TCP 连接,在一个 HTTP session 中进行多次请求, 通常这会显著提高 rps 结果。

ab -n 10000 -c 100 -k http://localhost:8080/hello

测试结果实例如下:

Server Software:
Server Hostname:        localhost
Server Port:            8080

Document Path:          /hello
Document Length:        7 bytes

Concurrency Level:      100
Time taken for tests:   0.362 seconds
Complete requests:      10000
Failed requests:        0
Keep-Alive requests:    10000
Total transferred:      1530000 bytes
HTML transferred:       70000 bytes
Requests per second:    27590.16 [#/sec] (mean)
Time per request:       3.624 [ms] (mean)
Time per request:       0.036 [ms] (mean, across all concurrent requests)
Transfer rate:          4122.36 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.1      0      17
Processing:     1    3   0.8      3       8
Waiting:        1    3   0.8      3       8
Total:          1    4   1.4      3      21

Percentage of the requests served within a certain time (ms)
  50%      3
  66%      3
  75%      4
  80%      4
  90%      4
  95%      5
  98%      7
  99%      8
 100%     21 (longest request)

它展示了几个重要指标。

  • Requests per second 即 RPS,是评价接口并发性能的主要指标;
  • Transfer rate 即传输速率,我们可以据此确定对应于此次测试条件的网络带宽要求;
  • Time per request(mean) 为单个用户(Each Concurrency)完成所有请求的平均时间,意义不大;
  • Time per request(mean, across all concurrent requests) 为单次请求的平均响应时间(Response Time);
  • 响应时间更主要的参考是百分比分布统计,这里最后也提供了。

ab 还可进行 HEAD、POST、PUT 请求,但不支持 DELETE,详情请参阅文档。 另外,ab 只支持 HTTP 1.0 接口测试。

总的来说,Apache Benchmark 是一个简单好用但功能单一的命令行工具,适用于开发、调试过程中简单的性能测试; 而到了正式测试的环节,我们则需要功能更全面的工具。

wrk

wrk 是现代化的 HTTP 性能测试工具。 相对于 Apache Benchmark,wrk 基于多线程技术和 epoll、kqueue 这样的事件通知系统, 能充分利用多核 CPU 以生成更多的负载。它可以作为 ab 的上位替代。wrk 的使用实例如下:

wrk -t 4 -c 100 -d 3 --latency http://localhost:8080/hello

参数 t(指 threads) 指示压测使用的线程数,理论上来说可以和 CPU 核心数相等; 参数 c(指 connections) 同样指示并发用户数;与 ab 不同的是, wrk 不要求给出总请求数,而是要求通过参数 d(指 duration) 给出测试总运行时间(老实说,我更喜欢这种方案); 最后的参数 latency 指示 wrk 显示响应时间的百分比分布统计。

wrk 的数字类型参数还可包括 SI 单位(如 1k, 1M, 1G),时间类型参数可包括时间单位(如 2s, 2m, 2h)。 例如:

wrk -t 4 -c 1k -d 2s --latency http://localhost:8080/hello

测试结果实例如下:

Running 3s test @ http://localhost:8080/hello
  4 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.87ms    1.37ms  35.78ms   89.73%
    Req/Sec    11.38k     1.09k   17.67k    86.67%
  Latency Distribution
     50%    1.63ms
     75%    1.95ms
     90%    2.80ms
     99%    6.43ms
  136185 requests in 3.01s, 16.75MB read
Requests/sec:  45233.10
Transfer/sec:      5.56MB

结果相较 ab 要简洁得多,但关键信息一个不少。

事实上,ab 与 wrk 的结果实例对应的待测接口完全相同,压测参数也同样是 100 个并发用户, 但是 wrk 实现了 45k 的 rps,相较于 ab 的 27k 是巨大的提升,这足以证明 wrk 的压测能力。

wrk 还支持通过 LuaJIT 脚本扩展功能,如访问授权接口、发送 DELETE 请求等。 wrk 的升级版 wrk2 额外支持生成固定 rps 的负载(可用于负载测试(load test)) 和更精确的响应时间记录。不过 wrk2 目前还在开发中,未必适合现在使用。

其他性能测试工具:

vegeta:由 Go 实现,可生成固定 rps 的负载,可同时用作命令行工具和库,支持详细的图表报告和实时图象, 支持 HTTP 2.0 和 HTTPS。

tsung:由 Erlang 实现,可压测 HTTP, WebDAV, SOAP, PostgreSQL, MySQL, LDAP 和 Jabber/XMPP 服务器,支持图表报告。

k6:由 ES6 JS 实现,支持 HTTP/1.1, HTTP/2, WebSocket 和 gRPC 协议,支持脚本录制

推荐阅读:

awesome-http-benchmark

Definition of Connect, Processing, Waiting in apache bench

Apache Bench: Mean vs Mean across all concurrent requests

Go HTTP server testing ab vs wrk so much difference in result