对cpu施加负载

在测试工作中,有时候需要对cpu施加压力来模拟环境

使用stress工具

./stress --cpu 3

类似windows环境下的consume.exe

使用shell

假设是4个cpu core的主机

for i in 1 2 3 4; do while : ; do : ; done & done

如果是bash,还支持 {1..4}

for i in {1..4}; do while : ; do : ; done & done

python脚本

from multiprocessing import Pool

def f(x):
    # Put any cpu (only) consuming operation here. I have given 1 below -
    while True:
        x * x

# decide how many cpus you need to load with.
no_of_cpu_to_be_consumed = 3

p = Pool(processes=no_of_cpu_to_be_consumed)
p.map(f, range(no_of_cpu_to_be_consumed))

参考

Last updated