当前程序是这样的
public void getData() {
//some staffA
otherObj.method1(); //A1
//some staff
otherObj2.method2(); //A2
//some staff
otherObj3.method3(); //A3
this.method4(); //A4
}
使用spring有办法获得方法内部方法执行时间吗?比如说我想知道getData()方法中A1,A2,A3,A4的执行时间
[b]问题补充:[/b]
当前程序是这样的
public void getData() {
//some staffA
otherObj.method1(); //A1
//some staff
otherObj2.method2(); //A2
//some staff
otherObj3.method3(); //A3
this.method4(); //A4
}
使用spring有办法获得方法内部方法执行时间吗?比如说我想知道getData()方法中A1,A2,A3,A4的执行时间
字节写方法类实现,你所说的执行时间是,这个方法执行了多长时间,还是什么时间执行的 ??
我现在用个interceptor监控了整个getData()方法的执行时间,现在就想生成个报告:
getData() 100ms
otherObj.method1(); 耗时20ms
otherObj2.method2(); //耗时20ms
otherObj3.method3(); //耗时20ms
this.method4(); //耗时20ms
这个样子,就是想知道一个方法内具体调用的每个方法的执行时间。
在Spring 里面可以利用AOP实现你说的功能
spring没有这个功能,你需要自己在方法里计算执行时间,方法开始时间减去方法结束时间
这个是做不到的。只能自己去弄了。。
字节写方法类实现,你所说的执行时间是,这个方法执行了多长时间,还是什么时间执行的
用jprofiler监控吧,很专业,很强大
interceptor只能监控到getData级别的方法,如果想具体到里面的方法,是要自己写代码的。
jprofiler可以看每个具体方法的运行时间
[code="java"]
public void getData() {
//some staffA
Long statDate = System.currentTimeMillis();
otherObj.method1(); //A1
Long endDate = System.currentTimeMillis();
System.out.println("直接用string='' 使用时间 : "
+ (endDate - statDate) + " milli seconds");
//some staff
Long statDate1 = System.currentTimeMillis();
otherObj2.method2(); //A2
Long endDate1 = System.currentTimeMillis();
System.out.println("用String str = new String('')使用时间 : "
+ (endDate1 - statDate1) + " milli seconds");
//some staff
otherObj3.method3(); //A3
this.method4(); //A4
}
在把相应的endDate1 - statDate1写入一个变量中,用来获取..
[/code]
用JAVA 动态代理吧。。。实现InvocationHandler
可以在每个方法执行之前获得当前时间。然后在方法执行完成之后在用当前时间减去开始时间。。最后把执行时间累加一下就行了。。