我想在Linux机器上创建一个接近100%的负载。这是四核系统,我要所有核都全速运转。理想情况下,CPU负载将持续一段指定的时间,然后停止。我希望bash里有什么妙招。我在想某种无限循环。
当前回答
你可以在这里下载一个程序
在Linux系统上轻松安装
./configure
make
make install
并在简单的命令行中启动它
stress -c 40
让所有的cpu(不管你有多少)都有40个线程,每个线程在随机生成的数字上运行复杂的根号计算。
您甚至可以定义程序的超时时间
stress -c 40 -timeout 10s
与提议的dd命令解决方案不同,dd命令主要处理IO,因此不会因为处理数据而真正使系统过载。
压力程序会使系统超负荷因为要处理计算。
其他回答
你可以在这里下载一个程序
在Linux系统上轻松安装
./configure
make
make install
并在简单的命令行中启动它
stress -c 40
让所有的cpu(不管你有多少)都有40个线程,每个线程在随机生成的数字上运行复杂的根号计算。
您甚至可以定义程序的超时时间
stress -c 40 -timeout 10s
与提议的dd命令解决方案不同,dd命令主要处理IO,因此不会因为处理数据而真正使系统过载。
压力程序会使系统超负荷因为要处理计算。
无限循环也是我的想法。一个看起来很怪异的例子是:
while :; do :; done
(:与true相同,不执行任何操作并以0退出)
你可以在子shell中调用它并在后台运行。执行$num_cores次数应该就足够了。在睡眠到所需的时间后,您可以将它们全部杀死,使用jobs -p获得pid(提示:xargs)
我会把它分成两个脚本:
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
#!/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
我结合了一些答案,并添加了一种将压力扩展到所有可用cpu的方法:
#!/bin/bash
function infinite_loop {
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
}
# Either use environment variables for DURATION, or define them here
NUM_CPU=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu)
PIDS=()
for i in `seq ${NUM_CPU}` ;
do
# Put an infinite loop on each CPU
infinite_loop &
PIDS+=("$!")
done
# Wait DURATION seconds then stop the loops and quit
sleep ${DURATION}
# Parent kills its children
for pid in "${PIDS[@]}"
do
kill $pid
done