monkey压力测试

 第一部分 背景 

1. 为什么要开展压力测试?
    提高产品的稳定性
    提高产品的留存率
    提高产品的使用率
2. 什么时候开展压力测试?
    首轮功能测试通过后
    下班后的夜间进行

第二部分 理论

1. 手工测试场景
  聊天步骤: 查找--添加--聊天
  TouchInput --- KeyEvent --- TouchSearch --- TouchName --- TouchFriend --- KeyEvent
  1.什么是Monkey? "Monkey是发送伪随机用户事件的功能"
  2.Monkey在那里? "在任意的安卓手机系统里"
  3.什么是ADB? " 安卓调试桥 Android Debug Bridge"
  建立硬的连接 /用USB线;
  建立软的连接 /用ADB.
  4.什么是MonkeyScript?
    " MonkeyScript是一组可以被Monkey识别的命令集合. "
    " MonkeyScript可以完成重复固定的操作 "
  5.什么是MonkeyRunner?
    " MonkeyRunner提供了一系列的API."
    " MonkeyRunner可以完成模拟事件及截图操作 "
  6. Monkey和MonkeyRunner的区别?
    " Monkey 在adb shell中, 生成用户或者系统的伪随机事件 "
    " MonkeyRunner 通过API定义特定命令和事件控制设备 "
  7.MonkeyRunner APIs
    1. MonkeyRunner : " 用来连接设备或者模拟器 "
    2. MonkeyDevice : " 提供安装>卸载应用, 发送模拟事件 "
    3. MonkeyImage : " 完成图像保存, 及对比的操作 "
  8.MonkeyRunner的测试类型?
    多设备控制
    功能测试
    回归测试
9. 压力测试结果?
  1. CRASH: 即奔溃, 应用程序在使用中, 非正常退出..
  2. ANR: Application Not Responding 应用程序无响应.

logcat是android中的一个命令行工具,可以用于得到程序的log信息

第三部分 实践

  1. 一个App的压力测试实践
    压力测试过程步骤:
    1. 在手机开发者选项中, 将USB调试选中.
    2. 确认手机和电脑成功连接. 确认软连接命令 在cmd中,输入adb devices
    3. 安装测试App 使用命令安装 "adb install package.apk" <需要将安装包放到platform-tools里>
    4. 压力测试 ,发送压力命令 adb shell monkey 1000 <进行1000次随机测试>
    当看到Events injected: 100 时说明成功执行完毕,
    5. 获取App包名 " adb logcat | grep START "
    windows 需要先执行" adb shell "进入shell, 然后在执行" logcat | grep START "
    点击 想要测试app, 就可以看到想要测试的包名."com.android.bbkcalculator "
    6. 给指定包大压力 " adb shell monkey -p com.android.bbkcalculator 1000 "

  2. Monkey高级参数的应用
    1. throttle 参数 " 指定事件之间的间隔 "
    adb shell monkey --throttle <milliseconds毫秒数>
例:adb shell monkey -p packagename --throttle 毫秒数 事件数
  2. seed 参数
    seed: 指定随机生成数的seed值, 也就唯一指定了随机操作序列了
    所谓随机,其实也是有规律的,特定的seed值,就对应唯一的随机操作序列.
    -s 后面的数字,就是seed值
命令: adb shell monkey -s <seed><event-count>
例:adb shell monkey -p packagename -s seed值 事件数
adb shell monkey -p packagename -s 100 50
  3. 触控事件
    设定触控事件百分比 " adb shell monkey --pct-touch <percent> "
    添加 -v 参数 ,可以将随机操作的详情列出来
    查看Event Percentages: 0表示触控事件.
例:adb shell monkey -p packagename --pct-touch 百分比 事件数
adb shell monkey -v -p packagename --pct-touch 百分比 事件数
  4. 动作事件
    设定动作事件的百分比 " adb shell monkey --pct-motion <percent> "
例:adb shell monkey -v -p packagename --pct-motion 百分比 事件数
联合使用:adb shell monkey -v -p packagename --pct-touch 百分比 --pct-motion 百分比 事件数
  5. 轨迹球事件
    设定轨迹球事件百分比 "adb shell monkey --pct-trackball <percent>"

  6. 基本导航事件
    设定基本导航事件百分比, 输入设备的上 下 左 右
    " adb shell monkey --pct-nav <percent> "
  7. 主要导航事件
    设定主要导航事件百分比, 兼容中间键, 返回键, 菜单按键
命令:adb shell monkey --pct-majornav 百分比
  8. 系统导航事件
    设定系统导航事件百分比, HOME, BACK, 拨号及音量键
    " adb shell monkey --pct-syskeys <percent> "
  9. 启动Activity事件
    设定启动Activity的事件百分比
    " adb shell monkey --pct-appswitch <percent> "
  10. 不常用事件
    设定不常用事件的百分比
    " adb shell monkey --pct-anyevent <percent> "
  11. 奔溃事件
    忽略奔溃和异常
    " adb shell monkey --ignore-crashes <event-count> "
  12 超时事件
    忽略超时事件
    " adb shell monkey --ignore-timeouts <event-count> "

  3. CRASH结果析取
    1. 出现crash后, 将cmd里面的, CRASH下面的log信息赋值出来给开发.
    2. 出现奔溃后, 可以记录下seed值,好进行奔溃的复现操作.
  4. ANR结果析取
    1. 析取ANR的异常信息
    2. 查看ANR下面的log信息
    3. 出现ANR,记录下日志,及seed值,可以进行后期的复现操作.
    4. 当然也可以通过查看文件,看anr信息
    先使用adb shell进入安卓系统的shell,
    使用 cd / data/ anr目录里找到anr的文件, 如trace.txt. 打开可以看到anr信息.

  复杂的参数进行测试

    adb shell monkey -v -v -v -s 123123 --throttle 300 --pct-touch 40 --pct-motion 25 --pct-appswitch 25 --pct-syskeys 10 --pct-majornav 0 --pct-nav 0 --pct-trackball 0 --ignore-crashes --ignore-timeouts --ignore-native-crashes -p xxx.xxx.xxx 100000 > d:\monkey.txt

 5. Monkey Script实例
    完成一些重复的固定的操作,使用monkeyScript
    执行monkey脚本的命令:"adb shell monkey -f <scriptfile脚本文件> <event-count执行次数>"
  1.DispatchTrackball命令,
    轨迹球事件
    DispatchTrackball(long downtime,long eventide,int action,float x,float y, float pressure, float size, int metastate,float xprecision, float yprecision,int device,int edgeflags)
    //action 0代表按下, 1代表弹起, x和y代表坐标点
    // 命令需要成对使用,一个按下0, 一个弹起1,
  2.DispatchPointer命令,
    点击事件
    DispatchPointer(long downtime,long eventide,int action,float x,float y, float pressure, float size, int metastate,float xprecision, float yprecision,int device,int edgeflags)
    //action 0代表按下, 1代表弹起, x和y代表坐标点
    // 命令需要成对使用,一个按下0, 一个弹起1,
  3.DispatchString命令,
    输入字符串事件
    DispatchString(String text)
  4.LaunchActivity命令
    启动应用
    LaunchActivity(package,Activity)
  5.UserWait命令
    等待事件
    UserWait(1000) //等待1秒
  6. DispatchPress命令
    按下键值
    DispatchPress(int keycode) //keycode 66 回车键

MonkeyScript实践

正常使用浏览器搜索关键字步骤

step1:adb install apk
step2:启动APP
step3: 点击输入框,输入查寻词,点击键盘回车
step4:点击搜索按钮
step5:等待结果的出现
step6: 点击clear按钮

MonkeyScript脚本实现(利用uiautomatorviewer.bat定位坐标)
新建脚本名:mook.script
type=user
count=10
speed=1.0
start data >>
LaunchActivity(包名, Activity名) #启动APP
UserWait(2000) #等待2秒
DispatchPointer(10, 10, 0, 100, 100, 1, 1, -1, 1, 1, 0, 0) #action 0代表按下, 1代表弹起, x和y 代表的坐标点
DispatchPointer(10, 10, 1, 100, 100, 1, 1, -1, 1, 1, 0, 0)
DispatchString(test) #输入查寻词
UserWait(1000)
DispatchPress(66)#点击键盘回车
UserWait(1000)
DispatchPointer(10, 10, 0, 400, 100, 1, 1, -1, 1, 1, 0, 0) #点击搜索按钮
DispatchPointer(10, 10, 1, 400, 100, 1, 1, -1, 1, 1, 0, 0)
UserWait(6000)

执行脚本
步骤一:adb push 脚本名 目录名 #将电脑上编写脚本push到测试设备中
如:adb push mook.script /data/local/tmp/
步骤二:adb shell monkey -f /data/local/tmp/mook.script 执行次数

 6. MonkeyRunner实例
    //monkeyruner脚本为python语言编写
    from com.android.monkeyrunner import MonkeyRunner
   1. MonkeyRunsner API - alert
    警告框
    void alert(string message,string title, string okTitle)
    实例: MonkeyRunner.alert('hello monkey friend','this is a title','OK')
  2. MonkeyRunner API - waitForConnection
    等待设备连接,有多个device id, 需要指明具体哪个设备.
    waitForConnection(float timeouts,string deviceid)
  3. MonkeyDevice API - drag
    拖动
    drag(tuple start,tuple end, float duration, integer steps)
    start 起点位置,
    end 终点位置,
    duration 手势持续的时间,
    step 插值点的步数,默认10.
  4. MonkeyDevice API - press
    按键
    press(string keycode,dictionary type)
    keycode名, DOWN , UP , DOWN_AND_UP
   5. MonkeyDevice API - startActivity
     启动应用
     startActivity(package+'/'+activity)
   6. MonkeyDevice API - touch
    点击
    touch(integer x, integer y, integer type)
    x和y 为坐标轴
    type: DOWN , UP , DOWN_AND_UP
   7. MonkeyDevice API - type
    输入
    type(string message)
   8. MonkeyDevice API - takeSnapshot
    截屏
    MonkeyImage takeSnapshot()
   9. MonkeyImage API - sameAs
    图像对比
    boolean sameAs(MonkeyImage other, float percent)
   10. MonkeyImage API - writetoFile
    保存图像文件
    void writeToFile(string path, string format)
    path: 文件存储的路径
    format: 图像文件的格式png ,JPG等
   11. MonkeyRunner.sleep(1) //添加等待时间

实现在搜索框中输入查询词,并截图
新建mook.py脚本
//monkeyrunner的python脚本实例
#-*-UTF-8 -*-
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
# 连接设备
device = MonkeyRunner.waitForConnection(3, "192.168.110.120:2525")
# 启动App
device.startActivity("com.example.testbrowser2/com.example.testbrowser2.myapplication.MainActivity")
MonkeyRunner.sleep(2)
# 点击搜索输入框
device.touch(100,100,"DOWN_AND_UP")
MonkeyRunner.sleep(1)
# 输入查询词
device.type('test input abcd')
MonkeyRunner.sleep(1)
# 点击回车键
device.press("KEYCODE_ENTER","DOWN_AND_UP")
MonkeyRunner.sleep(1)
# 点击搜索按钮
device.touch(400,100,"DOWN_AND_UP")
MonkeyRunner.sleep(5)
# 截图
image = device.takeSnapshot()
image.writeToFile("F://image/test.png","png")
# 点击清除按钮
device.touch(300,100,"DOWN_AND_UP")
MonkeyRunner.sleep(3)
//使用 AndroidSDK\tools目录的uiautomatorviewer.bat ,启动该软件来获取手机的按钮的坐标位置.

执行脚本
monkeyrunner mook.py

posted @ 2019-03-06 15:05  pathbreaker  阅读(202)  评论(0)    收藏  举报