流量测试--sim流量

测试项:

1:应用首次启动的流量

2:操作单个业务主流程消耗的流量

3:应用后台运行一段时间的总流量

4:应用在前台运行一段时间的流量

 

做上下版本做对比,或做竞品分析

 

1.第三方工具:安测试和流量宝 腾讯的TG等开源应用测试被测应用运行一段时间的流量

2.抓包分析:tcpdump + wireshark

  1:tcpdump用adb push到手机里面,然后cmd命令执行操作业务场景,ctrl+c结束采集

  2:.pcap文件adb push出来,wireshark过滤查看总流量=上行总流量+下行总流量

  

  https://www.cnblogs.com/xiaoluosun/p/6024030.html

3.通过进程id查看手机系统文件

先用adb  shell  ps | grep 包名获得进程id,adb shell cat /proc/"+pid+"/net/dev 获得接收和发送数据流量值,相加之和为初始的总流量KB

操作被测试应用业务,再次输入命令获得接收和发送流量,相加结尾的总流量KB,两次相减获得消耗的流量

 

''' 
adb shell ps | grep com.android.contacts      adb shell ps| findstr com.android.contacts
u0_a1     6131(id)  1375  999600 75312 ffffffff b7d846c5 S com.android.contacts
 
读取id对应文件
adb shell cat /proc/"+pid+"/net/dev
receive:接收流量
transmit:发送流量
每5秒取一次数据
10分钟消耗流量---定义次数10*60/5=120次
'''
 
import csv
import os
import string
import time
import decimal


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

# 单次测试过程
def testprocess(self):
# 执行获取进程的命令
result = os.popen("adb shell ps | findstr com.heytap.browser")
# 获取进程ID
pid = result.readlines()[0].split(" ")[5]

# 获取进程ID使用的流量
traffic = os.popen("adb shell cat /proc/" + pid + "/net/dev")
for line in traffic:
if "eth0" in line:
# 将所有空行换成#
line = "#".join(line.split())
# 按#号拆分,获取收到和发出的流量
receive = line.split("#")[1]
transmit = line.split("#")[9]
print(receive)
print(transmit)
elif "eth1" in line:
# 将所有空行换成#
line = "#".join(line.split())
# 按#号拆分,获取收到和发出的流量
receive2= line.split("#")[1]
transmit2 = line.split("#")[9]

# #计算所有流量的之和
alltraffic = int(receive) + int(transmit)
print(alltraffic)
# 按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')
writer = csv.writer(csvfile)
writer.writerows(self.alldata)
csvfile.close()


if __name__ == "__main__":
controller = Controller(120)
controller.run()
controller.SaveDataToCSV()
posted @ 2021-11-30 20:39  观呼吸  阅读(476)  评论(0)    收藏  举报