python APP性能测试

import os
import re
import time
import xlwt
work_book=xlwt.Workbook(encoding='utf-8')
sheet=work_book.add_sheet('sheet表名')



class UREALAPPTest(object):
    def __init__(self, num,app,activity):
        self.data = []
        self.num = num
        self.app = app
        self.activity = activity
    def check_devices(self):
        '''检查设备是否连接成功,如果成功返回True,否则返回False'''
        try:
            deviceInfo = os.popen('adb devices').read()
            if 'device' in deviceInfo.split('\n')[1]:
                print('=' * 21, '已连接设备,开始测试', '=' * 21)
                print(self.deviceInfo())
                return True
            else:
                return False
        except Exception as e:
            print(e)

    def deviceInfo(self):
        '''获取设备基础信息(如:厂商、型号、系统版本)'''
        deviceName = os.popen('adb shell getprop ro.product.model').read()
        platformVersion = os.popen('adb shell getprop ro.build.version.release').read()


        producer = os.popen('adb shell getprop ro.product.brand').read()
        return "手机型号:%s %s,系统版本:Android %s" % (
            producer.replace('\n', ''), deviceName.replace('\n', ''), platformVersion.replace('\n', ''))

    def start_adb(self):
        '''运行adb命令,并记录启动耗时'''
        start = 'adb shell am start -W %s/%s' % (self.app, self.activity)
        data = re.findall(r'.*ThisTime: (.*?)TotalTime:(.*?)WaitTime: (.*?)Complete',
                          os.popen(start).read().replace('\n', ''))
        if len(data) == 0:
            print("adb命令执行出错,数据为空")
        else:
            self.data.append(int(data[0][0]))
            return data

    def stop_adb(self):
        '''结束程序运行'''
        stop = 'adb shell am force-stop %s' % self.app
        os.popen(stop)


    def run_test_cold(self):
        '''app 冷启动耗时测试'''
        self.data.clear()
        if self.check_devices() == True:
            self.stop_adb()
            for i in range(self.num):
                title = '=' * 20, '冷启动测试:第%d次运行' % (i + 1), '=' * 20
                print(title)
                sheet.write(0,2*i,title)
                self.stop_adb()
                time.sleep(3)
                test = self.start_adb()
                print("ThisTime:%s,TotalTime:%s,WaitTime:%s" % (test[0][0], test[0][1], test[0][2]))
                sheet.write(0,2*i+1,'ThisTime')
                sheet.write(1,2*i+1,test[0][0])
                sheet.write(2,2*i+1,'TotalTime')
                sheet.write(3,2*i+1,test[0][1])
                sheet.write(4,2*i+1,'WaitTime')
                sheet.write(5,2*i+1,test[0][2])
                time.sleep(3)
            self.stop_adb()
            if len(self.data) == 0:
                print("adb命令执行出错,数据为空")
            else:
                re = '\n冷启动%s次平均耗时为:%s' % (len(self.data), sum(self.data) / len(self.data))
                print(re)
                sheet.write(0,2*self.num+2,re)
        else:
            print("未连接安卓设备,请连接设备(3秒后重试)")
            while True:
                time.sleep(3)
                self.run_test_cold()

    def run_test_hot(self):
        '''app 热启动耗时测试'''
        self.data.clear()
        if self.check_devices() == True:
            os.popen('adb shell am start -W %s/%s' % (self.app, self.activity))
            time.sleep(3)
            for i in range(self.num):
                title = '=' * 20, '热启动测试:第%d次运行' % (i + 1), '=' * 20
                print(title)
                sheet.write(6, 2 * i, title)
                os.popen('adb shell input keyevent 3')
                time.sleep(3)
                test = self.start_adb()
                time.sleep(3)
                print("ThisTime:%s,TotalTime:%s,WaitTime:%s" % (test[0][0], test[0][1], test[0][2]))
                sheet.write(7, 2 * i + 1, 'ThisTime')
                sheet.write(8, 2 * i + 1, test[0][0])
                sheet.write(9, 2 * i + 1, 'TotalTime')
                sheet.write(10, 2 * i + 1, test[0][1])
                sheet.write(11, 2 * i + 1, 'WaitTime')
                sheet.write(12, 2 * i + 1, test[0][2])

            self.stop_adb()
            if len(self.data) == 0:
                print("adb命令执行出错,数据为空")
            else:
                re = '\n热启动%s次平均耗时为:%s' % (len(self.data), sum(self.data) / len(self.data))
                print(re)
                sheet.write(6, 2 * self.num + 2, re)
        else:
            print("未连接安卓设备,请连接设备(3秒后重试)")
            while True:
                time.sleep(3)
                self.run_test_hot()


    def meminfo_adb(self):
        '''app 内存'''
        meminfo = 'adb shell dumpsys meminfo %s'%self.app
        data =re.findall(r'.*Native Heap(.*?)Dalvik Heap(.*?)Dalvik Other(.*?)TOTAL SWAP PSS:(.*?)',os.popen(meminfo).read().replace('\n', ''))
        NativeHeapAlloc=data[0][0].split()[-2]
        DalvikHeapAlloc=data[0][1].split()[-2]
        TotalPss=data[0][-2].split()[-1]
        if len(data) == 0:
            print("adb命令执行出错,数据为空")
        else:
            return NativeHeapAlloc,DalvikHeapAlloc,TotalPss

    def run_test_meminfo(self):
        '''app 内存测试'''
        if self.check_devices() == True:
            os.popen('adb shell am start -W %s/%s' % (self.app, self.activity))
            time.sleep(3)
            for i in range(self.num):
                title = '=' * 21, '内存测试:第%d次运行' % (i + 1), '=' * 21
                print(title)
                sheet.write(14,2*i,title)
                test = self.meminfo_adb()
                time.sleep(3)
                print("NativeHeapAlloc:%s,DalvikHeapAlloc:%s,TotalPss:%s" % (test[0],test[1],test[2]))
                sheet.write(14, 2 * i + 1, 'NativeHeapAlloc')
                sheet.write(15, 2 * i + 1, test[0][0])
                sheet.write(16, 2 * i + 1, 'DalvikHeapAlloc')
                sheet.write(17, 2 * i + 1, test[0][1])
                sheet.write(18, 2 * i + 1, 'TotalPss')
                sheet.write(19, 2 * i + 1, test[0][2])
            self.stop_adb()
            # print('\nCPU测试%s次平均CPU为:%s' % (len(self.data), sum(self.data) / len(self.data)) + '%')

        else:
            print("未连接安卓设备,请连接设备(3秒后重试)")
            while True:
                time.sleep(3)
                self.run_test_meminfo()

    def cpu_adb(self):
        '''app cpu'''
        cpu = 'adb shell dumpsys cpuinfo |findstr %s'%self.app
        data=re.findall(r'(.*?)%',os.popen(cpu).read())
        if len(data) == 0:
            print("adb命令执行出错,数据为空")
        else:
            CPUpercent = data[0]
            self.data.append(float(CPUpercent))
            return CPUpercent

    def run_test_cpu(self):
        '''app cpu 测试'''
        self.data.clear()
        if self.check_devices() == True:
            os.popen('adb shell am start -W %s/%s' % (self.app, self.activity))
            time.sleep(3)
            for i in range(self.num):
                title = '=' * 21, 'CPU测试:第%d次运行' % (i + 1), '=' * 22
                print(title)
                sheet.write(21,2*i,title)
                test = self.cpu_adb()
                time.sleep(3)
                print("CPUpercent:%s" % test+'%')
                sheet.write(21, 2 * i + 1, 'CPUpercent')
                sheet.write(22, 2 * i + 1, test+'%')

            self.stop_adb()
            if len(self.data) == 0:
                print("adb命令执行出错,数据为空")
            else:
                re = '\nCPU测试%s次平均CPU为:%s' % (len(self.data), sum(self.data) / len(self.data))+'%'
                print(re)
                sheet.write(21,2 * self.num + 2, re)
        else:
            print("未连接安卓设备,请连接设备(3秒后重试)")
            while True:
                time.sleep(3)
                self.run_test_cpu()

    def save(self):
        work_book.save('D:\Code_Test_AutoTest\AppPerformance\data\App性能测试结果.xls')



if __name__ == '__main__':
    userapp = 'com.hzureal.intelligent'
    useractivity = 'com.jiaoliutong.urzl.client.control.ClientActivity'

    apptest = UREALAPPTest(4, userapp, useractivity)
    # apptest.run_test_cold()
    # apptest.run_test_hot()
    # a= apptest.meminfo_adb()
    # b=apptest.cpu_adb()
    # print(a,b)
    apptest.run_test_meminfo()
    # apptest.run_test_cpu()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值