首页 运维

压测AB以及监控工具详解

发布于: 2020-06-27

客户端压测工具AB

1
ab(选项)(参数) 地址

-A:指定连接服务器的基本的认证凭据;
-c:指定一次向服务器发出请求数;
-C:添加cookie;
-g:将测试结果输出为“gnuolot”文件;
-h:显示帮助信息;
-H:为请求追加一个额外的头;
-i:使用“head”请求方式;
-k:激活HTTP中的“keepAlive”特性;
-n:指定测试会话使用的请求数;
-p:指定包含数据的文件;
-q:不显示进度百分比;
-T:使用POST数据时,设置内容类型头;
-v:设置详细模式等级;
-w:以HTML表格方式打印结果;
-x:以表格方式输出时,设置表格的属性;
-X:使用指定的代理服务器发送请求;
-y:以表格方式输出时,设置表格属性。

1
ab -c 200 -n 200 http://127.0.0.1:3000/

运行结果

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
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
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
Finished 200 requests


Server Software:
Server Hostname: 127.0.0.1 #域名
Server Port: 3000 #请求端口号

Document Path: / #文件路径
Document Length: 11 bytes #页面字节数

Concurrency Level: 200 #请求的并发数
Time taken for tests: 0.192 seconds #总访问时间
Complete requests: 200 #请求成功数量
Failed requests: 0 #请求失败数量
Total transferred: 29400 bytes #请求总数据大小(包括header头信息)
HTML transferred: 2200 bytes #html页面实际总字节数
Requests per second: 1041.68 [#/sec] (mean) #每秒多少请求
Time per request: 191.998 [ms] (mean) #用户平均请求等待时间
Time per request: 0.960 [ms] (mean, across all concurrent requests) # 服务器平均处理时间
Transfer rate: 149.54 [Kbytes/sec] received #每秒获取的数据长度

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 1
Processing: 9 94 49.2 95 179
Waiting: 3 90 51.0 91 178
Total: 9 95 49.2 95 179

Percentage of the requests served within a certain time (ms)
50% 95 #50%用户请求在95ms内返回
66% 122 #66%用户请求在122ms内返回
75% 138 #75%用户请求在138ms内返回
80% 146 #80%用户请求在146ms内返回
90% 163 #90%用户请求在163ms内返回
95% 171 #95%用户请求在171ms内返回
98% 176 #98%用户请求在176ms内返回
99% 178 #99%用户请求在178ms内返回
100% 179 (longest request)

Requests per second: 1041.68 [#/sec] (mean) (吞吐率,简称QPS)每秒服务器承载请求量是多少,以上测试数据表示每秒可以处理 1041.68并发量,也就是说有 1100个用户去访问这个服务器,这个服务器是可以抗的住
Time per request: 191.998 [ms] (mean) 每次请求花费多少毫秒
Time per request: 0.960 [ms] (mean, across all concurrent requests)并发情况下,0.960毫秒得到结果
Transfer rate: 149.54 [Kbytes/sec] received(吞吐量) 每秒这个服务器吞/吐的数据量是多少

性能分析工具

Node自带 profile

1
node --prof index.js

php分析工具xdebug+webgrind

1
2
pecl install xdebug
https://github.com/jokkedk/webgrind.git

php分析工具 xhprof

1
2
http://pecl.php.net/package/xhprof
https://github.com/tideways/php-xhprof-extension

服务端监控

客户端服务端也需要开启监控,检测具体问题所在

查看cpu和内存使用率

1
2
3
4
5
6
7
8
9
10
[root@VM_0_4_centos ~]# top
top - 01:19:19 up 6 days, 9 min, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 122 total, 1 running, 121 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.2 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3880224 total, 235108 free, 973224 used, 2671892 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 2615988 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 43692 3692 2288 S 0.0 0.1 0:32.31 systemd

第一行:概况

  1. 当前时间 01:19:19
  2. 从本次开机到现在经过的时间 up 6 days,9 min
  3. 当前有几个用户登录到该机器。1 user
  4. 系统1分钟、5分钟、15分钟内的平均负载值load average: 0.00, 0.01, 0.05

第二行:进程计数(Tasks)

  1. 进程总数122 total
  2. 正在运行的进程数1 running
  3. 睡眠的进程数121 sleeping
  4. 停止的进程数0 stopped
  5. 僵尸进程数0 zombie

第三行:CPU使用率(%Cpu(s))

  1. 进程在用户空间(user)消耗的CPU时间占比,不包含调整过优先级的进程。%Cpu(s): 0.2 us
  2. 进程在内核空间(system)消耗的CPU时间占比。0.2 sy
  3. 调整过用户态优先级的(niced)进程的CPU时间占比。0.0 ni
  4. 空闲的(idle)CPU时间占比。99.7 id
  5. 等待(wait)I/O完成的CPU时间占比。0.0 wa
  6. 处理硬中断(hardware interrupt)的CPU时间占比。0.0 hi
  7. 处理软中断(software interrupt)的CPU时间占比。0.0 si
  8. 当Linux系统是在虚拟机中运行时,等待CPU资源的时间(steal time)占比。0.0 st

第四、五行:物理内存和交换空间(Mem/Swap)

  1. 内存总量。3880224 total
  2. 空闲内存量。235108 free
  3. 使用中的内存量。973224 used
  4. 缓存和page cache占用的内存量。2671892 buff/cache

接下来就是进程详细信息

  1. PID进程ID
  2. USER进程所有者的用户名。
  3. PR从系统内核角度看的进程调度优先级。
  4. NI进程的nice值,即从用户空间角度看的进程优先级。值越低,优先级越高。
  5. VIRT进程申请使用的虚拟内存量。
  6. RES进程使用的驻留内存(即未被swap out的内存)量。
  7. SHR进程使用的共享内存量。
  8. S进程状态。R=running,S=interruptible sleeping,D=uninterruptible sleeping,T=stopped,Z=zombie。
  9. %CPU进程在一个更新周期内占用的CPU时间比例。
  10. %MEM进程占用的物理内存比例。
  11. TIME+进程创建后至今占用的CPU时间长度。
  12. COMMAND运行进程使用的命令。

iostat 查看硬盘带宽命令

1
2
3
4
5
6
7
8
9
10
11
[root@VM_0_4_centos ~]# yum install -y sysstat
[root@VM_0_4_centos ~]# iostat
Linux 3.10.0-1062.18.1.el7.x86_64 (VM_0_4_centos) 06/27/2020 _x86_64_ (2 CPU)

avg-cpu: %user %nice %system %iowait %steal %idle
0.35 0.00 0.20 0.09 0.00 99.35

Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
vda 3.10 3.39 22.73 1758510 11783276
vdb 1.69 5.79 14.56 3001589 7547888
scd0 0.00 0.00 0.00 910 0

%user:CPU处在用户模式下的时间百分比。

%nice:CPU处在带NICE值的用户模式下的时间百分比。

%system:CPU处在系统模式下的时间百分比。

%iowait:CPU等待输入输出完成时间的百分比。

%steal:管理程序维护另一个虚拟处理器时,虚拟CPU的无意识等待时间百分比。

%idle:CPU空闲时间百分比。

tps:该设备每秒的传输次数

kB_read/s:每秒从设备(drive expressed)读取的数据量;

kB_wrtn/s:每秒向设备(drive expressed)写入的数据量;

kB_read: 读取的总数据量;

kB_wrtn:写入的总数量数据量;

注意:

如果%iowait的值过高,表示硬盘存在I/O瓶颈

如果%idle值高,表示CPU较空闲

如果%idle值高但系统响应慢时,可能是CPU等待分配内存,应加大内存容量。

如果%idle值持续低于10,表明CPU处理能力相对较低,系统中最需要解决的资源是CPU。

常见命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 每隔2秒刷新显示,且显示3次
iostat 2 3

# 显示指定磁盘信息
iostat -d /dev/vdb1

# 显示tty和cpu信息
iostat -t

# 以M为单位显示所有信息
iostat -m

# 查看设备使用率(%util)、响应时间(await)
iostat -d -x -k 1 1