请关注我的微信公众号“测试开发进阶”,不定时的独家技术资料源码,跟我一起快速成长!

Android 性能测试实践(三)Cpu

上一篇 / 下一篇  2015-07-09 14:48:11 / 个人分类:Android

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
publicstaticStringGetCpu(StringpackageName)throwsIOException{Stringstr3=null;Runtimeruntime=Runtime.getRuntime();Processproc=runtime.exec("adb shell dumpsys cpuinfo  $"+packageName);try{if(proc.waitFor()!=0){System.err.println("exit value = "+proc.exitValue());}BufferedReaderin=newBufferedReader(newInputStreamReader(proc.getInputStream()));StringBufferstringBuffer=newStringBuffer();Stringline=null;while((line=in.readLine())!=null){stringBuffer.append(line+" ");}Stringstr1=stringBuffer.toString();Stringstr2=str1.substring(str1.indexOf(packageName),str1.indexOf(packageName)+28);str3=str2.substring(18,23);}catch(InterruptedExceptione){System.err.println(e);}finally{try{proc.destroy();}catch(Exceptione2){}}returnstr3;}}

(Top)

publicstaticdoublecpu(StringPackageName)throwsIOException{doubleCpu=0;try{Runtimeruntime=Runtime.getRuntime();Processproc=runtime.exec("adb shell top -n 1| grep "+PackageName);try{if(proc.waitFor()!=0){System.err.println("exit value = "+proc.exitValue());}BufferedReaderin=newBufferedReader(newInputStreamReader(proc.getInputStream()));StringBufferstringBuffer=newStringBuffer();Stringline=null;while((line=in.readLine())!=null){stringBuffer.append(line+" ");}Stringstr1=stringBuffer.toString();Stringstr3=str1.substring(str1.indexOf(PackageName)-43,str1.indexOf(PackageName));Stringcpu=str3.substring(0,4);cpu=cpu.trim();Cpu=Double.parseDouble(cpu);}catch(InterruptedExceptione){System.err.println(e);}finally{try{proc.destroy();}catch(Exceptione2){}}}catch(ExceptionStringIndexOutOfBoundsException){System.out.print("请检查设备是否连接");}returnCpu;}

2,直接 adb shell cat进去proc/cpuinfo/下面:

publicString[]getCpuInfo(){Stringstr1="/proc/cpuinfo";Stringstr2="";String[]cpuInfo={"",""};String[]arrayOfString;try{FileReaderfr=newFileReader(str1);BufferedReaderlocalBufferedReader=newBufferedReader(fr,8192);str2=localBufferedReader.readLine();arrayOfString=str2.split("\\s+");for(inti=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(IOExceptione){}returncpuInfo;}

取完你可以这么用》:

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

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

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


如果你想快速提升测试技术可以关注我的微信公众号:

TAG: Android

 

评分:0

我来说两句

a5201314

a5201314

8年测试开发经验,前华为,携程测试开发工程师,目前就职BAT中一家资深测试开发,希望帮大家一起共同进步。 请关注我的微信公众号 “测试开发进阶”,不定时的独家技术资料源码,跟我一起快速成长!

日历

« 2024-04-21  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 157405
  • 日志数: 24
  • 建立时间: 2014-08-01
  • 更新时间: 2017-04-27

RSS订阅

Open Toolbar