python脚本实现安卓app的cpu使用率测试。

一、用到的adb命令

adb shell "dumpsys cpuinfo | grep packagename"

二、脚本实现策略

(1)打开app进行测试(此处最好再写一个自动化测试的脚本)。
(2)执行脚本定时获取CPU使用状态。

三、测试数据分析

曲线图分析:cpu使用率如果保持恒定并合理则正常;如果随着使用cpu使用率持续上升,接近100%可能存在问题。

四、代码实现示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
#coding=utf-8

import os
import time
import csv

#控制类
class Controller(object):
def __init__(self,count):
self.counter = count
self.allData = [('timestamp','cpustatus')]

#单次查看CPU状态״̬
def CpuStatus(self):
cmd = 'adb shell "dumpsys cpuinfo | grep com.android.calculator2"'
cpustatus = os.popen(cmd).readlines()
for line in cpustatus:
cpuvalue = line.split('%')[0].strip()
break

currenttime = self.GetCurrentTime()
self.allData.append((currenttime, cpuvalue))


#多次查看CPU状态
def run(self):
while self.counter>0:
self.CpuStatus()
self.counter = self.counter-1
time.sleep(3)


#获取当前时间戳
def GetCurrentTime(self):
#时间格式如果写成%Y-%m-%d %H:%M:%S,写入csv时会被自定义格式隐藏秒
currentTime = time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())
return currentTime

#数据存储
def SaveDataToCSV(self):
with open('cpuStatus.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(self.allData)

if __name__ == '__main__':
controller = Controller(10)
controller.run()
controller.SaveDataToCSV()

五、测试结果示例

cpu使用率

持续更新…

最后更新: 2018年05月11日 14:52

原始链接: http://pythonfood.github.io/2018/01/01/安卓专项测试-cpu/

× 多少都行~
打赏二维码