我想在Linux机器上创建一个接近100%的负载。这是四核系统,我要所有核都全速运转。理想情况下,CPU负载将持续一段指定的时间,然后停止。我希望bash里有什么妙招。我在想某种无限循环。
当前回答
cat /dev/urandom > /dev/null
其他回答
如果你不想安装额外的软件,你可以使用自动利用所有CPU内核的压缩工具。例如,xz:
cat /dev/zero | xz -T0 > /dev/null
这将从/dev/zero中获取无限的虚拟数据流,并使用系统中所有可用的内核对其进行压缩。
增加负载或消耗CPU 100%或X%
sha1sum /dev/zero &
在某些系统上,这将增加X%插槽的负载,在这种情况下,你必须多次运行相同的命令。
然后你可以通过输入命令查看CPU使用情况
top
释放负荷
killall sha1sum
#!/bin/bash
duration=120 # seconds
instances=4 # cpus
endtime=$(($(date +%s) + $duration))
for ((i=0; i<instances; i++))
do
while (($(date +%s) < $endtime)); do :; done &
done
我用压力来做这种事情,你可以告诉它要最大化多少内核。它还允许对内存和磁盘施加压力。
例如,对2个核心施加压力60秒
压力——cpu 2——超时60
我会把它分成两个脚本:
infinite_loop。bash:
#!/bin/bash
while [ 1 ] ; do
# Force some computation even if it is useless to actually work the CPU
echo $((13**99)) 1>/dev/null 2>&1
done
cpu_spike。bash:
#!/bin/bash
# Either use environment variables for NUM_CPU and DURATION, or define them here
for i in `seq ${NUM_CPU}` : do
# Put an infinite loop on each CPU
infinite_loop.bash &
done
# Wait DURATION seconds then stop the loops and quit
sleep ${DURATION}
killall infinite_loop.bash