当前位置:网站首页>App performance test: (III) traffic monitoring

App performance test: (III) traffic monitoring

2022-06-13 06:16:00 Ming ruoxiao River

app Performance testing :( 3、 ... and ) Traffic monitoring

The flow monitoring is analyzed below :

  • Acquisition process ID Instructions
    adb shell “ps | grep Package name ”
  • Acquisition process ID Traffic
  • adb shell cat /proc/pid/net/dev
    pid Change to the process obtained in the first step ID

 Insert picture description here
receive It refers to the data received by the current process ,transmit It refers to the data requested by the current process , Flow is the sum of the two
In the end wlan0 representative wifi Upload and download volume identification ! The unit of upload and download is bytes /1024 The conversion KB
Here you can see the number of bytes downloaded 、 Data packets and Number of bytes sent 、 Data packets
rmnet_data0: On behalf of the mobile network

wlan0 These values are initialized 0 Turn on the mobile phone flight mode and then turn it off 0 了

Monitoring code :

#/usr/bin/python
#encoding:utf-8
import csv
import os
import string
import time

# The control class 
class Controller(object):
    def __init__(self, count):
        # Define the number of tests 
        self.counter = count
        # Define an array to collect data 
        self.alldata = [("timestamp", "traffic")]

    # Single test process 
    def testprocess(self):
        # Execute the command to get the process 
        result = os.popen("adb shell \"ps | grep  com.zahd.agriculturaltraceability.debug\"")
        print()
        # Acquisition process ID
        pid = result.readlines()[0].split(" ")[3]

        # Acquisition process ID Used flow 
        traffic = os.popen("adb shell cat /proc/"+pid+"/net/dev")
        for line in traffic:
            if "wlan0" in line:
                # Replace all spaces with #
                line = "#".join(line.split())
                # Press # No. split , Get the traffic received and sent 
                receive = line.split("#")[1]
                transmit = line.split("#")[9]
            elif "rmnet_data0" in line:
                #  Replace all spaces with #
                line =  "#".join(line.split())
                #  Press # No. split , Get the traffic received and sent 
                receive2 = line.split("#")[1]
                transmit2 = line.split("#")[9]

        # Calculate the sum of all flows 
        alltraffic = int(receive) + int(transmit) + int(receive2) + int(transmit2)
        # Press KB Calculate the flow value 
        alltraffic = alltraffic/1024
        # Get the current time 
        currenttime = self.getCurrentTime()
        # Save the obtained data in the array 
        self.alldata.append((currenttime, alltraffic))

    # Test process control many times 
    def run(self):
        while self.counter >0:
            self.testprocess()
            self.counter = self.counter - 1
            # Every time 5 Collect data once per second 
            time.sleep(5)

    # Get the current timestamp 
    def getCurrentTime(self):
        currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        return currentTime

    # Data storage 
    def SaveDataToCSV(self):
        csvfile = open('traffic.csv', 'w')
        writer = csv.writer(csvfile)
        writer.writerows(self.alldata)
        csvfile.close()

if __name__ == "__main__":
    controller = Controller(5)
    controller.run()
    controller.SaveDataToCSV()
原网站

版权声明
本文为[Ming ruoxiao River]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270556299952.html