android应用压测工具

第一步:创建主运行脚本run.sh

#!/bin/sh

path_current=`pwd`
path_script=$(cd "$(dirname "$0")"; pwd)

while true
do
   /bin/bash $path_script/script/check.sh
   read -p "input action[killall|start|stop|help|exit]:" mode
   case "$mode" in
     'start')
        read -p "input device IP[192.168.30.25]: " ip
        read -p "input watch type such as [temp,frame,none],default none: " type
        echo "start parameter: IP: $ip  -  type:$type"
        if [ "$ip" != "" ]; then
            /bin/bash $path_script/script/start.sh "$ip:5555" "$type"
        fi
        if [ "$ip" == "" ]; then
           echo "IP should not be empty"
        fi
     ;;
     'stop')
        read -p "input device IP[192.168.30.25]: " ip
        echo "stop parameter: IP: $ip  -  type:$type"
        if [ "$ip" != "" ]; then
           /bin/bash $path_script/script/stop.sh "$ip:5555"
        fi
     ;;
     'killall')
         /bin/bash $path_script/script/killall.sh
     ;;
     'exit')
         exit 0
     ;;
     *)
     ;;
   esac     
done

第二步:创建script/connect.sh脚本

#!/bin/sh

path_current=`pwd`
path_script=$(cd "$(dirname "$0")"; pwd)

target=$1
if [ "$target" == "" ]; then
   echo "should add parameter to connect command, like connect.sh 192.168.30.25:5555"
   exit 1
fi
result=$(adb devices|grep "${target}"|grep -v offline|grep -v grep)
if [ "$result" != "" ]; then
   exit 0
fi
adb connect $target
adb -s "$target" root

第三步:创建script/check.sh脚本

#!/bin/sh

path_current=`pwd`
path_script=$(cd "$(dirname "$0")"; pwd)
ps -ef | grep "mylogcat" | grep -v grep
ps -ef | grep "mytail" | grep -v grep
ps -ef | grep "adb" | grep -v grep

第三步:创建script/stop.sh脚本

#!/bin/sh

path_current=`pwd`
path_script=$(cd "$(dirname "$0")"; pwd)

target=$1
if [ "$target" == "" ]; then
   echo "should like 192.168.30.25:5555"
   exit 1
fi

ps -ef | grep "$target"| grep -v grep

while true
do
  app_process=`ps -ef | grep "$target"| grep -v grep`
  echo $app_process | awk '{print ($2)}'
  stop=1
  if test -n "$app_process"; then
     echo "had find app process informaton"
     echo $app_process | awk '{print ($2)}' | xargs kill -9
     stop=0
  fi
  if [ $stop -eq 1 ]; then
    break;
  fi
done

第四步:创建启动脚本script/start.sh

#!/bin/sh

path_current=`pwd`
path_script=$(cd "$(dirname "$0")"; pwd)

target=$1
param=$2
if [ "$target" == "" ]; then
   echo "should like run.sh 192.168.30.25:5555 temp,frame"
   exit 1
fi

nohup /bin/bash ${path_script}/mytail.sh $* &
nohup /bin/bash ${path_script}/mylogcat.sh $* &

第五步:创建mytail.sh脚本

#!/bin/sh

path_current=`pwd`
path_script=$(cd "$(dirname "$0")"; pwd)
path_data=$path_script/../

target=$1
param=$2
if [ "$target" == "" ]; then
   echo "should like mytail.sh 192.168.30.25 temp,frame"
   exit 1
fi

while true
do
  run_time=$(date "+%Y%m%d%H%M")
  if [ ! -d "$path_data/${target}" ]; then
     mkdir -p "$path_data/${target}"
  fi
  echo "--------[${run_time}]----------------" >> $path_data/${target}/tail.txt
  /bin/bash $path_script/connect.sh $*
  adb -s "$target" shell "free -h" >> $path_data/${target}/tail.txt
  adb -s "$target" shell "top -n 1|grep com.commaai." >> $path_data/${target}/tail.txt
  sleep 1
  adb -s "$target" shell "top -n 1|grep com.commaai." >> $path_data/${target}/tail.txt
  temp=$(echo $param | grep "temp")
  if [ "$temp" != "" ];then
     adb  -s "$target" shell "cat /sys/class/thermal/thermal_zone*/temp" >>  $path_data/${target}/tail.txt
  fi
  frame=$(echo $param | grep "frame")
  if [ "$frame" != "" ];then
     adb  -s "$target" shell "tail -n 2 /storage/emulated/0/Log/brokenflow.txt" >>  $path_data/${target}/tail.txt
  fi
  sleep 50
done

第六步:创建script/mylogcat.sh脚本

#!/bin/sh

path_current=`pwd`
path_script=$(cd "$(dirname "$0")"; pwd)
path_data=$path_script/../;

target=$1
while true
do
  run_time=$(date "+%Y%m%d%H%M")
  /bin/bash $path_script/connect.sh $*
  if [ ! -d "$path_data/${target}" ]; then
     mkdir -p "$path_data/${target}"
  fi
  echo "--------[${run_time}]----------------" >> $path_data/${target}/error.txt
  adb -s "$target" logcat *:E >> $path_data/${target}/error.txt
done

第七步:创建清除所有脚本

#!/bin/sh

path_current=`pwd`
path_script=$(cd "$(dirname "$0")"; pwd)
echo "try to kill mylogcat"
while true
do
  app_process=$(ps -ef | grep "mylogcat" | grep -v grep)
  echo $app_process | awk '{print ($2)}'
  stop=1
  if test -n "$app_process"; then
    echo "had find app process informaton"
    echo $app_process | awk '{print ($2)}' | xargs kill -9
    stop=0
  fi
  if [ $stop -eq 1 ]; then
    break
  fi
done

echo "try to kill mytail"
while true
do  
  app_process=$(ps -ef | grep "mytail" | grep -v grep)
  echo $app_process | awk '{print ($2)}'
  stop=1
  if test -n "$app_process"; then
    echo "had find app process informaton"
    echo $app_process | awk '{print ($2)}' | xargs kill -9
    stop=0
  fi
  if [ $stop -eq 1 ]; then
     break;
  fi
done


echo "try to kill adb"
while true
do  
  app_process=$(ps -ef | grep "adb" | grep -v grep)
  echo $app_process | awk '{print ($2)}'
  stop=1
  if test -n "$app_process"; then
    echo "had find app process informaton"
    echo $app_process | awk '{print ($2)}' | xargs kill -9
    stop=0
  fi
  if [ $stop -eq 1 ]; then
    break;
  fi
done