Emmagee是监控指定被测应用在使用过程中占用机器的CPU、内存、流量资源的性能测试小工具。项目如图:
主要源码分析:
public class CpuInfo { //测试cpu 封装cpu信息 当前时间被测应用占用的CPU使用率以及总体CPU使用量
public class EncryptData { //提供加密算法,可以对输入的字符串进行加密、解密操作
public class MailSender { //发送邮件给多个接收者、抄送邮件
public class MemoryInfo { //操作内存 封装内存消息 当前时间被测应用占用的内存量,以及占用的总体内存百分比,剩余内存量
public class ProcessInfo { //获得进程消息
public class TrafficInfo { //流量 信息 应用从启动开始到当前时间消耗的流量数
public class EmmageeService extends Service { //关键服务 所有的操作都基于该服务 操作如下:
private void readSettingInfo(Intent intent) { //读取配置文件
try {
Properties properties = new Properties();
properties.load(new FileInputStream(settingTempFile));
String interval = properties.getProperty("interval").trim();
isFloating = "true"
.equals(properties.getProperty("isfloat").trim()) ? true
: false;
sender = properties.getProperty("sender").trim();
password = properties.getProperty("password").trim();
recipients = properties.getProperty("recipients").trim();
time = "".equals(interval) ? "5" : interval;
recipients = properties.getProperty("recipients");
receivers = recipients.split("\\s+");
smtp = properties.getProperty("smtp");
} catch (IOException e) {
time = "5";
isFloating = true;
Log.e(LOG_TAG, e.getMessage());
}
}
private void createResultCsv() { //创建保存文件
//测试数据写入到CSV文件中,同时存储在手机中
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String mDateTime;
if ((Build.MODEL.equals("sdk")) || (Build.MODEL.equals("google_sdk")))
mDateTime = formatter.format(cal.getTime().getTime() + 8 * 60 * 60
* 1000);
else
mDateTime = formatter.format(cal.getTime().getTime());
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
resultFilePath = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Emmagee_TestResult_" + mDateTime + ".csv";
} else {
resultFilePath = getBaseContext().getFilesDir().getPath()
+ File.separator + "Emmagee_TestResult_" + mDateTime
+ ".csv";
}
try {
File resultFile = new File(resultFilePath);
resultFile.createNewFile();
out = new FileOutputStream(resultFile);
osw = new OutputStreamWriter(out, "GBK");
bw = new BufferedWriter(osw);
long totalMemorySize = memoryInfo.getTotalMemory();
String totalMemory = fomart.format((double) totalMemorySize / 1024);
bw.write("指定应用的CPU内存监控情况\r\n" + "应用包名:," + packageName + "\r\n"
+ "应用名称: ," + processName + "\r\n" + "应用PID: ," + pid
+ "\r\n" + "机器内存大小(MB):," + totalMemory + "MB\r\n"
+ "机器CPU型号:," + cpuInfo.getCpuName() + "\r\n"
+ "机器android系统版本:," + memoryInfo.getSDKVersion() + "\r\n"
+ "手机型号:," + memoryInfo.getPhoneType() + "\r\n" + "UID:,"
+ uid + "\r\n");
bw.write("时间" + "," + "应用占用内存PSS(MB)" + "," + "应用占用内存比(%)" + ","
+ " 机器剩余内存(MB)" + "," + "应用占用CPU率(%)" + "," + "CPU总使用率(%)"
+ "," + "流量(KB):" + "\r\n");
} catch (IOException e) {
Log.e(LOG_TAG, e.getMessage());
}
}
private void createFloatingWindow() { //创建浮动窗口
//可以选择开启浮窗功能,浮窗中实时显示被测应用占用性能数据信息
//在浮窗中可以快速启动或者关闭手机的wifi网络
SharedPreferences shared = getSharedPreferences("float_flag",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putInt("float", 1);
editor.commit();
windowManager = (WindowManager) getApplicationContext()
.getSystemService("window");
wmParams = ((MyApplication) getApplication()).getMywmParams();
wmParams.type = 2002;
wmParams.flags |= 8;
wmParams.gravity = Gravity.LEFT | Gravity.TOP;
wmParams.x = 0;
wmParams.y = 0;
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.format = 1;
windowManager.addView(viFloatingWindow, wmParams);
viFloatingWindow.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
x = event.getRawX();
y = event.getRawY() - 25;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// state = MotionEvent.ACTION_DOWN;
startX = x;
startY = y;
mTouchStartX = event.getX();
mTouchStartY = event.getY();
Log.d("startP", "startX" + mTouchStartX + "====startY"
+ mTouchStartY);
break;
case MotionEvent.ACTION_MOVE:
// state = MotionEvent.ACTION_MOVE;
updateViewPosition();
break;
case MotionEvent.ACTION_UP:
// state = MotionEvent.ACTION_UP;
updateViewPosition();
showImg();
mTouchStartX = mTouchStartY = 0;
break;
}
return true;
}
});
btnWifi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
btnWifi = (Button) viFloatingWindow.findViewById(R.id.wifi);
String buttonText = (String) btnWifi.getText();
String wifiText = getResources().getString(
R.string.openwifi);
if (buttonText.equals(wifiText)) {
wifiManager.setWifiEnabled(true);
btnWifi.setText(R.string.closewifi);
} else {
wifiManager.setWifiEnabled(false);
btnWifi.setText(R.string.openwifi);
}
} catch (Exception e) {
Toast.makeText(viFloatingWindow.getContext(), "操作wifi失败",
Toast.LENGTH_LONG).show();
Log.e(LOG_TAG, e.toString());
}
}
});
}
private void dataRefresh() {//刷新数据
int pidMemory = memoryInfo.getPidMemorySize(pid, getBaseContext());
long freeMemory = memoryInfo.getFreeMemorySize(getBaseContext());
String freeMemoryKb = fomart.format((double) freeMemory / 1024);
String processMemory = fomart.format((double) pidMemory / 1024);
ArrayList<String> processInfo = cpuInfo.getCpuRatioInfo();
if (isFloating) {
String processCpuRatio = "0";
String totalCpuRatio = "0";
String trafficSize = "0";
int tempTraffic = 0;
double trafficMb = 0;
boolean isMb = false;
if (!processInfo.isEmpty()) {
processCpuRatio = processInfo.get(0);
totalCpuRatio = processInfo.get(1);
trafficSize = processInfo.get(2);
if ("".equals(trafficSize) && !("-1".equals(trafficSize))) {
tempTraffic = Integer.parseInt(trafficSize);
if (tempTraffic > 1024) {
isMb = true;
trafficMb = (double) tempTraffic / 1024;
}
}
}
if ("0".equals(processMemory) && "0.00".equals(processCpuRatio)) {
closeOpenedStream();
isServiceStop = true;
return;
}
if (processCpuRatio != null && totalCpuRatio != null) {
txtUnusedMem.setText("占用内存:" + processMemory + "MB" + ",机器剩余:"
+ freeMemoryKb + "MB");
txtTotalMem.setText("占用CPU:" + processCpuRatio + "%"
+ ",总体CPU:" + totalCpuRatio + "%");
if ("-1".equals(trafficSize)) {
txtTraffic.setText("本程序或本设备不支持流量统计");
} else if (isMb)
txtTraffic.setText("消耗流量:" + fomart.format(trafficMb)
+ "MB");
else
txtTraffic.setText("消耗流量:" + trafficSize + "KB");
}
}
}
介绍完毕!项目下载!