Android自动化测试-自动获取cpu和内存信息
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和内存信息的更多相关文章
- Android获取cpu和内存信息、网址的代码
android获取手机cpu并判断是单核还是多核 /** * Gets the number of cores available in this device, across all proce ...
- 使用python获取CPU和内存信息的思路与实现(linux系统)
linux里一切皆为文件,在linux/unix的根文件夹下,有个/proc文件夹,这个/proc 是一种内核和内核模块用来向进程(process)发送信息的机制(所以叫做"/proc&qu ...
- 转载:Android自动化测试- 自动获取短信验证码
前言:android应用的自动化测试必然会涉及到注册登录功能,而许多的注册登录或修改密码功能常常需要输入短信验证码,因此有必要能够自动获得下发的短信验证码. 主要就是实时获取短信信息. android ...
- 方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息
CPU使用率: InputStream is = null; InputStreamReader isr = null; BufferedReader brStat = null; StringTok ...
- Android应用性能测试之CPU和内存占用
最近发现自己学的很多东西没有做好积淀的工作,也萌生了写一些东西的念头.本人也没有写博客的习惯,下边就写一下手机端的性能测试. 最近公司,要我们从事对竞品的性能测试,我负责CPU和内存的性能测试,下面就 ...
- iOS开发 - 在状态栏显示FPS,CPU和内存信息
原理 FPS的计算 CoreAnimation有一个很好用的类CADisplayLink,这个类会在每一帧绘制之前调用,并且可以获取时间戳.于是,我们只要统计出,在1s内的帧数即可. - (void) ...
- ubuntu系统中查看本机cpu和内存信息的命令和用法
https://zhidao.baidu.com/question/192966322.html 写出ubuntu linux系统中查看本机cpu和内存信息的命令和用法,以及如何解读这些命令 ubun ...
- Linux下使用java获取cpu、内存使用率
原文地址:http://www.voidcn.com/article/p-yehrvmep-uo.html 思路如下:Linux系统中可以用top命令查看进程使用CPU和内存情况,通过Runtime类 ...
- busybox devmem 直接获取、修改内存信息
/********************************************************************** * busybox devmem 直接获取.修改内存信息 ...
随机推荐
- Win7中怎么不经确认直接删除文件至回收站
Win7中怎么不经确认直接删除文件至回收站.. 1.双击桌面回收站的图标,进入回收站后,在空白处点击右键,选择属性:2.在回收站的属性对话框,最下边,把"显示删除确认对话框”前边的√去掉,应 ...
- Css中的Position属性
Css中的Position属性 Css属性在线查询地址: http://www.css88.com/book/css/properties/index.htm CSS 中的 position 属性 在 ...
- 深入理解javascript原型和闭包(12)——简介【作用域】
提到作用域,有一句话大家(有js开发经验者)可能比较熟悉:“javascript没有块级作用域”.所谓“块”,就是大括号“{}”中间的语句.例如if语句: 再比如for语句: 所以,我们在编写代码的时 ...
- english
I will keep you posted. :我会随时通知您. wow! :(表示极大的惊奇或钦佩)哇,呀 np :no problem Thanks in advance! :先行感谢 take ...
- Hadoop 2.6.0+ZooKeeper+Hive HA高可用集群安装
http://blog.csdn.net/totxian/article/details/45248399
- PHP同时上传“多个”文件示例,并格式化$_FILES数组信息
方法1: 在html表单,放置多个文件选择框, 使用数组名作为组件的名字,如下: <form action="upload.php" method="post&qu ...
- Linux 执行文件查找命令 which 详解
某个文件不知道放在哪里了,通常可以使用下面的一些命令来查找: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查 ...
- 转:浅谈UNIX下Apache的MPM及httpd.conf配置文件中相关参数配置
为什么要并发处理 以Apache为代表的web服务器中,如果不支持并发,则在一个客户端连接的时候,如果该客户端的任务没有处理完,其他连接的客户端将会一直处于等待状态,这事不可想象的,好像没有为什么要不 ...
- 字符串专题:map POJ 1002
第一次用到是在‘校内赛总结’扫地那道题里面,大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser]++ ...
- gdbsever 使用说明
gdbsever 使用说明 在新塘N3292x平台下 编译 gdbsever ./configure --target=arm-linux --host=arm-linux arm-linux-gdb ...