Skip to content

Commit

Permalink
Fixed: NetEase#11 delete abnormal data
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewleo committed Dec 17, 2013
1 parent 1e26fa7 commit 09028b5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 53 deletions.
9 changes: 7 additions & 2 deletions res/layout/floating.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@

<Button
android:id="@+id/wifi"
android:layout_width="80dp"
android:layout_height="40dp" />
android:layout_width="50dp"
android:layout_height="25dp" />

<Button
android:id="@+id/stop"
android:layout_width="50dp"
android:layout_height="25dp" />

</LinearLayout>
</RelativeLayout>
Expand Down
43 changes: 34 additions & 9 deletions src/com/netease/qa/emmagee/service/EmmageeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -388,29 +402,31 @@ 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;
trafficMb = (double) tempTraffic / 1024;
}
}
}
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 + "%");
txtBatt.setText("电量:" + totalBatt + ",电流:" + currentBatt + "mA");
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;
}
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -469,4 +490,8 @@ public void onDestroy() {
public IBinder onBind(Intent intent) {
return null;
}

private void is_double() {

}
}
87 changes: 45 additions & 42 deletions src/com/netease/qa/emmagee/utils/CpuInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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];
Expand All @@ -140,11 +135,10 @@ public String getCpuName() {
* @return network traffic ,used ratio of process CPU and total CPU in
* certain interval
*/
public ArrayList<String> getCpuRatioInfo(String totalBatt,
String currentBatt, String temperature, String voltage) {
public ArrayList<String> 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);

Expand All @@ -154,10 +148,8 @@ public ArrayList<String> 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";
Expand All @@ -174,45 +166,56 @@ public ArrayList<String> 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;
}

}

0 comments on commit 09028b5

Please sign in to comment.