| | |
| | | package org.yeshi.utils.statistic.http;
|
| | |
|
| | | import java.text.DateFormat;
|
| | | import java.text.ParseException;
|
| | | import java.text.SimpleDateFormat;
|
| | | import java.util.Date;
|
| | | import java.util.List;
|
| | | import java.util.Locale;
|
| | |
|
| | | public class LocalhostAccessLogUtil {
|
| | |
|
| | | /**
|
| | | * 将单行日志文件解析为对象
|
| | | * |
| | | * @param content
|
| | | * @return
|
| | | */
|
| | | public static HttpRequestInfo parseTomcatSingleLine(String content) {
|
| | |
|
| | | try {
|
| | | HttpRequestInfo info = new HttpRequestInfo();
|
| | | info.setIp(content.split("- -")[0]);
|
| | | content = content.split("- -")[1];
|
| | | |
| | |
|
| | | int start = content.indexOf("\"");
|
| | | String time = content.substring(0, start).trim().replace("[", "").replace("]", "");
|
| | | info.setDate(parseDate(time));
|
| | | content = content.substring(start, content.length());
|
| | | start = content.indexOf("\"", 1);
|
| | | String url = content.substring(1, start);
|
| | | info.setMethod(url.split(" ")[0]);
|
| | | // 链接
|
| | | info.setUrl(url.split(" ")[1].split("\\?")[0]);
|
| | | // 时间
|
| | | info.setProtocal(url.split(" ")[2]);
|
| | | String[] sts = content.substring(start + 1, content.length()).trim().split(" ");
|
| | | info.setStateCode(Integer.parseInt(sts[0]));
|
| | | info.setTime(sts[1].trim().equalsIgnoreCase("-") ? Integer.MAX_VALUE : Integer.parseInt(sts[1]));
|
| | |
|
| | | return info;
|
| | | } catch (Exception e) {
|
| | | }
|
| | |
|
| | | return null;
|
| | | }
|
| | |
|
| | | private static Date parseDate(String timeDesc) {
|
| | | DateFormat format = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z", Locale.US);
|
| | | try {
|
| | | Date date = format.parse(timeDesc);
|
| | | return date;
|
| | | } catch (ParseException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 求请求时间的平均值
|
| | | * |
| | | * @param list
|
| | | * @return
|
| | | */
|
| | | public static int getTimeAvg(List<HttpRequestInfo> list) {
|
| | | int count = 0;
|
| | | long total = 0;
|
| | | for (HttpRequestInfo info : list) {
|
| | | // 去除最大数值与最小数值
|
| | | if (info.getTime() > 0 && info.getTime() < Integer.MAX_VALUE) {
|
| | | total += info.getTime();
|
| | | count++;
|
| | | }
|
| | | }
|
| | | if (count <= 0)
|
| | | return 0;
|
| | |
|
| | | return (int) (total / count);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取方差
|
| | | * |
| | | * @param list
|
| | | * @return
|
| | | */
|
| | | public static int getVariance(List<HttpRequestInfo> list) {
|
| | | int avg = getTimeAvg(list);
|
| | | long total = 0;
|
| | | for (HttpRequestInfo info : list) {
|
| | | total += Math.pow(info.getTime() - avg, 2);
|
| | | }
|
| | | return (int) (total / list.size());
|
| | | }
|
| | |
|
| | | }
|
| | | package org.yeshi.utils.statistic.http; |
| | | |
| | | import java.text.DateFormat; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Locale; |
| | | |
| | | public class LocalhostAccessLogUtil { |
| | | |
| | | /** |
| | | * 将单行日志文件解析为对象 |
| | | * |
| | | * @param content |
| | | * @return |
| | | */ |
| | | public static HttpRequestInfo parseTomcatSingleLine(String content) { |
| | | |
| | | try { |
| | | HttpRequestInfo info = new HttpRequestInfo(); |
| | | info.setIp(content.split("- -")[0]); |
| | | content = content.split("- -")[1]; |
| | | |
| | | |
| | | int start = content.indexOf("\""); |
| | | String time = content.substring(0, start).trim().replace("[", "").replace("]", ""); |
| | | info.setDate(parseDate(time)); |
| | | content = content.substring(start, content.length()); |
| | | start = content.indexOf("\"", 1); |
| | | String url = content.substring(1, start); |
| | | info.setMethod(url.split(" ")[0]); |
| | | // 链接 |
| | | info.setUrl(url.split(" ")[1].split("\\?")[0]); |
| | | // 时间 |
| | | info.setProtocal(url.split(" ")[2]); |
| | | String[] sts = content.substring(start + 1, content.length()).trim().split(" "); |
| | | info.setStateCode(Integer.parseInt(sts[0])); |
| | | info.setTime(sts[1].trim().equalsIgnoreCase("-") ? Integer.MAX_VALUE : Integer.parseInt(sts[1])); |
| | | |
| | | return info; |
| | | } catch (Exception e) { |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | private static Date parseDate(String timeDesc) { |
| | | DateFormat format = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z", Locale.US); |
| | | try { |
| | | Date date = format.parse(timeDesc); |
| | | return date; |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 求请求时间的平均值 |
| | | * |
| | | * @param list |
| | | * @return |
| | | */ |
| | | public static int getTimeAvg(List<HttpRequestInfo> list) { |
| | | int count = 0; |
| | | long total = 0; |
| | | for (HttpRequestInfo info : list) { |
| | | // 去除最大数值与最小数值 |
| | | if (info.getTime() > 0 && info.getTime() < Integer.MAX_VALUE) { |
| | | total += info.getTime(); |
| | | count++; |
| | | } |
| | | } |
| | | if (count <= 0) |
| | | return 0; |
| | | |
| | | return (int) (total / count); |
| | | } |
| | | |
| | | /** |
| | | * 获取方差 |
| | | * |
| | | * @param list |
| | | * @return |
| | | */ |
| | | public static int getVariance(List<HttpRequestInfo> list) { |
| | | int avg = getTimeAvg(list); |
| | | long total = 0; |
| | | for (HttpRequestInfo info : list) { |
| | | total += Math.pow(info.getTime() - avg, 2); |
| | | } |
| | | return (int) (total / list.size()); |
| | | } |
| | | |
| | | } |