移动性能测试 Android-APP-CPU 耗电测试的例子

黑水 · 2015年05月31日 · 最后由 黑水 回复于 2016年10月10日 · 2049 次阅读

说明:

对比测试 APP(v3.2.2) 弹幕硬解和软解的耗电量。
主要使用 CPU ,所以只考虑 CPU 的耗电。
因为耗时较长,用 UIautomator 自动修改设置和播放。
需要 Root。

数据来源:

/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state
记录了 CPU 从开机到读取文件时,在各个频率下的运行时间,单位:10 mS
说明

PowerProfile
获取 CPU 在各个频率下运行时的平均电流,单位 mA。
说明
第三方 ROM 一般是错的。

/proc/[pid]/stat
进程已运行时间 utime + stime + cutime + cstime,pid 是进程号 (adb shell ps)。
说明

ActivityManager
通过 PackageName 获得 APP 下的进程号。
说明

测试场景:

准备工作:离线下载好待测视频,开飞行模式,清理后台。
1、记录 APP 已经运行的时间 targetAppTimeBefor ,所有 APP 已经运行时间 totalTimeBefor ,CPU 在各个频率的运行时间 timeInStateBefor[1] 、timeInStateBefor[2] 、……
2、播放有弹幕的视频。
3、结束播放,记录 APP 已经运行的时间 targetAppTimeAfter ,所有 APP 已经运行时间 totalTimeAfter,CPU 在各个频率的运行时间 timeInStateAfter[1] 、 timeInStateAfter[2] 、……
4、读取 CPU 在各个频率下运行时的平均电流 powerUsedOnDiffSpeeds[1] 、powerUsedOnDiffSpeeds[2] 、……

计算公式:

目标 APP 占总 CPU 时间的比例:
ratio = (targetAppTimeAfter - targetAppTimeBefor) / (totalTimeAfter - totalTimeBefor)

CPU 总耗电:
totalPower = ((timeInStateAfter[1] - timeInStateBefor[1]) / 100 * powerUsedOnDiffSpeeds[1]) + ((timeInStateAfter[2] - timeInStateBefor[2]) / 100 * powerUsedOnDiffSpeeds[2]) + ……

目标 APP 的 CPU 耗电(单位:mAh):
appCpuTotalPower = cpuTotalPower * ratio / 3600

源码

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 6 条回复 时间 点赞

最后图卦了。。。。

楼主,可以告诉联系扣号不?

#2 楼 @yangmingming 在群里呀,有啥问题

收藏了! 最近考虑把 CPU 和电量的性能测试实践起来!

隐藏 API 怎么关联进项目?

#5 楼 @jira
忘光了,当时看的这篇帖子获取程序耗电排行

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册