六、Monkey测试

一、什么是monkey

Monkey 是 Google 提供的一个用于 稳定性与压力测试 的命令行工具

二、为什么要做压力测试

  • 随机性
  • 完整性
  • 持续性

三、命令使用介绍

monkey 命令格式

adb shell --help
adb shell monkey [参数] {随机发送事件数} 

基本用法

  • 在命令提示符中输入adb devices 查看有无连接的设备
  • 确认有设备连接
  • 获取 app 的包名
  • 执行命令:adb shell monkey [参数] {随机发送事件数}

Monkey 命令1 - 添加事件数

adb shell monkey <事件数>

monkey 打印详细日志

adb shell monkey -v <事件数>
adb shell monkey -vv <事件数>
adb shell monkey -vvv <事件数>   ----打印最详细日志

monkey 测试停止条件

  • 1、指令执行完成

  • 2、遇到崩溃,或者异常

  • 3、杀掉进程

    adb shell
    ps | findstr monkey  -----查找monkey进程
    kill  -9    -------进程号
    

monkey 运行某个包

  • 指定一个包 adb shell monkey -p 包名 [数件数]
  • 指定多个包 adb shell monkey -p 包名 -p 包名 [事件数]

指定事件时间间隔

格式:--throttle <milliseconds>:每个事件结束后的间隔时间
adb shell monkey -p 包名 --throttle 时长(毫秒) 

image-20211224200652168

设置种子数

格式:-s  <seednumber>:用于指定伪随机数生成器的seed(种子)值
adb shell monkey -p 包名 --throttle 事件间隔时长(毫秒) -s 种子号 -vvv 事件数

根据上次的运行结果,拿到种子,然后再次执行,可以再现上次运行

设置事件比例

--pct-touch:触摸事件 (  设定点击事件百分比)
--pct-motion :滑动事件(设定动作事件百分比  )
--pct-trackball :轨迹球事件(  设定轨迹球事件百分比 )
--pct-nav :导航事件( 设定基本导航事件百分比,比如输入设备上、下、左、右键 )
--pct-majornav:主要导航事件( 设定主要导航事件百分比,比如中间键、返回键、菜单键 )
--pct-syskeys:系统按键事件( 设定系统导航事件百分比,比如HOME键、BACK建、拨号键及音量键 )
--pct-appswitch:activity之间的切换(  设定启动Activity事件百分比)
--pct-pinchzoom:缩放事件
--pct-rotation:屏幕旋转事件
--pct-flip:键盘事件
--pct-anyevent :任意事件 (设定不常用事件百分比)
adb shell monkey -p 包名 --pct-touch 80 --pct-motion 20 --throttle 1000 -vvv 200

忽略异常

--ignore-crashes:忽略崩溃
--ignore-timeouts:忽略超时
--ignore-security-exceptions:忽略安全异常
--kill-process-after-error:发生错误后直接杀掉进程
--dbg-no-events:初始化启动的activity,但是不产生任何事件
--hprof:指定该项后在事件序列发送前后会立即生成分析报告 
--monitor-native-crashes:跟踪本地方法的崩溃问题
--wait-dbg:直到连接了调试器才执行monkey测试
–bugreport:当ANR,系统无响应,Crash的时候获得bugreport
adb shell monkey -p 包名 --pct-touch 80 --pct-motion 20 --throttle 1000 --ignore-crashes -vvv 200

保存日志

方式一: adb shell monkey 1000> 文件路径/文件名

方式二: adb logcat -v time '*E' > monkey.log 过滤保存Error级别的日志到本地文本

本文来自博客园,作者:農碼一生,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/15728937.html


技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
个人开源代码链接:
GitHub:https://github.com/ITMingliang
Gitee:https://gitee.com/mingliang_it
GitLab:https://gitlab.com/ITMingliang
进开发学习交流群:

原文地址:https://www.cnblogs.com/wml-it/p/15728937.html