使用bc简单cpu测压shell
2024-07-16 / shell / 269 次围观 / 0 次吐槽 /Bash
#!/bin/bash
# 参数检查
if [ $# -ne 3 ]; then
echo "Usage: $0 <threads> <duration> <iterations>"
exit 1
fi
# 参数设置
THREADS=$1
DURATION=$2
ITERATIONS=$3
# 结果文件
RESULT_FILE="cpu_stress_test_results.txt"
echo "CPU Stress Test Results" > $RESULT_FILE
echo "Threads: $THREADS, Duration: $DURATION, Iterations: $ITERATIONS" >> $RESULT_FILE
echo "-------------------------------------------" >> $RESULT_FILE
# 测试函数
cpu_stress_test() {
end=$((SECONDS + DURATION))
while [ $SECONDS -lt $end ]; do
echo "scale=5000; a(1)*4" | bc -l > /dev/null
done
}
# 进行循环测试
for (( i=1; i<=ITERATIONS; i++ ))
do
echo "Iteration $i/$ITERATIONS" | tee -a $RESULT_FILE
START_TIME=$(date +%s)
# 启动多个线程
for (( j=1; j<=THREADS; j++ ))
do
cpu_stress_test &
done
# 等待所有后台任务完成
wait
END_TIME=$(date +%s)
ELAPSED_TIME=$((END_TIME - START_TIME))
echo "Test completed in $ELAPSED_TIME seconds" | tee -a $RESULT_FILE
echo "-------------------------------------------" >> $RESULT_FILE
sleep 1 # 间隔1秒后继续下一次测试
done
echo "测试完成,结果保存在 $RESULT_FILE 文件中。"
赋予执行权限:
Bash
chmod +x cpu_stress_test.sh
运行脚本:
Bash
./cpu_stress_test.sh <threads> <duration> <iterations>
nohup ./cpu_stress_test.sh <threads> <duration> <iterations> &> cpu_test.log &
<threads>
:测试使用的CPU线程数。<duration>
:每次测试的持续时间(秒)。<iterations>
:测试的循环次数。
- 上一篇:history启用命令执行时间记录
- 下一篇:macos连接蓝牙音响播放卡顿修复
Powered By Cheug's Blog
Copyright Cheug Rights Reserved.