diff --git a/res/layout/floating.xml b/res/layout/floating.xml
index 64cac08..7c8b703 100644
--- a/res/layout/floating.xml
+++ b/res/layout/floating.xml
@@ -69,8 +69,13 @@
+ android:layout_width="50dp"
+ android:layout_height="25dp" />
+
+
diff --git a/src/com/netease/qa/emmagee/service/EmmageeService.java b/src/com/netease/qa/emmagee/service/EmmageeService.java
index e7b091b..62c72e4 100644
--- a/src/com/netease/qa/emmagee/service/EmmageeService.java
+++ b/src/com/netease/qa/emmagee/service/EmmageeService.java
@@ -198,12 +198,26 @@ public void onStart(Intent intent, int startId) {
txtBatt.setTextColor(android.graphics.Color.RED);
imgViIcon = (ImageView) viFloatingWindow.findViewById(R.id.img2);
imgViIcon.setVisibility(View.GONE);
+ imgViIcon.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ Toast.makeText(EmmageeService.this, "测试结果文件:" + resultFilePath, Toast.LENGTH_LONG).show();
+ stopSelf();
+ }
+ });
createFloatingWindow();
}
createResultCsv();
handler.postDelayed(task, 1000);
}
+ /**
+ * 点击左上的关闭按钮可以停止测试
+ */
+ private void closeTest() {
+
+ }
+
/**
* read configuration file.
*
@@ -388,7 +402,7 @@ private void dataRefresh() {
processCpuRatio = processInfo.get(0);
totalCpuRatio = processInfo.get(1);
trafficSize = processInfo.get(2);
- if ("".equals(trafficSize) && !("-1".equals(trafficSize))) {
+ if (!("".equals(trafficSize)) && !("-1".equals(trafficSize))) {
tempTraffic = Integer.parseInt(trafficSize);
if (tempTraffic > 1024) {
isMb = true;
@@ -396,11 +410,7 @@ private void dataRefresh() {
}
}
}
- if ("0".equals(processMemory) && "0.00".equals(processCpuRatio)) {
- closeOpenedStream();
- isServiceStop = true;
- return;
- }
+ // 如果cpu使用率存在且都不小于0,则输出
if (processCpuRatio != null && totalCpuRatio != null) {
txtUnusedMem.setText("占用内存:" + processMemory + "MB" + ",机器剩余:" + freeMemoryKb + "MB");
txtTotalMem.setText("占用CPU:" + processCpuRatio + "%" + ",总体CPU:" + totalCpuRatio + "%");
@@ -408,9 +418,15 @@ private void dataRefresh() {
if ("-1".equals(trafficSize)) {
txtTraffic.setText("本程序或本设备不支持流量统计");
} else if (isMb)
- txtTraffic.setText("消耗流量:" + fomart.format(trafficMb) + "MB");
+ txtTraffic.setText("流量:" + fomart.format(trafficMb) + "MB");
else
- txtTraffic.setText("消耗流量:" + trafficSize + "KB");
+ txtTraffic.setText("流量:" + trafficSize + "KB");
+ }
+ // 当内存为0切cpu使用率为0时则是被测应用退出
+ if ("0".equals(processMemory) && "0.00".equals(processCpuRatio)) {
+ closeOpenedStream();
+ isServiceStop = true;
+ return;
}
}
}
@@ -429,8 +445,13 @@ private void updateViewPosition() {
*/
public static void closeOpenedStream() {
try {
- if (bw != null)
+ if (bw != null) {
+ // TODO 补充一些注释
+ bw.write("注释:已知部分不支持的机型可在此查阅:https://github.com/NetEase/Emmagee/wiki/Some-devices-are-not-supported \r\n");
+ bw.write("电流:小于0是放电,大于0是充电,部分机型可能无数据\r\n");
+ bw.write("N/A意味不支持或者数据异常\r\n");
bw.close();
+ }
if (osw != null)
osw.close();
if (out != null)
@@ -469,4 +490,8 @@ public void onDestroy() {
public IBinder onBind(Intent intent) {
return null;
}
+
+ private void is_double() {
+
+ }
}
\ No newline at end of file
diff --git a/src/com/netease/qa/emmagee/utils/CpuInfo.java b/src/com/netease/qa/emmagee/utils/CpuInfo.java
index e4050f8..41eeab9 100644
--- a/src/com/netease/qa/emmagee/utils/CpuInfo.java
+++ b/src/com/netease/qa/emmagee/utils/CpuInfo.java
@@ -37,8 +37,7 @@
*/
public class CpuInfo {
- private static final String LOG_TAG = "Emmagee-"
- + CpuInfo.class.getSimpleName();
+ private static final String LOG_TAG = "Emmagee-" + CpuInfo.class.getSimpleName();
private Context context;
private long processCpu;
@@ -80,8 +79,7 @@ public void readCpuStat() {
String cpuStatPath = "/proc/" + processPid + "/stat";
try {
// monitor cpu stat of certain process
- RandomAccessFile processCpuInfo = new RandomAccessFile(cpuStatPath,
- "r");
+ RandomAccessFile processCpuInfo = new RandomAccessFile(cpuStatPath, "r");
String line = "";
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.setLength(0);
@@ -103,10 +101,8 @@ public void readCpuStat() {
RandomAccessFile cpuInfo = new RandomAccessFile("/proc/stat", "r");
String[] toks = cpuInfo.readLine().split("\\s+");
idleCpu = Long.parseLong(toks[4]);
- totalCpu = Long.parseLong(toks[1]) + Long.parseLong(toks[2])
- + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
- + Long.parseLong(toks[6]) + Long.parseLong(toks[5])
- + Long.parseLong(toks[7]);
+ totalCpu = Long.parseLong(toks[1]) + Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
+ + Long.parseLong(toks[6]) + Long.parseLong(toks[5]) + Long.parseLong(toks[7]);
cpuInfo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
@@ -122,8 +118,7 @@ public void readCpuStat() {
*/
public String getCpuName() {
try {
- RandomAccessFile cpuStat = new RandomAccessFile("/proc/cpuinfo",
- "r");
+ RandomAccessFile cpuStat = new RandomAccessFile("/proc/cpuinfo", "r");
String[] cpu = cpuStat.readLine().split(":"); // cpu信息的前一段是含有processor字符串,此处替换为不显示
cpuStat.close();
return cpu[1];
@@ -140,11 +135,10 @@ public String getCpuName() {
* @return network traffic ,used ratio of process CPU and total CPU in
* certain interval
*/
- public ArrayList getCpuRatioInfo(String totalBatt,
- String currentBatt, String temperature, String voltage) {
+ public ArrayList getCpuRatioInfo(String totalBatt, String currentBatt, String temperature, String voltage) {
DecimalFormat fomart = new DecimalFormat();
-// fomart.setGroupingUsed(false);
+ // fomart.setGroupingUsed(false);
fomart.setMaximumFractionDigits(2);
fomart.setMinimumFractionDigits(2);
@@ -154,10 +148,8 @@ public ArrayList getCpuRatioInfo(String totalBatt,
try {
String mDateTime2;
Calendar cal = Calendar.getInstance();
- if ((Build.MODEL.equals("sdk"))
- || (Build.MODEL.equals("google_sdk"))) {
- mDateTime2 = formatterFile.format(cal.getTime().getTime() + 8
- * 60 * 60 * 1000);
+ if ((Build.MODEL.equals("sdk")) || (Build.MODEL.equals("google_sdk"))) {
+ mDateTime2 = formatterFile.format(cal.getTime().getTime() + 8 * 60 * 60 * 1000);
totalBatt = "N/A";
currentBatt = "N/A";
temperature = "N/A";
@@ -174,45 +166,56 @@ public ArrayList getCpuRatioInfo(String totalBatt,
traffic = -1;
else
traffic = (lastestTraffic - initialTraffic + 1023) / 1024;
- processCpuRatio = fomart
- .format(100 * ((double) (processCpu - processCpu2) / ((double) (totalCpu - totalCpu2))));
- totalCpuRatio = fomart
- .format(100 * ((double) ((totalCpu - idleCpu) - (totalCpu2 - idleCpu2)) / (double) (totalCpu - totalCpu2)));
+ processCpuRatio = fomart.format(100 * ((double) (processCpu - processCpu2) / ((double) (totalCpu - totalCpu2))));
+ totalCpuRatio = fomart.format(100 * ((double) ((totalCpu - idleCpu) - (totalCpu2 - idleCpu2)) / (double) (totalCpu - totalCpu2)));
long pidMemory = mi.getPidMemorySize(pid, context);
String pMemory = fomart.format((double) pidMemory / 1024);
long freeMemory = mi.getFreeMemorySize(context);
String fMemory = fomart.format((double) freeMemory / 1024);
String percent = "统计出错";
if (totalMemorySize != 0) {
- percent = fomart
- .format(((double) pidMemory / (double) totalMemorySize) * 100);
+ percent = fomart.format(((double) pidMemory / (double) totalMemorySize) * 100);
}
- // whether certain device supports traffic statics or not
- if (traffic == -1) {
- EmmageeService.bw.write(mDateTime2 + "," + pMemory + ","
- + percent + "," + fMemory + "," + processCpuRatio
- + "," + totalCpuRatio + "," + "本程序或本设备不支持流量统计"
- + "," + totalBatt + "," + currentBatt + ","
- + temperature + "," + voltage + "\r\n");
- } else {
- EmmageeService.bw.write(mDateTime2 + "," + pMemory + ","
- + percent + "," + fMemory + "," + processCpuRatio
- + "," + totalCpuRatio + "," + traffic + ","
- + totalBatt + "," + currentBatt + "," + temperature
- + "," + voltage + "\r\n");
+ // 当应用的cpu使用率大于0时才写入文件中,过滤掉异常数据
+ if (isDouble(processCpuRatio) && isDouble(totalCpuRatio)) {
+ // whether certain device supports traffic statics or not
+ if (traffic == -1) {
+ EmmageeService.bw.write(mDateTime2 + "," + pMemory + "," + percent + "," + fMemory + "," + processCpuRatio + ","
+ + totalCpuRatio + "," + "N/A" + "," + totalBatt + "," + currentBatt + "," + temperature + "," + voltage
+ + "\r\n");
+ } else {
+ EmmageeService.bw.write(mDateTime2 + "," + pMemory + "," + percent + "," + fMemory + "," + processCpuRatio + ","
+ + totalCpuRatio + "," + traffic + "," + totalBatt + "," + currentBatt + "," + temperature + "," + voltage + "\r\n");
+ }
+ totalCpu2 = totalCpu;
+ processCpu2 = processCpu;
+ idleCpu2 = idleCpu;
+ cpuUsedRatio.add(processCpuRatio);
+ cpuUsedRatio.add(totalCpuRatio);
+ cpuUsedRatio.add(String.valueOf(traffic));
}
}
- totalCpu2 = totalCpu;
- processCpu2 = processCpu;
- idleCpu2 = idleCpu;
- cpuUsedRatio.add(processCpuRatio);
- cpuUsedRatio.add(totalCpuRatio);
- cpuUsedRatio.add(String.valueOf(traffic));
} catch (IOException e) {
e.printStackTrace();
// PttService.closeOpenedStream()
}
return cpuUsedRatio;
}
+
+ /**
+ * 判断text是否是一个double类型数据
+ *
+ * @param text
+ * @return
+ */
+ private boolean isDouble(String text) {
+ try {
+ Double.parseDouble(text);
+ } catch (NumberFormatException e) {
+ return false;
+ }
+ return true;
+ }
+
}