KickJava   Java API By Example, From Geeks To Geeks.

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


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.junit.NbTestCase;
23 import org.netbeans.junit.NbTestSuite;
24
25 import org.openide.util.Lookup;
26
27
28 /**
29  *
30  * @author Petr Nejedly, adapted to test by Jaroslav Tulach
31  */

32 public class SimpleProxyLookupSpeedIssue42244Test extends NbTestCase {
33
34     public SimpleProxyLookupSpeedIssue42244Test (String JavaDoc name) {
35         super (name);
36     }
37
38     /**
39      * @param args the command line arguments
40      */

41     public static void main(String JavaDoc[] args) {
42         junit.textui.TestRunner.run(new NbTestSuite (SimpleProxyLookupSpeedIssue42244Test.class));
43     }
44     
45     public void testCompareTheSpeed () {
46         String JavaDoc content1 = "String1";
47         String JavaDoc content2 = "String2";
48         
49         Lookup fixed1 = Lookups.singleton(content1);
50         Lookup fixed2 = Lookups.singleton(content2);
51         
52         
53         Lookup.Template template = new Lookup.Template(String JavaDoc.class);
54         
55         MyProvider provider = new MyProvider();
56         provider.setLookup(fixed1);
57         
58         Lookup top = Lookups.proxy(provider);
59
60         Lookup.Result r0 = top.lookup(template);
61         r0.allInstances();
62
63         long time = System.currentTimeMillis();
64         top.lookup(template).allInstances();
65         long withOneResult = System.currentTimeMillis() - time;
66
67      
68         java.util.HashSet JavaDoc results = new java.util.HashSet JavaDoc ();
69         for (int i=0; i<10000; i++) {
70             Lookup.Result res = top.lookup (template);
71             results.add (res);
72             res.allInstances();
73         }
74         
75         provider.setLookup(fixed2);
76
77         time = System.currentTimeMillis();
78         top.lookup(template).allInstances();
79         long withManyResults = System.currentTimeMillis() - time;
80         
81         // if the measurement takes less then 10ms, pretend 10ms
82
if (withManyResults < 10) {
83             withManyResults = 10;
84         }
85         if (withOneResult < 10) {
86             withOneResult = 10;
87         }
88
89         if (withManyResults >= 10 * withOneResult) {
90             fail ("With many results the test runs too long.\n With many: " + withManyResults + "\n With one : " + withOneResult);
91         }
92     }
93     
94     private static class MyProvider implements Lookup.Provider {
95         private Lookup lookup;
96         public Lookup getLookup() {
97             return lookup;
98         }
99         
100         void setLookup(Lookup lookup) {
101             this.lookup = lookup;
102         }
103     }
104     
105 }
106
Popular Tags