zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android 性能测试实践(三)Cpu

2023-09-14 08:56:50 时间

关于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;}

取完你可以这么用》:

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

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

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


Android CPU性能测试 在经典的性能问题中,一般我们会说两种问题:一种是I/O密集型问题,另外一种就是CPU密集型的问题,今天我就来聊聊如何测试Android应用的 CPU性能。
Android内存性能测试 Android应用大部分性能问题归根结底都会成为内存的问题,今天我们就先以Out of Memory(OOM)为起点介绍一下Android内存的原理以及排查内存问题的方法。