You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProxyFactoryPerformanceTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package testproxy;
  2. import java.io.Serializable;
  3. import java.lang.reflect.Method;
  4. /**
  5. import net.sf.cglib.proxy.CallbackFilter;
  6. import net.sf.cglib.proxy.Enhancer;
  7. import net.sf.cglib.proxy.InvocationHandler;
  8. import net.sf.cglib.proxy.NoOp;
  9. */
  10. import javassist.util.proxy.MethodFilter;
  11. import javassist.util.proxy.MethodHandler;
  12. import javassist.util.proxy.ProxyFactory;
  13. import junit.framework.Test;
  14. import junit.framework.TestCase;
  15. import junit.framework.TestSuite;
  16. import junit.textui.TestRunner;
  17. @SuppressWarnings({"rawtypes","unchecked", "unused"})
  18. public class ProxyFactoryPerformanceTest extends TestCase {
  19. public static final int COUNT = 100;
  20. public static final int MAX_THREADS = 30;
  21. static Throwable error = null;
  22. public ProxyFactoryPerformanceTest() {}
  23. public ProxyFactoryPerformanceTest(String name) {
  24. super(name);
  25. }
  26. public void testJavassist() throws Throwable {
  27. callCreateClass("javassist", ProxyMaker.class);
  28. }
  29. /**
  30. public void testCglib() throws Exception {
  31. callCreateClass("cglib", EnhancerUser.class);
  32. }
  33. */
  34. public void callCreateClass(String translator, Class cl) throws Throwable {
  35. error = null;
  36. Thread[] threads = new Thread[MAX_THREADS];
  37. for (int i = 0; i < threads.length; ++i) {
  38. threads[i] = (Thread)cl.getDeclaredConstructor().newInstance();
  39. }
  40. long time = System.currentTimeMillis();
  41. for (int i = 0; i < threads.length; ++i) {
  42. threads[i].start();
  43. }
  44. for (int i = 0; i < threads.length; ++i) {
  45. threads[i].join();
  46. }
  47. time = System.currentTimeMillis() - time;
  48. System.out.println("ProxyFactoryPerformanceTest: " + translator + " time: " + time);
  49. if (error != null)
  50. throw error;
  51. }
  52. public static Test suite() {
  53. return new TestSuite(ProxyFactoryPerformanceTest.class);
  54. }
  55. public static void callOnce() {
  56. try {
  57. Thread t = new ProxyMaker();
  58. t.start();
  59. t.join();
  60. }
  61. catch (InterruptedException e) {
  62. e.printStackTrace();
  63. }
  64. System.out.println("** Done");
  65. }
  66. public static void main(String[] args) {
  67. // callOnce();
  68. ProxyFactory.useCache = args.length == 0;
  69. TestRunner.run(suite());
  70. }
  71. }
  72. @SuppressWarnings({"rawtypes","unused"})
  73. class ProxyMaker extends Thread implements MethodHandler {
  74. private static final MethodFilter FINALIZE_FILTER = new MethodFilter() {
  75. public boolean isHandled(Method m) {
  76. // skip finalize methods
  77. return !( m.getParameterTypes().length == 0 && m.getName().equals( "finalize" ) );
  78. }
  79. };
  80. public void run() {
  81. for (int i = 0; i < ProxyFactoryPerformanceTest.COUNT; ++i) {
  82. callCreateClass();
  83. }
  84. }
  85. public void callCreateClass() {
  86. try {
  87. ProxyFactory factory = new ProxyFactory();
  88. factory.setSuperclass(SampleBean.class);
  89. factory.setInterfaces(SampleBean.class.getInterfaces());
  90. factory.setFilter(FINALIZE_FILTER);
  91. // factory.setHandler(this);
  92. Class proxyClass = factory.createClass();
  93. //System.out.println("proxy name: " + proxyClass.getName());
  94. } catch (Throwable e) {
  95. e.printStackTrace();
  96. ProxyFactoryPerformanceTest.error = e;
  97. }
  98. }
  99. public Object invoke(Object arg0, Method arg1, Method arg2, Object[] arg3) throws Throwable {
  100. return null;
  101. }
  102. }
  103. /**
  104. class EnhancerUser extends Thread implements InvocationHandler {
  105. private static final CallbackFilter FINALIZE_FILTER = new CallbackFilter() {
  106. public int accept(Method method) {
  107. if ( method.getParameterTypes().length == 0 && method.getName().equals("finalize") ){
  108. return 1;
  109. }
  110. else {
  111. return 0;
  112. }
  113. }
  114. };
  115. public void run() {
  116. for (int i = 0; i < ProxyFactoryPerformanceTest.COUNT; ++i) {
  117. callCreateClass();
  118. }
  119. }
  120. public void callCreateClass() {
  121. try {
  122. Enhancer enhancer = new Enhancer();
  123. enhancer.setSuperclass(SampleBean.class);
  124. enhancer.setInterfaces(SampleBean.class.getInterfaces());
  125. enhancer.setCallbackTypes(new Class[] { InvocationHandler.class, NoOp.class });
  126. enhancer.setCallbackFilter(FINALIZE_FILTER);
  127. enhancer.setInterceptDuringConstruction(false);
  128. // TODO
  129. enhancer.setUseCache(false);
  130. enhancer.setUseFactory(false);
  131. Class proxyClass = enhancer.createClass();
  132. //System.out.println("proxy name: " + proxyClass.getName());
  133. } catch (Exception e) {
  134. e.printStackTrace();
  135. }
  136. }
  137. public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable {
  138. return null;
  139. }
  140. }
  141. */
  142. class SampleBean implements Serializable {
  143. /** default serialVersionUID */
  144. private static final long serialVersionUID = 1L;
  145. long oid;
  146. int version;
  147. SampleBean bean;
  148. public void setOid(long _oid) {
  149. oid = _oid;
  150. }
  151. public long getOid() {
  152. return oid;
  153. }
  154. public void setVersion(int _ver) {
  155. version = _ver;
  156. }
  157. public int getVersion() {
  158. return version;
  159. }
  160. public void setBean(SampleBean _bean) {
  161. bean = _bean;
  162. }
  163. public SampleBean getBean() {
  164. return bean;
  165. }
  166. }