- 实现功能
使用python自动化对android应用进行性能测试,将CPU、帧率、内存、数据流量、电量、电池温度数据绘制为图表
测试获取数据,每秒1次
记录测试数据
绘制为图表
- 获取数据代码(Perf_DataTest.py)
import os,re,csv
import time
import datetime
import subprocess
import numpy as np
from subprocess import Popen, PIPE
if os.path.exists(os.getcwd() + "/test_data"):
pass
else:
os.makedirs(os.getcwd() + "/test_data")
csv_path= os.getcwd() + "/test_data/"+time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())) + '.csv'
def get_mem(package,dev):
try:
cmd = r'adb -s %s shell dumpsys meminfo '%(dev) + package + ' | findstr "TOTAL"'
total = str((os.popen(cmd).readlines()))
return (re.findall(r"\d+\.?\d*", total)[0])
except Exception as e:
print(str(e),"get_mem(package),请检查包名是否正确……")
return -1
def dump_layer_stats(str_command):
L = []
p = Popen(str_command, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True)
for line in p.stdout:
if line != '\n':
ldata1 =(line[:-1].split('\t'))
ldata=[]
for i in ldata1:
ldata.append(int(i))
if len(ldata)== 1:
pass
else:
if (ldata[1]) >= 9223372036854775807:
continue
elif (ldata[1]) == 0:
continue
L.append((ldata[1]))
return L
def get_fps(str_command):
try:
while True:
L =dump_layer_stats(str_command)
size = len(L)
interval = 0
if size > 0:
interval = L[size - 1] - L[0]
else:
return -1
if interval == 0:
continue
fps = 1000000000 * (size - 1) / interval
return round(fps)
except:
return -1
def get_battery(dev):
try:
cmd = 'adb -s %s shell dumpsys battery'%dev
redcmd = str((os.popen(cmd).readlines())).replace("'", "").replace("\\n", " ").replace("]", " ").replace("[",
" ")
battery_dic = {
}
redcmd = (redcmd).split("n', '")[0].split(',')
for i in redcmd[1:]:
if ":" in i:
b_dic = {
i.split(":")[0].replace(" ", ""): i.split(":")[1]}
battery_dic.update(b_dic)
return battery_dic
except Exception as e:
print(e,"get_battery(),请检查包名是否正确……")
bat_dic={
'ACpowered': ' false ',
'USBpowered': ' false ',
'Wirelesspowered': ' false ',
'Maxchargingcurrent': ' 0 ',
'Maxchargingvoltage': ' 0 ',
'Chargecounter': ' 2172420 ',
'status': ' 3 ',
'health': ' 2 ',
'present': ' true ',
'level': ' -1 ',
'scale': ' 100 ',
'voltage': ' 3843 ',
'temperature': ' -1 ',
'technology': ' Li-poly '}
return bat_dic
def getUid(package_name,dev):
try:
p1 = subprocess.Popen('adb -s %s shell dumpsys package '%dev + package_name + ' | grep "userId"',
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
uidLongString = p1.stdout<