zoukankan      html  css  js  c++  java
  • 性能测试——流量测试

    原文资料:

    http://testerhome.com/topics/2643

    http://testerhome.com/topics/2068

    -------------------

    流量篇

    最近在研究IOS 的性能测试,时间太紧没来得及发帖,加通宵挤出时间给大家分享一点东西,希望对大家有所帮助!

    Android 2.2之前
    对于Android2.2 的流量 版本以前的系统的流量信息都存放在 proc/net/dev(或者 proc/self/net/dev)文件下,读取文件然后对其进行解析就行了。读取某一个应用的流量,则读取proc/uid_stat/uid /tcp_rcv 文件进行解析(注:模拟器下不存在这个目录)。如需查看某个应用的流量信息,可以通过以下命令来实现:

        adb devices                         列出所有设备
        adb -s 设备名称 shell                进入对应的设备
        cd proc                             进入设备的属性目录
        cd uid_stat                         进入 user id 状态目录,每个应用程序在安装的时候系统会为每个应用分配一个对应的 uid
        ls                                  列出 uid_stat 目录下所有应用对应的 user id 目录
        cd uid                              进入对应应用的 uid 目录
        ls                                  查看对应 uid 目录下的 tcp_rcv tcp_snd 目录
        cat tcp_rcv                         查看该应用接收的数据信息
        cat tcp_snd                         查看该应用发送的数据信息
    

    Android 2.2之后

    我这里有两种办法:

    第一种
    通过PID下面的net/dev
    先找到应用的PID

    adb shell ps
    
     

    这边拿到PID:21896 然后在去/proc目录下的PID/net/dev面可以看到:

    adb shell cat /proc/"+Pid+"/net/dev"
    
    
     

    这边的wlan0代表wifi 上传下载量标识! 上传下载量单位是字节可以/1024换算成KB
    这里可以看到下载的字节数 、数据包 和 发送的字节数 、数据包

    小技巧:wlan0这些值如何初始化0 很简单 你打开手机飞行模式再关掉就清0了

    第二种

    通过proc/net/xt_qtaguid/stats

    在说第二种获取流量方法之前先给这边先给大家说下uid

    uid的获取可以在对应的PID下面去查看status,里面会查到uid

    adb shell cat /proc/<pid>/status
    
     

    下面这个方法是通过PackageManager去取:

    try {
                PackageManager pm = getPackageManager();
                ApplicationInfo ai = pm.getApplicationInfo("PackageName", PackageManager.GET_ACTIVITIES);
                Log.d("!!", "!!" + ai.uid);
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
    

    拿到UID后呢继续:

    adb shell cat /proc/net/xt_qtaguid/stats | grep uid
    
     

    其中第6和8列为 rx_bytes(接收数据)和tx_bytes(传输数据)包含tcp,udp等所有网络流量传输的统计。
    一个uid可能对应多个 进程,所以这有两行流量是累加的就求和就行。

    用java去获取打印
    我这边是用先获取PID然后调用!你可以把获取PID作为一个变量传到GetFlow里面来!
    我这边只获取下载流量,你可以把上传下载的流量都获取出来!

          //获取PID
          public static String PID(String PackageName) throws IOException {
    
                String PID=null;
                Runtime runtime = Runtime.getRuntime();
                Process proc = runtime.exec("adb shell ps |grep "+PackageName);
                try {
                    if (proc.waitFor() != 0) {
                        System.err.println("exit value = " + proc.exitValue());
                    }
                    BufferedReader in = new BufferedReader(new InputStreamReader(
                            proc.getInputStream()));
                    StringBuffer stringBuffer = new StringBuffer();
                    String line = null;
                    while ((line = in.readLine()) != null) {
                        stringBuffer.append(line+" ");                                  
                    }
                String str1=stringBuffer.toString();
                String str2=str1.substring(str1.indexOf(" "+PackageName)-46,str1.indexOf(" "+PackageName));
                String str3 =str2.substring(0,7);
                str3 = str3.trim();
                PID=str3;  
                } catch (InterruptedException e) {
                    System.err.println(e);
                }finally{
                    try {
                        proc.destroy();
                    } catch (Exception e2) {
                    }
                }
    
                return PID;
          }     
    
    
    
    
    
    
                //获取下载流量
          public static double GetFlow(String PackageName) throws IOException {
                double FlowSize=0;
                String Pid=PID(PackageName);
            try{
                Runtime runtime = Runtime.getRuntime();
                Process proc = runtime.exec("adb shell cat /proc/"+Pid+"/net/dev");
                try {
                    if (proc.waitFor() != 0) {
                        System.err.println("exit value = " + proc.exitValue());
                    }
                    BufferedReader in = new BufferedReader(new InputStreamReader(
                            proc.getInputStream()));
                    StringBuffer stringBuffer = new StringBuffer();
                    String line = null;
                    while ((line = in.readLine()) != null) {
                        stringBuffer.append(line+" ");
    
    
                    }
                String str1=stringBuffer.toString();
                String str2=str1.substring(str1.indexOf("wlan0:"),str1.indexOf("wlan0:")+90);
                String str4=str2.substring(7,16);
                str4 = str4.trim();
                int Flow=Integer.parseInt(str4);
                FlowSize=Flow/1024;
    
                } catch (InterruptedException e) {
                    System.err.println(e);
                }finally{
                    try {
                        proc.destroy();
                    } catch (Exception e2) {
                    }
                }
            }
                catch (Exception StringIndexOutOfBoundsException)
                {
    
                }
    
                return FlowSize;
               }
    
    

    获取每秒下载流量:

    public static double Flow(String PackageName) throws IOException, InterruptedException
        {
    
            double Flow1=GetFlow(PackageName);
            Thread.sleep(1000);
            double Flow=GetFlow(PackageName)-Flow1;
            //System.out.println(GetFlow()-Flow1);
            return Flow ;
    
        }
    
    

    场景设计

    拿到流量值后在步骤前 将流量打印,再步骤完成后再打印一遍,再用步骤完成的流量值减去之前的流量值 得到这个步骤所消耗的流量!

    场景案例:

     
     

    拓展

    下面的方法都是集成在Android 内部的方法:(仅供参考)

    Android的TrafficStats类
    前四个读取的/proc/net/dev里面的数据

       Android架构对流量的统计通过一个TrafficStats类可以直接获取
       获取总接受流量TrafficStats.getTotalRxBytes()获取总发送流量TrafficStats.getTotalTxBytes());
       获取不包含WIFI的手机GPRS接收量TrafficStats.getMobileRxBytes());
       获取不包含Wifi的手机GPRS发送量TrafficStats.getMobileTxBytes());
       统计某一个进程的总接收量TrafficStats.getUidRxBytes(Uid));
       统计某一个进程的总发送量TrafficStats.getUidTxBytes(Uid));
    
    
    
    
    package cn.sunzn.trafficmanger;
    import android.app.Activity;
    import android.net.TrafficStats;
    import android.os.Bundle;
    import android.view.Menu;
    public class MainActivity extends Activity {
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           /** 获取手机通过 2G/3G 接收的字节流量总数 */
           TrafficStats.getMobileRxBytes();
           /** 获取手机通过 2G/3G 接收的数据包总数 */
           TrafficStats.getMobileRxPackets();
           /** 获取手机通过 2G/3G 发出的字节流量总数 */
           TrafficStats.getMobileTxBytes();
           /** 获取手机通过 2G/3G 发出的数据包总数 */
           TrafficStats.getMobileTxPackets();
           /** 获取手机通过所有网络方式接收的字节流量总数(包括 wifi) */
           TrafficStats.getTotalRxBytes();
           /** 获取手机通过所有网络方式接收的数据包总数(包括 wifi) */
           TrafficStats.getTotalRxPackets();
           /** 获取手机通过所有网络方式发送的字节流量总数(包括 wifi) */
           TrafficStats.getTotalTxBytes();
           /** 获取手机通过所有网络方式发送的数据包总数(包括 wifi) */
           TrafficStats.getTotalTxPackets();
           /** 获取手机指定 UID 对应的应程序用通过所有网络方式接收的字节流量总数(包括 wifi) */
           TrafficStats.getUidRxBytes(uid);
           /** 获取手机指定 UID 对应的应用程序通过所有网络方式发送的字节流量总数(包括 wifi) */
           TrafficStats.getUidTxBytes(uid);
       }
       public boolean onCreateOptionsMenu(Menu menu) {
           getMenuInflater().inflate(R.menu.activity_main, menu);
           return true;
       }
    }

    ------------------------------

    Android 性能统计--网络流量统计 using ADB

    移动性能测试 · oscar · 于3 月前发布 · 最后由 oscar 于3 月前回复 · 1684 次阅读
    214

    因之前性能脚本针对流量统计在某些机型如(红米note)上不支持,故对流量部分做了调研和改进
    1、 之前统计是cat proc/uid_stat/(uid#)/tcp_rcv路径下

    adb shell cat proc/uid_stat/(uid#)/tcp_rcv
    adb shell cat proc/uid_stat/(uid#)/tcp_snd
    

    对二者求和,劣势是只针对tcp协议网络的消耗统计,且在某些机型上不存在该路径。
    故进行了优化。

    2、 经调查/proc/net/xt_qtaguid/stats 基本覆盖目前所有机型且统计流量全面

    adb shell cat /proc/net/xt_qtaguid/stats | grep (uid#)

    如: adb shell cat /proc/net/xt_qtaguid/stats | grep 10127

    48 wlan0 0x0 10127 0 316574 2279 472562 3651 316574 2279 0 0 0 0 472562 3651 0 0 0 0
    49 wlan0 0x0 10127 1 6172960 4936 415951 5215 6172960 4936 0 0 0 0 415951 5215 0 0 0 0
    50 wlan0 0x3792d5b400000000 10127 0 29678 208 32168 296 29678 208 0 0 0 0 32168 296 0 0 0 0
    51 wlan0 0x3792d5b400000000 10127 1 226170 222 25745 265 226170 222 0 0 0 0 25745 265 0 0 0 0
    56 wlan0 0xfa1dcc4b00000000 10127 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    57 wlan0 0xfa1dcc4b00000000 10127 1 3014885 2127 139857 2117 3014885 2127 0 0 0 0 139857 2117 0 0 0 0

    其中第6和8列为 rx_bytes(接收数据)和tx_bytes(传输数据)包含tcp,udp等所有网络流量传输的统计。
    [具体参考url:​http://stackoverflow.com/questions/12904809/tracking-an-applications-network-statistics-netstats-using-adb]


    添加了代码,论坛的兄弟姐妹们可以帮忙参考一下可行性如何

        def get_networkTraffic(self):
            """基于UID获取App的网络流量的方法
               从/proc/net/xt_qtaguid/stats获取网络流量统计,进行判断,不存在使用之前的方法。
            """
    
            flag_net = self.adb.shell('{0} cat /proc/net/xt_qtaguid/stats'.format(BUSYBOX_PATH),timeout=TIMEOUT)
            # print flag_net
            if "No such file or directory" not in flag_net:
                list_rx = [] # 接收网络数据流量列表
                list_tx = [] # 发送网络数据流量列表
                str_uid_net_stats = self.adb.shell('{0} cat /proc/net/xt_qtaguid/stats|{0} grep {1}'.format(BUSYBOX_PATH,self.strUID),timeout=ADB_TIMEOUT)
                # print str_uid_net_stats
                try:
                    for item in str_uid_net_stats.splitlines():
                        rx_bytes = item.split()[5] # 接收网络数据流量
                        tx_bytes = item.split()[7] # 发送网络数据流量
                        list_rx.append(int(rx_bytes))
                        list_tx.append(int(tx_bytes))
                    # print list_rx, sum(list_rx)
                    floatTotalNetTraffic = (sum(list_rx) + sum(list_tx))/1024.0/1024.0
                    floatTotalNetTraffic = round(floatTotalNetTraffic,4)
                    return floatTotalNetTraffic
                except:
                    print "[ERROR]: cannot get the /proc/net/xt_qtaguid/stats, return 0.0"
                    return 0.0
    
            else:
                strTotalTxBytes = self.adb.shell('{0} cat /proc/uid_stat/{1}/tcp_snd'.format(BUSYBOX_PATH,self.strUID),timeout=TIMEOUT)
                strTotalRxBytes = self.adb.shell('{0} cat /proc/uid_stat/{1}/tcp_rcv'.format(BUSYBOX_PATH,self.strUID),timeout=TIMEOUT)
                try:
                    floatTotalTraffic = (int(strTotalTxBytes) + int(strTotalRxBytes))/1024.0/1024.0
                    floatTotalTraffic = round(floatTotalTraffic,4)
                    return floatTotalTraffic
                except:
                    return 0.0  
  • 相关阅读:
    IDEA中用好Lombok,撸码效率至少提升5倍
    在 IDEA 中使用 Debug,真是太厉害了!
    彻底理解cookie,session,token
    优秀的程序员一定要多写博客!
    IntelliJ IDEA 从入门到上瘾教程,2019图文版!
    注解配置
    过滤器(登录认证)
    过滤器
    Session监听器
    request监听器
  • 原文地址:https://www.cnblogs.com/LittleRedPoint/p/4561765.html
Copyright © 2011-2022 走看看