web性能测试:apache benchmark(ab),性能测试benchmark


开发完网站或者web接口后,一个比较负责任的工作就是测试一下接口性能,也叫做压力测试。web接口性能直接反映了接口的并发处理能力,一个数值评估通常可以给系统性能给出一个比较好的反馈。

本文介绍比较常用的web性能测试工具ab(apache benchmark)。

安装

ab命令来源于apache工具包,ubuntu可以通过下面的命令安装:

sudo apt-get install apache2-utils

用法

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

如上所示,-n表示测试请求数目,-c表示并发度。测试结果显示如下:

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (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:        
Server Hostname:        127.0.0.1                        
Server Port:            4001                                    

Document Path:          /
Document Length:        0 bytes

// 以上是你打压力的host, port等一部分的信息

Concurrency Level:      100              # 并发度
Time taken for tests:   2.987 seconds    # 总时间
Complete requests:      1000             # 完成请求数目

Failed requests:        0                # 失败次数
Write errors:           0

Total transferred:      637272 bytes     # 总共传输数据
HTML transferred:       0 bytes

Requests per second:    334.74 [#/sec] (mean)   # QPS,每秒完成的请求数目,是系统最重要的指标
Time per request:       298.739 [ms] (mean)     # 每组请求用时
Time per request:       2.987 [ms] (mean, across all concurrent requests)    # 平均每个请求用时
Transfer rate:          208.32 [Kbytes/sec] received   # 网络传输速率

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.6      0       3
Processing:    24  297 780.6     39    2648
Waiting:       23  295 780.5     36    2646
Total:         25  298 781.1     39    2650
# 以上这段数据标志了一个请求从连接,发送数据,接收数据这个三个大致的时间,最小以及平均值

# 以下是请求完成时间的分布,可以看出80%的请求在41毫秒内完成
Percentage of the requests served within a certain time (ms)
  50%     39
  66%     40
  75%     41
  80%     41
  90%   2624
  95%   2647
  98%   2650
  99%   2650
 100%   2650 (longest request)

ab命令选项参数

ab命令通常还有许多选项,列举一些常用的:

-t timelimit  测试时间限制,单位秒
-s timeout    每个请求时间限制,单位秒
-v verbosity  日志输出级别,可以选择1, 2等,调试使用
-T content-type  POST/PUT接口的content-type
-p postfile      POST请求发送的数据文件

测试POST请求

POST请求经常用到,在这里把用法写下来。

以x-www-form-urlencoded形式发送

ab -n 1000 -c 100 -p post.txt -T 'application/x-www-form-urlencoded' http://www.test.com/test/api

假定需要发送的json数据为 {name : “hello,world”}。

post.txt文件内容:

name=hello,world

注意,在网上找到这种用法,普遍都这么用,但是经过实际测试,数据始终发送不成功

经过摸索,发现可以通过multipart/form-data形式发送。

以multipart/form-data形式发送

ab -n 1000 -c 100 -p post.txt -T 'multipart/form-data; boundary=--WebKitFormBoundaryE19zNvXGzXaLvS5C' http://www.test.com/test/api

post.txt文件内容如下:

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="name"

hello,world
----WebKitFormBoundaryE19zNvXGzXaLvS5C


POST的文件内容可以通过postman测试接口来查看。


性能测试得到的最重要的指标就是QPS(Requests per second),反映了接口的并发承受能力,也就是系统的峰值性能。如果对接口的调用超过了这一限制,就要考虑提升硬件或者做一些优化了。

参考

ab - Apache HTTP server benchmarking tool

Make a POST request using ab (apache benchmarking) on a django server

POSTing multipart/form-data with Apache Bench (ab)

相关内容

    暂无相关文章