+
80
-

如何在linux服务器上模拟cpu压力测试?

如何在linux服务器上模拟cpu压力测试?


网友回复

+
0
-

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

参数和用法

-c 2 : 生成2个worker循环调用sqrt()产生cpu压力 -i 1 : 生成1个worker循环调用sync()产生io压力 -m 1 : 生成1个worker循环调用malloc()/free()产生内存压力 -c N :运行N worker CPU压力测试进程 --cpu-method all :worker从迭代使用30多种不同的压力算法,包括pi, crc16, fft等等 -tastset N:将压力加到指定核心上 -d N:运行N worker HDD write/unlink测试 -i N:运行N worker IO测试

比如, 从下面可以看出经过30秒的压力后,系统负载从0.00提升至0.57。

[root@~]# uptime
00:14:44 up 18 min, 1 user, load average: 0.00, 0.01, 0.02
[root@~]# stress -c 2 -t 30
stress: info: [2312] dispatching hogs: 2 cpu, 0 io, 0 vm, 0 hdd
stress: info: [2312] successful run completed in 30s
[root@~]# uptime
00:15:40 up 19 min, 1 user, load average: 0.57, 0.18, 0.07

产生2个worker做圆周率算法压力:

stress-ng -c 2 --cpu-method pi

产生2个worker从迭代使用30多种不同的压力算法,包括pi, crc16, fft等等。

stress-ng -c 2 --cpu-method all

产生2个worker调用socket相关函数产生压力

stress-ng --sock 2

产生2个worker读取tsc产生压力

stress-ng --tsc 2

除了能够产生不同类型的压力,strss-ng还可以将压力指定到特定的cpu上,比如下面的命令将压力指定到cpu 0,2,3,6:

stress-ng --sock 4 --taskset 0,2-3,6

源码位置:https://github.com/ColinIanKing/stress-ng

我知道答案,我要回答