1 19 20 package org.netbeans.modules.java.source.usages; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.FileReader ; 26 import java.io.IOException ; 27 import java.io.OutputStreamWriter ; 28 import java.io.PrintWriter ; 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.LinkedList ; 34 import java.util.List ; 35 import java.util.Map ; 36 import java.util.Random ; 37 import java.util.Set ; 38 import java.util.TreeSet ; 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 50 public class LucenePerformanceTest extends NbTestCase { 51 52 53 public LucenePerformanceTest (final String name) { 54 super (name); 55 } 56 57 58 protected @Override void setUp() throws Exception { 59 super.setUp(); 60 this.clearWorkDir(); 61 FSDirectory.setDisableLocks(true); 63 } 65 66 public void testPerformance () throws Exception { 67 final File indexDir = new File (this.getWorkDir(),"index"); 68 indexDir.mkdirs(); 69 final Index index = LuceneIndex.create (indexDir); 70 Map <String ,List <String >> data = prepareData(20000,1000,50); 71 long startTime = System.currentTimeMillis(); 74 index.store (data, Collections.<String >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 <String > result = new HashSet <String >(); 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 <ElementHandle<TypeElement>> result2 = new HashSet <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 <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 <String , List <String >> prepareData (final int count, final int pkgLimit, final int refLimit) { 117 final Map <String ,List <String >> result = new HashMap <String ,List <String >> (); 118 final List <String > refs = new LinkedList <String >(); 119 final Random r = new Random (System.currentTimeMillis()); 120 for (int i=0; i<count; i++) { 121 final int refCount = r.nextInt(refLimit); 122 final List <String > l = new ArrayList <String >(refCount); 123 for (int j=0; j<refCount && refs.size()>0; j++) { 124 int index = r.nextInt(refs.size()); 125 String s = refs.get (index) + "+++++"; 126 if (!l.contains(s)) { 127 l.add(s); 128 } 129 } 130 String 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 file, Map <String , List <String >> data) throws IOException { 139 PrintWriter out = new PrintWriter (new OutputStreamWriter (new FileOutputStream (file))); 140 try { 141 for (Map.Entry <String ,List <String >> e : data.entrySet()) { 142 String key = e.getKey(); 143 List <String > value = e.getValue(); 144 out.println(key); 145 for (String v : value) { 146 out.println("\t"+v); 147 } 148 } 149 } finally { 150 out.close (); 151 } 152 } 153 154 private static void storeResult (File file, Set <String >data) throws IOException { 155 PrintWriter out = new PrintWriter (new OutputStreamWriter (new FileOutputStream (file))); 156 try { 157 for (String s : data) { 158 out.println(s); 159 } 160 } finally { 161 out.close (); 162 } 163 } 164 165 private static Map <String ,List <String >> loadData (File file) throws IOException { 166 assert file != null && file.exists() && file.canRead(); 167 final Map <String ,List <String >> result = new HashMap <String ,List <String >> (); 168 BufferedReader in = new BufferedReader (new FileReader (file)); 169 try { 170 String key = null; 171 List <String > value = null; 172 String 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 <String >(); 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 |