Android性能测试工具原理
来源:互联网 发布:腾讯nba数据库 编辑:程序博客网 时间:2024/05/19 13:07
工具的原理
Cpu取值:
adb shell top -n 1| grep "+PackageName
在代码怎么取呢?看下面
public static double Cpu(String PackageName) throws IOException { double Cpu = 0; try{ Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec("adb shell top -n 1| 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 str3=str1.substring(str1.indexOf(PackageName)-43,str1.indexOf(PackageName)); //System.out.println(str3); String cpu= str3.substring(0,4); cpu=cpu.trim(); Cpu=Double.parseDouble(cpu); } catch (InterruptedException e) { System.err.println(e); }finally{ try { proc.destroy(); } catch (Exception e2) { } } } catch (Exception StringIndexOutOfBoundsException) { System.out.print("请检查设备是否连接"); } return Cpu; }
内存取值:
adb shell dumpsys meminfo "+PackageName
public static double GetFlow(String PackageName) throws IOException { double str3=0; String Pid=PID(PackageName); try{ Runtime runtime = Runtime.getRuntime(); /* Process proc2 = runtime.exec("");*/ Process proc = runtime.exec("adb shell cat /proc/"+Pid+"/net/dev"); try { if (proc.waitFor() != 0) { System.err.println("exit value = " + proc.exitValue()); //JOptionPane.showMessageDialog(new JFrame(), "哥们抱歉,好像出问题了!关掉重试吧!"); } 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 b=Integer.parseInt(str4); str3=b/1024; //System.out.println(str3); } catch (InterruptedException e) { System.err.println(e); }finally{ try { proc.destroy(); } catch (Exception e2) { } } } catch (Exception StringIndexOutOfBoundsException) { //System.out.print("请检查设备是否连接"); } return str3; }
流量取值:
adb shell cat /proc/Pid/net/dev
public static double GetFlow(String PackageName) throws IOException { double str3=0; String Pid=PID(PackageName); try{ Runtime runtime = Runtime.getRuntime(); /* Process proc2 = runtime.exec("");*/ Process proc = runtime.exec("adb shell cat /proc/"+Pid+"/net/dev"); try { if (proc.waitFor() != 0) { System.err.println("exit value = " + proc.exitValue()); //JOptionPane.showMessageDialog(new JFrame(), "哥们抱歉,好像出问题了!关掉重试吧!"); } 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 b=Integer.parseInt(str4); str3=b/1024; //System.out.println(str3); } catch (InterruptedException e) { System.err.println(e); }finally{ try { proc.destroy(); } catch (Exception e2) { } } } catch (Exception StringIndexOutOfBoundsException) { //System.out.print("请检查设备是否连接"); } return str3; }
取到流量后可以用步骤后的流量减去步骤前的流量得到步骤消耗流量!也可以用时间差来计算!
电量取值:
adb shell dumpsys battery
这里只是剩余电量!
public static double battery() throws IOException { double batt=0; Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec("adb shell dumpsys battery"); String str3; 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("level"),str1.indexOf("level")+10); str3=str2.substring(6,10); str3.trim(); batt=Double.parseDouble(str3); } catch (InterruptedException e) { System.err.println(e); }finally{ try { proc.destroy(); } catch (Exception e2) { } } return batt; }
0 0
- Android性能测试工具原理
- 性能测试工具原理
- 性能测试工具原理
- 性能测试-性能测试工具工作原理
- Android 性能测试工具
- android性能测试工具
- android性能测试工具
- android测试性能工具
- android 性能测试工具
- android 性能测试工具
- Android性能测试工具--Oprofile
- Android程序性能测试工具
- Android性能测试工具 Emmagee
- Android性能测试工具:Emmagee
- Android性能测试工具:Emmagee
- Android性能测试工具:Emmagee
- Android性能测试工具 Emmagee
- Android app 性能测试工具
- AlwaysON同步的原理及可用模式
- Android NDK (学习笔记六) —— JNI交互间数组的处理
- msg_cp (should be mapped with insert="false" update="false")
- mysql基础知识回顾
- 引用和指针的区别C/C++
- Android性能测试工具原理
- 数据结构(一)---链表:
- [C++ Object Model] 继承对于member布局的影响
- python ddos
- Merge Two Sorted Lists
- Hive学习笔记02.txt
- MVCC浅析
- Best Time to Buy and Sell Stock
- c++Poco库