如何使用“Stress-ng”工具在 Linux 上施加高 CPU 负载和压力测试
作为系统管理员,您可能希望在 Linux 系统处于高负载压力下时检查和监视 Linux 系统的状态。对于系统管理员和程序员来说,这可能是一个好方法:
- 微调系统上的活动。
- 监视操作系统内核接口。
- 测试您的 Linux 硬件组件,例如 CPU、内存、磁盘设备和许多其他组件,以观察它们在压力下的性能。
- 测量系统上不同的功耗负载。
在本指南中,我们将介绍两个重要的工具,stress 和 stress-ng,用于在 Linux 系统下进行压力测试。
1. 压力 – 是一种工作负载生成器工具,旨在使您的系统承受可配置的 CPU、内存、I/O 和磁盘压力测量。
2. stress-ng – 是压力工作负载生成器工具的更新版本,用于测试您的系统的以下功能:
- CPU计算
- 驱动压力
- I/O 同步
- 管道输入/输出
- 缓存颠簸
- 虚拟机压力
- 插座应力
- 进程的创建和终止
- 上下文切换属性
尽管这些工具非常适合检查您的系统,但它们不应该只由任何系统用户使用。
重要:强烈建议您以 root 用户权限使用这些工具,因为它们会给您的 Linux 机器带来如此快的压力,并避免在设计不良的硬件上出现某些系统错误。
如何在 Linux 中安装“stress”工具
要在 Debian 及其衍生产品(例如 Ubuntu 和 Mint)上安装压力工具,请运行以下命令。
sudo apt-get install stress
要在 RHEL/CentOS 和 Fedora Linux 上安装压力,您需要打开 EPEL 存储库,然后键入以下 yum 命令来安装相同的:
yum install stress
使用stress的一般语法是:
sudo stress option argument
一些可以与压力一起使用的选项。
- 要在 sqrt() 函数上生成 N 个工作线程,请使用 –cpu N 选项,如下所示。
- 要在sync()函数上生成N个工作线程,请使用–io N选项,如下所示。
- 要在 malloc()/free() 函数上生成 N 个工作线程,请使用 –vm N 选项。
- 要为每个虚拟机工作线程分配内存,请使用–vm-bytes N选项。
- 您可以使用–vm-keep选项来重新分配内存,而不是释放和重新分配内存资源。
- 使用 –vm-hang N 选项将睡眠设置为 N 秒,然后释放内存。
- 要在 write()/unlink() 函数上生成 N 个工作线程,请使用 –hdd N 选项。
- 您可以使用–timeout N选项将超时设置为N秒后。
- 使用 -backoff N 选项在任何工作开始之前设置 N 微秒的等待因子,如下所示。
- 要在运行压力时显示更详细的信息,请使用 -v 选项。
- 使用-help 查看使用压力的帮助或查看联机帮助页。
如何在 Linux 系统上使用压力?
1. 要在每次运行该命令时检查该命令的效果,请首先运行 uptime 命令并记下平均负载。
接下来,运行 stress 命令生成 8 个在 sqrt() 上旋转的工作线程,超时时间为 20 秒。运行压力后,再次运行 uptime 命令并比较平均负载。
tecmint@tecmint ~ $ uptime
tecmint@tecmint ~ $ sudo stress --cpu 8 --timeout 20
tecmint@tecmint ~ $ uptime
样本输出
tecmint@tecmint ~ $ uptime
17:20:00 up 7:51, 2 users, load average: 1.91, 2.16, 1.93 [<-- Watch Load Average]
tecmint@tecmint ~ $ sudo stress --cpu 8 --timeout 20
stress: info: [17246] dispatching hogs: 8 cpu, 0 io, 0 vm, 0 hdd
stress: info: [17246] successful run completed in 21s
tecmint@tecmint ~ $ uptime
17:20:24 up 7:51, 2 users, load average: 5.14, 2.88, 2.17 [<-- Watch Load Average]
2. 要在 sqrt() 上生成 8 个工作进程,超时时间为 30 秒,并显示有关操作的详细信息,请运行以下命令:
tecmint@tecmint ~ $ uptime
tecmint@tecmint ~ $ sudo stress --cpu 8 -v --timeout 30s
tecmint@tecmint ~ $ uptime
样本输出
tecmint@tecmint ~ $ uptime
17:27:25 up 7:58, 2 users, load average: 1.40, 1.90, 1.98 [<-- Watch Load Average]
tecmint@tecmint ~ $ sudo stress --cpu 8 -v --timeout 30s
stress: info: [17353] dispatching hogs: 8 cpu, 0 io, 0 vm, 0 hdd
stress: dbug: [17353] using backoff sleep of 24000us
stress: dbug: [17353] setting timeout to 30s
stress: dbug: [17353] --> hogcpu worker 8 [17354] forked
stress: dbug: [17353] using backoff sleep of 21000us
stress: dbug: [17353] setting timeout to 30s
stress: dbug: [17353] --> hogcpu worker 7 [17355] forked
stress: dbug: [17353] using backoff sleep of 18000us
stress: dbug: [17353] setting timeout to 30s
stress: dbug: [17353] --> hogcpu worker 6 [17356] forked
stress: dbug: [17353] using backoff sleep of 15000us
stress: dbug: [17353] setting timeout to 30s
stress: dbug: [17353] --> hogcpu worker 5 [17357] forked
stress: dbug: [17353] using backoff sleep of 12000us
stress: dbug: [17353] setting timeout to 30s
stress: dbug: [17353] --> hogcpu worker 4 [17358] forked
stress: dbug: [17353] using backoff sleep of 9000us
stress: dbug: [17353] setting timeout to 30s
stress: dbug: [17353] --> hogcpu worker 3 [17359] forked
stress: dbug: [17353] using backoff sleep of 6000us
stress: dbug: [17353] setting timeout to 30s
stress: dbug: [17353] --> hogcpu worker 2 [17360] forked
stress: dbug: [17353] using backoff sleep of 3000us
stress: dbug: [17353] setting timeout to 30s
stress: dbug: [17353] --> hogcpu worker 1 [17361] forked
stress: dbug: [17353] tecmint@tecmint ~ $ uptime
17:27:59 up 7:59, 2 users, load average: 5.41, 2.82, 2.28 [<-- Watch Load Average]
3. 要生成一个 malloc() 和 free() 函数的工作线程,超时时间为 60 秒,运行以下命令。
tecmint@tecmint ~ $ uptime
tecmint@tecmint ~ $ sudo stress --vm 1 --timeout 60s
tecmint@tecmint ~ $ uptime
样本输出
tecmint@tecmint ~ $ uptime
17:34:07 up 8:05, 2 users, load average: 1.54, 2.04, 2.11 [<-- Watch Load Average]
tecmint@tecmint ~ $ sudo stress --vm 1 --timeout 60s
stress: info: [17420] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: info: [17420] successful run completed in 60s
tecmint@tecmint ~ $ uptime
17:35:20 up 8:06, 2 users, load average: 2.45, 2.24, 2.17 [<-- Watch Load Average]
4. 4个工作线程在sqrt()上运行,2个工作线程在sync()上运行,2个工作线程在malloc()/free()上运行,超时时间为20秒,并分配内存每个虚拟机工作线程 256MB,运行下面的命令。
tecmint@tecmint ~ $ uptime
tecmint@tecmint ~ $ sudo stress --cpu 4 --io 3 --vm 2 --vm-bytes 256M --timeout 20s
tecmint@tecmint ~ $ uptime
样本输出
tecmint@tecmint ~ $ uptime
17:40:33 up 8:12, 2 users, load average: 1.68, 1.84, 2.02 [<-- Watch Load Average]
tecmint@tecmint ~ $ sudo stress --cpu 4 --io 3 --vm 2 --vm-bytes 256M --timeout 20s
stress: info: [17501] dispatching hogs: 4 cpu, 3 io, 2 vm, 0 hdd
stress: info: [17501] successful run completed in 20s
tecmint@tecmint ~ $ uptime
17:40:58 up 8:12, 2 users, load average: 4.63, 2.54, 2.24 [<-- Watch Load Average]