android 專項測試 耗流量測試

靜置耗流量測試方法

主要步驟(一)

1、靜置耗流量,測試前手機與PC端Fiddler建立連接,用來待機測試的過程中同時用電腦端抓包;

2、adb shell ps | grep package_name :獲取應用PID

3、adb shell cat /proc/PID/status :獲取應用UID

4、adb shell cat /proc/net/xtqtaguid/stats|grep UID|awk '{rxbytes+=$6}END{print rx_bytes} :tcp、udp接收數據流量和

5、adb shell cat /proc/net/xtqtaguid/stats|grep UID|awk '{txbytes+=$8}END{print tx_bytes} :tcp、udp發送的數據流量和

6、第4步和第5步的數值相加即爲當前應用從使用到現在消耗的流量,單位爲B,除以1024換算爲KB;

7、測試應用靜置耗流量的時候,設定好初始條件(如操作完主要網絡業務後,放置入後臺開始待機),執行前6步,記錄當前應用從使用到現在消耗的流量;待機12小時完成後,再次執行前6步獲取應用從使用到現在消耗的流量,二次測試數據相減獲取應用待機這段時間內獲取的流量消耗,就是靜置這段時間內應用消耗的流量。

備註:靜置耗流量一般晚上測試,應用進入後臺待機,統計12小時左右的靜置流量消耗,通常這部分消耗與發送數據包或者長連接相關

方法(一):集成一些腳本處理

其基本思路是:測試單項業務功能前,(比如更換用戶個人資料圖像)運行腳本獲取當應用已經消耗的累計流量,操作完業務操作後,再次運行腳本獲取已消耗的累計流量,前後兩次流量做差值即爲當前業務操作下的流量消耗;如果是WIFI下操作,則統計WIFI下的流量消耗,如果是移動網絡,則統計移動網絡下的流量消耗;

    package UItest;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.rmi.server.UID;
    import java.text.DecimalFormat;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;


    public class JavaPerformanceTest {
    public static void main(String[] args) throws Exception{
    // String res = testGetPackage();
    // String res = "com.xdja.HDSafeEMailClient";
    // String res = "com.xdja.ryhzw";
    // String res = "com.tencent.mm";
    // String res = "com.tencent.mobileqq";
    // String res = "com.xdja.eoa";
    // String res = "com.kingsoft.email";
    String res = "com.xdja.jxclient";
    getFlowWifi(res);
    getFlowdata(res);
    }

    /**
    * getuid:當前應用WIFI下流量消耗的總量統計,用於測試靜置流量消耗,二次做一下差值即可
    * @author lzz
    *
    */
    public static void getFlowWifi(String pkg){
    String uid = null;
    try {
    uid = getuid(pkg);
    System.out.println("應用"+pkg+"的UID信息是:"+uid);
    } catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    float flow = 0;
    try {
    flow = getliuliangwifi(uid);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println("從開機到現在累計的流量消耗,靜置耗流量需要測試前後收集二次數據並做差值");
    System.out.println("當前被測應用爲:"+pkg);
    System.out.println("當前應用WIFI流量累計總消耗是:"+flow+"單位:Byte");
    DecimalFormat df = new DecimalFormat("0.00");//添加數據格式化對象
    System.out.println("當前應用WIFI流量累計總消耗是:"+df.format(flow/1024)+"單位:KB");
    System.out.println("當前應用WIFI流量累計總消耗是:"+df.format(flow/1024/1024)+"單位:MB.\n\n");
    }

    /**
    * getuid:當前應用數據下流量消耗的總量統計,用於測試靜置流量消耗,二次做一下差值即可
    * @author lzz
    *
    */

    public static void getFlowdata(String pkg){
    String uid = null;
    try {
    uid = getuid(pkg);
    System.out.println("應用"+pkg+"的UID信息是:"+uid);
    } catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    float flow = 0;
    try {
    flow = getliulianggprs(uid);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println("從開機到現在累計的流量消耗,靜置耗流量需要測試前後收集二次數據並做差值");
    System.out.println("當前被測應用爲:"+pkg);
    System.out.println("當前應用數據流量累計總消耗是:"+flow+"單位:Byte");
    DecimalFormat df = new DecimalFormat("0.00");//添加數據格式化對象
    System.out.println("當前應用數據流量累計總消耗是:"+df.format(flow/1024)+"單位:KB");
    System.out.println("當前應用數據流量累計總消耗是:"+df.format(flow/1024/1024)+"單位:MB.\n\n");
    }

    /**
    * getuid:根據應用包名獲取Android APP uid信息
    * @author lzz
    *
    */
    public static String getuid(String packagename) throws Exception
    {
    Process p=Runtime.getRuntime().exec("adb shell dumpsys package "+packagename+" |grep userId");
    java.io.InputStream in=p.getInputStream();
    InputStreamReader ir=new InputStreamReader(in);
    BufferedReader br=new BufferedReader(ir);
    // System.out.println(br.readLine());//br.readLine()一旦被執行一次,則影響下一次的執行結果
    String uid=br.readLine().split("=")[1].split(" ")[0];
    return uid;
    }

    /**
    * PID:根據應用包名獲取Android APP PID信息
    * @author lzz
    *
    */
    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;
    }

    /**
    * package:根據應用置頂app包名,暫時還未調試好
    * @author lzz
    *
    */
    public static String getPackage() throws Exception
    {
    Process proc=Runtime.getRuntime().exec("adb shell dumpsys window |grep mFocus");
    String str1 = "";
    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+" ");
    }
    str1=stringBuffer.toString();
    // System.out.println(str1);
    }catch (InterruptedException e) {
    System.err.println(e);
    }finally{
    try {
    proc.destroy();
    } catch (Exception e2) {
    }
    }

    return str1;
    }

    /**
    * package:獲取應用流量消耗,WIFI的流量消耗,累計到這段時間內,WIFI消耗的總流量。
    * @author lzz
    *
    */
    public static float getliuliangwifi(String uid) throws IOException
    {
    Process p=Runtime.getRuntime().exec("adb shell cat /proc/net/xt_qtaguid/stats |grep "+uid+" |grep wlan0");
    java.io.InputStream in=p.getInputStream();
    InputStreamReader ir=new InputStreamReader(in);
    BufferedReader br=new BufferedReader(ir);
    String str;
    float total=0;
    while((str=br.readLine())!=null)
    {
    total=total+Integer.parseInt(str.split(" ")[5])+Integer.parseInt(str.split(" ")[7]);
    str=br.readLine();
    }
    return total;
    }

    /**
    * getCmdOut:以字符串的形式輸出Cmd命令的結果
    * @author lzz
    *
    */
    public static String getCmdOut(String cmd) throws IOException
    {
    Process p=Runtime.getRuntime().exec(cmd);
    java.io.InputStream in=p.getInputStream();
    InputStreamReader ir=new InputStreamReader(in);
    BufferedReader br=new BufferedReader(ir);
    String str;
    StringBuffer stringBuffer = new StringBuffer();//字符串接收對象
    while((str=br.readLine())!=null)
    {
    stringBuffer.append(str);
    str=br.readLine();
    }
    str = stringBuffer.toString();
    return str;
    }

    /**
    * package:獲取應用流量消耗,GPRS的流量消耗,累計到這段時間內,數據消耗的總流量。單位爲Byte.
    * @author lzz
    *
    */
    public static float getliulianggprs(String uid) throws IOException
    {
    Process p=Runtime.getRuntime().exec("adb shell cat /proc/net/xt_qtaguid/stats | grep "+uid+" |grep rmnet");
    java.io.InputStream in=p.getInputStream();
    InputStreamReader ir=new InputStreamReader(in);
    BufferedReader br=new BufferedReader(ir);
    String str;
    float total=0;
    while((str=br.readLine())!=null)
    {
    total=total+Integer.parseInt(str.split(" ")[5])+Integer.parseInt(str.split(" ")[7]);
    str=br.readLine();
    }
    return total;
    }

    /**
    * testGetPackage:點擊打開當前應用,以獲取當前應用的包名
    * @author lzz
    *
    */
    public static String testGetPackage(){
    String pkg= "";
    try {
    // Process process = Runtime.getRuntime().exec("ipconfig /all");
    Process process = Runtime.getRuntime().exec("adb shell dumpsys window |grep mFocus");
    // System.out.println(process);
    // Process p = null;
    BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = null;//初始化變量
    String str = "";
    while ((line = input.readLine()) != null) {
    // System.out.println(line);
    str =str+line;
    }
    // System.out.println(str);
    String pattern = "(com.*?)/";
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(str);
    if (m.find( )) {
    // System.out.println("Found value: " + m.group(0) );
    // System.out.println("Found value: " + m.group(1) );
    pkg = m.group(1);

    } else {
    System.out.println("NO MATCH");
    }

    } catch (IOException e) {
    e.printStackTrace();
    }
    return pkg;
    }
    }

fiddler的使用步驟

1、首先,確保安裝 Fiddler 的電腦和你的手機在同一局域網內,因爲Fiddler只是一個代理,需要將手機的代理指向 PC 機,不能互相訪問是不行的。(或者在PC機上開啓一個熱點WIFI)

2、開啓Fiddler的遠程連接,Fiddler 主菜單 Tools → Fiddler Options…→ Connections頁籤,選中Allowremote computers to connect。

3、開啓好遠程連接之後,重啓Fiddler,不然就不會更新你剛開啓的遠程配置

4、設置手機連接PC端熱點WIFI,長按手機端WIFI修改網絡,選擇高級選項配置手機端WIFI代理,例如獲取PC的IP地址192.168.2.121(打開cmd,輸入ipconfig來查看電腦端的本地ip),配置手機端ip爲是:192.168.2.121(長按連接的WIFI網絡-高級選項-輸入代理ip地址和端口,默認爲8888的端口);

5、關閉手機端第三方聯網應用和PC機聯網應用,打開被測APP進行操作,可以開始抓包了。

6、如果手機端沒有證書,請安裝證書後使用。手機端訪問“192.168.2.121:8888”(PC的ip地址和端口號),如果出現響應頁面說明成功,操作手機端聯網APP,可以在fiddler中看到抓取的數據包詳情。
網絡請求測試和說明

測試步驟和方法:

1、一般是進入有網絡請求的頁面,比如:進入個人中心頁面、進入某展示頁面等,則查看這段時間fiddler抓取的數據包,看是否有重複的網絡請求(url及數據返回結果一樣,則可能是重複請求);如右上圖展示了在進入某頁面時存在兩個一樣的網絡請求。
這裏寫圖片描述

2、反覆進入某一有網絡請求頁面,比如:進入個人中心頁面,如果每次進入都會請求相同的數據,則需要懷疑,邏輯是否正常,是否應該將數據緩存到本地,減少網絡請求的個數。如右中圖展示了請求了兩張一樣的圖片(雖然圖片的名稱不一樣,但是經比對是一樣的)。
這裏寫圖片描述

3、服務器有緩存,客戶端沒有使用;可以通過查看數據包的request headers 和response headers信息,第一次請求,如果在response headers中存在Entity的數據信息,說明服務器端有緩存信息;第二次請求,假如request headers 中沒有帶上etag和last-modified 字段,說明客戶端並沒有利用緩存,會額外的消耗流量。見下圖
這裏寫圖片描述

生活不易,兼職賺點錢,謝謝關注的朋友支持
下載“”蜜源“”免費app領京東天貓優惠券,既省錢,又可以賺錢,(平臺會付給我激活費3元每人,激活後,你也可以邀請其他人賺激活費):
Step 1: 應用寶或者蘋果商店下載“蜜源”,這樣下載比較安全,經過平臺的病毒等檢測
Setp 2: 使用手機註冊後,需要激活碼激活,輸入 FaIlFA 激活即可
不懂的,可以私信聊
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章