分享几个自己用过的获取app性能几个性能指标的代码
获取对应进程内存占用信息:
1 public void MemMonitor(){ 2 3 4 new Thread() { 5 public void run() { 6 final ActivityManager ActivityList=(ActivityManager)getApplicationContext().getSystemService(ACTIVITY_SERVICE); 7 try { 8 Runtime.getRuntime().exec("am start --user 0 -n 包名/mainactivity名"); 9 starttime=System.currentTimeMillis(); 10 11 } catch (IOException e) { 12 Log.e("baih","启动应用失败"); 13 } 14 while (true) { 15 Log.e("baih", "开始计算....."); 16 ActivityManager.MemoryInfo Mem1 = new ActivityManager.MemoryInfo(); 17 ActivityList.getMemoryInfo(Mem1); 18 Long MemSize = Mem1.availMem; 19 Long Mem12=Mem1.threshold; 20 String leftMemSize = Formatter.formatFileSize(getBaseContext(), MemSize); 21 String leftMemSize1 = Formatter.formatFileSize(getBaseContext(), Mem12); 22 23 //获取受控极限堆值,获取非受控极限堆值 24 int i=ActivityList.getMemoryClass();//受控堆 25 int y=ActivityList.getLargeMemoryClass();//非受控堆 26 27 //计算进程内存占用 28 List<ActivityManager.RunningAppProcessInfo> AppList = ActivityList.getRunningAppProcesses(); 29 for (ActivityManager.RunningAppProcessInfo am : AppList) { 30 if (am.processName.equals("包名")) { 31 long startedtime=System.currentTimeMillis(); 32 long time=startedtime-starttime; 33 Log.e("baih","启动时间为:"+time+"MS"); 34 int[] CMpid=new int[]{am.pid}; 35 Debug.MemoryInfo[] MemInfo = ActivityList.getProcessMemoryInfo(CMpid); 36 AppPid=am.pid; 37 double MemorySize1 = MemInfo[0].dalvikPrivateDirty / 1024.0; 38 int temp = (int) (MemorySize1 * 100); 39 MemorySize1 = temp / 100.0; 40 String ProInfo = ""; 41 ProInfo += "本机内存阀值:" + i + "MB\n " + 42 "程序包名:" + am.processName + " " + "\nPID:" + am.pid 43 + " " + "\n内存占用:" + MemorySize1 + "MB\n" + "\r\n"; 44 Log.e("baih", ProInfo); 45 if(AppPid!=0) 46 { 47 Cpuinfo();//调用计算CPU占用率函数 48 } 49 50 Mem1 = null; 51 System.gc(); 52 break; 53 } else { 54 Log.e("baih", "被测应用未运行"); 55 Mem1 = null; 56 System.gc(); 57 break; 58 } 59 } 60 try { 61 Thread.sleep(5000); 62 } catch (InterruptedException e) { 63 // TODO Auto-generated catch block 64 e.printStackTrace(); 65 } 66 } 67 } 68 }.start(); 69 70 }
获取进程CPU占用率:
//计算CPU总使用时间 private static long getTotalCpuTime() { // 获取系统总CPU使用时间 String[] cpuInfos = null; try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/stat")), 1000); String load = reader.readLine(); reader.close(); cpuInfos = load.split(" "); } catch (IOException ex) { ex.printStackTrace(); } long totalCpu = Long.parseLong(cpuInfos[2]) + Long.parseLong(cpuInfos[3]) + Long.parseLong(cpuInfos[4]) + Long.parseLong(cpuInfos[6]) + Long.parseLong(cpuInfos[5]) + Long.parseLong(cpuInfos[7]) + Long.parseLong(cpuInfos[8])+Long.parseLong(cpuInfos[9])+Long.parseLong(cpuInfos[10]); //Log.e("baih", "CPU总使用时间:" + totalCpu + "======" + op); return totalCpu; } //计算进程使用CPU时间 private static long getAppCpuTime() { // 获取应用占用的CPU时间 String[] cpuInfos = null; try { BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream("/proc/" +AppPid + "/stat")), 1000); String load = reader.readLine(); reader.close(); cpuInfos = load.split(" "); } catch (IOException ex) { ex.printStackTrace(); } long appCpuTime = Long.parseLong(cpuInfos[13]) + Long.parseLong(cpuInfos[14]) + Long.parseLong(cpuInfos[15]) + Long.parseLong(cpuInfos[16]); //Log.e("baih", "应用使用CPU时间:" + appCpuTime); return appCpuTime; } //计算进程占用CPU率 public void Cpuinfo(){ //计算CPU占用率 new Thread(){ public void run(){ long TotalCpu1 = getTotalCpuTime(); long AppCpu1 = getAppCpuTime(); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } long TotalCpu2 = getTotalCpuTime(); long AppCpu2 = getAppCpuTime(); long CpuInfo = 100 * (AppCpu2 - AppCpu1) / (TotalCpu2 - TotalCpu1); Log.e("baih", "CPU占用率=" + CpuInfo + "%"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); }
获取应用启动时间:
public void StartTime(){ //计算启动时间 ActivityManager ac1=(ActivityManager)getApplicationContext().getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> Applist1=ac1.getRunningAppProcesses(); while (true) { for (ActivityManager.RunningAppProcessInfo mc1 : Applist1) { if (mc1.processName.equals("包名")) { startedtime = System.currentTimeMillis(); continue; } else { starttime = System.currentTimeMillis(); } } time = startedtime - starttime; Log.e("baih", "启动时间为:" + time + "ms"); } }
时间: 2024-12-21 18:49:09