Appium统计iOS或者Android应用的性能

目录

1、手动统计方法

2、自动化统计方法

3、问题记录

我们知道可以通过Appium来控制iOS或者Android设备,进而做一些自动化测试的任务,那么如果我们想收集应用(APP)的基础性能数据呢?

1、手动统计方法

Andriod设备可以通过adb命令("adb shell top")来简单的手机,iOS设备可以xCode的instruments工具来统计,如下所示,事实上也适用于Mac设备。

2、自动化统计方法

使用传统的方式 Appium + xcode的Instruments命令会出现各种各样的问题,比如Appium异常退出、Instruments命令内存泄露、采集不到数据等等。所以推荐试用是顺序:2.1 > 2.2 > 2.3

2.1 阿里同学开源的taobao-iphone-device库

参考:https://github.com/alibaba/taobao-iphone-device

主要功能说明如下:

代码示例:ios_debug.py

import time
import tidevice
from tidevice._perf import DataType
from logzero import logger

t = tidevice.Device()
# perf = tidevice.Performance(t, [DataType.CPU, DataType.MEMORY, DataType.NETWORK, DataType.FPS, DataType.PAGE,
#                             DataType.SCREENSHOT, DataType.GPU])
perf = tidevice.Performance(t, [DataType.CPU, DataType.MEMORY])

result_cpu = []
result_memory = []


def callback(_type: tidevice.DataType, value: dict):
    # logger.info("R: {}, {}".format(_type.value, value))

    # 转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
    time_array = time.localtime(value["timestamp"]/1000)
    other_style_time = time.strftime("%Y-%m-%d %H:%M:%S", time_array)

    if _type.value == "memory":
        result_memory.append({"memory": value["value"], "timestamp": other_style_time})
    elif _type.value == "cpu":
        result_cpu.append({"cpu": value["value"], "timestamp": other_style_time})


# com.xxx.xx替换为自己的应用
perf.start("com.xxx.xx", callback=callback)
time.sleep(10)

logger.info("result_cpu: {}".format(result_cpu))
logger.info("result_memory: {}".format(result_memory))

# 根据时间戳合并2个数组
x_values = {x['timestamp']: x['cpu'] for x in result_cpu}
res_list = [{**y, **{'cpu': x_values[y['timestamp']]}} for y in result_memory]

logger.info("res_list: {}".format(res_list))

perf.stop()

输出结果:

Stopped
[I 220320 22:29:05 ios_debug:31] result_cpu: [{'cpu': 3.585903582731264, 'timestamp': '2022-03-20 22:28:58'}, {'cpu': 0.6396585121383214, 'timestamp': '2022-03-20 22:28:59'}, {'cpu': 0.42271327814517523, 'timestamp': '2022-03-20 22:29:00'}, {'cpu': 1.8402275760576445, 'timestamp': '2022-03-20 22:29:01'}, {'cpu': 0.4501805132687883, 'timestamp': '2022-03-20 22:29:02'}, {'cpu': 0.6061838366665879, 'timestamp': '2022-03-20 22:29:03'}, {'cpu': 0.46928509407834135, 'timestamp': '2022-03-20 22:29:04'}]
[I 220320 22:29:05 ios_debug:32] result_memory: [{'memory': 148.19058227539062, 'timestamp': '2022-03-20 22:28:58'}, {'memory': 148.19058227539062, 'timestamp': '2022-03-20 22:28:59'}, {'memory': 148.19058227539062, 'timestamp': '2022-03-20 22:29:00'}, {'memory': 148.17495727539062, 'timestamp': '2022-03-20 22:29:01'}, {'memory': 148.17495727539062, 'timestamp': '2022-03-20 22:29:02'}, {'memory': 148.17495727539062, 'timestamp': '2022-03-20 22:29:03'}, {'memory': 148.17495727539062, 'timestamp': '2022-03-20 22:29:04'}]
[I 220320 22:29:05 ios_debug:38] res_list: [{'memory': 148.19058227539062, 'timestamp': '2022-03-20 22:28:58', 'cpu': 3.585903582731264}, {'memory': 148.19058227539062, 'timestamp': '2022-03-20 22:28:59', 'cpu': 0.6396585121383214}, {'memory': 148.19058227539062, 'timestamp': '2022-03-20 22:29:00', 'cpu': 0.42271327814517523}, {'memory': 148.17495727539062, 'timestamp': '2022-03-20 22:29:01', 'cpu': 1.8402275760576445}, {'memory': 148.17495727539062, 'timestamp': '2022-03-20 22:29:02', 'cpu': 0.4501805132687883}, {'memory': 148.17495727539062, 'timestamp': '2022-03-20 22:29:03', 'cpu': 0.6061838366665879}, {'memory': 148.17495727539062, 'timestamp': '2022-03-20 22:29:04', 'cpu': 0.46928509407834135}]

Process finished with exit code 0

自动化测试:使用XCTest

请先确保手机上已经安装有WebDriverAgent应用,我使用的是Xcode编译WDA的方式在手机上安装:

可以使用tidevice applist命令查看手机已经安装的应用列表,示例如下,可以看到对应WDA的名字是:com.facebook.WebDriverAgentRunner.xctrunner

启动WDA命令:

tidevice xctest -B com.facebook.WebDriverAgentRunner.xctrunner

 WDA正常启动的日志如下:

然后启动Appium,预期可以进行对应的自动化测试了,即不需要再使用xcode build的方式启动WDA了。

2.2 py-ios-device库

参考:(无需编译 WDA) 纯 Python 在 windows/mac 系统执行 iOS 自动化测试

功能说明:Win,Mac 跨平台方案,通过 Instruments 私有协议获取 iOS 相关性能指标数据。

中文文档:https://github.com/YueChen-C/py-ios-device/blob/main/README_CN.md

Demo:https://github.com/YueChen-C/py-ios-device/blob/main/test/test.py

2.3 原生的统计方法

那么这些统计方式怎么集成到我们的自动化工具中呢?

appium的官方给我们提供了对应封装,如:“Execute Mobile Command”方法,参考官网:Execute Mobile Command - Appium

我在使用的时候主要用的"mobile: startPerfRecord"“mobile: stopPerfRecord” ,具体参数等使用参考官网:https://github.com/appium/appium-xcuitest-driver#platform-specific-extensions

代码片段如下,timeout字段单位为毫秒(ms),代表的是最大的采集时间,默认为5分钟,如果你想要采集的时间大于5min,需要更改下。pid不填的话代表采集的是所有的进程,为current时代表的是当前活跃的进程。

# profileName不填的话默认为: {"profileName": "Activity Monitor"}

# 开启采集
driver.execute_script("mobile: startPerfRecord", {"profileName": "Activity Monitor",
                                                  "timeout": 600000, "pid": "current"})

# 采集时间: 80s
time.sleep(80)

# 结束采集, 并保存zip文件,如: trace.zip
b64_zip = str(driver.execute_script("mobile: stopPerfRecord"))
bytes_zip = base64.b64decode(b64_zip)

trace_zip_filename = "trace.zip"
with open(trace_zip_filename, 'wb') as fz:
    fz.write(bytes_zip)

3、问题记录

1、在使用"mobile: startPerfRecord"“mobile: stopPerfRecord统计资源的时候发现偶发失败,我的服务报错举例:

[E 220304 17:54:14 get_resource:410] get_hi_resource_data error, err: Message: An unknown server-side error occurred while processing the command. Original error: There is no .trace file found for performance profile 'Activity Monitor' and device db797ee3c5925cc5efc1c91c2c7237b104e26e54. Make sure the selected profile is supported on this device

Appium报错日志:[xctrace] Timed out waiting for device to boot: “R.S”的 iPhone (12.3.1)

[xctrace] Timed out waiting for device to boot: “Rong,Song”的 iPhone (12.3.1)
[Activit...@db797ee3] 
[Activit...@db797ee3] Performance recording exited with error code 13, signal null

报错截图: 

解决办法:

(1)添加重试逻辑

唉,暂时想到的是只能重试,iOS结合Appium使用,速度慢和稳定性不好貌似很多人说过,谷歌查询关键词:“appium ios slow”结果如下:

(2)换根数据线直连或换个手机

比如:直接连接Mac和iOS,不要通过转接头什么。

2、Appium偶发退出报错"Thread 0 Crashed:: CrBrowserMain  Dispatch queue: com.apple.main-thread"

Logical CPU:     0
Error Code:      0x00000006 (no mapping for user data write)
Trap Number:     14

网上查了半天,具体也不知道什么原因,于是准备加重试逻辑。

思路:使用命令行模式启动Appium,如果挂了后循环再启动。启动命令如下:

node /Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js

效果如下: 

 循环启动的脚本如下,可以使用kill -9杀死appium进程的方法观察是否可以拉起,当然下面的方式比较粗暴,你也可以使用守护进程等方式。

python3 appium_server_run.py

import subprocess
import time
from logzero import logger

while True:
    logger.info("appium will start by node...")
    try:
        subprocess.run("node /Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js", shell=True)
    except Exception as e:
        logger.error("appium is abnormal exit,will start agin in 5s later...")
        time.sleep(5)

3、Xcode WebDriverAgent Memory内存占用过高报错

2022.05.09:额,下面的这种方法好像没什么用,看看第4点吧

Thread 1: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit=1400 MB, unused=0x0)

Google搜索词:webDriverAgent Memory limit

结果参考:iOS WebDriverAgentRunner-Runner process growth high in memory usage and crashes during long test sessions (12+ hours) · Issue #15457 · appium/appium · GitHub

4、xcode启动的WebDriveAgentRunner随着Appium自动化的运行,内存不断的上升,如下截图所示

简而言之:就是尽量不要用driver.swipe()等操作,具体的可以看下面的描述。

Ps:经过测试发现是Appium自动化测试中的driver.swipe()操作引起的内存上升(当然可能只是其中一种情况),如果只是单纯的click()等操作内存基本在30M以内波动。

  • 有使用driver.swipe()时

  • 没有使用driver.swipe()时

在网上搜了下,也有类似的情况:https://github.com/appium/appium/issues/15457

只有先少用和尽量不要用driver.swipe()等操作了。

5.taobao-iphone-device库使用期间报错"UsbmuxReplyCode.ConnectionRefuse"

谷歌搜了下,作者回复了这个问题并修复了,Issunes地址:tidevice.exceptions.MuxReplyError: UsbmuxReplyCode.ConnectionRefused · Issue #148 · alibaba/taobao-iphone-device · GitHub

在0.6.11版本中修复了,升级下tidevice版本(我看了下我的版本是早期安装的0.6.8)

6、taobao-iphone-device库长时间运行测试获取内存数据报错[Errno 32] Broken pipe 

谷歌搜了下,作者回复了这个问题并修复了,可能和第5点的问题一样,Issunes地址:长时间运行测试获取内存数据报错[Errno 32] Broken pipe · Issue #159 · alibaba/taobao-iphone-device · GitHub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rs勿忘初心

您的鼓励将是我的最大创动原动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值