写在前面
首先感谢github上分享的python工程:https://github.com/ztwo/Auto_Analysis
工程中关于报告生成那块想法和思路很不错,所以我基于他的写法和方式稍微修改了一下报告中的输出。
一、自动化收集数据
根据之前博客中写的内容,通过adb命令实时获取应用所占用的CPU、内存等信息。
内存信息只计算应用在dalvik中占用的(java代码执行的内存消耗)。
一个应用在dalvik中可申请的最大内存为:
有些设备可能需要这样获取:

应用占用内存获取:

其中dalvik heap对应的heap size就是应用占用内存。
内存占用率就是使用应用占用内存除以内存可申请的最大内存。
CPU占用率使用top命令:

实时统计数据并保存到文件中
内存、CPU统计数据实时写入到本地文件内,当测试完成后,再读取文件并分别解析,并保存到excel文件中。

操作应用并记录测试步骤及截图
具体操作执行封装到方法中:

根据事件不同进行不同操作并截图:

实时判断当前activity是否是被测目标app,如果不是则结束测试并保存结果:

二、Log过滤及crash关键字分析
通过关键字进行筛选过滤anr及crash等,并将问题文件写入到对应的文件中:
写入到对应文件中并且以时间戳格式命名,方便定位及分析问题:

三、测试结束生成报告
创建几个文件夹用于保存本次测试的结果:

Img:用于保存每次操作的截图。
Log:用于存储测试过程的logcat。
Per:保存性能测试数据。

生成html文件使用的是python的字符串format格式化,将本地保存的数据内容解析及拼接,完成html的生成。
Python字符串的format格式化也比较简单。
例如我上边的{app_info}是要进行格式化显示的内容,在format中添加要传入的字符串参数就可以实现。
生成的html中有对excel文件的读取并写入到html中的操作。每一个<td></td>标签的内容进行格式化操作,然后再将多个td标签组合到tr标签中,tr标签使用%{XXX}s的方式进行格式化。如下图的使用方式:

更多format格式化语法没有用到,具体情况具体分析使用。
最终展示一下报告样子:

四、最后可以上报性能数据到服务端,测试结果可以让部门内其他人看到。