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