CpuInfo.java

package com.dtest;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStreamReader; class CpuInfo{
public static String getcpu(String packageName) throws Exception{
String str = null; try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("adb shell dumpsys cpuinfo | grep " + packageName); try{
//如果执行时非正常终止,则打印进程退出的返回值,waitFor()=0 为正常终止.
//waitFor()方法将导致当前的线程等待.如果必要的话.直到由该Process对象表示的进程已经终止.此方法将立即返回,如果子进程已经终止.
//如果子进程尚未终止,则调用线程将被阻塞,直到子进程退出.
if(proc.waitFor() != 0){
System.err.println("exit value = " + proc.exitValue());
} //创建一个BufferedReader对象,且里边装的内容为执行proc返回的值(将proc的返回值作为输入流)
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream())); //创建一个空StringBuffer对象,用来装输出内容
StringBuffer sr = new StringBuffer();
String line = null; //逐行读取返回输入流内容并添加到stringbuffer对象中,每次添加都进行换行.
while((line = br.readLine()) != null){
sr.append(line + "\n");
} String str1 = sr.toString();
System.out.println(str1);
/*
String str2 = str1.substring(str1.indexOf(packageName),str1.indexOf(packageName) + 28);
str = str2.substring(18,23);*/ FileWriter fw = new FileWriter("d:\\cpuinfo.txt",true);
fw.flush();
fw.write(str1);
//fw.write("==========================" + "\n");
fw.close(); }catch(InterruptedException e){
System.out.println(e);
}finally{
try{
proc.destroy();
}catch(Exception e2){
//System.out.println(e2);
}
}
} catch (Exception StringIndexOutOfBoundsExcepiton) {
// TODO Auto-generated catch block
System.out.println("请检查设备是否连接");
} return str;
}
} /*
public static double Cpu(String packageName) throws IOException{ double Cpu = 0; try{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("adb shell top -n 5 | grep " + packageName);
try{
//如果执行时非正常终止,则打印进程退出的返回值,waitFor()=0 为正常终止.
//waitFor()方法将导致当前的线程等待.如果必要的话.直到由该Process对象表示的进程已经终止.此方法将立即返回,如果子进程已经终止.
//如果子进程尚未终止,则调用线程将被阻塞,直到子进程退出.
if(proc.waitFor() != 0){
System.err.println("exit value = " + proc.exitValue());
} //创建一个BufferedReader对象,且里边装的内容为执行proc返回的值(将proc的返回值作为输入流)
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream())); //创建一个空StringBuffer对象,用来装输出内容
StringBuffer stringBuffer = new StringBuffer(); String line = null;
//逐行读取返回输入流内容并添加到stringbuffer对象中,每次添加都进行换行.
while((line = br.readLine()) != null){
stringBuffer.append(line + "\n");
} String str1 = stringBuffer.toString();
System.out.println(str1); //BufferedReader reader = new BufferedReader(new FileReader("d:\\a.csv"));
FileWriter fw = new FileWriter("d:\\cpuinfo.text",true);
fw.flush();
fw.write(str1);
fw.close(); String str3 = str1.substring(str1.indexOf(packageName)-43,str1.indexOf(packageName)); 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){
//System.out.println("test");
}
}
}catch(Exception StringIndexOutOfBoundsExcepiton){
//System.out.println("请检查设备是否连接");
}
return Cpu;
}
*/

MemInfo.java

package com.dtest;

import java.io.*;
import java.lang.StringBuffer ; class MemInfo{
public static String getMemory(String packageName) throws IOException, InterruptedException{
String str = null; 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 br = new BufferedReader(new InputStreamReader(proc.getInputStream())); StringBuffer sf = new StringBuffer(); String line = null;
while((line = br.readLine()) != null){
sf.append(line + "\n");
} String str1 = sf.toString();
System.out.println(str1);
/*
String str2 = str1.substring(str1.indexOf("Objects")-60,str1.indexOf("Objects"));
str = str2.substring(0,10);
str.trim();*/ FileWriter fw = new FileWriter("d:\\meminfo.txt",true);
fw.flush();
fw.write(str1);
//fw.write("==========================" + "\n");
fw.close(); }catch(InterruptedException e){
System.out.println(e);
}finally{
try{
proc.destroy();
}catch(Exception e2){
System.out.println(e2);
}
}
} catch (Exception StringIndexOutOfBoundsExcepiton) {
// TODO Auto-generated catch block
System.out.println("请检查设备是否连接");
} return str;
}
}

Android自动化测试-自动获取cpu和内存信息的更多相关文章

  1. Android获取cpu和内存信息、网址的代码

      android获取手机cpu并判断是单核还是多核 /** * Gets the number of cores available in this device, across all proce ...

  2. 使用python获取CPU和内存信息的思路与实现(linux系统)

    linux里一切皆为文件,在linux/unix的根文件夹下,有个/proc文件夹,这个/proc 是一种内核和内核模块用来向进程(process)发送信息的机制(所以叫做"/proc&qu ...

  3. 转载:Android自动化测试- 自动获取短信验证码

    前言:android应用的自动化测试必然会涉及到注册登录功能,而许多的注册登录或修改密码功能常常需要输入短信验证码,因此有必要能够自动获得下发的短信验证码. 主要就是实时获取短信信息. android ...

  4. 方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息

    CPU使用率: InputStream is = null; InputStreamReader isr = null; BufferedReader brStat = null; StringTok ...

  5. Android应用性能测试之CPU和内存占用

    最近发现自己学的很多东西没有做好积淀的工作,也萌生了写一些东西的念头.本人也没有写博客的习惯,下边就写一下手机端的性能测试. 最近公司,要我们从事对竞品的性能测试,我负责CPU和内存的性能测试,下面就 ...

  6. iOS开发 - 在状态栏显示FPS,CPU和内存信息

    原理 FPS的计算 CoreAnimation有一个很好用的类CADisplayLink,这个类会在每一帧绘制之前调用,并且可以获取时间戳.于是,我们只要统计出,在1s内的帧数即可. - (void) ...

  7. ubuntu系统中查看本机cpu和内存信息的命令和用法

    https://zhidao.baidu.com/question/192966322.html 写出ubuntu linux系统中查看本机cpu和内存信息的命令和用法,以及如何解读这些命令 ubun ...

  8. Linux下使用java获取cpu、内存使用率

    原文地址:http://www.voidcn.com/article/p-yehrvmep-uo.html 思路如下:Linux系统中可以用top命令查看进程使用CPU和内存情况,通过Runtime类 ...

  9. busybox devmem 直接获取、修改内存信息

    /********************************************************************** * busybox devmem 直接获取.修改内存信息 ...

随机推荐

  1. LogStash配置、使用(三)

    LogStash配置 官方文档:https://www.elastic.co/guide/en/logstash/current/index.html 查看yum安装路径 rpm -ql logsta ...

  2. 《Python核心编程》18.多线程编程(三)

    18.6使用threading模块 #!/usr/bin/env python # -*- coding:utf-8 -*- """从Thread类中派生出一个子例,创建 ...

  3. Yii rules常用规则

    public function rules() {     return array(         //必须填写         array('email, username, password, ...

  4. JVM垃圾回收算法

  5. [小程序]那些icons

    摘要 为了提供更友好的提示信息,会使用icon+信息的方式,向用户提示当前操作的成功,失败或者一些警告信息.小程序也为我们定义了一些icons,足够大部分情况的使用了. 那些icons 我们新建一个名 ...

  6. Tomcat SSL的安装及配置中遇到问题

    配置tomcat服务器利用SSL进行加密. 一.生成密钥库 具体生成方式就不讲了,tomcat支持的keystore的格式有JKS,PKCS11和PKCS12 JKS是jdk /bin目录下keyto ...

  7. tyvj1011 传纸条

    背景 NOIP2008复赛提高组第三题 描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端, ...

  8. linux source

    清华TUNA镜像源https://mirrors.tuna.tsinghua.edu.cn/ 中科大USTC镜像源 https://mirrors.ustc.edu.cn/ ali http://mi ...

  9. JDK source 之 ArrayList 需要注意事项

    线程安全 ArrayList内部没有实现原子性操作,所以是非线程安全的.如果需要在线程安全的环境下使用List的话,需要使用Vector 或者CopyOnWriteArrayList,具体场景,自行深 ...

  10. php函数parse_url

    1.需求 了解parse_url的使用方法 2.实例 $uri = parse_url('http://dummy'.$_SERVER['REQUEST_URI']); var_dump($uri); ...