How to create a CPU spike with a bash command

后端 未结 23 1124
小蘑菇
小蘑菇 2020-11-30 16:11

I want to create a near 100% load on a Linux machine. It\'s quad core system and I want all cores going full speed. Ideally, the CPU load would last a designated amount of

相关标签:
23条回答
  • 2020-11-30 16:36
    cat /dev/urandom > /dev/null
    
    0 讨论(0)
  • 2020-11-30 16:37

    I use stress for this kind of thing, you can tell it how many cores to max out.. it allows for stressing memory and disk as well.

    Example to stress 2 cores for 60 seconds

    stress --cpu 2 --timeout 60

    0 讨论(0)
  • 2020-11-30 16:39

    Utilizing ideas here, created code which exits automatically after a set duration, don't have to kill processes --

    #!/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
    
    0 讨论(0)
  • 2020-11-30 16:40

    I've used bc (binary calculator), asking them for PI with a big lot of decimals.

    $ for ((i=0;i<$NUMCPU;i++));do
        echo 'scale=100000;pi=4*a(1);0' | bc -l &
        done ;\
        sleep 4; \
        killall bc
    

    with NUMCPU (under Linux):

    $ NUMCPU=$(grep $'^processor\t*:' /proc/cpuinfo |wc -l)
    

    This method is strong but seem system friendly, as I've never crashed a system using this.

    0 讨论(0)
  • 2020-11-30 16:41

    One core (doesn't invoke external process):

    while true; do true; done
    

    Two cores:

    while true; do /bin/true; done
    

    The latter only makes both of mine go to ~50% though...

    This one will make both go to 100%:

    while true; do echo; done
    
    0 讨论(0)
  • 2020-11-30 16:45

    To load 3 cores for 5 seconds:

    seq 3 | xargs -P0 -n1 timeout 5 yes > /dev/null
    

    This results in high kernel (sys) load from the many write() system calls.

    If you prefer mostly userland cpu load:

    seq 3 | xargs -P0 -n1 timeout 5 md5sum /dev/zero
    

    If you just want the load to continue until you press Ctrl-C:

    seq 3 | xargs -P0 -n1 md5sum /dev/zero
    
    0 讨论(0)
提交回复
热议问题