批处理bat 之 Android Monkey

实现功能:

1.运行monkey_run.bat,直接执行monkey命令;

2.运行monkey_stop.bat,输入PID,kill mongkey进程;

3.同步获取logcat;

4.同步保存CPU和内存值(top命令);

 

monkey_run.bat 代码:

@echo off

if "%time:~0,1%"==" " set "time=0%time:~1%"
set date_folder=%date:~0,4%-%date:~5,2%-%date:~8,2%

if NOT EXIST MonkeyLog_%date_folder% mkdir MonkeyLog_%date_folder%

adb wait-for-device
adb shell "ps|grep com.android.commands.monkey"

if "%ERRORLEVEL%"=="1" ( 
    start %~dp0Batch\logcat.bat
    start %~dp0Batch\top.bat
    start %~dp0Batch\monkey.bat
) else (
    echo Monkey早就运行了,不需要重复运行。
    echo 如果想重新运行,请先停止monkey!
    pause
)

 

locat.bat 代码:

@echo off

if "%time:~0,1%"==" " set "time=0%time:~1%"
set date_folder=%date:~0,4%-%date:~5,2%-%date:~8,2%
set time_folder=%time:~0,2%-%time:~3,2%-%time:~6,2%
set log_name=logcat_%date_folder%-%time_folder%
set log_path=MonkeyLog_%date_folder%

adb wait-for-device

echo - - - logcat window- - -
adb logcat > ./%log_path%/%log_name%.log
echo - - - Over! - - -

 

top.bat 代码:

@echo off

if "%time:~0,1%"==" " set "time=0%time:~1%"
set date_folder=%date:~0,4%-%date:~5,2%-%date:~8,2%
set time_folder=%time:~0,2%-%time:~3,2%-%time:~6,2%
set log_name=top_%date_folder%-%time_folder%
set log_path=MonkeyLog_%date_folder%

adb wait-for-device
echo - - - top window - - -
adb shell top -m 10 -d 1 > ./%log_path%/%log_name%.log
echo - - - Over! - - -

 

monkey.bat 代码:

@echo off

if "%time:~0,1%"==" " set "time=0%time:~1%"
set date_folder=%date:~0,4%-%date:~5,2%-%date:~8,2%
set time_folder=%time:~0,2%-%time:~3,2%-%time:~6,2%
set log_name=monkey_%date_folder%-%time_folder%
set log_path=MonkeyLog_%date_folder%

adb wait-for-device

echo - - - Monkey window - - -
adb shell monkey ^
-p package1 ^
-p package2 ^
--pct-touch 30 ^
--pct-motion 20 ^
--pct-trackball 20 ^
--pct-pinchzoom 10 ^
--pct-appswitch 20 ^
--ignore-crashes ^
--ignore-timeouts ^
--throttle 1000 -v -v -v 2808000 > ./%log_path%/%log_name%.log
echo - - - Over! - - -


::--ignore-crashes  
::应用程序发生任何超时错误时(如“Application Not responding”对话框),monkey继续运行
::--ignore-timeouts
::调整触摸事件的百分比,一次ACTION_DOWN,一次ACTION_UP 例如:–pct-touch 10 //百分之十为触摸事件
::--pct-touch 30 
::滑动事件,按下->移动->抬起,ACTION_DOWN ACTION_MOVE ACTION_UP
::--pct-motion 20 
::轨迹事件
::--pct-trackball 20
::双指缩放
::-–pct-pinchzoom 10
::启动Activity
::--pct-appswitch 20 
::系统按键事件,如Home、Back、Start Call、End Call及音量控制键
::-–pct-syskeys 0
::15小时:1080000(周内:晚上6点--早上9点)
::39小时:2808000(周末:周六晚上6点--周一早上9点)

 

monkey_stop.bat 代码:

@echo off

::方法一,直接kill进程
adb shell "kill -9 `ps -ef | grep com.android.commands.monkey | grep -v 'grep' | awk '{print $2}'`"
echo Monkey 已停止运行!!!
pause

::方法二,手动输入pid后,kill进程
::adb shell "ps -ef | grep com.android.commands.monkey | grep -v 'grep' | awk '{print $2,$8}'"
::if NOT "%ERRORLEVEL%"=="1" (
::    set /p killmonkey=请输入pid:
::    ::设置变bai量延迟
::    setlocal EnableDelayedExpansion
::    adb shell kill !killmonkey!
::    echo Monkey 已停止运行!!!
::    pause
::) else (
::    echo Monkey 不存在!
::    pause
::)

 

 

问题点:

1.关于文件名称获得的当前时间,01:00~09:00时间段,文件名乱码

  =>解决方法:if "%time:~0,1%"==" " set "time=0%time:~1%"

2.获取当前bat脚本相对路径

  =>解决方法:%~dp0

3.代码太长,换行显示

  =>解决方法:连接符 ^

4.if语句内使用set

  =>解决方法:设置变bai量延迟 setlocal EnableDelayedExpansion ,并且变量引用时使用感叹号!不使用百分号%

5.ps后不显示grep xxx

  =>解决方法:添加 grep -v 'grep'

6.筛选显示

  =>解决方法:添加 awk '{print $2,$8}'

7.结合kill

  =>解决方法:使用反单引号 kill -9 ` `

posted @ 2021-01-03 12:26  张小力  阅读(535)  评论(0编辑  收藏  举报