KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > lookup > ProxyLookupTest


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.lookup;
21
22 import org.netbeans.performance.Benchmark;
23 import java.util.*;
24 import org.openide.util.Lookup;
25 import org.openide.util.lookup.*;
26
27 /**
28  * Comparison beween tree lookup structure and flat lookup structure.
29  * Uses three different Lookup configurations:
30  * <UL><LI>ProxyLookup containing 16 InstanceLookups
31  * <LI>ProxyLookup containing 2 ProxyLookups of 8 InstanceLookups each
32  * <LI>ProxyLookup of 2 ProxyLookups of 2 ProxyLookups of 4 InstanceLookups each
33  * </UL>
34  */

35 public class ProxyLookupTest extends Benchmark {
36
37     public ProxyLookupTest(String JavaDoc name) {
38         super( name, new String JavaDoc[] {"tree3", "tree2", "flat"} );
39     }
40
41     private Lookup lookup;
42     
43     protected void setUp() {
44         String JavaDoc type = (String JavaDoc)getArgument();
45     if("tree3".equals(type)) {
46         lookup = new ProxyLookup( new Lookup[] {
47         new ProxyLookup( new Lookup[] {
48             createProxy(4),
49             createProxy(4)
50         }),
51         new ProxyLookup( new Lookup[] {
52             createProxy(4),
53             createProxy(4)
54         })
55         });
56     } else if("tree2".equals(type)) {
57         lookup = new ProxyLookup( new Lookup[] {
58         createProxy(8),
59         createProxy(8)
60         });
61     } else {
62         lookup = createProxy(16);
63     }
64     }
65     
66     private Lookup createOne() {
67     InstanceContent ic = new InstanceContent();
68     ic.add(new Object JavaDoc());
69     ic.add("");
70     return new AbstractLookup(ic);
71     }
72     
73     private Lookup createProxy(int subs) {
74         Lookup[] delegates = new Lookup[subs];
75     for(int i=0; i<subs; i++) delegates[i] = createOne();
76     return new ProxyLookup(delegates);
77     }
78     
79     protected void tearDown() {
80         lookup=null;
81     }
82
83     public void testLookupObject() throws Exception JavaDoc {
84         int count = getIterationCount();
85
86         while( count-- > 0 ) {
87             // do the stuff here,
88
lookup.lookup(Object JavaDoc.class);
89         }
90     }
91
92     public void testLookupString() throws Exception JavaDoc {
93         int count = getIterationCount();
94
95         while( count-- > 0 ) {
96             // do the stuff here,
97
lookup.lookup(String JavaDoc.class);
98         }
99     }
100
101     public void testAllInstances() throws Exception JavaDoc {
102         int count = getIterationCount();
103     Lookup.Result result = lookup.lookup(new Lookup.Template(String JavaDoc.class));
104
105         while( count-- > 0 ) {
106             // do the stuff here,
107
Collection c = result.allInstances();
108         }
109     }
110
111     public void testIterateInstances() throws Exception JavaDoc {
112         int count = getIterationCount();
113     Lookup.Result result = lookup.lookup(new Lookup.Template(String JavaDoc.class));
114
115         while( count-- > 0 ) {
116             // do the stuff here,
117
Iterator i = result.allInstances().iterator();
118         while (i.hasNext()) i.next();
119         }
120     }
121
122     public static void main( String JavaDoc[] args ) {
123     junit.textui.TestRunner.run( new junit.framework.TestSuite( ProxyLookupTest.class ) );
124     }
125 }
126
Popular Tags