001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.util;
043:
044: import java.beans.PropertyChangeListener;
045: import java.lang.ref.WeakReference;
046: import java.lang.reflect.*;
047: import java.lang.reflect.Method;
048: import java.util.Arrays;
049: import java.util.Vector;
050: import junit.framework.*;
051: import org.netbeans.junit.*;
052:
053: public class WeakListenersSpeedTest extends NbTestCase implements
054: java.lang.reflect.InvocationHandler {
055: private static java.util.HashMap times = new java.util.HashMap();
056: private long time;
057:
058: private static final int COUNT = 100000;
059:
060: public WeakListenersSpeedTest(java.lang.String testName) {
061: super (testName);
062: }
063:
064: public static void main(java.lang.String[] args) {
065: junit.textui.TestRunner.run(new NbTestSuite(
066: WeakListenersSpeedTest.class));
067: }
068:
069: protected void setUp() throws Exception {
070: for (int i = 0; i < 10; i++) {
071: try {
072: super .runTest();
073: } catch (Throwable t) {
074: }
075: }
076:
077: time = System.currentTimeMillis();
078: }
079:
080: protected void tearDown() throws Exception {
081: long now = System.currentTimeMillis();
082: times.put(getName(), new Long(now - time));
083:
084: assertNumbersAreSane();
085: }
086:
087: public void testThisIsTheBasicBenchmark() throws Exception {
088: // RequestProcessor is a class with string argument that does
089: // nearly nothing in its constructor, so it should be good reference
090: // point
091: Constructor c = org.openide.util.RequestProcessor.class
092: .getConstructor(new Class[] { String.class });
093: String orig = "Ahoj";
094: for (int i = 0; i < COUNT; i++) {
095: // this is slow
096: //java.lang.reflect.Proxy.newProxyInstance (getClass().getClassLoader(), new Class[] { Runnable.class }, this);
097: c.newInstance(new Object[] { orig });
098: }
099: }
100:
101: public void testCreateListeners() {
102: java.beans.PropertyChangeListener l = new java.beans.PropertyChangeListener() {
103: public void propertyChange(java.beans.PropertyChangeEvent ev) {
104: }
105: };
106:
107: for (int i = 0; i < COUNT; i++) {
108: org.openide.util.WeakListeners.create(
109: java.beans.PropertyChangeListener.class, l, this );
110: }
111: }
112:
113: public void testMoreTypesVariousListeners() {
114: class X implements java.beans.PropertyChangeListener,
115: java.beans.VetoableChangeListener {
116: public void propertyChange(java.beans.PropertyChangeEvent ev) {
117: }
118:
119: public void vetoableChange(java.beans.PropertyChangeEvent ev) {
120: }
121: }
122: ;
123:
124: X x = new X();
125: for (int i = 0; i < COUNT / 2; i++) {
126: org.openide.util.WeakListeners.create(
127: java.beans.PropertyChangeListener.class, x, this );
128: org.openide.util.WeakListeners.create(
129: java.beans.VetoableChangeListener.class, x, this );
130: }
131: }
132:
133: /** Compares that the numbers are in sane bounds */
134: private void assertNumbersAreSane() {
135: StringBuffer error = new StringBuffer();
136: {
137: java.util.Iterator it = times.entrySet().iterator();
138: while (it.hasNext()) {
139: java.util.Map.Entry en = (java.util.Map.Entry) it
140: .next();
141: error.append("Test ");
142: error.append(en.getKey());
143: error.append(" took ");
144: error.append(en.getValue());
145: error.append(" ms\n");
146: }
147: }
148:
149: long min = Long.MAX_VALUE;
150: long max = Long.MIN_VALUE;
151:
152: {
153: java.util.Iterator it = times.values().iterator();
154: while (it.hasNext()) {
155: Long l = (Long) it.next();
156: if (l.longValue() > max)
157: max = l.longValue();
158: if (l.longValue() < min)
159: min = l.longValue();
160: }
161: }
162:
163: if (min * 5 < max) {
164: fail("Too big differences when various number of shadows is used:\n"
165: + error.toString());
166: }
167:
168: System.err.println(error.toString());
169: }
170:
171: public Object invoke(Object proxy, Method method, Object[] args)
172: throws Throwable {
173: throw new Throwable();
174: }
175:
176: }
|