app专项性能测试——获取手机cpu、内存、流量 脚本

https://www.cnblogs.com/rookie-c/tag/

1、Cpu获取

了解过腾讯GT工具的源码,他们用的是通过读取/proc/stat的数据,将每一个核的cpu使用跟闲置数据提取。使用率永远是增量式计算。计算方法为100*(cpu忙时增量-cpu整体增量),从计算方法来看,可能会导致负数出现。 getProcessCpuUsage:计算进程的CPU使用率,主要通过"/proc/" + pid + "/stat"来计算,在这里回京过一系列计算,拿到进程的CPU时间片。这样的方式比较麻烦,但是兼容性可能会好一些。下面方法是直接adb shell top -n来找到应用的top信息,直接取cpu那一列的数

// 获取手机指定packagename的cpu占比
public static String getCPU(String PackageName) {

    String Cpu = null;
    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();

// System.out.println(“返回是#####”+str1);
// 以空格区分正则
String[] toks = str1.split(" +");
/// / String str2 = str1.substring(str1.indexOf(" " + PackageName) - 46, str1.indexOf(" " + PackageName));
Cpu = toks[4];
// System.out.println("当前进程的cpu占用是*** " + Cpu );

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

        System.out.println("请检查设备是否连接");

    }

    return Cpu;

}

2、内存获取
adb shell dumpsys meminfo|grep packagename

public static double getMemory(String PackageName) {

    double Heap = 0;

    try {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("adb shell dumpsys meminfo|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 str2 = str1.substring(str1.indexOf(": "+PackageName) - 12, str1.indexOf(": "+PackageName)).trim();
            String str3 = str2.substring(0, str2.indexOf("K"));
            str3 = str3.trim().replace(",","");
            Heap = Double.parseDouble(str3) / 1024;
            DecimalFormat df = new DecimalFormat("#.000");
            String memory = df.format(Heap);
            Heap = Double.parseDouble(memory);
            System.out.println(Heap + "MB");
        } catch (InterruptedException e) {
            System.err.println(e);
        } finally {
            try {
                proc.destroy();
            } catch (Exception e2) {
            }
        }
    }

    catch (Exception StringIndexOutOfBoundsException) {
        System.out.print("请检查设备是否连接");

    }
    return Heap;
}

3、获取流量

通过看 /proc/$pid/net/dev,查看应用流量使用情况。wlan0: 一行显示了接受、发送流量的字节数,把两个相加就是app累计消耗的流量(bytes),除以1024得到kbps。打开手机飞行模式、会使该数据清零。

下面通过时间查计算流量,两次获取流量除以时间(下面用的是1秒,所以得到的每秒消耗流量 kbps/s)

public static double getFlowAction(String PackageName) {
double flowAction = 0;
try {

        String Pid = PID(PackageName);

        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("adb shell cat /proc/" + Pid + "/net/dev");
        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("wlan0:"), str1.indexOf("wlan0:") + 100);

// System.out.println(“先获取的句子 " + str2);
// 空格分成字符串数组取第2个,第10个数,分别是发送流量和接受流量
String[] toks = str2.split(” +");
String str4 = toks[1];
String str6 = toks[9];
int b = Integer.parseInt(str4);
int a = Integer.parseInt(str6);

            double sendFlow = a / 1024 ;
            double revFlow = b / 1024 ;
            flowAction = sendFlow + revFlow;
            System.out.println("当前应用使用流量为: " + flowAction + "Kbps");
        } catch (InterruptedException e) {
            System.err.println(e);
        } finally {
            try {
                proc.destroy();
            } catch (Exception e2) {
            }
        }
    } catch (Exception StringIndexOutOfBoundsException) {
        System.out.println("请检查设备是否连接");

    }

    return flowAction;
}

public static double getFlow() {
    double flow = 0;
    double flowbefore = 0;
    double flowafter = 0;
    flowbefore = getFlowAction("com.umetrip.android.msky.app");
    try {
        sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    flowafter = getFlowAction("com.umetrip.android.msky.app");
    flow = flowafter - flowbefore;
    return flow;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值