app專項效能測試 獲取手機cpu 記憶體 流量

2021-08-20 16:36:55 字數 3180 閱讀 5896

1、cpu獲取

// 獲取手機指定packagename的cpu佔比

public static string getcpu(string packagename)

bufferedreader in = new bufferedreader(new inputstreamreader(proc.getinputstream()));

stringbuffer stringbuffer = new stringbuffer();

string line = null;

while ((line = in.readline()) != null)

string str1 = stringbuffer.tostring();

// system.out.println("返回是#####"+str1);

// 以空格區分正則

string toks = str1.split(" +");

/// / string str2 = str1.substring(str1.indexof(" " + packagename) - 46, str1.indexof(" " + packagename));

cpu = toks[4];

// system.out.println("當前程序的cpu占用是*** " + cpu );

} catch (interruptedexception e) finally catch (exception e2)

}} catch (exception stringindexoutofbound***ception)

return cpu;

}

2、記憶體獲取

adb shell dumpsys meminfo|grep packagename

public static double getmemory(string packagename) 

bufferedreader in = new bufferedreader(new inputstreamreader(proc.getinputstream()));

stringbuffer stringbuffer = new stringbuffer();

string line = null;

while ((line = in.readline()) != null)

string str1 = stringbuffer.tostring();

string str2 = str1.substring(str1.indexof(": "+packagename) - 12, str1.indexof(": "+packagename)).trim();

string str3 = str2.substring(0, str2.indexof("k"));

str3 = str3.trim().replace(",","");

heap = double.parsedouble(str3) / 1024;

decimalformat df = new decimalformat("#.000");

string memory = df.format(heap);

heap = double.parsedouble(memory);

system.out.println(heap + "mb");

} catch (interruptedexception e) finally catch (exception e2) }}

catch (exception stringindexoutofbound***ception)

return heap;

}

3、獲取流量

下面通過時間查計算流量,兩次獲取流量除以時間(下面用的是1秒,所以得到的每秒消耗流量 kbps/s)

public static double getflowaction(string packagename) 

bufferedreader in = new bufferedreader(new inputstreamreader(proc.getinputstream()));

stringbuffer stringbuffer = new stringbuffer();

string line = null;

while ((line = in.readline()) != null)

string str1 = stringbuffer.tostring();

string str2 = str1.substring(str1.indexof("wlan0:"), str1.indexof("wlan0:") + 100);

// system.out.println("先獲取的句子 " + str2);

// 空格分成字串陣列取第2個,第10個數,分別是傳送流量和接受流量

string toks = str2.split(" +");

string str4 = toks[1];

string str6 = toks[9];

int b = integer.parseint(str4);

int a = integer.parseint(str6);

double sendflow = a / 1024 ;

double revflow = b / 1024 ;

flowaction = sendflow + revflow;

system.out.println("當前應用使用流量為: " + flowaction + "kbps");

} catch (interruptedexception e) finally catch (exception e2)

}} catch (exception stringindexoutofbound***ception)

return flowaction;

}public static double getflow() catch (interruptedexception e)

flow = flowafter - flowbefore;

return flow;

}

手機APP測試之專項測試

前言 1 我應該在什麼階段去做專項測試。2 每個階段做什麼。3 應該做到什麼顆粒度。4 怎麼樣才算完成了專項測試。下面我們就來聊聊專項測試在專案不同階段的不同策略及專項基線 規範。一 專案中的專項實踐流程 1.1 第一階段 專案需求階段 該階段屬於專案需求說明書 測試分析 系統分析三個文件的評審階段...

APP專項測試 效能分析

1 先sdk platform tools 安裝adb 進入介面 2 安裝完成後可以使用adb 命令了 檢視下當前裝置 adb devices 檢視下當前包名 啟動某個應用,檢視應用包名和activity 三 adb logcat 使用 進入指定裝置shell adb s 127.0.0.1 755...

手機app效能測試

v1.0 bug fix a month ago gitignore initial commit 6 months ago license initial commit 6 months ago listener.apk add support test api 6 months ago read...