分享

安卓apk客户端性能测试

 小猪窝969 2019-12-11

话不多说,直接上脚本:

  1. 冷热启动:

#/usr/bin/python

#encoding:utf-8

import csv

import os

import time

class App(object):

    def __init__(self):

        self.content = ""

        self.startTime = 0

    #启动App

    def LaunchApp(self):

        print("启动程序.....")

        cmd = 'adb shell am start -W -n com.android.browser/.BrowserActivity'

        self.content=os.popen(cmd)

    #停止App

    def StopApp(self):

        #冷启动

        #cmd = 'adb shell am force-stop com.android.browser'

        #热启动

        cmd = 'adb shell input keyevent 3'

        print("关闭程序......")

        os.popen(cmd)

    #获取启动时间

    def GetLaunchedTime(self):

        for line in self.content.readlines():

            if "ThisTime" in line:

                self.startTime = line.split(":")[1].strip()

                break

        return self.startTime

#控制类    pasedtime:启动时间

class Controller(object):

    def __init__(self, count):

        self.app = App()

        self.counter = count

        self.alldata = [("currenttime", "pasedtime")]

    #单次测试过程

    def testprocess(self):

        self.app.LaunchApp()

        time.sleep(5)

        elpasedtime = self.app.GetLaunchedTime()

        self.app.StopApp()

        time.sleep(3)

        currenttime = self.getCurrentTime()

        self.alldata.append((currenttime, elpasedtime))

    #多次执行测试过程

    def run(self):

        while self.counter >0:

            self.testprocess()

            self.counter = self.counter - 1

    #获取当前的时间戳

    def getCurrentTime(self):

        currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

        return currentTime

    #数据的存储

    def SaveDataToCSV(self):

        csvfile = open('starttime.csv', 'wb')

        writer = csv.writer(csvfile)

        writer.writerows(self.alldata)

        csvfile.close()

if __name__ == "__main__":

    controller = Controller(2)

    controller.run()

    controller.SaveDataToCSV()

2.内存:

#/usr/bin/python

#encoding:utf-8

import csv

import os

import  time

"""

请先用命令行运行 adb shell top -d num--->(数字是收取数据间隔时间) 收取数据,并且另存为meminfo

"""

#控制类

class Controller(object):

    def __init__(self):

        #定义收集数据的数组 vss:虚拟耗用内存(包含共享库占用的内存)  rss:实际使用物理内存(包含共享库占用的内存)

        self.alldata = [("id", "vss", "rss")]

    #分析数据

    def analyzedata(self):

        content = self.readfile()

        i = 0

        for line in content:

            if "com.android.browser" in line:

                print (line)

                line = "#".join(line.split())

                vss = line.split("#")[5].strip("K")

                rss = line.split("#")[6].strip("K")

                #将获取到的数据存到数组中

                self.alldata.append((i, vss, rss))

                i = i + 1

    #数据的存储

    def SaveDataToCSV(self):

        csvfile = open('meminfo.csv', 'wb')

        writer = csv.writer(csvfile)

        writer.writerows(self.alldata)

        csvfile.close()

    #读取数据文件

    def readfile(self):

        mfile = open("meminfo", "r")

        content = mfile.readlines()

        mfile.close()

        return  content

if __name__ == "__main__":

    controller = Controller()

    controller.analyzedata()

    controller.SaveDataToCSV()

3.流量

#/usr/bin/python

#encoding:utf-8

import csv

import os

import string

import time

#控制类

class Controller(object):

    def __init__(self, count):

        #定义测试的次数

        self.counter = count

        #定义收集数据的数组  traffic:总流量

        self.alldata = [("timestamp", "traffic")]

    #单次测试过程

    def testprocess(self):

        #执行获取进程的命令

        result = os.popen("y_adb shell ps | findstr com.android.browser")

        #获取进程ID

        pid = result.readlines()[0].split(" ")[4]

        #获取进程ID使用的流量

        traffic = os.popen("y_adb shell cat /proc/"+pid+"/net/dev")

        for line in traffic:

            if "lo" in line:

                #将所有空行换成#

                line = "#".join(line.split())

                #按#号拆分,获取收到和发出的流量

                receive = line.split("#")[1]

                transmit = line.split("#")[9]

            elif "eth1" in line:

                # 将所有空行换成#

                line =  "#".join(line.split())

                # 按#号拆分,获取收到和发出的流量

                receive2 = line.split("#")[1]

                transmit2 = line.split("#")[9]

        #计算所有流量的之和

        alltraffic = string .atoi(receive) + string .atoi(transmit) + string .atoi(receive2) + string .atoi(transmit2)

        #按KB计算流量值

        alltraffic = alltraffic/1024

        #获取当前时间

        currenttime = self.getCurrentTime()

        #将获取到的数据存到数组中

        self.alldata.append((currenttime, alltraffic))

    #多次测试过程控制

    def run(self):

        while self.counter >0:

            self.testprocess()

            self.counter = self.counter - 1

            #每5秒钟采集一次数据

            time.sleep(5)

    #获取当前的时间戳

    def getCurrentTime(self):

        currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

        return currentTime

    #数据的存储

    def SaveDataToCSV(self):

        csvfile = open('traffic.csv', 'wb')

        writer = csv.writer(csvfile)

        writer.writerows(self.alldata)

        csvfile.close()

if __name__ == "__main__":

    controller = Controller(2)

    controller.run()

    controller.SaveDataToCSV()

4.电量

#/usr/bin/python

#encoding:utf-8

import csv

import os

import time

#控制类

class Controller(object):

    def __init__(self, count):

        #定义测试的次数

        self.counter = count

        #定义收集数据的数组   power:电量,timestamp:时间

        self.alldata = [("timestamp", "power")]

    #单次测试过程

    def testprocess(self):

        #执行获取电量的命令

        result = os.popen("adb shell dumpsys battery")

        #获取电量的level

        for line in result:

            if "level" in line:

                power = line.split(":")[1].strip()

        #获取当前时间

        currenttime = self.getCurrentTime()

        #将获取到的数据存到数组中

        self.alldata.append((currenttime, power))

    #多次测试过程控制

    def run(self):

        #设置手机进入非充电状态

        os.popen("adb shell dumpsys battery set status 1")

        while self.counter >0:

            self.testprocess()

            self.counter = self.counter - 1

            #每5秒钟采集一次数据

            time.sleep(5)

    #获取当前的时间戳

    def getCurrentTime(self):

        currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

        return currentTime

    #数据的存储

    def SaveDataToCSV(self):

        csvfile = open('dianliang.csv', 'wb')

        writer = csv.writer(csvfile)

        writer.writerows(self.alldata)

        csvfile.close()

if __name__ == "__main__":

    controller = Controller(5)

    controller.run()

    controller.SaveDataToCSV()

5.cpu

#/usr/bin/python

#encoding:utf-8

import csv

import os

import time

#控制类  cpustatus:cpu利用率

class Controller(object):

    def __init__(self, count):

        self.counter = count

        self.alldata = [("timestamp", "cpustatus")]

    #单次测试过程

    def testprocess(self):

        result = os.popen("adb shell dumpsys cpuinfo | findstr com.android.browser")

        for line in result.readlines():

            cpuvalue = line.split("%")[0]

            currenttime = self.getCurrentTime()

            self.alldata.append((currenttime,cpuvalue))

            print(self.alldata)

    #多次执行测试过程

    def run(self):

        while self.counter >0:

            self.testprocess()

            self.counter = self.counter - 1

            time.sleep(3)

    #获取当前的时间戳

    def getCurrentTime(self):

        currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

        return currentTime

    #数据的存储

    def SaveDataToCSV(self):

        csvfile = open('cpcinfo.csv', 'wb')

        writer = csv.writer(csvfile)

        writer.writerows(self.alldata)

        csvfile.close()

if __name__ == "__main__":

    controller = Controller(2)

    controller.run()

    controller.SaveDataToCSV()

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多