命令
adb shell ps

- 拿到pid
adb shell ps | findstr com.android.browser

- 这边拿到PID:20094然后在去/proc目录下的PID/net/dev面可以看到:
adb shell cat /proc/20094/net/dev

lo :localhost 本地流量
Receive:接收流量
Transmit:发送流量
如果是手机的话
wlan0代表wifi 上传下载量标识! 上传下载量单位是字节可以/1024换算成KB
这里可以看到下载的字节数 、数据包 和 发送的字节数 、数据包
小技巧:wlan0这些值如何初始化0 很简单 你打开手机飞行模式再关掉就清0了
python 代码
#/usr/bin/python#encoding:utf-8import csvimport subprocessimport stringimport time#控制类class Controller(object):def __init__(self, count):#定义测试的次数self.counter = count#定义收集数据的数组self.alldata = [("timestamp", "traffic")]#单次测试过程def testprocess(self):#执行获取进程的命令result = subprocess.Popen("adb shell ps | findstr com.android.browser",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)#获取进程IDpids = result.stdout.readlines()pids = pids[0]pids = pids.decode('utf-8')pids =pids.split()pid= pids[1]#获取进程ID使用的流量traffic = subprocess.Popen("adb shell cat /proc/"+pid+"/net/dev",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)receive=0transmit=0receive2=0transmit2=0for line in traffic.stdout.readlines():line = line.decode('utf-8')if "eth0" 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 = int(receive) + int(transmit) + int(receive2) + int(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', 'w',encoding='utf-8',newline='')writer = csv.writer(csvfile)writer.writerows(self.alldata)csvfile.close()if __name__ == "__main__":controller = Controller(5)controller.run()controller.SaveDataToCSV()
