Android 性能测试实践(二) 实时监控工具

前言

欢迎一起交流,一起进步 可以关注我的微信公众号:扫描二维码关注哦:

这里写图片描述


各位 这个工具是本人写的非常简陋体验也非常一般,大家手下留情,不要吐槽!能用上的拿去用吧!

这个工具问题估计很多,写的不是很好会有很多手机不支持所以有什么问题可以联系我!我能帮助你的尽力而为!

文件下载地址

http://download.csdn.net/detail/liyu520131414/8720319

工具说明

1,一定要是Root过的Android主流的手机
2,一定要有Adb 环境 (Android Debug Bridge)如果没有的话我文件里面放了

把这个文件夹 路径放到环境变量里面去,Path下面哦
放完后check一下:

这样环境就Ok了

工具的原理

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;

      }


使用说明

第一步:先检查设备是否连接。

adb devices

第二步:把被测程序打开。

第三步:输入包名点击开始。

查包名的方法(列出所有的包名):

adb  shell  ps

工具展示

日志的收集放在D:/log

Monkey


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值