您的位置:首页 > 移动开发 > Android开发

Android 性能测试实践(三)Cpu

2015-05-20 14:36 246 查看
Cpu篇

关于Android 的Cpu占用率需要注意以下三种情况:

1.空闲状态下的应用CPU消耗情况 简单说这种情况呢就是说被测应用在系统资源非常空闲的情况下的占用率,比如只开一个被测应用

2.中等规格状态下的应用CPU消耗情况 简单说这种情况就是后台已经有几个应用在运行已经并且消耗了系统的一些资源的情况下进行测试。

3.满规格状态下的应用CPU消耗情况 这个就不要说了,你们懂得!

数据采集方案:

1.
adb shell dumpsys cpuinfo







这里可以看到所有进程的Cpu占用率:

大家看第一个应用CPU占用率68%,这个过程是在用户(user)中花61%的时间,并在内核空间(kernel)花费7.1%的时间。

如果你想筛选出你自己的应用的话可以用下面这一段:
adb shell dumpsys cpuinfo |grep packagename







2.使用top命令:

进入Adb shell
adb shell

top -m 10 -s cpu







可查看占用cpu最高的前10个程序(-t 显示进程名称,-s 按指定行排序,-n 在退出前刷新几次,-d 刷新间隔,-m 显示最大数量)

如果你想筛选出你自己的应用的话可以用下面这一段:
adb shell top -n 1| grep PackageName


拿到这些数据怎么用

1,你可以从代码里面获取:

(dumpsys)
adb shell  dumpsys  cpuinfo

public static String GetCpu(String packageName) throws IOException {
String str3=null;
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("adb shell dumpsys cpuinfo  $"+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),str1.indexOf(packageName)+28);
str3=str2.substring(18,23);

} catch (InterruptedException e) {
System.err.println(e);
}finally{
try {
proc.destroy();
} catch (Exception e2) {
}
}
return str3;

}

}


(Top)
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));
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;

}


2,直接 adb shell cat进去proc/cpuinfo/下面:
public String[] getCpuInfo() {
String str1 = "/proc/cpuinfo";
String str2="";
String[] cpuInfo={"",""};
String[] arrayOfString;
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
for (int i = 2; i < arrayOfString.length; i++) {
cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
}
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
cpuInfo[1] += arrayOfString[2];
localBufferedReader.close();
} catch (IOException e) {
}
return cpuInfo;
}


取完你可以这么用》:

配合一些场景去采集数据:






这样可以看到每个步骤消耗的资源情况






然后汇总数据分析(最好多取几次求平均值):




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: