add mybatis support

This commit is contained in:
huoyo 2021-04-22 20:41:31 +08:00
parent da4d5f59ac
commit 3f368a6863
10 changed files with 68 additions and 15 deletions

View File

@ -16,7 +16,7 @@ http://huoyo.gitee.io/ko-time/
缺点:
> * 对项目中每个方法进行监控,在性能层面会有一定的影响,建议在开发阶段使用
> * 由于对项目中每个方法进行监控,在性能层面会有一影响,建议在开发阶段使用
#### 使用教程
@ -26,7 +26,7 @@ http://huoyo.gitee.io/ko-time/
<dependency>
<groupId>cn.langpy</groupId>
<artifactId>ko-time</artifactId>
<version>1.7</version>
<version>1.8</version>
</dependency>
```
@ -45,10 +45,9 @@ koTime.log.enable=false # 是否开启控制输出非必填默认false
koTime.log.language=chinese # 控制台输出语言english/chinese非必填默认chinese
koTime.time.threshold=800.0 # 时间阈值用于前端展示大于阈值显示红色小于阈值显示绿色非必填默认800
koTime.pointcut=execution(* com.huoyo.demo.controller.*(..)) || execution(* com.huoyo.demo.service.*(..)) 需要监测的切面范围比如监测controller层和service层参考aop的@pointcut v1.4开始加入的功能用来替代下面的步骤3
koTime.ui.template=thymeleaf 前端页面模板默认为freemarker可选thymeleaf 与引入的pom依赖对应
koTime.ui.template=thymeleaf 前端页面模板,非必填,默认为freemarker可选thymeleaf 与引入的pom依赖对应
```
`注意暂不支持mybatis的mapper监测`
3. 新建一个类实现ComputeTimeHandlerInterface并在 @Pointcut 写入 需要监测的范围
@ -122,6 +121,8 @@ public class RunTimeHandler implements ComputeTimeHandlerInterface {
> V1.7修复未调用接口时No value present异常
> V1.8支持Mybatis的Mapper监测、新增最大/最小运行时间、修复小数位数过长页面边界溢出的bug
#### 特别说明
1.本项目使用java8开发其他版本未曾试验如有什么bug还请告知

View File

@ -6,7 +6,7 @@
<groupId>cn.langpy</groupId>
<artifactId>ko-time</artifactId>
<version>1.7</version>
<version>1.8</version>
<name>koTime</name>
<description>koTime</description>
<licenses>

View File

@ -12,7 +12,8 @@ public class RunTimeHandler implements MethodInterceptor {
long begin = System.nanoTime();
Object obj=invocation.proceed();
long end =System.nanoTime();
String packName = invocation.getThis().getClass().getPackage().getName();
// String packName = invocation.getThis().getClass().getPackage().getName();
String packName = invocation.getMethod().getDeclaringClass().getPackage().getName();
RunTimeNode parent = InvokeService.getParentRunTimeNode(packName);
RunTimeNode current = InvokeService.getCurrentRunTimeNode(invocation,((end-begin)/1000000.0));
InvokeService.createGraph(parent,current);

View File

@ -11,6 +11,8 @@ public class RunTimeNode implements Comparable<RunTimeNode> {
private String className;
private String methodName;
private Double avgRunTime = 0.0;
private Double maxRunTime = 0.0;
private Double minRunTime = 10000.0;
private Double value = 0.0;
private String avgRunTimeUnit = "ms";
private MethodType methodType;
@ -79,6 +81,21 @@ public class RunTimeNode implements Comparable<RunTimeNode> {
this.avgRunTime = avgRunTime;
}
public Double getMaxRunTime() {
return maxRunTime;
}
public void setMaxRunTime(Double maxRunTime) {
this.maxRunTime = maxRunTime;
}
public Double getMinRunTime() {
return minRunTime;
}
public void setMinRunTime(Double minRunTime) {
this.minRunTime = minRunTime;
}
public List<RunTimeNode> getChildren() {
return children;
@ -112,6 +129,8 @@ public class RunTimeNode implements Comparable<RunTimeNode> {
"className='" + className + '\'' +
", methodName='" + methodName + '\'' +
", avgRunTime=" + avgRunTime +
", maxRunTime=" + maxRunTime +
", minRunTime=" + minRunTime +
", avgRunTimeUnit='" + avgRunTimeUnit + '\'' +
", methodType=" + methodType +
", children=" + children +

View File

@ -6,6 +6,7 @@ import cn.langpy.kotime.util.Common;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
@ -33,13 +34,18 @@ public class InvokeService {
}
public static RunTimeNode getCurrentRunTimeNode(MethodInvocation pjp, Double runTime) {
String className = pjp.getThis().getClass().getName();
// String className = pjp.getThis().getClass().getName();
String className = pjp.getMethod().getDeclaringClass().getName();
String methodName = pjp.getMethod().getName();
RunTimeNode current = new RunTimeNode();
current.setName(className.substring(className.lastIndexOf(".")+1)+"."+methodName);
current.setClassName(className);
current.setMethodName(methodName);
BigDecimal bg = new BigDecimal(runTime);
runTime = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
current.setAvgRunTime(runTime);
current.setMaxRunTime(runTime);
current.setMinRunTime(runTime);
current.setChildren(new ArrayList<>());
current.setMethodType(Common.getMethodType(pjp));
return current;

View File

@ -5,6 +5,7 @@ import cn.langpy.kotime.util.Context;
import cn.langpy.kotime.util.GraphMap;
import cn.langpy.kotime.util.MethodType;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@ -13,7 +14,16 @@ public class RunTimeNodeService {
public static void addOrUpdate(String key, RunTimeNode runTimeNode) {
if (GraphMap.containsKey(key)) {
GraphMap.get(key).setAvgRunTime(runTimeNode.getAvgRunTime());
RunTimeNode oldNode = GraphMap.get(key);
if (0 == oldNode.getAvgRunTime()) {
GraphMap.get(key).setAvgRunTime((runTimeNode.getAvgRunTime()));
} else {
BigDecimal bg = new BigDecimal((runTimeNode.getAvgRunTime()+oldNode.getAvgRunTime())/2.0);
double avg = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
GraphMap.get(key).setAvgRunTime(avg);
}
GraphMap.get(key).setMaxRunTime(runTimeNode.getMaxRunTime()>oldNode.getMaxRunTime()?runTimeNode.getMaxRunTime():oldNode.getMaxRunTime());
GraphMap.get(key).setMinRunTime(runTimeNode.getMinRunTime()<oldNode.getMinRunTime()?runTimeNode.getMinRunTime():oldNode.getMinRunTime());
}else{
GraphMap.put(key,runTimeNode);
}
@ -57,8 +67,18 @@ public class RunTimeNodeService {
public static void updateChildren(RunTimeNode child, List<RunTimeNode> hisRunTimeNodeChildren) {
int hisLength = hisRunTimeNodeChildren.size();
for (int i = 0; i < hisLength; i++) {
if (hisRunTimeNodeChildren.get(i)==child) {
child.setAvgRunTime((child.getAvgRunTime()+hisRunTimeNodeChildren.get(i).getAvgRunTime())/2.0);
RunTimeNode hisRunTimeNodeChild = hisRunTimeNodeChildren.get(i);
if (hisRunTimeNodeChild.equals(child)) {
double avg = (child.getAvgRunTime()+hisRunTimeNodeChild.getAvgRunTime())/2.0;
BigDecimal bg = new BigDecimal(avg);
avg = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
child.setAvgRunTime(avg);
if (hisRunTimeNodeChild.getMaxRunTime()>child.getMaxRunTime()) {
child.setMaxRunTime(hisRunTimeNodeChild.getMaxRunTime());
}
if (hisRunTimeNodeChild.getMinRunTime()<child.getMinRunTime()) {
child.setMinRunTime(hisRunTimeNodeChild.getMinRunTime());
}
hisRunTimeNodeChildren.set(i,child) ;
break;
}
@ -80,6 +100,8 @@ public class RunTimeNodeService {
Double max = controllerApis.stream().map(api->api.getAvgRunTime()).max(Double::compareTo).get();
Double min = controllerApis.stream().map(api->api.getAvgRunTime()).min(Double::compareTo).get();
Double avg = controllerApis.stream().map(api->api.getAvgRunTime()).collect(Collectors.averagingDouble(Double::doubleValue));
BigDecimal bg = new BigDecimal(avg);
avg = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
systemStatistic.setMaxRunTime(max);
systemStatistic.setMinRunTime(min);
systemStatistic.setAvgRunTime(avg);

View File

@ -36,7 +36,7 @@ public class Common {
}else if (targetClass.getAnnotation(Repository.class)!=null) {
return MethodType.Dao;
}
String className = pjp.getThis().getClass().getName().toLowerCase();
String className = pjp.getMethod().getDeclaringClass().getName().toLowerCase();
if (className.contains("controller")) {
return MethodType.Controller;
}else if (className.contains("service")) {

View File

@ -30,9 +30,13 @@ function getOption(data,threshold){
'{'+bg+'| 指标}',
' {aa|}方法:'+params.name+" ",
'{hr|}',
' {aa|}耗时: '+params.data.avgRunTime+" ms ",
' {aa|}类型: '+params.data.methodType+" ",
'{hr|}',
' {aa|}类型: '+params.data.methodType+" "
' {aa|}平均耗时: '+params.data.avgRunTime+" ms ",
'{hr|}',
' {aa|}最大耗时: '+params.data.maxRunTime+" ms ",
'{hr|}',
' {aa|}最小耗时: '+params.data.minRunTime+" ms "
].join('\n');
},
backgroundColor: '#ddd',

View File

@ -75,7 +75,7 @@
<#else>layui-bg-green
</#if>">平均响应 ${runtime.avgRunTime} 毫秒</span>
</h2>
<div class="layui-colla-content viewer" style="width: 1500px;height:600px;">
<div class="layui-colla-content viewer" style="width: 98%;height:600px;">
</div>
</div>
</#list>

View File

@ -80,7 +80,7 @@
<span th:if="${runtime.avgRunTime ge config.timeThreshold}" class="'layui-badge layui-bg-red" th:text="'平均响应 '+${runtime.avgRunTime}+' 毫秒'"></span>
<span th:if="${runtime.avgRunTime lt config.timeThreshold}" class="'layui-badge layui-bg-green" th:text="'平均响应 '+${runtime.avgRunTime}+' 毫秒'"></span>
</h2>
<div class="layui-colla-content viewer" style="width: 1500px;height:600px;"> </div>
<div class="layui-colla-content viewer" style="width: 98%;height:600px;"> </div>
</div>
</div>