importjava.lang.reflect.Method;importjava.util.Calendar;importjava.util.Date;importnet.sf.cglib.beans.BeanMap;importnet.sf.cglib.proxy.Enhancer;importnet.sf.cglib.proxy.MethodInterceptor;importnet.sf.cglib.proxy.MethodProxy;publicclassTestPerformance
{publicstaticvoidmain(String[] args)
{inttimes=10000000;//TestBean(times);//=15//TestCglib(times);//=516//TestBeanMap(times);//=256TestReflection(times);//=11359}publicstaticvoidTestBean(inttimes)
{
MyBean bean=newMyBean();
Date start=Calendar.getInstance().getTime();for(inti=0; i
{
bean.setName("helloworld");
Object v=bean.getName();
}
Date end=Calendar.getInstance().getTime();
System.out.println(end.getTime()-start.getTime());
}publicstaticvoidTestCglib(inttimes)
{
Enhancer enhancer=newEnhancer();
enhancer.setSuperclass(MyBean.class);
enhancer.setCallback(newTestMethodInterceptorImpl());
MyBean my=(MyBean) enhancer.create();
Date start=Calendar.getInstance().getTime();for(inti=0; i
{
my.setName("helloworld");
Object v=my.getName();
}
Date end=Calendar.getInstance().getTime();
System.out.println(end.getTime()-start.getTime());
}publicstaticvoidTestBeanMap(inttimes)
{
MyBean bean=newMyBean();
BeanMap map=BeanMap.create(bean);
Date start=Calendar.getInstance().getTime();for(inti=0; i
{
map.put(bean,"name","helloworld");
Object v=bean.getName();
}
Date end=Calendar.getInstance().getTime();
System.out.println(end.getTime()-start.getTime());
}publicstaticvoidTestReflection(inttimes)
{
MyBean bean=newMyBean();
Class c=MyBean.class;try{
Method get=c.getDeclaredMethod("getName",null);
Method set=c.getDeclaredMethod("setName", String.class);
Date start=Calendar.getInstance().getTime();for(inti=0; i
{
set.invoke(bean,"helloworld");
Object v=get.invoke(bean,null);
}
Date end=Calendar.getInstance().getTime();
System.out.println(end.getTime()-start.getTime());
}catch(Exception ex)
{
}
}
}classTestMethodInterceptorImplimplementsMethodInterceptor
{publicObject intercept(Object obj, Method method, Object[] args,
MethodProxy proxy)throwsThrowable
{returnproxy.invokeSuper(obj, args);
}
}classMyBean
{privateString name;publicString getName()
{returnname;
}publicvoidsetName(String name)
{this.name=name;
}
}