Skip to content

Instantly share code, notes, and snippets.

@il-katta
Last active August 29, 2015 13:57
Show Gist options
  • Save il-katta/9582040 to your computer and use it in GitHub Desktop.
Save il-katta/9582040 to your computer and use it in GitHub Desktop.
cpu/mem stresstest
#!/bin/bash
# stresstest.sh
# Author: Andrea Cattaneo < andrea.cattaneo.dev@gmail.com >
# Description: very simple stress test for ram and cpu
# Version: 0.15
mount_point="/mnt/ram"
output_file="$mount_point/stresstest.raw"
run_file="/tmp/stresstest.run"
if ! ( which cpulimit &>/dev/null ) ; then
## Implementazione in bash di cpulimit
cpulimit_usage(){
echo -e "usage:\n\tcpulimit -l <cpu percentage> -p <pid>"
exit 1
}
cpulimit(){
## Parsing args ( TODO: si può fare di meglio! )
if [ "x$1" == "x-l" ] ; then
if [ "x$3" == "x-p" ] ; then
perc=$2
pid=$4
else
cpulimit_usage
fi
else
cpulimit_usage
fi
perc=$2
[[ $pid =~ ^[0-9]+$ ]] || cpulimit_usage
[[ $perc =~ ^[0-9]+$ ]] || cpulimit_usage
run_time=$( echo $perc | awk '{ printf( "%.4f", $1/1000) }' )
sleep_time=$( echo $run_time | awk '{ printf( "%.4f", 0.1-$1 ) }' )
(
( kill -0 $pid &>/dev/null ) && \
echo "starting cpulimit for pid $pid ..."
while ( kill -s SIGCONT $pid ) ; do
sleep $run_time
kill -s SIGSTOP $pid
sleep $sleep_time
done
echo "cpulimit terminated"
) &
}
fi
if ! ( which awk &>/dev/null ) ; then
echo "please install awk"
fi
usage(){
echo -e "usage:\n\t$0 [-s] <percentage of memory> <percentage cpu>"
[[ -f "$run_file" ]] && rm "$run_file"
exit 1
}
## Codice eseguito da ogni sub-processo
run_stresstest(){
random_file=/dev/urandom
[[ -r /dev/frandom ]] && random_file=/dev/frandom
nice -n -20 dd if=$random_file of=$1 bs=1K count=$3 &>/dev/null &
DD_PID=$!
cpulimit -l $2 -p $DD_PID &>/dev/null
wait $DD_PID
}
## Parsing argomenti passati allo script
if [ "x$1" == "x-s" ] ; then
echo "single-core mode"
mem_perc=$2
cpu_perc=$3
nproc=1
else
echo "multi-core mode"
mem_perc=$1
cpu_perc=$2
nproc=`nproc`
fi
## Controllo argomenti numerici
## ( 0 < x <= 100 )
[[ $mem_perc =~ ^[0-9]+$ ]] && \
[[ $mem_perc -le 100 ]] && \
[[ $mem_perc -gt 0 ]] || \
usage
[[ $mem_perc =~ ^[0-9]+$ ]] && \
[[ $mem_perc -le 100 ]] && \
[[ $mem_perc -gt 0 ]] || \
usage
## Mounting della memoria RAM
mkdir -p $mount_point
mount ramfs -t ramfs -o x-mount.mkdir $mount_point || exit 1
## Calcolo della quantità di memoria da occupare
## RAM totale
tot_mem=`grep "^MemTotal:" /proc/meminfo | awk '{ print $2 }'`
## memoria totale da occupare
mem=$(( $tot_mem/100*$mem_perc ))
## memoria da occupare per ogni sub-processo
mem_per_core=$(( $mem/$nproc ))
## RAM libera/disponibile
free_mem=`grep "^MemFree:" /proc/meminfo | awk '{ print $2 }'`
## percentuale memoria da occupare da ogni sub-processo
mem_perc_per_core=`echo $mem_perc $nproc | awk '{ printf( "%.2f", $1/$2 )}'`
## Controllo ( inutile? ) della quantità di memoria disponibile
if [ $mem -gt $free_mem ] ; then
echo "ERROR: too low free memory!"
exit 1
fi
## Creazione file per terminazione processi figli
echo $$ >> $run_file || exit 1
## Cattura segnali terminazione:
## terminazione corretta sub-processi
trap "rm $run_file ; wait" 1
trap "rm $run_file ; wait" 2
trap "rm $run_file ; wait" 3
trap "rm $run_file ; exit 1" 9
trap "rm $run_file ; wait" 15
## get a better priority
renice -20 -p $$
## Creazione di un processo per ogni core di cpu
for I in `seq $nproc` ; do
echo "staring thread ${I}/${nproc}"
echo -e "\t mem: $mem_per_core kB ( ${mem_perc_per_core}% )"
echo -e "\t cpu: ${cpu_perc}%"
echo -e "\t file: ${output_file}.$I"
(
file="${output_file}.$I"
while [ -f $run_file ]; do
run_stresstest "${file}" $cpu_perc $mem_per_core
done
) &
sleep 0.1
done
wait
## Rimozione file da memoria
find $mount_point \
-regex ".*$( basename $output_file ).\([0-9]+\)" \
-delete
## Umount tmpfs
sync && \
umount $mount_point
rmdir $mount_point
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment