public class performancemonitor {
// 通过一个threadlocal保存与调用线程相关的性能监视信息
private static threadlocal<methodperformance> performancerecord = new threadlocal<methodperformance>();
// 启动对某一目标方法的性能监视
public static void begin(string method) {
system.out.println("begin monitor...");
methodperformance mp = new methodperformance(method);
performancerecord.set(mp);
}
public static void end() {
system.out.println("end monitor...");
methodperformance mp = performancerecord.get();
// 打印出方法性能监视的结果信息
mp.printperformance();
}
}
用于记录性能监控信息:
public class performancemonitor {
// 通过一个threadlocal保存与调用线程相关的性能监视信息
private static threadlocal<methodperformance> performancerecord = new threadlocal<methodperformance>();
// 启动对某一目标方法的性能监视
public static void begin(string method) {
system.out.println("begin monitor...");
methodperformance mp = new methodperformance(method);
performancerecord.set(mp);
}
public static void end() {
system.out.println("end monitor...");
methodperformance mp = performancerecord.get();
// 打印出方法性能监视的结果信息
mp.printperformance();
}
}
1、jdk动态代理
public class performancemonitor {
// 通过一个threadlocal保存与调用线程相关的性能监视信息
private static threadlocal<methodperformance> performancerecord = new threadlocal<methodperformance>();
// 启动对某一目标方法的性能监视
public static void begin(string method) {
system.out.println("begin monitor...");
methodperformance mp = new methodperformance(method);
performancerecord.set(mp);
}
public static void end() {
system.out.println("end monitor...");
methodperformance mp = performancerecord.get();
// 打印出方法性能监视的结果信息
mp.printperformance();
}
}
public class forumservicetest {
@test
public void proxy() {
forumservice forumservice = new forumserviceimpl();
performancehandler handler = new performancehandler(forumservice);
forumservice proxy = (forumservice) proxy.newproxyinstance(forumservice.getclass().getclassloader(),
forumservice.getclass().getinterfaces(), handler);
proxy.removeforum(10);
proxy.removetopic(1012);
}
}
得到以下输出信息:
begin monitor...
模拟删除forum记录:10
end monitor...
com.hand.proxy.forumserviceimpl.removeforum花费21毫秒
begin monitor...
模拟删除topic记录:1012
end monitor...
com.hand.proxy.forumserviceimpl.removetopic花费21毫秒