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


当前回答

使用这里提到的例子,以及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

其他回答

cat /dev/urandom > /dev/null
:(){ :|:& };:

这个fork炸弹将对CPU造成严重破坏,并可能使您的计算机崩溃。

我认为这个更简单。打开“终端”,输入以下命令并按“Enter”。

yes > /dev/null &

要充分利用现代CPU,一行命令是不够的,您可能需要重复该命令以耗尽所有CPU功率。

为了结束这一切,简单地说

killall yes

这个想法最初是在这里发现的,尽管它是为Mac用户设计的,但这应该也适用于*nix。

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

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

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

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

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