1 19 20 package org.openide.util; 21 22 import java.beans.PropertyChangeListener ; 23 import java.lang.ref.WeakReference ; 24 import java.lang.reflect.*; 25 import java.lang.reflect.Method ; 26 import java.util.Arrays ; 27 import java.util.Vector ; 28 import junit.framework.*; 29 import org.netbeans.junit.*; 30 31 public class WeakListenersSpeedTest extends NbTestCase 32 implements java.lang.reflect.InvocationHandler { 33 private static java.util.HashMap times = new java.util.HashMap (); 34 private long time; 35 36 private static final int COUNT = 100000; 37 38 public WeakListenersSpeedTest (java.lang.String testName) { 39 super(testName); 40 } 41 42 public static void main(java.lang.String [] args) { 43 junit.textui.TestRunner.run(new NbTestSuite(WeakListenersSpeedTest.class)); 44 } 45 46 protected void setUp () throws Exception { 47 for (int i = 0; i < 10; i++) { 48 try { 49 super.runTest (); 50 } catch (Throwable t) { 51 } 52 } 53 54 time = System.currentTimeMillis (); 55 } 56 57 protected void tearDown () throws Exception { 58 long now = System.currentTimeMillis (); 59 times.put (getName (), new Long (now - time)); 60 61 assertNumbersAreSane (); 62 } 63 64 public void testThisIsTheBasicBenchmark () throws Exception { 65 Constructor c = org.openide.util.RequestProcessor.class.getConstructor (new Class [] { String .class }); 69 String orig = "Ahoj"; 70 for (int i = 0; i < COUNT; i++) { 71 c.newInstance (new Object [] { orig }); 74 } 75 } 76 77 78 public void testCreateListeners () { 79 java.beans.PropertyChangeListener l = new java.beans.PropertyChangeListener () { 80 public void propertyChange (java.beans.PropertyChangeEvent ev) { 81 } 82 }; 83 84 for (int i = 0; i < COUNT; i++) { 85 org.openide.util.WeakListeners.create (java.beans.PropertyChangeListener .class, l, this); 86 } 87 } 88 public void testMoreTypesVariousListeners () { 89 class X implements java.beans.PropertyChangeListener , java.beans.VetoableChangeListener { 90 public void propertyChange (java.beans.PropertyChangeEvent ev) { 91 } 92 public void vetoableChange (java.beans.PropertyChangeEvent ev) { 93 } 94 }; 95 96 X x = new X (); 97 for (int i = 0; i < COUNT / 2; i++) { 98 org.openide.util.WeakListeners.create (java.beans.PropertyChangeListener .class, x, this); 99 org.openide.util.WeakListeners.create (java.beans.VetoableChangeListener .class, x, this); 100 } 101 } 102 103 104 105 private void assertNumbersAreSane () { 106 StringBuffer error = new StringBuffer (); 107 { 108 java.util.Iterator it = times.entrySet ().iterator (); 109 while (it.hasNext ()) { 110 java.util.Map.Entry en = (java.util.Map.Entry)it.next (); 111 error.append ("Test "); error.append (en.getKey ()); 112 error.append (" took "); error.append (en.getValue ()); 113 error.append (" ms\n"); 114 } 115 } 116 117 long min = Long.MAX_VALUE; 118 long max = Long.MIN_VALUE; 119 120 { 121 java.util.Iterator it = times.values ().iterator (); 122 while (it.hasNext ()) { 123 Long l = (Long )it.next (); 124 if (l.longValue () > max) max = l.longValue (); 125 if (l.longValue () < min) min = l.longValue (); 126 } 127 } 128 129 if (min * 5 < max) { 130 fail ("Too big differences when various number of shadows is used:\n" + error.toString ()); 131 } 132 133 System.err.println(error.toString ()); 134 } 135 136 public Object invoke (Object proxy, Method method, Object [] args) throws Throwable { 137 throw new Throwable (); 138 } 139 140 } 141 | Popular Tags |