这篇文章主要讲述怎么用python调用adb命令来测试性能,我以启动时间为列。
调用之前必须知道怎么用python调用adb命令
#启动apk
message = os.popen("adb shell am start -W -n com.qihoo.appstore/.home.MainActivity")
for line in message.readlines():
if "ThisTime" in line:
print(line.split(":")[1])
仔细看这段代码是截取apk启动的时间,可以看出是用os.popen函数执行后,把信息返回给message,然后截取,获取启动时间下面我们来用python代码来实现
思路如下:
python 来实现方式
1定义App类方法有startApp stopApp getStartTime
2 Manager类 方法 testRunTime(执行单次) run 开启执行
App类代码如下:
import os,time
class App():
def __init__(self,pagename,firstActivity):
"""构造方法"""
self.pagename = pagename
self.firstActivty= firstActivity
self.content="" #执行命令的文本
self.startTime="" #启动的时间
def startApp(self):
"""开启app"""
cmd = "adb shell am start -W -n "+self.pagename+self.firstActivty
self.content = os.popen(cmd)
def stopApp(self):
"""停止app"""
cmd = "adb shell am force-stop "+self.pagename
os.popen(cmd)
def getStartTime(self):
"""获取启动时间"""
for line in self.content.readlines():
if "ThisTime" in line:
self.startTime = line.split(":")[1]
break
return self.startTime
Manager类代码如下:
from qhcs.starttime.App import App
import time
import csv
class Manager():
def __init__(self,count,App):
self.count= count
self.app =App
self.file = open("starttime.txt","w")
def run(self):
while self.count>0:
self.app.startApp()
time.sleep(2)
#获取时间
self.file.write(self.app.getStartTime())
self.app.stopApp()
time.sleep(2)
self.count=self.count-1
#关闭资源
self.file.close()
app= App("com.swimmi.windnote","/.Welcome")
m = Manager(6,app)
m.run()
代码相对比较简单,我解释下思路,如有不明白的可以吐槽,
当执行app= App("com.swimmi.windnote","/.Welcome")
代码时会执行执行Manager 中的run方法我们重点来看下run方法
m = Manager(6,app)
m.run()
含义是根据你传入的参数来启动多少次,每次启动的时间都保存在当前目录的starttime.txt文件中,当然也可以保存到xlsx文件中。有了这些数据,我们就可以根据数据来制作报表
177
998
1312
1134
1004
1132
报表如下
通过报表很自然的看出启动时间的是否正常。
当然还有其他cpu,内存,等,我就不一一列出,提供代码如下:
cpu
#/usr/bin/python
#encoding:utf-8
import os
import time
#控制类
class Manager(object):
def __init__(self, count,pagename):
self.counter = count
self.file = open('cpumessage.txt', 'w')
self.pagename=pagename
#单次测试过程
def testRunTime(self):
result = os.popen("adb shell dumpsys cpuinfo | findstr "+self.pagename).readlines()
cputotle = 0
for line in result:
if len(line) > 0:
cpu = line.split("%")[0].strip()
if len(cpu) > 0:
cputotle = cputotle + float(cpu)
self.file.write(str(cputotle)+"\n")
#多次执行测试过程
def run(self):
while self.counter >0:
self.testRunTime()
self.counter = self.counter - 1
time.sleep(2)
#关闭资源
self.file.close()
if __name__ == "__main__":
controller = Manager(10,"com.swimmi.windnote")
controller.run()
内存
#/usr/bin/python
#encoding:utf-8
import os
import time
#控制类
class Manager(object):
def __init__(self, count,pagename):
self.counter = count
self.memfile = open('mem.txt', 'w')
self.pagename = pagename
#单次测试过程
def testRunTime(self):
cmd = "adb shell dumpsys meminfo "+self.pagename
result = os.popen(cmd).readlines()
for line in result:
if "TOTAL" in line:
mem = line.split(" ")[3]
break
print("内存:",mem)
self.memfile.write(mem+"\n")
#多次执行测试过程
def run(self):
while self.counter >0:
self.testRunTime()
self.counter = self.counter - 1
time.sleep(2)
#关闭资源
self.memfile.close()
if __name__ == "__main__":
controller = Manager(10,"com.swimmi.windnote")
controller.run()
除了代码测试app的性能之前肯定还有比较好的工具来测试性能,有哪些在下篇文件中有详解。