1.安装及配置

1
2
3
4
sudo apt-get install apache2
sudo vi /etc/apache2/port.conf 将 80 修改为 81
目的:避免 80 端口被占用导致 nginx 无法使用,安装 apache2 目的是使用 ab 工具对接口做压力测试,并不是使用 apache 搭建服务器

2.使用

1
2
3
4
5
6
ab -h 查看帮助文档
-n 发送请求个数
-C 同一时间内请求并发数
-k 服务器 keeplive 时间
-t 设置测试时间 (如果有对接口请求时间需求可配置该参数)

3.GET请求测试

说明:因为当前 API 设计是使用 POST 方式请求,如果想使用 GET 方式测试接口性能,首先将接口请求方式调整。

模拟 GET 请求网站首页接口,10000次请求,并发数为100

1
ab -n10000 -C100 url

4.POST请求测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
关闭接口缓存测试,1000个请求同时10个并发测试列表接口性能
ab -n1000 -c10 -k -T 'application/x-www-form-urlencoded' -p post.json url
post.json post 请求参数文件,存放接口需要的参数值
格式:
{"pagesize":48,"page":1,"sort":"xin","type":"xiaoyuan"}
{
"pagesize":48,
"page":1,
"sort":"xin",
"type":"xiaoyuan"
}

5.测试数据分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Benchmarking server_name (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: nginx
Server Hostname: server_name
Server Port: 80
Document Path: /list/
Document Length: 36705 bytes
Concurrency Level: 10
Time taken for tests: 14.384 seconds
Complete requests: 1000
Failed requests: 0
Keep-Alive requests: 0
Total transferred: 36951000 bytes
Total body sent: 245000
HTML transferred: 36705000 bytes
Requests per second: 69.52 [#/sec] (mean)
Time per request: 143.840 [ms] (mean)
Time per request: 14.384 [ms] (mean, across all concurrent requests)
Transfer rate: 2508.68 [Kbytes/sec] received
16.63 kb/s sent
2525.32 kb/s total
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 83 143 74.2 139 1974
Waiting: 77 136 73.9 131 1962
Total: 83 144 74.2 139 1974
Percentage of the requests served within a certain time (ms)
50% 139
66% 146
75% 151
80% 155
90% 168
95% 181
98% 198
99% 221
100% 1974 (longest request)
每个请求时间消耗 143.840ms ≈ 0.1s

6.标准

说明: 关闭接口缓存设置,单纯测试接口性能。

6.1 参数配置:

1
-n10000 -c10 -k -t1 -T 'application/x-www-form-urlencoded'

6.2 结果分析:

  • 每次请求时间(最优时间 0.1s, 最低时间 0.5s)
  • 观察有效请求成功率

6.3 外在因素:

  • 机器本身配置
  • 数据库数据体量
  • 服务器和数据库配置