#!/bin/bash          
# Setting variables
SIMULT_CALLS=(
100
200
300
400
500
600
700
800
900
1000
1100
1200
1300
)
#calls per second during measuring
FREQ_CALLS=(
0
10
15
20
25
30
)
SERVER_BIN="freeswitch"
#target number on tested server
TARGET="5000"
TARGET_IP="192.168.10.176:5060"
SERVER_PID=`ps -e | grep $SERVER_BIN | awk '{ print $1; }'`
LOG="./sipp_benchmark.log"
TEMP_LOG="/tmp/sipp_benchmark_temp_log"
SIPP_PATH="."
####################################################################
rm *.csv > /dev/null
echo "************************************************" > $TEMP_LOG
echo "`date` Starting SIPp benchmark script" >> $TEMP_LOG
echo "***********************************************" >> $TEMP_LOG
echo "Total call;Rate;<10ms;<20ms;<30ms;<40ms;<50ms;<100ms;<150ms;<200ms;>200ms;CPU" > delay.csv
echo "Please wait, testing in progress, this may take a few hours"
for simult in ${SIMULT_CALLS[*]}; do
   echo `date`" Test for $simult simultaneous calls" >> $TEMP_LOG
   for freq in ${FREQ_CALLS[*]}; do
    if [[ $freq -eq 0 ]]; then
	freq=11
	LIMIT=$simult
	TIMER=`expr 1000 \* $simult / $freq + 25000`
    else
        LIMIT=`expr $simult + 100`
        TIMER=`expr $simult / $freq \* 1000`
    fi
    echo `date`" Test for $freq calls/s" >> $TEMP_LOG
    CALLS=`expr 25 \* $freq + $simult`
    $SIPP_PATH/sipp -sn uac -m $CALLS -r $freq -d $TIMER -s $TARGET -l $LIMIT -bg -i 192.168.10.176 -trace_stat $TARGET_IP
    WAIT=`expr $simult / $freq + 10`
    sleep $WAIT
    echo `date` > /tmp/sipp_top_temp.log
    top -d 1 -n 10 -b -p $SERVER_PID >> /tmp/sipp_top_temp.log
    echo `date` >> /tmp/sipp_top_temp.log
    WAIT=`expr $simult / $freq + 10`
    sleep $WAIT
    for k in `cat /tmp/sipp_top_temp.log |grep $SERVER_BIN | awk '{ print $9 }'`;
	do
	    CPU=`expr $CPU + $k`
    done
   CPU=`expr $CPU / 10`
   echo "CPU utilization:  $CPU %">> $TEMP_LOG
   CPU=0
   mv uac*.csv "bench_res_$simult calls $freq per sec.csv"
   sleep 5
   DATA=`tail -1 "bench_res_$simult calls $freq per sec.csv" | awk -F ";" '{ print $13 ";" $6 ";" $70 ";" $71 ";" $72 ";" $73 ";" $74 ";" $75 ";" $76 ";" $77 ";" $78 }'`
   echo "$DATA;$CPU">> "delay.csv"
   done
done
cat bench*.csv > cumulative_results.csv
echo `date`" Benchmark completed, look at file delay.csv" >> $TEMP_LOG
cat $TEMP_LOG >> $LOG
rm $TEMP_LOG
exit 0