统计对象
class StatisticsItem { private AtomicLong totalAccess=new AtomicLong(1); private AtomicLong totalRt; public StatisticsItem(long rt){ totalRt=new AtomicLong(rt); } public AtomicLong getTotalAccess() { return totalAccess; } public void setTotalAccess(AtomicLong totalAccess) { this.totalAccess = totalAccess; } public AtomicLong getTotalRt() { return totalRt; } public void setTotalRt(AtomicLong totalRt) { this.totalRt = totalRt; } }
统计方法
private void accessStatistics(String name, String urlP) throws FileNotFoundException, IOException { Pattern rtPattern = Pattern.compile("[\\d]+.[\\d]+.[\\d]+.[\\d]+\\s[\\d]+"); FileReader fr; BufferedReader br; String accessLog; Pattern urlPattern = Pattern.compile(urlP); for (int i = 0; i < 10; i++) { String timeP = "2012:10:00:0"; fr = new FileReader(name); br = new BufferedReader(fr); timeP = timeP + i; Pattern timePattern = Pattern.compile(timeP); Map<String, StatisticsItem> urlMap = new HashMap<String, StatisticsItem>(); int requestNum = 0; long rtSum = 0; while ((accessLog = br.readLine()) != null) { Matcher rtMatch = rtPattern.matcher(accessLog); Matcher urlMatch = urlPattern.matcher(accessLog); Matcher timeMatch = timePattern.matcher(accessLog); if (timeMatch.find()) { if (urlMatch.find()) { requestNum++; boolean rtFind=rtMatch.find(); long tempRt=0; if (rtFind) { tempRt=Long.valueOf(rtMatch.group().split(" ")[1]); rtSum = rtSum +tempRt; } String key = urlMatch.group(); if (key.contains("?")) { key = key.split("\\?")[0]; } if (urlMap.containsKey(key)) { urlMap.get(key).getTotalAccess().incrementAndGet(); urlMap.get(key).getTotalRt().addAndGet(tempRt); } else { urlMap.put(key, new StatisticsItem(tempRt)); } } } } List<Entry<String, StatisticsItem>> list=new ArrayList<Map.Entry<String, StatisticsItem>>(); Set<Entry<String, StatisticsItem>> entrySet = urlMap.entrySet(); for (Entry<String, StatisticsItem> entry : entrySet) { list.add(entry); } Collections.sort(list, new Comparator<Entry<String, StatisticsItem>>() { @Override public int compare(Entry<String, StatisticsItem> o1, Entry<String, StatisticsItem> o2) { if (o1.getValue().getTotalAccess().get() > o2.getValue().getTotalAccess().get()) { return -1; } else if (o1.getValue().getTotalAccess().get() < o2.getValue().getTotalAccess().get()) { return 1; } else { return 0; } } }); for (Entry<String, StatisticsItem> entry : list) { System.out.println(entry.getKey() + "-" + entry.getValue().getTotalAccess()+"-"+entry.getValue().getTotalRt().get()/entry.getValue().getTotalAccess().get()/1000); } System.out.println(timeP + "-" +requestNum+"-"+rtSum/requestNum/1000); } }
调用方式
String name = "D:/2012-05-18-XXX-access_log.txt"; String urlP = "[GET|POST]+\\s[http://XXX.com][\\S]+.htm"; //String urlP = "GET http://XXX.htm"; accessStatistics(name, urlP);