KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > WeakListenersSpeedTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.lang.reflect.*;
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Vector JavaDoc;
28 import junit.framework.*;
29 import org.netbeans.junit.*;
30
31 public class WeakListenersSpeedTest extends NbTestCase
32 implements java.lang.reflect.InvocationHandler JavaDoc {
33     private static java.util.HashMap JavaDoc times = new java.util.HashMap JavaDoc ();
34     private long time;
35
36     private static final int COUNT = 100000;
37
38     public WeakListenersSpeedTest (java.lang.String JavaDoc testName) {
39         super(testName);
40     }
41     
42     public static void main(java.lang.String JavaDoc[] args) {
43         junit.textui.TestRunner.run(new NbTestSuite(WeakListenersSpeedTest.class));
44     }
45     
46     protected void setUp () throws Exception JavaDoc {
47         for (int i = 0; i < 10; i++) {
48             try {
49                 super.runTest ();
50             } catch (Throwable JavaDoc t) {
51             }
52         }
53         
54         time = System.currentTimeMillis ();
55     }
56     
57     protected void tearDown () throws Exception JavaDoc {
58         long now = System.currentTimeMillis ();
59         times.put (getName (), new Long JavaDoc (now - time));
60         
61         assertNumbersAreSane ();
62     }
63     
64     public void testThisIsTheBasicBenchmark () throws Exception JavaDoc {
65         // RequestProcessor is a class with string argument that does
66
// nearly nothing in its constructor, so it should be good reference
67
// point
68
Constructor c = org.openide.util.RequestProcessor.class.getConstructor (new Class JavaDoc[] { String JavaDoc.class });
69         String JavaDoc orig = "Ahoj";
70         for (int i = 0; i < COUNT; i++) {
71             // this is slow
72
//java.lang.reflect.Proxy.newProxyInstance (getClass().getClassLoader(), new Class[] { Runnable.class }, this);
73
c.newInstance (new Object JavaDoc[] { orig });
74         }
75     }
76
77     
78     public void testCreateListeners () {
79         java.beans.PropertyChangeListener JavaDoc l = new java.beans.PropertyChangeListener JavaDoc () {
80             public void propertyChange (java.beans.PropertyChangeEvent JavaDoc ev) {
81             }
82         };
83         
84         for (int i = 0; i < COUNT; i++) {
85             org.openide.util.WeakListeners.create (java.beans.PropertyChangeListener JavaDoc.class, l, this);
86         }
87     }
88     public void testMoreTypesVariousListeners () {
89         class X implements java.beans.PropertyChangeListener JavaDoc, java.beans.VetoableChangeListener JavaDoc {
90             public void propertyChange (java.beans.PropertyChangeEvent JavaDoc ev) {
91             }
92             public void vetoableChange (java.beans.PropertyChangeEvent JavaDoc 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 JavaDoc.class, x, this);
99             org.openide.util.WeakListeners.create (java.beans.VetoableChangeListener JavaDoc.class, x, this);
100         }
101     }
102     
103     
104     /** Compares that the numbers are in sane bounds */
105     private void assertNumbersAreSane () {
106         StringBuffer JavaDoc error = new StringBuffer JavaDoc ();
107         {
108             java.util.Iterator JavaDoc 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 JavaDoc it = times.values ().iterator ();
122             while (it.hasNext ()) {
123                 Long JavaDoc l = (Long JavaDoc)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 JavaDoc invoke (Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
137         throw new Throwable JavaDoc ();
138     }
139     
140 }
141
Popular Tags