CPU 压力测试命令大全
以下是 Linux/Unix 系统下常用的 CPU 压力测试命令和工具,可用于测试 CPU 性能、稳定性和散热能力。
1. 基本压力测试命令
1.1 使用 yes
命令
yes > /dev/null & # 启动一个无限循环进程
yes > /dev/null & # 启动第二个进程
# 按需启动更多进程...
killall yes # 结束后台所有 yes 进程
1.2 使用 dd
命令
dd if=/dev/zero of=/dev/null bs=1M count=1000000
1.3 使用 sha1sum
计算哈希
cat /dev/zero | sha1sum
2. 专业压力测试工具
2.1 stress 工具
# 安装 stress
sudo apt-get install stress # Debian/Ubuntu
sudo yum install stress # CentOS/RHEL
# 测试所有 CPU 核心 100% 负载 60秒
stress --cpu $(nproc) --timeout 60s
# 指定 CPU 核心数
stress --cpu 4 --timeout 30s
# 同时测试 CPU、内存和 IO
stress --cpu 8 --io 4 --vm 2 --vm-bytes 1G --timeout 60s
2.2 stress-ng 工具 (更强大的 stress 版本)
# 安装
sudo apt-get install stress-ng # Debian/Ubuntu
sudo yum install stress-ng # CentOS/RHEL
# 基本 CPU 测试
stress-ng --cpu $(nproc) --timeout 60s
# 使用多种 CPU 压力算法
stress-ng --cpu $(nproc) --cpu-method all --timeout 60s
# 矩阵计算压力测试
stress-ng --matrix 1 --timeout 60s
2.3 sysbench 工具
# 安装
sudo apt-get install sysbench # Debian/Ubuntu
sudo yum install sysbench # CentOS/RHEL
# CPU 素数计算测试
sysbench cpu --cpu-max-prime=20000 run
# 多线程测试
sysbench cpu --threads=4 --cpu-max-prime=20000 run
3. 高级压力测试工具
3.1 cpuburn
# 下载编译
git clone https://github.com/chaos/cpuburn
cd cpuburn
make
# 运行 (谨慎使用,可能导致过热)
./cpuburn
3.2 Intel Processor Diagnostic Tool
# 下载
wget https://downloadmirror.intel.com/28640/eng/ipdt_2021.2.0.43.tgz
tar zxvf ipdt_*.tgz
cd ipdt_*/64-bit
# 运行测试
sudo ./ipdt
4. 监控命令
在压力测试期间可以配合以下命令监控 CPU 状态:
# 实时监控 CPU 使用率
top
htop
# 每1秒刷新一次 CPU 信息
mpstat -P ALL 1
# 查看 CPU 温度 (需要 lm-sensors)
sensors
# 查看 CPU 频率
watch -n 1 "cat /proc/cpuinfo | grep MHz"
5. 注意事项
- 压力测试可能导致系统温度升高,请确保散热良好
- 在生产环境执行前,建议先在测试环境验证
- 长时间高负载测试可能影响硬件寿命
- 笔记本用户建议连接电源并确保良好散热
- 测试完成后及时停止压力测试进程
6. 停止测试
对于后台运行的测试进程,可以使用以下命令停止:
# 停止所有 stress 进程
pkill stress
# 停止特定进程
killall [进程名]
# 查看并杀死占用 CPU 高的进程
top # 然后按 k 输入进程 ID