app 自动化测试 流量

该博客介绍了如何通过ADB命令获取Android应用的进程ID及其流量消耗。利用Python的os模块执行ADB命令,抓取com.android.browser进程的流量数据,并进行统计计算。测试过程包括多次迭代,每次迭代间隔5秒,最终将数据保存到CSV文件中,便于分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取进程ID指令

    adb  shell ps | grep packagename

获取进程ID流量

    adb shell cat /proc/pid/net/dev

import csv

import os

import string

import time

class Controller():

       def __init__(self,count):

         #定义测试的次数

              self.count = count

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

         #单次测试过程

      def testprocess(self):

              #执行获取进程的命令

             result = os.popen("adb shell ps | grep com.android.browser")

      #进程ID

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

#获取进程ID使用的流量

            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("#")[9]

                  elif  "etho1"   in line:

                #将所有空行换成#

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

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

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

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

#计算所有的流量之和

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

#用KB计算流量

              alltraffic = alltraffic / 1024

 #获取当前时间

             cuttenttime = self.getcurrenttime() 

#将获取到的数据存入CSV

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

      def run (self):

             while self.count > 0:

                          self.testprocess()

                          self.counter = self.counter - 1

                          time.sleep(5)

#获取当前时间

       def getcurrenttime(self):

             time = time.strftime("%Y-%-%m  %H:%M:%S")

             return time

#存储数据

       def SaveDataCsv(self) :

            csvfile = file("flow.csv","wb")

            writer = csvfile.writer(csvfile)

            writer.writerows(self.alldata) 

            csvfile.close()

         

                

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

毛毛虫会长大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值