我想在Linux机器上创建一个接近100%的负载。这是四核系统,我要所有核都全速运转。理想情况下,CPU负载将持续一段指定的时间,然后停止。我希望bash里有什么妙招。我在想某种无限循环。


当前回答

一个核心(不调用外部进程):

while true; do true; done

两个核心:

while true; do /bin/true; done

后者只会让我的两个都达到50%…

这将使两者都达到100%:

while true; do echo; done

其他回答

#!/bin/bash
while [ 1 ]
do
        #Your code goes here
done

我会把它分成两个脚本:

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

您可以尝试测试加密算法的性能。

openssl speed -multi 4

利用这里的想法,创建的代码在设定的持续时间后自动退出,不需要杀死进程

#!/bin/bash
echo "Usage : ./killproc_ds.sh 6 60  (6 threads for 60 secs)"

# Define variables
NUM_PROCS=${1:-6} #How much scaling you want to do
duration=${2:-20}    # seconds

function infinite_loop {
endtime=$(($(date +%s) + $duration))
while (($(date +%s) < $endtime)); do
    #echo $(date +%s)
    echo $((13**99)) 1>/dev/null 2>&1
    $(dd if=/dev/urandom count=10000 status=none| bzip2 -9 >> /dev/null) 2>&1 >&/dev/null
done
echo "Done Stressing the system - for thread $1"
}


echo Running for duration $duration secs, spawning $NUM_PROCS threads in background
for i in `seq ${NUM_PROCS}` ;
do
# Put an infinite loop
    infinite_loop $i  &
done

虽然我迟到了,但这篇文章是谷歌搜索“在linux中生成负载”的顶级结果之一。

标记为solution的结果可以用来生成系统负载,我更倾向于使用sha1sum /dev/zero来对cpu内核施加负载。

其思想是从一个无限的数据流(例如。/dev/zero, /dev/urandom,…)该进程将尝试最大化一个cpu-core,直到进程终止。 要为更多内核生成负载,可以将多个命令连接在一起。

如。产生2个核心负荷: Sha1sum /dev/zero | Sha1sum /dev/zero