当前位置:   article > 正文

Android 性能测试实践(三)Cpu_android中一个进程一般正常占用cpu是多少 csdn

android中一个进程一般正常占用cpu是多少 csdn

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
  1. public static String GetCpu(String packageName) throws IOException {
  2. String str3=null;
  3. Runtime runtime = Runtime.getRuntime();
  4. Process proc = runtime.exec("adb shell dumpsys cpuinfo $"+packageName);
  5. try {
  6. if (proc.waitFor() != 0) {
  7. System.err.println("exit value = " + proc.exitValue());
  8. }
  9. BufferedReader in = new BufferedReader(new InputStreamReader(
  10. proc.getInputStream()));
  11. StringBuffer stringBuffer = new StringBuffer();
  12. String line = null;
  13. while ((line = in.readLine()) != null) {
  14. stringBuffer.append(line+" ");
  15. }
  16. String str1=stringBuffer.toString();
  17. String str2=str1.substring(str1.indexOf(packageName),str1.indexOf(packageName)+28);
  18. str3=str2.substring(18,23);
  19. } catch (InterruptedException e) {
  20. System.err.println(e);
  21. }finally{
  22. try {
  23. proc.destroy();
  24. } catch (Exception e2) {
  25. }
  26. }
  27. return str3;
  28. }
  29. }

(Top)

  1. public static double cpu(String PackageName) throws IOException {
  2. double Cpu = 0;
  3. try{
  4. Runtime runtime = Runtime.getRuntime();
  5. Process proc = runtime.exec("adb shell top -n 1| grep "+PackageName);
  6. try {
  7. if (proc.waitFor() != 0) {
  8. System.err.println("exit value = " + proc.exitValue());
  9. }
  10. BufferedReader in = new BufferedReader(new InputStreamReader(
  11. proc.getInputStream()));
  12. StringBuffer stringBuffer = new StringBuffer();
  13. String line = null;
  14. while ((line = in.readLine()) != null) {
  15. stringBuffer.append(line+" ");
  16. }
  17. String str1=stringBuffer.toString();
  18. String str3=str1.substring(str1.indexOf(PackageName)-43,str1.indexOf(PackageName));
  19. String cpu= str3.substring(0,4);
  20. cpu=cpu.trim();
  21. Cpu=Double.parseDouble(cpu);
  22. } catch (InterruptedException e) {
  23. System.err.println(e);
  24. }finally{
  25. try {
  26. proc.destroy();
  27. } catch (Exception e2) {
  28. }
  29. }
  30. }
  31. catch (Exception StringIndexOutOfBoundsException)
  32. {
  33. System.out.print("请检查设备是否连接");
  34. }
  35. return Cpu;
  36. }

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

  1. public String[] getCpuInfo() {
  2. String str1 = "/proc/cpuinfo";
  3. String str2="";
  4. String[] cpuInfo={"",""};
  5. String[] arrayOfString;
  6. try {
  7. FileReader fr = new FileReader(str1);
  8. BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
  9. str2 = localBufferedReader.readLine();
  10. arrayOfString = str2.split("\\s+");
  11. for (int i = 2; i < arrayOfString.length; i++) {
  12. cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
  13. }
  14. str2 = localBufferedReader.readLine();
  15. arrayOfString = str2.split("\\s+");
  16. cpuInfo[1] += arrayOfString[2];
  17. localBufferedReader.close();
  18. } catch (IOException e) {
  19. }
  20. return cpuInfo;
  21. }

取完你可以这么用》:

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/773187
推荐阅读
相关标签
  

闽ICP备14008679号