#!/bin/bash
#################################################################################
#                  说   明
# 1、下载耗时测试
# 2、多线程测试
#
# 使用方法: ./webtest     
#   使用本脚本程序,可进程对网关web进行压力测试,测试
# 功能主要包括:
# 1、对自身机器的压力测试
# 2、对其他机器的压力测试
#   
###################################################################################
######################################自定义部分####################################
#
# 参数简介:
# SERVER为配置要测试的机器IP地址
# NAME定义设备的名称
# GRAPH定义是否生成折线图的参数文件
# VALUE此值为定义对下载速度测试的次数
# TIME 配置是否开启长时间测试,不间断测试
# MODE 配置本机器担当的角色
#
#####################################################################################
#要测试的URL地址,也可写ftp的。
URL=https://192.168.0.133/wget.jsp
export URL
#服务器标识
NAME=lanse
#设置是否生成图表数据
#GRAPH=OFF
GRAPH=ON
#设置下载速度测试的次数;
#如果在SERVER端配置此参数后,客户端无法同步,可以tailf test.logfor;
#当wget到这个数值时,再在客户端执行脚本。如果不需要可以将此处设置为0;
#SC模式不受其影响
VALUE=0
#VALUE=5000
#长时间压力测试单位(分钟);
#配置TIME小于MAXTIME为开启长时间压力测试。如果相等为不开启。
MAXTIME=6000
TIME=6000
#设置模式,分(SC/SERVER/CLIENT)
#SC 即是客户端模式,又是服务器模式,主要用于自压测试;
#SERVER 主要用来测试别的设备;
#CLIENT 当作客户端来用,主要配合另一台SERVER对自己的压力测试来提取系统资源信息。
#MODE=CLIENT
MODE=SERVER
#MODE=SC
#配置压力级别, 分0/1/2/3, 4个级别,级别越高,压力越小, 默认0为最大压力;
#适合稳定性测试。
LEVEL=0
#LEVEL=1
#LEVEL=2
#LEVEL=3
#配置停止几秒后增加一个线程。
SPACE=3
#配置本次压力的测试,一共下载多少次,用来精确分析数据,最好不要与长时间同时配置;
#主要可以得到对不同设备测试,下载所用时间,成功率,失败率;
#如果同时配置,那个先执行完就结束进程;
#配置NUMBER小于MAXNUMBER为开启状态。
MAXNUMBER=9000000
NUMBER=3000

###########################程序运行部分########################################
>file
>startfail
>test.logfor
echo 2 >sta
date +%s > time
echo 2 > whstat
>OK
>test.logwhile
>graph
>established
  #1^$#
  if [ $MODE = SC -o $MODE = SERVER ] ; then
     echo 'time=`date +"%Y-%m-%d %H:%M:%S"`' > test.sh
     echo "while [ 1 ] " >> test.sh
     echo "do" >> test.sh
     echo "wget $URL -c -q -t 3 -T 5 --no-check-certificate" >>test.sh
     echo ' if [ $? -eq 0 ] ; then ' >>test.sh
     echo '   echo $time wget ok  >> test.logwhile ' >>test.sh
     echo " else " >> test.sh
     echo ' echo $time wget fail >> test.logwhile ' >>test.sh
     echo " fi" >>test.sh
      if [ $LEVEL -eq 1 ] ; then
         echo "sleep 1 " >> test.sh
      fi
      if [ $LEVEL -eq 2 ] ; then
         echo "sleep 2 " >> test.sh
      fi
      if [ $LEVEL -eq 3 ] ; then
         echo " sleep 3 " >> test.sh
      fi
     echo "done" >> test.sh
     chmod a+x test.sh   
     for ((i=0;i<=$VALUE;i++))
     do
       #2^#
       wget $URL -c -q -t 3 -T 5 --no-check-certificate
       if [ $? -eq 0 ] ; then
         echo $time wget ok   $i  >> test.logfor
       else
         echo $time wget fail $i  >> test.logfor
       fi
       #2$#
      done
      T=`cat time`
      N=`date +%s `
      S=`echo "$N-$T" | bc `
      M=`echo "scale=1;$S/60"|bc |awk -F. '{print $1}'`
      MS=`echo "scale=1;$S/60"|bc |awk -F. '{print $2*0.01*60}'|awk -F. '{print $1}'`
      echo \ ; echo \ >> file
      echo "当前模式为 【 $MODE 】 模式" >> file
      echo \ ; echo \ >> file
      echo "下载速度测试结果:" >>file
      if [ $S -lt 60 ] ; then
        echo \ ; echo \ >> file
        echo "本次下载用了$S 秒" >>file
      else
        echo \ ; echo \ >> file
        echo "本次下载用了$M 分 $MS 秒" >>file
      fi
      F=`grep fail test.logfor |wc -l `
      O=`grep ok  test.logfor  |wc -l `
      echo \ ; echo \ >>file
      echo "下载成功 $O 个" >>file
      echo \ ; echo \ >> file
      echo "下载失败 $F 个" >>file
  fi
  #1$#
      BTIME=`echo "$TIME*60"|bc`
      MAXTIME=`echo "$MAXTIME*60"|bc`
  #3^#
  if [ $MODE = SC -o $MODE = SERVER -o $MODE = CLIENT ] ; then
    while [ 1 ]
    do
    #4^#
    grep 1 whstat &>/dev/null
    if [ $? -eq 0 ] ; then
       echo \ ; echo \ >> file
          if [ $MODE = CLIENT ] ; then
             echo \ ; echo \ >> file
             echo "当前模式为 【 $MODE 】 模式" >> file
          fi
          if [ $MODE = SERVER -o $MODE = SC ] ; then
             echo \ ; echo \ >> file
             echo "本次测试的对象链接为 $URL " >> file
          fi
       echo \ ; echo \ >> file
       echo "下载压力测试结果:" >>file
       #5^#
       if [ $MODE = SC -o $MODE = SERVER  ] ; then
          OK=`tail -n 1 ok |awk '{print $2}'`
          echo \ ; echo \ >> file
          echo "本次测试共成功启动了 $OK 次进程" >>file
          T=`cat time`
          N=`date +%s `
          if [ $BTIME -lt $MAXTIME -o $NUMBER -lt $MAXNUMBER ] ; then
             S=`echo "$N-$T" |bc`
             M=`echo "scale=1;$S/60"|bc |awk -F. '{print $1}'`
             MS=`echo "scale=1;$S/60"|bc |awk -F. '{print $2*0.01*60}'|awk -F. '{print $1}'`
            if [ $S -lt 60 ] ; then
               echo \ ; echo \ >> file
               echo "本次测试共用了 $S 秒" >>file
            else
               echo \ ; echo \ >> file
               echo "本次下载用了 $M 分 $MS 秒" >>file
            fi
          else
            O=`wc -l ok|awk '{print $1}'`
            L=`echo "$O*3" | bc `
            S=`echo "$N-$T-$L" | bc `
            echo \ ; echo \ >> file
            echo "本次测试共用了 $S 秒" >>file
            echo ; echo ;
            M=`echo "scale=1;$S/60"|bc |awk -F. '{print $1}'`
            MS=`echo "scale=1;$S/60"|bc |awk -F. '{print $2*0.01*60}'|awk -F. '{print $1}'`
            if [ $S -lt 60 ] ; then
               echo \ ; echo \ >> file
               echo "本次测试共用了 $S 秒" >>file
            else
               echo \ ; echo \ >> file
               echo "本次下载用了 $M 分 $MS 秒" >>file
            fi
          fi
        fi
        #5$#
     #6^#
     if [ $GRAPH = ON ] ; then
        if [ $MODE = CLIENT -o $MODE = SC ] ; then
          ESTAB=`wc -l established|awk '{print $1}'`
          AVEESTAB=`cat established | awk -vESTAB="$ESTAB" '{total+=$1}END {print total/ESTAB}'|awk -F. '{print $1}'`
          MEM=`wc -l mem |awk '{print $1}'`
          AVEMEM=`cat mem |awk -vMEM="$MEM" '{total+=$1}END {print total/MEM"%"}' `
          echo \ ; echo \  >>file
          echo "本次测试机器内存平均使用用率为 $AVEMEM " >> file
          CPU=`wc -l cpu |awk '{print $1 }'`
          AVECPU=`cat cpu |grep -v CPU | awk -vCPU="$CPU" '{total+=$1}END {print total/CPU"%"}' `
          echo \ ; echo \  >>file
          echo "本次测试机器CPU平均使用用率为 $AVECPU " >> file
          echo \ ; echo \  >>file
          echo "本次测试平均建立的连接为 $AVEESTAB 个"  >> file
          echo \ ; echo \  >>file
          echo "提示:【将文件graph用excel打开,生成折线图.】" >>file
          echo
          echo " $NAME graph " >> graph
          cat ok |awk '{print $2}' | paste -s >> graph
          cat cpu | paste -s >> graph
          cat mem | paste -s >> graph
          cat established |paste -s >> graph
        fi
     fi
     #6$#
     #7^#
     if [ $MODE = SC -o $MODE = SERVER  ] ; then
       #8^#
       if [ $BTIME -lt $MAXTIME -o $NUMBER -lt $MAXNUMBER ] ; then
          sum=`cat sum`
          echo \ ; echo \  >>file
          echo "本次测试共下载了 $sum 次" >> file
          succok=`cat succok`
          echo \ ; echo \  >>file
          echo "本次测试共下载成功了 $succok 次" >> file
          SUCCOK=`echo "scale=2;$succok/$sum*100" |bc`%
          echo \ ; echo \  >>file
          echo "本次测试的成功率为  $SUCCOK " >> file
          fail=`cat fail`
          echo \ ; echo \  >>file
          echo "本次测试共下载失败了 $fail 次" >>file
          FAIL=`echo "scale=2;$fail/$sum*100" | bc`%
          echo \ ; echo \  >>file
          echo "本次测试的失败率为 $FAIL" >> file
          startfail=`cat startfail |awk '{print $2}'`
          grep fail test.logwhile &> /dev/null
          if [ $? -eq 0 ] ; then
            echo \ ; echo \  >>file
            echo "本次测试在启动第 $startfail 次开始出现下载失败现象。 " >> file
          fi
        fi
        #8$#
      fi
      #7$#
           killall bash &>/dev/null
           ps aux |grep wget |grep -v grep |awk '{print $2}'|xargs kill -9 &>/dev/null
           ps aux |grep test.sh |grep -v grep |awk '{print $2}'|xargs kill -9 &>/dev/null
           sleep 3
           clear
           if [ $MODE = SC -o $MODE = SERVER ] ; then
             for i in {1..1000}
             do
               echo $i > /dev/null
               echo ;
              done
           fi
           clear
           echo ; echo ;
           cat file;rm -f test.logfor;rm -f ok;rm -f whstat;rm -f mem; rm -f cpu; rm -f fail; rm -f succok
           rm -f sum;rm -f startfail;rm -rf established;rm -f test.logwhile;rm -f sta
           echo ; echo ;
           ps aux |grep webtest|grep -v grep |awk '{print $2}'|xargs kill -9 &>/dev/null
           break
           exit
    fi
    #4$#
      echo "Start threads" > ok
      echo "CPU Rate" > cpu
      echo "MEM Rate" > mem
      echo "ESTABLISHED"  > established
      I=1000
    #9^#
      if [ $MODE = CLIENT ] ; then 
        SPACE=`echo "$SPACE-2"|bc`
      fi
    if [ $BTIME -lt $MAXTIME ] ; then
          I=99999999999999999999999999
    fi
    if [ $NUMBER -lt $MAXNUMBER ] ; then
         I=$MAXNUMBER
    fi
    #9$#
    for ((i=1;i<=$I;i++))
    do
    #10^#
    if [ $MODE = SC -o $MODE = SERVER   ] ; then
        ./test.sh &
    fi
    #10$#
        sleep $SPACE
        echo "start $i " >> ok
      STAT=`grep 1 sta &>/dev/null ; echo $?`
      if [ $STAT -eq 1 ] ; then
        grep fail test.logwhile &>/dev/null
        if [ $? -eq 0 ] ; then
          tail -n 1 ok > startfail
          echo 1 > sta
        fi
      fi
    #11^#     
    if [ $GRAPH = ON ] ; then
      if [ $MODE = CLIENT -o $MODE = SC ] ; then
         free -m |grep Mem |awk '{print $2,$4+$6+$7}'|awk '{print $1,$1-$2}'| awk '{print $2/$1*100}'>>mem
         top -n 2 |grep Cpu |tail -n 1 |awk -F, '{print $1}' |awk -F: '{print $2}' |awk -F% '{print $1}'|awk -F" " '{print $2}' >>cpu
         netstat -antlp |egrep '[443|ESTAB|$SERVER]' | wc -l >>established
      fi
    fi
    #11$#
      #12^#
      if [ $BTIME = $MAXTIME -o $BTIME -gt $MAXTIME ] ; then  
        if [  $NUMBER = $MAXNUMBER -o $NUMBER -gt $MAXNUMBER ] ; then
            grep fail test.logwhile &>/dev/null
           #13^#
           if [ $? -eq 0 ] ; then
              echo 1  > whstat
              break
           fi
           #13$#
         fi
      #12--#
      else
        #14^#
        if [ $BTIME -lt $MAXTIME  ] ; then
          T=`cat time`
          C=`echo "$TIME+$T" |bc`
          N=`date +%s`
          V=`echo "$N-$C"|bc`
          #15^#
          if [ $V -gt $BTIME ] ; then
            grep fail test.logwhile | wc -l |awk '{print $1}' >fail
            grep ok test.logwhile |wc -l |awk '{print $1}' >succok
            wc -l test.logwhile |awk '{print $1}'  > sum
            echo 1 > whstat
            break
          fi
          #15$#
       fi
       #14$#
    fi
    #12$#
    #18^#
    if [ $NUMBER -eq $MAXNUMBER -o $NUMBER -gt $MAXNUMBER ] ; then
       #13^#
      # if [ ! $BTIME = $MAXTIME -o $BTIME -gt $MAXTIME ] ;then
           grep fail test.logwhile &>/dev/null
           if [ $? -eq 0 ] ; then
              echo 1  > whstat
              break
           fi
           #13$#
       # fi
        #13$#
     else
       if [ $NUMBER -lt $MAXNUMBER ] ; then
         LINE=`wc -l test.logwhile |awk '{print $1}'`
         #19^#
         if [ $LINE -gt $NUMBER ] ; then
           grep fail test.logwhile | wc -l |awk '{print $1}' >fail
           grep ok test.logwhile |wc -l |awk '{print $1}' >succok
           wc -l test.logwhile |awk '{print $1}'  > sum
           echo 1 > whstat
           break
         fi
         #19$#
       fi
       #17$#
    fi
    #18$#
    done
    done
  fi
  #3$#
 
效果图
webtest_压力测试
 
webtest_职场_02