Stress-ng

介绍如何在 Linux 系统上使用 stress-ng 负载测试工具,产生 CPU、内存等资源满载的状况。

stress-ng
stress-ng 与旧的 stress 都可以用来产生系统负载,但新的 stress-ng 功能较丰富,所以这里我们只介绍 stress-ng 的使用方式。

stress-ng 支持多种产生系统负载的方式,包含 CPU 的浮点运算、整数运算、位元运算与控制流程等,可以用来测试系统在高负载的状况下的稳定性。

stress-ng 必须小心使用,某些测试可能会造成设计不良的硬件过热,另外也可能让系统过载而难以停止负载测试。

安装 stress-ng
若在 Ubuntu Linux 中可以使用 apt 来安装 stress-ng 套件:

安装 stress-ng 套件

sudo apt install stress-ng
Stressors
stress-ng 会使用各种不同的 stressors 来产生不同性质的系统负载,stressor 的种类非常多,包含:cpu、cpu-cache、device、io、interrupt、filesystem、memory、network、os、pipe、scheduler 与 vm。

我们可以使用以下指令查找 stress-ng 所有支持的 stressors:

查找支持的 stressors

stress-ng --stressors
af-alg affinity aio aiol apparmor atomic bigheap bind-mount branch brk bsearch cache cap chdir chmod chown chroot clock clone context copy-file cpu cpu-online crypt cyclic daemon dccp dentry dev dir dirdeep dnotify dup dynlib enosys epoll eventfd exec fallocate fanotify fault fcntl fiemap fifo file-ioctl filename flock fork fp-error fstat full funccall futex get getdent getrandom handle hdd heapsort hrtimers hsearch icache icmp-flood inode-flags inotify io iomix ioport ioprio itimer kcmp key kill klog lease link locka lockbus lockf lockofd longjmp lsearch madvise malloc matrix mcontend membarrier memcpy memfd memrate memthrash mergesort mincore mknod mlock mmap mmapaddr mmapfixed mmapfork mmapmany mq mremap msg msync netdev netlink-proc nice nop null numa oom-pipe opcode open personality physpage pipe poll procfs pthread ptrace pty qsort quota radixsort rawdev rdrand readahead remap rename resources revio rlimit rmap rtc schedpolicy sctp seal seccomp seek sem sem-sysv sendfile shm shm-sysv sigfd sigfpe sigio sigpending sigpipe sigq sigsegv sigsuspend sleep sock sockdiag sockfd sockpair softlockup spawn splice stack stackmmap str stream swap switch symlink sync-file sysinfo sysfs tee timer timerfd tlb-shootdown tmpfs tree tsc tsearch udp udp-flood unshare urandom userfaultfd utime vecmath vfork vforkmany vm vm-addr vm-rw vm-splice wait wcs xattr yield zero zlib zombie
若要查找指定类别之下有哪些 stressors 可用,可以使用 --class 参数指定类别名称,再加上一个问号(?),例如若要查找 vm 类别下的 stressors,就可以执行:

查找 vm 类别的 stressors

stress-ng --class vm?
class ‘vm’ stressors: bigheap brk madvise malloc mlock mmap mmapaddr mmapfixed mmapfork mmapmany mremap msync physpage shm shm-sysv stack stackmmap swap tmpfs userfaultfd vm vm-addr vm-rw vm-splice
关于这些 stressors 的详细用法与说明,可以参考 stress-ng 的在线手册:

查找 stress-ng 在线手册

man stress-ng
用户可以依据需求,组合各种 stressors 创造出适合的负载环境,以下是一些常见的组合范例。

测试 CPU 满载状况
若要产生 2 个 CPU 内核满载的状况,可以使用 2 个 CPU stressors:

产生 CPU 满载状况(2 CPU stressors、持续 30 秒)

stress-ng --cpu 2 --timeout 30s
stress-ng: info: [31788] dispatching hogs: 2 cpu
stress-ng: info: [31788] successful run completed in 30.02s
事实上 CPU 的 stressors 种类非常多,单纯指定 --cpu 参数的话,会轮流使用各种 CPU 类型的 stressors,若要明确测试指定的 CPU 负载类型,可以指定要使用的 stressor。

测试 zlib 压缩、解压缩
使用 2 个 zlib stressors 压缩与解压缩随机数据,进行 CPU 负载测试:

以 2 个 zlib stressors 压缩、解压缩数据,产生 CPU 满载状况

stress-ng --zlib 2 --timeout 30s
使用 zlib 压缩与解压缩数据时,除了 CPU 之外,同时也会产生缓存(cache)与内存(memory)的负载。

测试矩阵运算
使用 2 个 matrix stressors 产生各种矩阵运算,进行 CPU 负载测试:

以两个 matrix stressors 进行矩阵运算,产生 CPU 满载状况

stress-ng --matrix 2 --timeout 30s
内存测试
以 8 个 vm stressors 使用 80% 的可用内存(1 个 stressor 使用 10% 可用内存)执行内存测试,持续 1 小时:

使用 8 个 vm stressors 执行内存测试

总共使用 80% 的可用内存,持续 1 小时

stress-ng --vm 8 --vm-bytes 80% -t 1h
I/O 测试
以 2 个 iomix stressors 使用 10% 磁盘空间(1 个 stressor 使用 5% 磁盘空间)执行 I/O 测试,持续 10 分钟:

使用 2 个 iomix stressors 执行混合 I/O,

使用 10% 磁盘空间,持续 10 分钟

stress-ng --iomix 2 --iomix-bytes 10% -t 10m
轮流执行 8 个 io 类型的 stressors,每个类型持续 5 分钟,并产生执行时间报表:

轮流执行 8 个 io 类型的 stressors,每个类型持续 5 分钟

并产生执行时间报表

stress-ng --sequential 8 --class io -t 5m --times
混合多种 Stressors
我们也可以混合不同的 stressors 来使用,例如一个 zlib 与一个 matrix:

以 1 个 zlib 与 1 个 matrix stressors

stress-ng --zlib 1 --matrix 1 --timeout 30s
FFT 计算测试
以 2 个 FFT 的 stressors,执行 5000 个 bogo 运算,产出报表:

以 2 个 FFT 的 stressors,执行 5000 个 bogo 运算,产出报表

stress-ng --cpu 2 --cpu-method fft --cpu-ops 5000 --metrics-brief
混合测试
使用 4 个各种类的 stressors 持续 5 分钟:

使用 4 个各种类的 stressors 持续 5 分钟

stress-ng --all 4 --timeout 5m
每一种 stressors 轮流执行,每个持续 10 分钟:

每一种 stressors 轮流执行,每个持续 10 分钟

stress-ng --sequential 0 -t 10m
在所有的 CPU 上执行各种 CPU stressors,持续 1 小时:

在所有的 CPU 上执行各种 stressors,持续 1 小时

stress-ng --cpu 0 --cpu-method all -t 1h

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值