1. 写在前面
本文主要介绍:在 Linux 系统上进行压力测试两个重要工具:stress
& stress-ng
。
2. Linux 压力测试工具:stress 和 stress-ng
stress
和 stress-ng
是评估和测试 Linux 系统在各种条件下性能的重要工具。
stress:
stress
是一款简单但功能强大的工具,可对 Linux 系统施加可配置的 CPU、内存、I/O 或磁盘压力。通过模拟繁重的工作负载,系统管理员可以观察系统在压力下的表现反应。
这一工具的价值在于找出系统潜在的薄弱环节,确保系统能够在不影响性能的情况下处理繁重的任务。
stress-ng:
stress-ng
是 stress
的扩展版本,它超越了前者的基本功能,提供的压力测试范围更广,不仅包括 CPU、内存、I/O 和磁盘压力,还包括对进程间通信、套接字和各种文件操作的额外测试。
stress
和 stress-ng
对 Linux 系统的前瞻性管理都有很大帮助,使管理员和程序员能够优化系统配置、识别潜在问题并提高系统的整体可靠性。
3. 如何在 Linux 中安装 “stress” 工具
要在 Linux 上安装 stress
,请针对具体的 Linux 发行版使用以下适当的命令:
sudo apt install stress [On Debian, Ubuntu and Mint]
sudo yum install stress [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/stress [On Gentoo Linux]
sudo apk add stress [On Alpine Linux]
sudo pacman -S stress [On Arch Linux]
sudo zypper install stress [On OpenSUSE]
sudo pkg install stress [On FreeBSD]
stress 基本语法:
sudo stress option argument
常规选项:
查看帮助信息:
root@jpzhang:~# stress --help
--------------------------------------------------------------------------------
`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).
对应介绍:
-
-c, –cpu N: 产生 N 个进程,每个进程都反复不停的计算随机数的平方根;
-
-i, –io N: 产生 N 个进程,每个进程反复调用 sync() 将内存上的内容写到硬盘上;
-
-m, –vm N: 产生 N 个进程,每个进程不断分配和释放内存;
-
–vm-bytes B: 指定分配内存的大小;
-
–vm-stride B: 不断的给部分内存赋值,让 COW(Copy On Write)发生;
-
–vm-hang N: 指示每个消耗内存的进程在分配到内存后转入睡眠状态 N 秒,然后释放内存,一直重复执行这个过程;
-
–vm-keep: 一直占用内存,区别于不断的释放和重新分配(默认是不断释放并重新分配内存);
-
-d, –hadd N: 产生 N 个不断执行 write 和 unlink 函数的进程(创建文件,写入内容,删除文件);
-
–hadd-bytes B: 指定文件大小;
-
-t, –timeout N: 在 N 秒后结束程序;
-
–backoff N: 等待N微妙后开始运行;
-
-q, –quiet: 程序在运行的过程中不输出信息;
-
-n, –dry-run: 输出程序会做什么而并不实际执行相关的操作;
-
–version: 显示版本号;
-
-v, –verbose: 显示详细的信息;
3.1 如何在 Linux 系统上使用 stress ?
要检查每次运行该命令 (stress) 的效果,首先运行
uptime
命令并记录下平均负载;
3.2 消耗 CPU 资源
示例1:
命令: sudo stress --cpu 8 --timeout 20
stress
消耗 CPU 资源是通过调用 sqrt() 函数(计算由 rand()
函数产生的随机数的平方根)实现。下面的命令会产生 8 个这样的子进程不断计算,超时时间为20秒,结束后再次运行 uptime
比较平均负载:
root@jpzhang:~# uptime
--------------------------------------------------------------------------------
00:21:12 up 26 days, 39 min, 1 user, load average: 0.00, 0.03, 0.02
root@jpzhang:~# sudo stress --cpu 8 --timeout 20
--------------------------------------------------------------------------------
stress: info: [32180] dispatching hogs: 8 cpu, 0 io, 0 vm, 0 hdd
stress: info: [32180] successful run completed in 20s
root@jpzhang:~# uptime
--------------------------------------------------------------------------------
00:21:46 up 26 days, 40 min, 1 user, load average: 2.09, 0.54, 0.19
也可以使用 top
命令查看 CPU 的状态(CPU 在用户态满负荷运转):
root@jpzhang:~# top
--------------------------------------------------------------------------------
top - 00:25:47 up 26 days, 44 min, 2 users, load average: 1.27, 0.50, 0.23
Tasks: 115 total, 9 running, 65 sleeping, 0 stopped, 0 zombie
%Cpu0 : 99.7 us, 0.3 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8167832 total, 5846128 free, 175968 used, 2145736 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 7688540 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
32303 root 20 0 8248 92 0 R 25.2 0.0 0:03.29 stress
32304 root 20 0 8248 92 0 R 25.2 0.0 0:03.29 stress
32305 root 20 0 8248 92 0 R 25.2 0.0 0:03.28 stress
32306 root 20 0 8248 92 0 R 25.2 0.0 0:03.28 stress
32308 root 20 0 8248 92 0 R 25.2 0.0 0:03.30 stress
32301 root 20 0 8248 92 0 R 24.8 0.0 0:03.28 stress
32302 root 20 0 8248 92 0 R 24.8 0.0 0:03.27 stress
32307 root 20 0 8248 92 0 R 24.8 0.0 0:03.27 stress
32271 bolean 20 0 107988 3560 2552 S 0.3 0.0 0:00.05 sshd
示例2:
启动 8 个 sqrt() 子进程,超时时间 30s,同时显示有关操作的详细信息;
命令: sudo stress --cpu 8 -v --timeout 30s
root@jpzhang:~# uptime
--------------------------------------------------------------------------------
00:29:47 up 26 days, 48 min, 2 users, load average: 0.05, 0.34, 0.23
root@jpzhang:~# sudo stress --cpu 8 -v --timeout 30s
--------------------------------------------------------------------------------
stress: info: [32312] dispatching hogs: 8 cpu, 0 io, 0 vm, 0 hdd
stress: dbug: [32312] using backoff sleep of 24000us
stress: dbug: [32312] setting timeout to 30s
stress: dbug: [32312] --> hogcpu worker 8 [32313] forked
stress: dbug: [32312] using backoff sleep of 21000us
stress: dbug: [32312] setting timeout to 30s
stress: dbug: [32312] --> hogcpu worker 7 [32314] forked
stress: dbug: [32312] using backoff sleep of 18000us
stress: dbug: [32312] setting timeout to 30s
stress: dbug: [32312] --> hogcpu worker 6 [32315] forked
stress: dbug: [32312] using backoff sleep of 15000us
stress: dbug: [32312] setting timeout to 30s
stress: dbug: [32312] --> hogcpu worker 5 [32316] forked
stress: dbug: [32312] using backoff sleep of 12000us
stress: dbug: [32312] setting timeout to 30s
stress: dbug: [32312] --> hogcpu worker 4 [32317] forked
stress: dbug: [32312] using backoff sleep of 9000us
stress: dbug: [32312] setting timeout to 30s
stress: dbug: [32312] --> hogcpu worker 3 [32318] forked
stress: dbug: [32312] using backoff sleep of 6000us
stress: dbug: [32312] setting timeout to 30s
stress: dbug: [32312] --> hogcpu worker 2 [32319] forked
stress: dbug: [32312] using backoff sleep of 3000us
stress: dbug: [32312] setting timeout to 30s
stress: dbug: [32312] --> hogcpu worker 1 [32320] forked
stress: dbug: [32312] <-- worker 32316 signalled normally
stress: dbug: [32312] <-- worker 32313 signalled normally
stress: dbug: [32312] <-- worker 32314 signalled normally
stress: dbug: [32312] <-- worker 32315 signalled normally
stress: dbug: [32312] <-- worker 32317 signalled normally
stress: dbug: [32312] <-- worker 32318 signalled normally
stress: dbug: [32312] <-- worker 32319 signalled normally
stress: dbug: [32312] <-- worker 32320 signalled normally
stress: info: [32312] successful run completed in 30s
root@jpzhang:~# uptime
--------------------------------------------------------------------------------
00:30:58 up 26 days, 49 min, 2 users, load average: 1.80, 0.95, 0.46
3.3 消耗内存资源
示例1:
产生 8 个子进程,超时时间 20s,每个进程不断分配和释放内存;
命令: sudo stress --vm 8 --timeout 20s
root@jpzhang:~# uptime
--------------------------------------------------------------------------------
00:34:59 up 26 days, 53 min, 2 users, load average: 0.23, 0.47, 0.37
root@jpzhang:~# sudo stress --vm 8 --timeout 20s
--------------------------------------------------------------------------------
stress: info: [32332] dispatching hogs: 0 cpu, 0 io, 8 vm, 0 hdd
stress: info: [32332] successful run completed in 20s
root@jpzhang:~# uptime
--------------------------------------------------------------------------------
00:35:38 up 26 days, 54 min, 2 users, load average: 1.89, 0.90, 0.52
示例2:
产生 2 个子进程,每个进程分配 2048M 内存;
命令: stress --vm 2 --vm-bytes 2048M --vm-keep --timeout 20s
root@jpzhang:~# stress --vm 2 --vm-bytes 2048M --vm-keep --timeout 20s
--------------------------------------------------------------------------------
stress: info: [32377] dispatching hogs: 0 cpu, 0 io, 2 vm, 0 hdd
stress: info: [32377] successful run completed in 21s
检查内存:
运行前:
root@jpzhang:~# free -h
--------------------------------------------------------------------------------
total used free shared buff/cache available
Mem: 7.8G 171M 5.6G 676K 2.0G 7.3G
Swap: 0B 0B 0B
运行后:
root@jpzhang:~# free -h
--------------------------------------------------------------------------------
total used free shared buff/cache available
Mem: 7.8G 4.2G 1.6G 676K 2.0G 3.3G
Swap: 0B 0B 0B
父进程处于睡眠状态,两个子进程负责资源消耗。
示例3: –vm-keep
& –vm-hang N
–vm-keep:
一直占用内存,区别于不断的释放和重新分配(默认是不断释放并重新分配内存);
–vm-hang N:
指示每个消耗内存的进程在分配到内存后转入睡眠状态 N 秒,然后释放内存,一直重复执行这个过程;
–vm-keep
和 --vm-hang
都可以用来模拟只有少量内存的机器,但是指定它们时 CPU 的使用情况是不一样的,参考如下对比实验:
例如:
运行前 top
:
root@jpzhang:~# top
--------------------------------------------------------------------------------
top - 00:47:09 up 26 days, 1:05, 2 users, load average: 0.36, 0.73, 0.64
Tasks: 103 total, 1 running, 63 sleeping, 0 stopped, 0 zombie
%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 0.3 us, 0.7 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8167832 total, 5846336 free, 175604 used, 2145892 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 7688904 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
32384 root 20 0 44576 4024 3388 R 0.7 0.0 0:00.08 top
32271 bolean 20 0 107988 3560 2552 S 0.3 0.0 0:00.16 sshd
-
--vm-keep
:CPU 负载高
命令: stress --vm 2 --vm-bytes 2048M --vm-keep --timeout 20s
root@jpzhang:~# stress --vm 2 --vm-bytes 2048M --vm-keep --timeout 20s
--------------------------------------------------------------------------------
stress: info: [32386] dispatching hogs: 0 cpu, 0 io, 2 vm, 0 hdd
stress: info: [32386] successful run completed in 21s
一直在进行默认的 stride 操作,cpu user 非常高(cpu 在用户态忙碌)
内存负载:
运行前:
root@jpzhang:~# free -h
--------------------------------------------------------------------------------
total used free shared buff/cache available
Mem: 7.8G 171M 5.6G 676K 2.0G 7.3G
Swap: 0B 0B 0B
运行后:
root@jpzhang:~# free -h
--------------------------------------------------------------------------------
total used free shared buff/cache available
Mem: 7.8G 4.2G 1.6G 676K 2.0G 3.3G
Swap: 0B 0B 0B
CPU负载:
root@jpzhang:~# top
--------------------------------------------------------------------------------
top - 00:48:25 up 26 days, 1:06, 2 users, load average: 0.54, 0.65, 0.62
Tasks: 107 total, 3 running, 64 sleeping, 0 stopped, 0 zombie
%Cpu0 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 99.7 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
KiB Mem : 8167832 total, 1645564 free, 4376376 used, 2145892 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 3488132 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
32387 root 20 0 2105404 2.000g 212 R 99.7 25.7 0:12.51 stress
-
-vm-hang N
:CPU 负载不高
命令: stress --vm 2 --vm-bytes 2048M --vm-hang 5 --timeout 20s
root@jpzhang:~# stress --vm 2 --vm-bytes 2048M --vm-hang 5 --timeout 20s
--------------------------------------------------------------------------------
stress: info: [32390] dispatching hogs: 0 cpu, 0 io, 2 vm, 0 hdd
stress: info: [32390] successful run completed in 20s
内存负载:
运行前:
root@jpzhang:~# free -h
--------------------------------------------------------------------------------
total used free shared buff/cache available
Mem: 7.8G 170M 5.6G 676K 2.0G 7.3G
Swap: 0B 0B 0B
运行后:
root@jpzhang:~# free -h
--------------------------------------------------------------------------------
total used free shared buff/cache available
Mem: 7.8G 4.2G 1.6G 676K 2.0G 3.3G
Swap: 0B 0B 0B
CPU负载:
root@jpzhang:~# top
--------------------------------------------------------------------------------
top - 00:54:46 up 26 days, 1:13, 2 users, load average: 0.14, 0.22, 0.42
Tasks: 107 total, 1 running, 66 sleeping, 0 stopped, 0 zombie
%Cpu0 : 7.6 us, 33.4 sy, 0.0 ni, 58.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 6.3 us, 34.1 sy, 0.0 ni, 59.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8167832 total, 1645436 free, 4376496 used, 2145900 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 3488012 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
32391 root 20 0 2105404 2.000g 212 S 40.5 25.7 0:04.79 stress
32392 root 20 0 2105404 2.000g 212 S 40.5 25.7 0:04.78 stress
示例4: –vm-stride B
不断的给部分内存赋值,让 COW(Copy On Write)发生,只要指定了内存相关的选项,这个操作就会执行,默认大小为的 4096M,赋值内存的比例由参数决定:
for (i = 0; i < bytes; i += stride)
ptr[i] = ‘Z’; /* Ensure that COW happens. */
bytes 为消耗的总内存大小,stride 为间隔。该参数会影响 CPU:us 和 sy。
为什么会产生这样的结果?原因是单独的赋值和对比操作可以让 CPU 在用户态的负载占到 99% 以上。–vm-stride 值增大就意味着减少赋值和对比操作,这样就增加了内存的释放和分配次数(cpu在内核空间的负载)。
不指定 –vm-stride 选项就使用默认值是 4096;
命令1: stress --vm 2 --vm-bytes 500M --vm-stride 64M --timeout 20s
root@jpzhang:~# stress --vm 2 --vm-bytes 500M --vm-stride 64M --timeout 20s
--------------------------------------------------------------------------------
stress: info: [32410] dispatching hogs: 0 cpu, 0 io, 2 vm, 0 hdd
stress: info: [32403] successful run completed in 20s
CPU 负载:
root@jpzhang:~# top
--------------------------------------------------------------------------------
top - 01:23:26 up 26 days, 1:41, 2 users, load average: 0.44, 0.27, 0.18
Tasks: 107 total, 3 running, 64 sleeping, 0 stopped, 0 zombie
%Cpu0 : 13.2 us, 85.4 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 1.3 si, 0.0 st
%Cpu1 : 12.3 us, 86.7 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 1.0 si, 0.0 st
KiB Mem : 8167832 total, 5772220 free, 249656 used, 2145956 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 7614852 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
32420 root 20 0 520252 324 212 R 95.0 0.0 0:09.10 stress
32421 root 20 0 8248 304 212 R 94.7 0.0 0:09.08 stress
命令2: stress --vm 2 --vm-bytes 500M --vm-stride 1024M --timeout 20s
root@jpzhang:~# stress --vm 2 --vm-bytes 500M --vm-stride 1024M --timeout 20s
--------------------------------------------------------------------------------
stress: info: [32422] dispatching hogs: 0 cpu, 0 io, 2 vm, 0 hdd
stress: info: [32422] successful run completed in 20s
CPU 负载:
root@jpzhang:~# top
--------------------------------------------------------------------------------
top - 01:27:40 up 26 days, 1:46, 2 users, load average: 0.47, 0.30, 0.21
Tasks: 107 total, 3 running, 64 sleeping, 0 stopped, 0 zombie
%Cpu0 : 14.2 us, 85.8 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 12.3 us, 87.7 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8167832 total, 5836312 free, 185536 used, 2145984 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 7678972 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
32427 root 20 0 520252 308 212 R 97.3 0.0 0:14.02 stress
32428 root 20 0 8248 308 212 R 97.3 0.0 0:14.07 stress
3.4 消耗 IO 资源
示例1:
产生 8 个进程,每个进程都反复调用 sync() 函数将内存上的内容写到硬盘上;
命令: stress -i 8 --timeout 20s
root@jpzhang:~# stress -i 8 --timeout 20s
--------------------------------------------------------------------------------
stress: info: [32435] dispatching hogs: 0 cpu, 8 io, 0 vm, 0 hdd
stress: info: [32435] successful run completed in 20s
使用 top 命令查看 CPU 的状态,sy 升高,wa(iowait) 非常高。
3.5 压测磁盘及 IO
示例1:
产生 1 个进程不断的在磁盘上创建 10M 大小的文件并写入内容;
命令: stress -d 1 --hdd-bytes 10M --timeout 20s
root@jpzhang:~# stress -d 1 --hdd-bytes 10M --timeout 20s
--------------------------------------------------------------------------------
stress: info: [32451] dispatching hogs: 0 cpu, 0 io, 0 vm, 1 hdd
stress: info: [32451] successful run completed in 20s
使用 top 命令查看 CPU 的状态(此时的 CPU 主要消耗在内核态),iostat 2
输出,高 iowait,瓶颈是写磁盘。
root@jpzhang:~# iostat 2
--------------------------------------------------------------------------------
Linux 4.15.0-201-generic (jpzhang) 03/20/24 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.06 0.00 0.02 0.02 0.00 99.89
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
loop0 0.00 0.00 0.00 5 0
vda 0.85 0.69 3.01 1545426 6788058
avg-cpu: %user %nice %system %iowait %steal %idle
0.28 0.00 18.33 81.39 0.00 0.00
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
loop0 0.00 0.00 0.00 0 0
vda 132.50 0.00 99192.00 0 198384
3.6 其它选项
-
–verbose: 显示 stress 程序运行过程中的详细信息;
-
–timeout N: 在 N 秒后结束程序;
-
–quiet: stress 程序运行的过程中不输出信息;
-
-n, –dry-run: 不实际执行相关的操作;
-
–backoff N: 让新 fork 出来的进程 sleep N 微秒再开始运行;
除了单独指定某一类的选项,还可以同时执行多个类型的任务,比如产生 4 个 CPU 进程、3 个 IO 进程、2 个 256M 的 vm 进程,并且每个 vm 进程中循环分配释放内存:
sudo stress --cpu 4 --io 3 --vm 2 --vm-bytes 256M --vm-keep --timeout 20s
4. 如何在 Linux 中安装 “stress-ng” 工具
stress-ng
完全兼容 stress
, 并且在此基础上通过几百个参数,可以产生各种复杂的压力。
要在 Linux 上安装 stress-ng
,请针对具体的 Linux 发行版使用以下适当的命令:
sudo apt install stress-ng [On Debian, Ubuntu and Mint]
sudo yum install stress-ng [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/stress-ng [On Gentoo Linux]
sudo apk add stress-ng [On Alpine Linux]
sudo pacman -S stress-ng [On Arch Linux]
sudo zypper install stress-ng [On OpenSUSE]
sudo pkg install stress-ng [On FreeBSD]
stress-ng 基本语法:
sudo stress-ng option argument
常规选项:
-
-h,–help:显示帮助信息;
-
–version:显示版本信息;
-
-t,–timeout:指定程序运行结束的时间,后面直接跟数字,单位为秒;
-
-c,–-cpu:后面空格跟一个整数,表示测试CPU的进程数,–cpu 4 :表示生成4个worker循环调用sqrt()产生cpu压力;
-
-i,–io:后面空格跟一个整数,表示测试磁盘I/O的进程数, –io 4 :表示生成4个worker循环调用sync()产生io压力;
-
-m,–vm:后面空格跟一个整数,表示测试内存的进程数,–vm 4 :表示生成4个worker循环调用malloc()/free()产生内存压力;
-
-d,–hdd:后面空格直接跟数字,表示产生执行write和unlink函数的进程数,用于磁盘负载测试;
-
–cpu-method:指定CPU的测试方法,后面空格直接跟方法名;
-
–metrics:输出命令执行指标;
-
–vm-bytes:指定在内存测试时malloc的字节数,默认256M;
-
–class:指定测试类别,后面空格跟类别名;
-
–matrix:启用矩阵测试模式,后面空格跟矩阵模式名;
-
–random-seed:指定测试随机种子,后面空格跟种子名;
-
–sequential:启用顺序测试模式;
-
–hdd-bytes:指定写的字节数;
-
–cpu-load:CPU负载占比,后面空格直接跟数字,单位为百分比;
4.1 如何在 Linux 系统中使用 “stress-ng”?
4.2 CPU 负载测试
示例1:
使用 4 个 CPU 进程数,进行矩阵乘法计算以进行压力测试,持续时间为20秒;
命令: stress-ng --cpu 4 --cpu-method matrixprod --timeout 20s
root@jpzhang:~# stress-ng --cpu 4 --cpu-method matrixprod --timeout 20s
--------------------------------------------------------------------------------
stress-ng: info: [2451] dispatching hogs: 4 cpu
stress-ng: info: [2451] successful run completed in 17.25s
压力测试过程中,通过 top 命令查看系统负载情况:
root@jpzhang:~# top
--------------------------------------------------------------------------------
top - 00:06:35 up 27 days, 25 min, 2 users, load average: 0.72, 0.16, 0.05
Tasks: 109 total, 5 running, 64 sleeping, 0 stopped, 0 zombie
%Cpu0 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8167832 total, 5817108 free, 183396 used, 2167328 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 7678976 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2453 root 20 0 54448 2356 1396 R 49.8 0.0 0:05.35 stress-ng-cpu
2454 root 20 0 54448 2356 1396 R 49.8 0.0 0:05.36 stress-ng-cpu
2455 root 20 0 54448 2356 1396 R 49.8 0.0 0:05.36 stress-ng-cpu
2452 root 20 0 54448 2356 1396 R 49.5 0.0 0:05.35 stress-ng-cpu
示例2:
产生 2 个 worker 做圆周率算法压力:
命令: stress-ng -c 2 --cpu-method pi --timeout 20s
root@jpzhang:~# stress-ng -c 2 --cpu-method pi --timeout 20s
--------------------------------------------------------------------------------
stress-ng: info: [2785] dispatching hogs: 2 cpu
stress-ng: info: [2785] successful run completed in 20.00s
压力测试过程中,通过 top 命令查看系统负载情况:
root@jpzhang:~# top
--------------------------------------------------------------------------------
top - 00:41:19 up 27 days, 59 min, 3 users, load average: 0.90, 0.53, 0.35
Tasks: 113 total, 3 running, 70 sleeping, 0 stopped, 0 zombie
%Cpu0 : 99.7 us, 0.3 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8167832 total, 5809832 free, 189544 used, 2168456 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 7672824 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2787 root 20 0 53804 312 4 R 99.7 0.0 0:11.84 stress-ng-cpu
2786 root 20 0 53804 312 4 R 99.3 0.0 0:11.83 stress-ng-cpu
示例3:
产生 2 个 worker 迭代使用 30 多种不同的压力算法,包括pi, crc16, fft等等;
命令: stress-ng -c 2 --cpu-method all --timeout 20s
示例4:
产生 2 个 worker 调用 socket 相关函数产生压力;
命令: stress-ng --sock 2 --timeout 20s
示例5:
产生 2 个 worker 读取 tsc 产生压力;
命令: stress-ng --tsc 2 --timeout 20s
示例6:
除了能够产生不同类型的压力,strss-ng 还可以将压力指定到特定的cpu上,比如下面的命令将压力指定到 cpu 0:
命令: stress-ng --tsc 2 --taskset 0 --timeout 20s
root@jpzhang:~# stress-ng --tsc 2 --taskset 0 --timeout 20s
--------------------------------------------------------------------------------
stress-ng: info: [2805] dispatching hogs: 2 tsc
stress-ng: info: [2805] successful run completed in 20.01s
通过 top 命令可以看到 Cpu0 负载100%:
top - 00:47:22 up 27 days, 1:05, 3 users, load average: 0.56, 0.52, 0.39
Tasks: 113 total, 3 running, 70 sleeping, 0 stopped, 0 zombie
%Cpu0 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8167832 total, 5809084 free, 189984 used, 2168764 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 7672388 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2806 root 20 0 53804 320 8 R 49.8 0.0 0:06.37 stress-ng-tsc
2807 root 20 0 53804 320 8 R 49.8 0.0 0:06.38 stress-ng-tsc
2781 root 20 0 44576 4028 3404 R 0.3 0.0 0:01.84 top
1 root 20 0 159896 9080 6612 S 0.0 0.1 0:54.96 systemd
4.3 内存负载测试
示例1:
如下命令使用 4 个内存分配进程, 每次分配大小512M,分配后不释放,保持测试20秒
命令: stress-ng --vm 4 --vm-bytes 512M --timeout 20s
root@jpzhang:~# stress-ng --vm 4 --vm-bytes 512M --timeout 20s
--------------------------------------------------------------------------------
stress-ng: info: [2692] dispatching hogs: 4 vm
stress-ng: info: [2692] successful run completed in 20.02s
通过 free 命令查看测试前后系统内存使用情况:
测试前:
root@jpzhang:~# free -h
--------------------------------------------------------------------------------
total used free shared buff/cache available
Mem: 7.8G 175M 5.6G 676K 2.1G 7.3G
Swap: 0B 0B 0B
测试后:
root@jpzhang:~# free -h
--------------------------------------------------------------------------------
total used free shared buff/cache available
Mem: 7.8G 707M 5.0G 2.8M 2.1G 6.8G
Swap: 0B 0B
4.4 IO 负载测试
示例1:
使用 4 个 IO 进程 ,保持测试20秒
命令: stress-ng --io 4 --timeout 20s
root@jpzhang:~# stress-ng --io 4 --timeout 20s
--------------------------------------------------------------------------------
stress-ng: info: [2719] dispatching hogs: 4 io
stress-ng: info: [2719] successful run completed in 20.00s
4.5 磁盘及 IO 测试
示例1:
使用 4 个I/O进程, 1 个写进程,每次写1G 文件块,测试20秒;
命令: stress-ng --io 4 --hdd 1 --hdd-bytes 1G --timeout 20s
root@jpzhang:~# stress-ng --io 4 --hdd 1 --hdd-bytes 1G --timeout 20s
--------------------------------------------------------------------------------
stress-ng: info: [2766] dispatching hogs: 4 io, 1 hdd
stress-ng: info: [2766] successful run completed in 29.67s
通过 iostat 2
输出可以看到 iowait
100%;
avg-cpu: %user %nice %system %iowait %steal %idle
0.50 0.00 16.75 0.00 0.00 82.75
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
loop0 0.00 0.00 0.00 0 0
vda 20.50 0.00 582.00 0 1164
avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 0.00 100.00 0.00 0.00
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
loop0 0.00 0.00 0.00 0 0
vda 0.00 0.00 0.00 0 0
感谢您花时间阅读文章!
收藏本站不迷路!
原文始发于微信公众号(滑翔的纸飞机):Linux 压力测试工具:stress & stress-ng
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/260768.html