KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > usages > LucenePerformanceTest


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.netbeans.modules.java.source.usages;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStreamWriter JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Random JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.TreeSet JavaDoc;
39 import javax.lang.model.element.TypeElement;
40 import org.apache.lucene.store.FSDirectory;
41 import org.netbeans.api.java.source.ClassIndex.NameKind;
42 import org.netbeans.api.java.source.ElementHandle;
43 import org.netbeans.junit.NbTestCase;
44 import org.netbeans.modules.java.source.usages.ResultConvertor;
45
46 /**
47  *
48  * @author Tomas Zezula
49  */

50 public class LucenePerformanceTest extends NbTestCase {
51     
52     /** Creates a new instance of LucenePerformanceTest */
53     public LucenePerformanceTest (final String JavaDoc name) {
54         super (name);
55     }
56     
57     
58     protected @Override JavaDoc void setUp() throws Exception JavaDoc {
59         super.setUp();
60     this.clearWorkDir();
61         //jlahoda: disabling Lucene locks for tests (hopefully correct):
62
FSDirectory.setDisableLocks(true);
63         //Prepare indeces
64
}
65     
66     public void testPerformance () throws Exception JavaDoc {
67         final File JavaDoc indexDir = new File JavaDoc (this.getWorkDir(),"index");
68         indexDir.mkdirs();
69         final Index index = LuceneIndex.create (indexDir);
70         Map JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> data = prepareData(20000,1000,50);
71 // Map<String,List<String>> data = loadData(new File ("/tmp/data"));
72
// storeData(new File ("/tmp/data"),data);
73
long startTime = System.currentTimeMillis();
74         index.store (data, Collections.<String JavaDoc>emptySet());
75         long endTime = System.currentTimeMillis();
76         long delta = (endTime-startTime);
77         System.out.println("Indexing: " + delta);
78         if (delta > 60000) {
79             assertTrue("Indexing took too much time: " +delta+ "ms",false);
80         }
81         
82         
83         Set JavaDoc<String JavaDoc> result = new HashSet JavaDoc<String JavaDoc>();
84         startTime = System.currentTimeMillis();
85         index.getPackageNames("",true,result);
86         endTime = System.currentTimeMillis();
87         delta = (endTime-startTime);
88         System.out.println("Packages: " + delta);
89         if (delta > 500) {
90             assertTrue("All packages took too much time: " +delta+ "ms",false);
91         }
92         
93         
94         Set JavaDoc<ElementHandle<TypeElement>> result2 = new HashSet JavaDoc<ElementHandle<TypeElement>>();
95         startTime = System.currentTimeMillis();
96         index.getDeclaredTypes("", NameKind.PREFIX,ResultConvertor.elementHandleConvertor(),result2);
97         endTime = System.currentTimeMillis();
98         delta = (endTime-startTime);
99         System.out.println("All classes: " + delta);
100         if (delta > 1000) {
101             assertTrue("All classes took too much time: " +delta+ "ms",false);
102         }
103         
104         result2 = new TreeSet JavaDoc<ElementHandle<TypeElement>>();
105         startTime = System.currentTimeMillis();
106         index.getDeclaredTypes("Class7", NameKind.PREFIX,ResultConvertor.elementHandleConvertor(),result2);
107         endTime = System.currentTimeMillis();
108         delta = (endTime-startTime);
109         System.out.println("Prefix classes: " + delta + " size: " + result.size());
110         if (delta > 500) {
111             assertTrue("Some classes took too much time: " +delta+ "ms",false);
112         }
113     }
114     
115     
116     private static Map JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> prepareData (final int count, final int pkgLimit, final int refLimit) {
117         final Map JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> result = new HashMap JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> ();
118         final List JavaDoc<String JavaDoc> refs = new LinkedList JavaDoc<String JavaDoc>();
119         final Random JavaDoc r = new Random JavaDoc (System.currentTimeMillis());
120         for (int i=0; i<count; i++) {
121             final int refCount = r.nextInt(refLimit);
122             final List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>(refCount);
123             for (int j=0; j<refCount && refs.size()>0; j++) {
124                 int index = r.nextInt(refs.size());
125                 String JavaDoc s = refs.get (index) + "+++++";
126                 if (!l.contains(s)) {
127                     l.add(s);
128                 }
129             }
130             String JavaDoc name = String.format("pkg%d.Class%d",r.nextInt(pkgLimit),i);
131             result.put(name,l);
132             refs.add (name);
133         }
134         return result;
135     }
136     
137     
138     private static void storeData (File JavaDoc file, Map JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> data) throws IOException JavaDoc {
139         PrintWriter JavaDoc out = new PrintWriter JavaDoc (new OutputStreamWriter JavaDoc (new FileOutputStream JavaDoc (file)));
140         try {
141             for (Map.Entry JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> e : data.entrySet()) {
142                 String JavaDoc key = e.getKey();
143                 List JavaDoc<String JavaDoc> value = e.getValue();
144                 out.println(key);
145                 for (String JavaDoc v : value) {
146                     out.println("\t"+v);
147                 }
148             }
149         } finally {
150             out.close ();
151         }
152     }
153     
154     private static void storeResult (File JavaDoc file, Set JavaDoc<String JavaDoc>data) throws IOException JavaDoc {
155         PrintWriter JavaDoc out = new PrintWriter JavaDoc (new OutputStreamWriter JavaDoc (new FileOutputStream JavaDoc (file)));
156         try {
157             for (String JavaDoc s : data) {
158                 out.println(s);
159             }
160         } finally {
161             out.close ();
162         }
163     }
164     
165     private static Map JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> loadData (File JavaDoc file) throws IOException JavaDoc {
166         assert file != null && file.exists() && file.canRead();
167         final Map JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> result = new HashMap JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> ();
168         BufferedReader JavaDoc in = new BufferedReader JavaDoc (new FileReader JavaDoc (file));
169         try {
170             String JavaDoc key = null;
171             List JavaDoc<String JavaDoc> value = null;
172             String JavaDoc line;
173             while ((line = in.readLine()) != null) {
174                 if (line.charAt(0) != '\t') {
175                     if (key != null) {
176                         result.put(key,value);
177                     }
178                     key = line;
179                     value = new ArrayList JavaDoc<String JavaDoc>();
180                 }
181                 else {
182                     value.add(line.substring(1));
183                 }
184             }
185         } finally {
186             in.close();
187         }
188         return result;
189     }
190      
191     
192 }
193
Popular Tags