Kivi

没有什么远大理想,只是永远都不会满足而已


  • 首页

  • 关于

  • 标签

  • 归档

Linux的CPU,内存,磁盘IO,网络压力测试方法

发表于 2017-08-07 更新于 2019-08-08 分类于 linux 阅读次数:
本文字数: 9.4k 阅读时长 ≈ 9 分钟

适用范围

centos

CPU和内存

stress

安装

1
sudo yum install stress -y

命令介绍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# stress
`stress' imposes certain types of compute stress on your system

Usage: stress [OPTION [ARG]] ...
-?, --help show this help statement
--version show version statement
-v, --verbose be verbose
-q, --quiet be quiet
-n, --dry-run show what would have been done
-t, --timeout N timeout after N seconds
--backoff N wait factor of N microseconds before work starts
-c, --cpu N spawn N workers spinning on sqrt()
-i, --io N spawn N workers spinning on sync()
-m, --vm N spawn N workers spinning on malloc()/free()
--vm-bytes B malloc B bytes per vm worker (default is 256MB)
--vm-stride B touch a byte every B bytes (default is 4096)
--vm-hang N sleep N secs before free (default none, 0 is inf)
--vm-keep redirty memory instead of freeing and reallocating
-d, --hdd N spawn N workers spinning on write()/unlink()
--hdd-bytes B write B bytes per hdd worker (default is 1GB)

Example: stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 10s

Note: Numbers may be suffixed with s,m,h,d,y (time) or B,K,M,G (size).

说明

stress可以从cpu,内存,磁盘io性能等指标综合起来,以系统负载为最终指标,来衡量单机的性能。这个命令更适合去评测综合能力,cpu,内存和磁盘io的同时负载能力

使用命令

1
2
3
uptime
stress -c 4 -i 1 -m 1 --vm-bytes 128M -t 10s
uptime

磁盘

dd

使用说明

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Usage: dd [OPERAND]...
or: dd OPTION
Copy a file, converting and formatting according to the operands.

bs=BYTES read and write BYTES bytes at a time (also see ibs=,obs=)
cbs=BYTES convert BYTES bytes at a time
conv=CONVS convert the file as per the comma separated symbol list
count=N copy only N input blocks
ibs=BYTES read BYTES bytes at a time (default: 512)
if=FILE read from FILE instead of stdin
iflag=FLAGS read as per the comma separated symbol list
obs=BYTES write BYTES bytes at a time (default: 512)
of=FILE write to FILE instead of stdout
oflag=FLAGS write as per the comma separated symbol list
seek=BLOCKS skip BLOCKS obs-sized blocks at start of output
skip=BLOCKS skip BLOCKS ibs-sized blocks at start of input
status=WHICH WHICH info to suppress outputting to stderr;
'noxfer' suppresses transfer stats, 'none' suppresses all

BLOCKS and BYTES may be followed by the following multiplicative suffixes:
c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M
GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.

Each CONV symbol may be:

ascii from EBCDIC to ASCII
ebcdic from ASCII to EBCDIC
ibm from ASCII to alternate EBCDIC
block pad newline-terminated records with spaces to cbs-size
unblock replace trailing spaces in cbs-size records with newline
lcase change upper case to lower case
nocreat do not create the output file
excl fail if the output file already exists
notrunc do not truncate the output file
ucase change lower case to upper case
sparse try to seek rather than write the output for NUL input blocks
swab swap every pair of input bytes
noerror continue after read errors
sync pad every input block with NULs to ibs-size; when used
with block or unblock, pad with spaces rather than NULs
fdatasync physically write output file data before finishing
fsync likewise, but also write metadata

Each FLAG symbol may be:

append append mode (makes sense only for output; conv=notrunc suggested)
direct use direct I/O for data
directory fail unless a directory
dsync use synchronized I/O for data
sync likewise, but also for metadata
fullblock accumulate full blocks of input (iflag only)
nonblock use non-blocking I/O
noatime do not update access time
noctty do not assign controlling terminal from file
nofollow do not follow symlinks
count_bytes treat 'count=N' as a byte count (iflag only)

Sending a USR1 signal to a running `dd' process makes it
print I/O statistics to standard error and then resume copying.

$ dd if=/dev/zero of=/dev/null& pid=$!
$ kill -USR1 $pid; sleep 1; kill $pid
18335302+0 records in
18335302+0 records out
9387674624 bytes (9.4 GB) copied, 34.6279 seconds, 271 MB/s

Options are:

--help display this help and exit
--version output version information and exit

Report dd bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'dd invocation'

上面说明有很多,通畅情况下,用下面的命令去测试磁盘的读写性能

写性能测试

1
time dd if=/dev/xvda1 of=/test.disk bs=8k count=300000

用dd命令进行测试,以每次写入8k的数据,执行300000次,time命令显示执行过程的时间,执行完成后生成/test.disk文件,大小为2.5G

上面的命令其实是会将数据写入到文件系统缓存(系统默认操作,提高效率),而且是磁盘连续写,所以理论上是一个磁盘写入速度的极限值,如果是直接写入到磁盘,可以添加参数oflag

1
time dd if=/dev/xvda1 of=/test.disk bs=8k count=300000 oflag=direct

读性能测试

1
time dd if=/test.disk of=/dev/xvda1 bs=8k

读写联合测试

1
time dd if=/test.disk of=/tmp/test.disk2 bs=8k

iostat

显示所有设备负载情况

1
iostat

cpu属性值说明:

  • %user:CPU处在用户模式下的时间百分比。
  • %nice:CPU处在带NICE值的用户模式下的时间百分比。
  • %system:CPU处在系统模式下的时间百分比。
  • %iowait:CPU等待输入输出完成时间的百分比。
  • %steal:管理程序维护另一个虚拟处理器时,虚拟CPU的无意识等待时间百分比。
  • %idle:CPU空闲时间百分比。

注意:如果%iowait的值过高,表示硬盘存在I/O瓶颈,%idle值高,表示CPU较空闲,如果%idle值高但系统响应慢时,有可能是CPU等待分配内存,此时应加大内存容量。%idle值如果持续低于10,那么系统的CPU处理能力相对较低,表明系统中最需要解决的资源是CPU。

disk属性值说明:

  • rrqm/s: 每秒进行 merge 的读操作数目。即 rmerge/s
  • wrqm/s: 每秒进行 merge 的写操作数目。即 wmerge/s
  • r/s: 每秒完成的读 I/O 设备次数。即 rio/s
  • w/s: 每秒完成的写 I/O 设备次数。即 wio/s
  • rsec/s: 每秒读扇区数。即 rsect/s
  • wsec/s: 每秒写扇区数。即 wsect/s
  • rkB/s: 每秒读K字节数。是 rsect/s 的一半,因为每扇区大小为512字节。
  • wkB/s: 每秒写K字节数。是 wsect/s 的一半。
  • avgrq-sz: 平均每次设备I/O操作的数据大小 (扇区)。
  • avgqu-sz: 平均I/O队列长度。
  • await: 平均每次设备I/O操作的等待时间 (毫秒)。
  • svctm: 平均每次设备I/O操作的服务时间 (毫秒)。
  • %util: 一秒中有百分之多少的时间用于 I/O 操作,即被io消耗的cpu百分比

备注:如果 %util 接近 100%,说明产生的I/O请求太多,I/O系统已经满负荷,该磁盘可能存在瓶颈。如果 svctm 比较接近 await,说明 I/O 几乎没有等待时间;如果 await 远大于 svctm,说明I/O 队列太长,io响应太慢,则需要进行必要优化。如果avgqu-sz比较大,也表示有当量io在等待

定时显示所有信息

1
2
# 每2s打印一次,一共打印3次
iostat 2 3

查看TPS和吞吐量

1
iostat -d -k 1 1

说明:

  • tps:该设备每秒的传输次数(Indicate the number of transfers per second that were issued to the device.)。“一次传输”意思是“一次I/O请求”。多个逻辑请求可能会被合并为“一次I/O请求”。“一次传输”请求的大小是未知的。
  • kB_read/s:每秒从设备(drive expressed)读取的数据量;
  • kB_wrtn/s:每秒向设备(drive expressed)写入的数据量;
  • kB_read:读取的总数据量;kB_wrtn:写入的总数量数据量;

查看设备使用率(%util)和响应时间(await)

1
iostat -d -x -k 1 1

rrqm/s: 每秒进行 merge 的读操作数目.即 delta(rmerge)/s
wrqm/s: 每秒进行 merge 的写操作数目.即 delta(wmerge)/s
r/s: 每秒完成的读 I/O 设备次数.即 delta(rio)/s
w/s: 每秒完成的写 I/O 设备次数.即 delta(wio)/s
rsec/s: 每秒读扇区数.即 delta(rsect)/s
wsec/s: 每秒写扇区数.即 delta(wsect)/s
rkB/s: 每秒读K字节数.是 rsect/s 的一半,因为每扇区大小为512字节.(需要计算)
wkB/s: 每秒写K字节数.是 wsect/s 的一半.(需要计算)
avgrq-sz:平均每次设备I/O操作的数据大小 (扇区).delta(rsect+wsect)/delta(rio+wio)
avgqu-sz:平均I/O队列长度.即 delta(aveq)/s/1000 (因为aveq的单位为毫秒).
await: 平均每次设备I/O操作的等待时间 (毫秒).即 delta(ruse+wuse)/delta(rio+wio)
svctm: 平均每次设备I/O操作的服务时间 (毫秒).即 delta(use)/delta(rio+wio)
%util: 一秒中有百分之多少的时间用于 I/O 操作,或者说一秒中有多少时间 I/O 队列是非空的,即 delta(use)/s/1000 (因为use的单位为毫秒)
如果 %util 接近 100%,说明产生的I/O请求太多,I/O系统已经满负荷,该磁盘可能存在瓶颈。 idle小于70% IO压力就较大了,一般读取速度有较多的wait。 同时可以结合vmstat 查看查看b参数(等待资源的进程数)和wa参数(IO等待所占用的CPU时间的百分比,高过30%时IO压力高)。

另外 await 的参数也要多和 svctm 来参考。差的过高就一定有 IO 的问题。

avgqu-sz 也是个做 IO 调优时需要注意的地方,这个就是直接每次操作的数据的大小,如果次数多,但数据拿的小的话,其实 IO 也会很小。如果数据拿的大,才IO 的数据会高。也可以通过 avgqu-sz × ( r/s or w/s ) = rsec/s or wsec/s。也就是讲,读定速度是这个来决定的。

svctm 一般要小于 await (因为同时等待的请求的等待时间被重复计算了),svctm 的大小一般和磁盘性能有关,CPU/内存的负荷也会对其有影响,请求过多也会间接导致 svctm 的增加。await 的大小一般取决于服务时间(svctm) 以及 I/O 队列的长度和 I/O 请求的发出模式。如果 svctm 比较接近 await,说明 I/O 几乎没有等待时间;如果 await 远大于 svctm,说明 I/O 队列太长,应用得到的响应时间变慢,如果响应时间超过了用户可以容许的范围,这时可以考虑更换更快的磁盘,调整内核 elevator 算法,优化应用,或者升级 CPU。

队列长度(avgqu-sz)也可作为衡量系统 I/O 负荷的指标,但由于 avgqu-sz 是按照单位时间的平均值,所以不能反映瞬间的 I/O 洪水。

形象的比喻:
r/s+w/s 类似于交款人的总数
平均队列长度(avgqu-sz)类似于单位时间里平均排队人的个数
平均服务时间(svctm)类似于收银员的收款速度
平均等待时间(await)类似于平均每人的等待时间
平均I/O数据(avgrq-sz)类似于平均每人所买的东西多少
I/O 操作率 (%util)类似于收款台前有人排队的时间比例
设备IO操作:总IO(io)/s = r/s(读) +w/s(写)

平均等待时间=单个I/O服务器时间*(1+2+…+请求总数-1)/请求总数

每秒发出的I/0请求很多,但是平均队列就4,表示这些请求比较均匀,大部分处理还是比较及时。

网络

iperf

测试方法

两台机器,先安装iperf工具,然后一台机器以server方式启动,另外一台以client方式运行,进行网络压力测试

参数说明

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
-s 以server模式启动,eg:iperf -s
-c 以client模式启动,host是server端地址,eg:iperf -c 1.1.1.1

通用参数
-f [k|m|K|M] 分别表示以Kbits, Mbits, KBytes, MBytes显示报告,默认以Mbits为单位,eg:iperf -c 222.35.11.23 -f K

-i sec 以秒为单位显示报告间隔,eg:iperf -c 222.35.11.23 -i 2
iperf是client端向server端发送数据
server端显示的是接收速率,最好加i参数,进行速率跟踪
client 显示的是发送速率
server 显示接收速率

-l 缓冲区大小,默认是8KB,eg: iperf -c 1.1.1.1 -l 16 可以使用不同的包长,进行测试

-m 显示tcp最大mtu值
-o 将报告和错误信息输出到文件 eg: iperf -c 1.1.1.1 -o c:iperflog.txt
-p 指定服务器端使用的端口或客户端所连接的端口 eg: iperf -s -p 9999;iperf -c 222.35.11.23 -p 9999

-u 使用udp协议, 测试htb的时候最好用udp,udp通信开销小,测试的带宽更准确
-w 指定TCP窗口大小,默认是8KB,如果窗口太小,有可能丢包

-B 绑定一个主机地址或接口(当主机有多个地址或接口时使用该参数)
-C 兼容旧版本(当server端和client端版本不一样时使用)
-M 设定TCP数据包的最大mtu值
-N 设定TCP不延时
-V 传输ipv6数据包

server专用参数
-D 以服务方式运行ipserf,eg: iperf -s -D
-R 停止iperf服务,针对-D,eg: iperf -s -R

client端专用参数
-d 同时进行双向传输测试
-n 指定传输的字节数,eg: iperf -c 222.35.11.23 -n 100000
-r 单独进行双向传输测试

-b 指定发送带宽,默认是1Mbit/s

在测试qos的时候,这是最有用的参数。
-t 测试时间,默认10秒, eg: iperf -c 222.35.11.23 -t 5

默认是10s

-F 指定需要传输的文件
-T 指定ttl值

###@ 使用示例
服务端:

1
iperf -s -u

-s 标记此端为服务端
-u 标记自己为UDP监听
-p 指定自己监听端口

客户端:

1
iperf -c 1.1.1.1 -i 1 -u -t 60 -F /root/a.zip -P 5

-c 标记自己为客户端
-i 设定输出值间隔
-u 使用传输协议为UDP
-t 设定测试时间为60秒
-F 指定传输文件(该项可有可无)
-P 指定进程数,如果设置为5,那么也就相当与对端建立五个连接

注意事项:
1.发包测试需要分为UDP测试与TCP测试,其中服务端需要用-u命令去区分监听协议。
2.TCP协议测试不能计算出时延与丢包率,而且还不能指定发送带宽。

参考链接

How To Stress Test CPU and Memory (VM) On a Linux and Unix With Stress-ng
iostat 监视I/O子系统,这个连接下面有非常多实用的Linux工具使用说明
使用iperf测试网络性能

# linux
nginx流量复制
峨眉山
  • 文章目录
  • 站点概览
kivi

kivi

nodejs | server
58 日志
17 分类
32 标签
RSS
  1. 1. 适用范围
  2. 2. CPU和内存
    1. 2.1. stress
      1. 2.1.1. 安装
      2. 2.1.2. 命令介绍
      3. 2.1.3. 说明
      4. 2.1.4. 使用命令
  3. 3. 磁盘
    1. 3.1. dd
      1. 3.1.1. 使用说明
      2. 3.1.2. 写性能测试
      3. 3.1.3. 读性能测试
      4. 3.1.4. 读写联合测试
    2. 3.2. iostat
      1. 3.2.1. 显示所有设备负载情况
      2. 3.2.2. 定时显示所有信息
      3. 3.2.3. 查看TPS和吞吐量
      4. 3.2.4. 查看设备使用率(%util)和响应时间(await)
  4. 4. 网络
    1. 4.1. iperf
      1. 4.1.1. 测试方法
      2. 4.1.2. 参数说明
  5. 5. 参考链接
© 2019 kivi | 173k | 2:37
由 Hexo 强力驱动 v3.9.0
|
主题 – NexT.Pisces v7.3.0
|