#!/bin/sh
#
# name    : Frenzy stress test utility
# version : 0.1
# author  : Sergei Mozhaisky (technix@frenzy.org.ua)
#           Based on stress test methodics, 
#           described on http://dedic.ru/node/66
#

mi="\033[0;40;32m[*]\033[0m"
ix="\033[0;40;36m#\033[0m"

stress_bin="/usr/local/bin/stress"
memtest_bin="/usr/local/bin/memtest"
TESTPATH="/tmp"
MEM="32"

. gettext.sh
export TEXTDOMAIN="frenzytools"                                                 
export TEXTDOMAINDIR="/usr/local/share/locale"
msg_stage1=$(gettext "Step 1: High load (5 min)")
msg_stage2=$(gettext "Step 2: Memory test (one pass)")
msg_stage3=$(gettext "Step 3: Combined load and memory test")
msg_stage4=$(gettext "Step 4: Compression test")
msg_stage5=$(gettext "Step 5: CPU overload (1 min)")
msg_stage6=$(gettext "Step 6: Disk IO and Memory subsystems")
msg_quitprog=$(gettext "Exit")
msg_title=$(gettext "Stress testing")
msg_backtitle=$(gettext "Frenzy stress testing utility 0.1")
msg_selectstages=$(gettext "Select test stages")
msg_error=$(gettext "Error")
msg_notfound=$(gettext "not found")
msg_configtest=$(gettext "Configure")
msg_memsize=$(gettext "Set memory size for test (in Mb)")
msg_testpath=$(gettext "Set path to temporary dir on HDD (must be RW)")

# FUNCTIONS

stress_test ()
{
case $1 in
1)
printf "${mi} $msg_stage1 \n"
cmd="$stress_bin -c 13 -t 5m"
printf "${ix} ${cmd} \n"
eval "${cmd}"
;;
2)
printf "${mi} $msg_stage2 \n"
cmd="$memtest_bin ${MEM} 1"
printf "${ix} ${cmd} \n"
eval "${cmd}"
;;
3)
printf "${mi} $msg_stage3 \n"
cmd="$stress_bin -c 13 --quiet & $memtest_bin ${MEM} 1"
printf "${ix} ${cmd} \n"
eval "${cmd}"
cmd="killall stress"
printf "${ix} ${cmd} \n"
eval "${cmd}"
;;
4)
printf "${mi} $msg_stage4 \n"
IMGSIZE=`expr ${MEM} \* 2`
cmd="dd if=/dev/urandom of=${TESTPATH}/random.dat bs=1M count=${IMGSIZE}"
printf "${ix} ${cmd} \n"
eval "${cmd}"
cmd="bzip2 -c < ${TESTPATH}/random.dat > ${TESTPATH}/random2.dat.bz2"
printf "${ix} ${cmd} \n"
eval "${cmd}"
cmd="bzip2 -dc < ${TESTPATH}/random2.dat.bz2 > /dev/null "
printf "${ix} ${cmd} \n"
eval "${cmd}"
cmd="rm ${TESTPATH}/random.dat"
printf "${ix} ${cmd} \n"
eval "${cmd}"
cmd="rm ${TESTPATH}/random2.dat.bz2"
printf "${ix} ${cmd} \n"
eval "${cmd}"
;;
5)
printf "${mi} $msg_stage5 \n"
cmd="$stress_bin --quiet --cpu 1k -t 1m"
printf "${ix} ${cmd} \n"
eval "${cmd}"
;;
6)
printf "${mi} $msg_stage6 \n"
cmd="$stress_bin -i 4 --quiet & $memtest_bin ${MEM} 5"
printf "${ix} ${cmd} \n"
eval "${cmd}"
;;
esac
}

set_memsize()
{
${DIALOG} \
--cancel-label "${msg_quitprog}" --title " ${msg_configtest} " \
--backtitle "${msg_backtitle}" --no-collapse \
--inputbox "${msg_memsize}" 9 50 "$MEM" 2>${DIALOGRES}
MEM=`cat ${DIALOGRES}`
}

set_path()
{
${DIALOG} \
--cancel-label "${msg_quitprog}" --title " ${msg_configtest} " \
--backtitle "${msg_backtitle}" --no-collapse \
--inputbox "${msg_testpath}" 9 50 "${TESTPATH}" 2>${DIALOGRES}
TESTPATH=`cat ${DIALOGRES}`
}


init_dialog()
{
DIALOG="cdialog "
d=`basename $0`
DIALOGRES=`mktemp -q /tmp/${d}.XXXXXX`
msg_quit="<"
}

destroy_dialog()
{
rm ${DIALOGRES}
}



# MAIN

init_dialog

if [ ! -f $stress_bin ]; then
${DIALOG} \
--cancel-label "${msg_quitprog}" --title " ${msg_error} " \
--backtitle "${msg_backtitle}" --no-collapse \
--msgbox "\nstress: $msg_notfound" 7 30
destroy_dialog
exit
fi

if [ ! -f $memtest_bin ]; then
${DIALOG} \
--cancel-label "${msg_quitprog}" --title " ${msg_error} " \
--backtitle "${msg_backtitle}" --no-collapse \
--msgbox "\nmemtest: $msg_notfound" 7 30
destroy_dialog
exit
fi

CHOICE=""
while [ "${CHOICE}" != "${msg_quit}" ]; do

# DIALOG: select action
${DIALOG} \
--cancel-label "${msg_quitprog}" --title " ${msg_title} " \
--backtitle "${msg_backtitle}" --no-collapse \
--checklist "${msg_selectstages}" 15 65 8 \
"1" "$msg_stage1" on \
"2" "$msg_stage2" on \
"3" "$msg_stage3" on \
"4" "$msg_stage4" on \
"5" "$msg_stage5" on \
"6" "$msg_stage6" on 2>${DIALOGRES}

case $? in
0)
CHOICE=`cat ${DIALOGRES}`
clear
set_memsize
if [ -n "`echo ${CHOICE} | grep 4`" ]; then
set_path
fi
destroy_dialog
for stage in `echo ${CHOICE} | sed "s/\"//g"`
do
stress_test $stage
done
exit
;;
1)
CHOICE=${msg_quit}
;;
255)
CHOICE=${msg_quit}
;;
esac
done

destroy_dialog
