我想在Linux机器上创建一个接近100%的负载。这是四核系统,我要所有核都全速运转。理想情况下,CPU负载将持续一段指定的时间,然后停止。我希望bash里有什么妙招。我在想某种无限循环。
当前回答
#!/bin/bash
while [ 1 ]
do
#Your code goes here
done
其他回答
使用这里提到的例子,以及IRC的帮助,我开发了自己的CPU压力测试脚本。它使用每个线程一个子shell和无限循环技术。您还可以交互地指定线程数和时间量。
#!/bin/bash
# Simple CPU stress test script
# Read the user's input
echo -n "Number of CPU threads to test: "
read cpu_threads
echo -n "Duration of the test (in seconds): "
read cpu_time
# Run an endless loop on each thread to generate 100% CPU
echo -e "\E[32mStressing ${cpu_threads} threads for ${cpu_time} seconds...\E[37m"
for i in $(seq ${cpu_threads}); do
let thread=${i}-1
(taskset -cp ${thread} $BASHPID; while true; do true; done) &
done
# Once the time runs out, kill all of the loops
sleep ${cpu_time}
echo -e "\E[32mStressing complete.\E[37m"
kill 0
一个核心(不调用外部进程):
while true; do true; done
两个核心:
while true; do /bin/true; done
后者只会让我的两个都达到50%…
这将使两者都达到100%:
while true; do echo; done
这对我来说很有用:
bash -c 'for (( I=100000000000000000000 ; I>=0 ; I++ )) ; do echo $(( I+I*I )) & echo $(( I*I-I )) & echo $(( I-I*I*I )) & echo $(( I+I*I*I )) ; done' &>/dev/null
它只使用bash。
增加负载或消耗CPU 100%或X%
sha1sum /dev/zero &
在某些系统上,这将增加X%插槽的负载,在这种情况下,你必须多次运行相同的命令。
然后你可以通过输入命令查看CPU使用情况
top
释放负荷
killall sha1sum
你可以在这里下载一个程序
在Linux系统上轻松安装
./configure
make
make install
并在简单的命令行中启动它
stress -c 40
让所有的cpu(不管你有多少)都有40个线程,每个线程在随机生成的数字上运行复杂的根号计算。
您甚至可以定义程序的超时时间
stress -c 40 -timeout 10s
与提议的dd命令解决方案不同,dd命令主要处理IO,因此不会因为处理数据而真正使系统过载。
压力程序会使系统超负荷因为要处理计算。