admin
2021-01-04 aa6ef62aef83e277d4171df1d9f0803f91738216
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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;
        }
    }
 
}