本人在作APP性能測試的時候,須要統計一下APP啓動時間和進入首頁的時間,以前採起的方案是圖片作對比,後來採起了錄屏,效果都不是很理想,在參考了網上關於手機log分析手機啓動activity的教程,本身寫了一個多線程類經過不停地啓動關閉APP,同時分析log中關於activity的lauch時間獲得須要的數據。測試了一下效果很不錯,分享代碼,供你們參考。java
package monkeytest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.regex.Matcher; import java.util.regex.Pattern; import source.AppLocalMySql; import source.Common; public class LauchTime extends Thread { public static String ADB_PATH = Common.ADB_PATH;; public static String package_name = Common.PAGEKAGE; public static String test_name = "normal"; public static Pattern pattern = Pattern.compile("\\+.+?ms"); public static boolean LauchKey = false; public static void main(String[] args) { // String timess = args[0]; // int times = Common.getInstance().changeStringToInt(timess); execCmdAdb("adb logcat -c"); Common.getInstance().sleep(2000); LauchTime lauchTime = new LauchTime(); lauchTime.start();// 啓動logcat統計線程 StartApp startApp = new StartApp();// 獲取startAPP實例 for (int i = 0; i < 5; i++) { startApp.startJuziApp();// 啓動APP Common.getInstance().sleep(9000); startApp.stopJuziApp();// 關閉APP Common.getInstance().sleep(1000); } lauchTime.stopLauch();// 結束統計 } @Override public void run() { execCmdAdb("adb logcat"); } /** * 中止logcat線程 */ public void stopLauch() { LauchTime.LauchKey = true; } /** * 執行adb命令 * * @param cmd * 命令內容 * @param fileName * 輸入文件路徑 */ private static void execCmdAdb(String cmd) { System.out.println("正在執行:" + cmd); String OSname = System.getProperty("os.name"); try { Process p = null; if (OSname.contains("Mac")) { p = Runtime.getRuntime().exec(Common.ADB_PATH + cmd); } else { p = Runtime.getRuntime().exec("cmd /c " + cmd); } // 正確輸出流 InputStream input = p.getInputStream();// 建立並實例化輸入字節流 BufferedReader reader = new BufferedReader(new InputStreamReader(input));// 先經過inputstreamreader進行流轉化,在實例化bufferedreader,接收內容 String line = ""; while ((line = reader.readLine()) != null) {// 循環讀取 if (LauchKey) { p.destroy();// 結束線程 reader.close();// 此處reader依賴於input,應先關閉 input.close(); return; } //截取到log信息,分別統計兩個activity的時間 if (line.contains("Displayed")) { if (line.contains("SplashActivity")) { double time = getLauchTime(line); AppLocalMySql.getInstance().saveLauchTime(test_name, package_name, "SplashActivity", time); } if (line.contains("HomeActivity")) { double time = getLauchTime(line); AppLocalMySql.getInstance().saveLauchTime(test_name, package_name, "HomeActivity", time); } } } reader.close();// 此處reader依賴於input,應先關閉 input.close(); // 錯誤輸出流 InputStream errorInput = p.getErrorStream();// 建立並實例化輸入字節流 BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorInput));// 先經過inputstreamreader進行流轉化,在實例化bufferedreader,接收內容 String eline = ""; while ((eline = errorReader.readLine()) != null) {// 循環讀取 output(eline);// 輸出 } errorReader.close();// 此處有依賴關係,先關閉errorReader errorInput.close(); } catch ( IOException e) { Common.getInstance().output("執行" + cmd + "失敗!"); e.printStackTrace(); } } /** * 獲取啓動時間 * * @param line * 截取到的log信息 * @return 返回double時間,單位s,默認0.00 */ public static double getLauchTime(String line) { Matcher matcher = pattern.matcher(line); if (matcher.find()) { line = matcher.group(0); line = line.substring(1, line.length() - 2); line = line.replace("s", "."); if (!line.contains(".")) { line = "0." + line; } double time = Common.getInstance().changeStringToDouble(line); return time; } return 0.00; } public static void output(String text) { System.out.println(text); } public static void output(Object... object) { if (object.length == 1) { output(object[0].toString()); return; } for (int i = 0; i < object.length; i++) { System.out.println("第" + (i + 1) + "個:" + object[i]); } } }
中間startAPP類用到的方法以下:android
/** * 啓動橘子APP */ public void startJuziApp() { if (Monkey.package_name.contains("happyjuzi")) { execCmdAdb("adb shell am start -n com.happyjuzi.apps.juzi/.SplashActivity"); } else if (Monkey.package_name.contains("article.news")) { execCmdAdb("adb shell am start -n com.ss.android.article.news/.activity.SplashBadgeActivity"); } } public void stopJuziApp() { if (Monkey.package_name.contains("happyjuzi")) { execCmdAdb("adb shell am force-stop com.happyjuzi.apps.juzi"); } else if (Monkey.package_name.contains("article.news")) { execCmdAdb("adb shell force-stop com.ss.android.article.news"); } }