package com.hxh.spring.test;
|
|
import java.io.File;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Scanner;
|
|
import org.junit.Test;
|
|
import jxl.Workbook;
|
import jxl.write.Label;
|
import jxl.write.WritableSheet;
|
import jxl.write.WritableWorkbook;
|
|
//API请求时长分析
|
public class APITimeStatistics {
|
@Test
|
public void test() {
|
statisticsApiTime();
|
}
|
|
public void statisticsApiTime() {
|
try {
|
Scanner scaner = new Scanner(new File("C:\\Users\\Administrator\\Desktop\\日志分析\\BuWan_counttime.log.2016-12-08"));
|
Map<String, List<TestResult>> map = new HashMap<String, List<TestResult>>();
|
while (scaner.hasNextLine()) {
|
String next = scaner.nextLine();
|
try {
|
int index = next.trim().indexOf("[countTimeLogger]-[INFO]");
|
String[] contents = new String[] { next.substring(0, index),
|
next.substring(index + "[countTimeLogger]-[INFO]".length() + 2, next.trim().length()) };
|
|
if (contents.length > 1) {
|
String time = contents[0].trim();
|
String method = contents[1].split("#")[0].trim();
|
String count = contents[1].split("#")[1];
|
TestResult tr = new TestResult();
|
tr.setCount(Integer.parseInt(count.trim()));
|
tr.setTime(time);
|
if (map.get(method) == null)
|
map.put(method, new ArrayList<APITimeStatistics.TestResult>());
|
map.get(method).add(tr);
|
}
|
} catch (Exception e) {
|
|
}
|
}
|
scaner.close();
|
|
createExcel(map);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
private void createExcel(Map<String, List<TestResult>> map) {
|
Iterator<String> its = map.keySet().iterator();
|
try {
|
// 打开文件
|
if (new File("C:/Users/Administrator/Desktop/日志分析/statistic.xls").exists())
|
new File("C:/Users/Administrator/Desktop/日志分析/statistic.xls").delete();
|
new File("C:/Users/Administrator/Desktop/日志分析/statistic.xls").createNewFile();
|
WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/Users/Administrator/Desktop/日志分析/statistic.xls"));
|
|
int count = 0;
|
while (its.hasNext()) {
|
String key = its.next();
|
List<TestResult> list = map.get(key);
|
|
WritableSheet sheet = workbook.createSheet(key.split("\\.")[key.split("\\.").length - 1], count);
|
// 以及单元格内容为test
|
for (int i = 0; i < list.size(); i++) {
|
Label label = new Label(0, i, list.get(i).getTime());
|
sheet.addCell(label);
|
label = new Label(1, i, list.get(i).getCount() + "");
|
sheet.addCell(label);
|
}
|
count++;
|
}
|
|
workbook.write();
|
workbook.close();
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
private class TestResult {
|
private String time;
|
private int count;
|
|
public String getTime() {
|
return time;
|
}
|
|
public void setTime(String time) {
|
this.time = time;
|
}
|
|
public int getCount() {
|
return count;
|
}
|
|
public void setCount(int count) {
|
this.count = count;
|
}
|
}
|
|
}
|