APP专项测试3 -- 流量

#获取进程ID

adb shell ps | grep packagename

wlan0代表wifi 下载上传所有数据,数值单位是字节,可以/1024换算成KB

Receiver 接收的数据   Transmit 发送的数据

主要关注eth0  和 eth 1   网卡

python代码如下   (得到的结果可以用最后一个减去第一个得出一个流量的差值,再用appium执行一系统固定的动作,迭代新版本时在观察这个差值变化,如果差值变大要提给开发(版本与版本对比),还可以用于竞品对比)

#获取进程ID流量

import os
import time
import csv
import string


class Controller(object):
    def __init__(self,count):
        #定义测试的次数
        self.counter = count
        #定义收集数据的数组
        self.alldata = [("timestamp","traffic")]


    #单次测试过程
    def testprocess(self):
        #执行获取进程ID的命令
        result = os.popen("adb shell ps | grep com.example.shineapp")
        #获取到PID
        pid = result.readlines()[0].split(" ")[5]
        #获取进程PID使用的流量
        traffic = os.popen("adb shell cat /proc/"+pid+"/net/dev")
        for line in traffic:
            if "etho" 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)
        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")
        return currentTime


    #数据的存储
    def SaveDataToCsv(self):
        csvfile = open("traffic.csv","wb")
        writer = csv.writer(csvfile)
        writer.writerows(self.alldata)
        csvfile.close()

if __name__ == '__main__':
    #如果想要测试指定时间内的结果,例如10分钟,可以转换成秒再除以5秒的等待时间得到要执行的次数120
    controller = Controller(10)
    controller.run()
    controller.SaveDataToCsv()
    
原文地址:https://www.cnblogs.com/lexus168/p/12691142.html