stress Linux模拟cpu、内存、硬盘消耗
工具:stress,stress-ng,stress-ng是stress的升级版,完全兼容stress,两个工具都在epel源中
[root@centos ~]# yum -y install epel-release
[root@centos ~]# yum -y install stress stress-ng
参数说明
-c:模拟CPU使用个数
-m:模拟N个进程频繁调用/释放内存
-d:模拟磁盘写(创建文件,写入内容,删除文件)
-i:模拟I/O,将内存缓冲区中的数据立即写入磁盘
--hdd-bytes N:指定写入文件大小,默认为1GB(与 -d 使用)
--vm-bytes N:指定分配内存的大小(与 -m 使用)
--vm-keep :一直占用内存,而不是循环调用/释放(与 -m 使用)
--timeout:N秒后退出(同 -t)
--backoff:工作开始前等待N微秒(1秒=1000000微秒)
-v:详细输出
-q:静默输出
注: 数字可以以时间(s、m、h、d、y)或大小(B、K、M、G)作为后缀
模拟cpu负载
stress-ng -c 6
[root@centos ~]# stress-ng -c 6
stress-ng: info: [59555] defaulting to a 86400 second run per stressor
stress-ng: info: [59555] dispatching hogs: 6 cpu
^Cstress-ng: info: [59555] successful run completed in 38.32s
[root@centos ~]#
top查看一下
top - 17:20:56 up 3 days, 13:39, 5 users, load average: 2.52, 3.07, 2.84
Tasks: 253 total, 7 running, 246 sleeping, 0 stopped, 0 zombie
%Cpu(s): 75.0 us, 0.0 sy, 0.0 ni, 24.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 16247820 total, 14884656 free, 618592 used, 744572 buff/cache
KiB Swap: 4063228 total, 4063228 free, 0 used. 15211272 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
59643 root 20 0 74220 6116 1180 R 99.7 0.0 0:12.13 stress-ng-cpu
59644 root 20 0 74220 6116 1168 R 99.7 0.0 0:12.13 stress-ng-cpu
59645 root 20 0 74220 6116 1164 R 99.7 0.0 0:12.13 stress-ng-cpu
59646 root 20 0 74220 6116 1168 R 99.7 0.0 0:12.13 stress-ng-cpu
59647 root 20 0 74220 6116 1164 R 99.7 0.0 0:12.13 stress-ng-cpu
59648 root 20 0 74220 6116 1180 R 99.7 0.0 0:12.13 stress-ng-cpu
cpu模拟圆周率计算
[root@centos ~]# stress-ng -c 2 --cpu-method pi
stress-ng: info: [59650] defaulting to a 86400 second run per stressor
stress-ng: info: [59650] dispatching hogs: 2 cpu
cpu模拟所有算法
[root@centos ~]# stress-ng -c 2 --cpu-method all
stress-ng: info: [59655] defaulting to a 86400 second run per stressor
stress-ng: info: [59655] dispatching hogs: 2 cpu
[root@centos ~]#
模拟内存消耗
频繁消耗/释放内存:stress-ng -m 8
频繁消耗不释放内存:stress-ng -m 8 –vm-keep
[root@centos ~]# stress-ng -m 8 --vm-keep
stress-ng: info: [59693] defaulting to a 86400 second run per stressor
stress-ng: info: [59693] dispatching hogs: 8 vm
如果嫌消耗的不够,可以将数字8改大一些
模拟磁盘读入
stress-ng -d 8
[root@centos ~]# stress-ng -d 4
stress-ng: info: [59717] defaulting to a 86400 second run per stressor
stress-ng: info: [59717] dispatching hogs: 4 hdd
结果
[root@centos ~]# iostat -d 1
Linux 3.10.0-957.el7.x86_64 (centos) 03/21/2023 _x86_64_ (8 CPU)
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.03 0.15 0.13 45897 41700
sdb 0.29 1.48 43.98 457398 13598220
dm-0 0.30 1.37 44.18 424191 13657629
dm-1 0.00 0.01 0.00 2588 0
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.00 0.00 0.00 0 0
sdb 986.00 0.00 504832.00 0 504832
dm-0 992.00 0.00 507904.00 0 507904
dm-1 0.00 0.00 0.00 0 0
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 2.00 0.00 8.00 0 8
sdb 988.00 0.00 505356.50 0 505356
dm-0 973.00 0.00 497676.50 0 497676
dm-1 0.00 0.00 0.00 0 0
模拟I/O
stress-ng -i 8
[root@centos ~]# stress-ng -i 8
stress-ng: info: [59736] defaulting to a 86400 second run per stressor
stress-ng: info: [59736] dispatching hogs: 8 io
结果
[root@centos ~]# mpstat -P ALL 5
Linux 3.10.0-957.el7.x86_64 (centos) 03/21/2023 _x86_64_ (8 CPU)
05:39:43 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
05:39:48 PM all 0.26 0.00 59.42 0.00 0.00 0.00 0.00 0.00 0.00 40.32
05:39:48 PM 0 0.21 0.00 54.66 0.00 0.00 0.00 0.00 0.00 0.00 45.13
05:39:48 PM 1 0.41 0.00 60.37 0.00 0.00 0.00 0.00 0.00 0.00 39.21
05:39:48 PM 2 0.42 0.00 59.29 0.00 0.00 0.00 0.00 0.00 0.00 40.29
05:39:48 PM 3 0.21 0.00 64.11 0.00 0.00 0.00 0.00 0.00 0.00 35.68
05:39:48 PM 4 0.41 0.00 62.73 0.00 0.00 0.00 0.00 0.00 0.00 36.85
05:39:48 PM 5 0.42 0.00 59.38 0.00 0.00 0.00 0.00 0.00 0.00 40.21
05:39:48 PM 6 0.21 0.00 57.29 0.00 0.00 0.00 0.00 0.00 0.00 42.51
05:39:48 PM 7 0.42 0.00 57.29 0.00 0.00 0.00 0.00 0.00 0.00 42.29
05:39:48 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
05:39:53 PM all 0.29 0.00 61.52 0.00 0.00 0.00 0.00 0.00 0.00 38.19
05:39:53 PM 0 0.21 0.00 58.56 0.00 0.00 0.00 0.00 0.00 0.00 41.24
05:39:53 PM 1 0.21 0.00 63.83 0.00 0.00 0.21 0.00 0.00 0.00 35.76
05:39:53 PM 2 0.21 0.00 62.60 0.00 0.00 0.00 0.00 0.00 0.00 37.19
05:39:53 PM 3 0.21 0.00 63.62 0.00 0.00 0.00 0.00 0.00 0.00 36.17
05:39:53 PM 4 0.41 0.00 64.74 0.00 0.00 0.00 0.00 0.00 0.00 34.85
05:39:53 PM 5 0.21 0.00 59.58 0.00 0.00 0.00 0.00 0.00 0.00 40.21
05:39:53 PM 6 0.21 0.00 62.79 0.00 0.00 0.00 0.00 0.00 0.00 37.01
05:39:53 PM 7 0.21 0.00 56.37 0.00 0.00 0.00 0.00 0.00 0.00 43.42