日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

安卓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))

    #多次執(zhí)行測試過程

    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

    #數(shù)據(jù)的存儲

    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.內(nèi)存:

#/usr/bin/python

#encoding:utf-8

import csv

import os

import  time

"""

請先用命令行運行 adb shell top -d num--->(數(shù)字是收取數(shù)據(jù)間隔時間) 收取數(shù)據(jù),并且另存為meminfo

"""

#控制類

class Controller(object):

    def __init__(self):

        #定義收集數(shù)據(jù)的數(shù)組 vss:虛擬耗用內(nèi)存(包含共享庫占用的內(nèi)存)  rss:實際使用物理內(nèi)存(包含共享庫占用的內(nèi)存)

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

    #分析數(shù)據(jù)

    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")

                #將獲取到的數(shù)據(jù)存到數(shù)組中

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

                i = i + 1

    #數(shù)據(jù)的存儲

    def SaveDataToCSV(self):

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

        writer = csv.writer(csvfile)

        writer.writerows(self.alldata)

        csvfile.close()

    #讀取數(shù)據(jù)文件

    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):

        #定義測試的次數(shù)

        self.counter = count

        #定義收集數(shù)據(jù)的數(shù)組  traffic:總流量

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

    #單次測試過程

    def testprocess(self):

        #執(zhí)行獲取進程的命令

        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())

                #按#號拆分,獲取收到和發(fā)出的流量

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

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

            elif "eth1" in line:

                # 將所有空行換成#

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

                # 按#號拆分,獲取收到和發(fā)出的流量

                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()

        #將獲取到的數(shù)據(jù)存到數(shù)組中

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

    #多次測試過程控制

    def run(self):

        while self.counter >0:

            self.testprocess()

            self.counter = self.counter - 1

            #每5秒鐘采集一次數(shù)據(jù)

            time.sleep(5)

    #獲取當前的時間戳

    def getCurrentTime(self):

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

        return currentTime

    #數(shù)據(jù)的存儲

    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):

        #定義測試的次數(shù)

        self.counter = count

        #定義收集數(shù)據(jù)的數(shù)組   power:電量,timestamp:時間

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

    #單次測試過程

    def testprocess(self):

        #執(zhí)行獲取電量的命令

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

        #獲取電量的level

        for line in result:

            if "level" in line:

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

        #獲取當前時間

        currenttime = self.getCurrentTime()

        #將獲取到的數(shù)據(jù)存到數(shù)組中

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

    #多次測試過程控制

    def run(self):

        #設置手機進入非充電狀態(tài)

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

        while self.counter >0:

            self.testprocess()

            self.counter = self.counter - 1

            #每5秒鐘采集一次數(shù)據(jù)

            time.sleep(5)

    #獲取當前的時間戳

    def getCurrentTime(self):

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

        return currentTime

    #數(shù)據(jù)的存儲

    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)

    #多次執(zhí)行測試過程

    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

    #數(shù)據(jù)的存儲

    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()

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多