1 9 10 package org.netbeans.spi.editor.highlighting.performance; 11 12 import java.io.FileInputStream ; 13 import java.io.InputStream ; 14 import java.io.File ; 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 import java.util.Map.Entry; 21 import javax.swing.text.AttributeSet ; 22 import javax.swing.text.Document ; 23 import javax.swing.text.PlainDocument ; 24 import javax.swing.text.SimpleAttributeSet ; 25 import org.netbeans.api.java.lexer.JavaTokenId; 26 import org.netbeans.api.lexer.Language; 27 import org.netbeans.api.lexer.TokenHierarchy; 28 import org.netbeans.api.lexer.TokenSequence; 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.modules.editor.lib2.highlighting.SyntaxHighlighting; 31 import org.netbeans.spi.editor.highlighting.HighlightsSequence; 32 33 37 public class SyntaxHighlightingTest extends NbTestCase { 38 39 public static void main(String [] args) { 40 junit.textui.TestRunner.run(SyntaxHighlightingTest.class); 41 } 42 43 44 public SyntaxHighlightingTest(String name) { 45 super(name); 46 } 47 48 private Document document; 49 private TokenHierarchy hierarchy; 50 private SyntaxHighlighting layer; 51 52 protected void setUp() { 53 Document d = new PlainDocument (); 55 try { 56 File dataFolder = new File (getClass().getResource("data").toURI()); 57 InputStream io = new FileInputStream (new File (dataFolder, "VeryBigClassThatWasStolenAndDoesNotCompile-java")); 58 try { 59 byte [] buffer = new byte [1024]; 60 int size; 61 62 while (0 < (size = io.read(buffer, 0, buffer.length))) { 63 String s = new String (buffer, 0, size); 64 d.insertString(d.getLength(), s, SimpleAttributeSet.EMPTY); 65 } 66 } finally { 67 io.close(); 68 } 69 } catch (Exception e) { 70 e.printStackTrace(); 71 fail(e.getMessage()); 72 } 73 74 Language<JavaTokenId> javaLang = JavaTokenId.language(); 75 d.putProperty(Language.class, javaLang); 76 d.putProperty("mimeType", javaLang.mimeType()); 77 78 79 this.document = d; 80 this.hierarchy= TokenHierarchy.get(document); 81 this.layer = new SyntaxHighlighting(document); 82 } 83 84 public void testLexer() { 85 lexerWarmUpFirst(); 86 lexerWarmUpNext(); 87 highlightingCheckFirst(); 88 highlightingCheckNext(); 89 } 90 91 private void lexerWarmUpFirst() { 92 long timestamp1, timestamp2; 93 timestamp1 = System.currentTimeMillis(); 94 TokenSequence ts = hierarchy.tokenSequence(); 95 timestamp2 = System.currentTimeMillis(); 96 System.out.println("0. TokenHierarchy.tokenSequence() took " + (timestamp2 - timestamp1) + " msecs."); 97 98 timestamp1 = System.currentTimeMillis(); 99 iterateOver(ts, null, null); 100 timestamp2 = System.currentTimeMillis(); 101 System.out.println("0. Iterating through TokenSequence took " + (timestamp2 - timestamp1) + " msecs."); 102 } 103 104 private void lexerWarmUpNext() { 105 long timestamp1, timestamp2; 106 for(int i = 1; i < 5; i++) { 107 timestamp1 = System.currentTimeMillis(); 108 TokenSequence ts = hierarchy.tokenSequence(); 109 timestamp2 = System.currentTimeMillis(); 110 System.out.println(i + ". TokenHierarchy.tokenSequence() took " + (timestamp2 - timestamp1) + " msecs."); 111 112 timestamp1 = System.currentTimeMillis(); 113 iterateOver(ts, null, null); 114 timestamp2 = System.currentTimeMillis(); 115 System.out.println(i + ". Iterating through TokenSequence took " + (timestamp2 - timestamp1) + " msecs."); 116 } 117 } 118 119 private void highlightingCheckFirst() { 120 long timestamp1, timestamp2; 121 timestamp1 = System.currentTimeMillis(); 122 HighlightsSequence hs = layer.getHighlights(0, Integer.MAX_VALUE); 123 timestamp2 = System.currentTimeMillis(); 124 System.out.println("0. SyntaxHighlighting.getHighlights() took " + (timestamp2 - timestamp1) + " msecs."); 125 126 timestamp1 = System.currentTimeMillis(); 127 iterateOver(hs); 128 timestamp2 = System.currentTimeMillis(); 129 System.out.println("0. Iterating through HighlightsSequence took " + (timestamp2 - timestamp1) + " msecs."); 130 } 131 132 private void highlightingCheckNext() { 133 long timestamp1, timestamp2; 134 for(int i = 1; i < 5; i++) { 135 timestamp1 = System.currentTimeMillis(); 136 HighlightsSequence hs = layer.getHighlights(0, Integer.MAX_VALUE); 137 timestamp2 = System.currentTimeMillis(); 138 System.out.println(i + ". SyntaxHighlighting.getHighlights() took " + (timestamp2 - timestamp1) + " msecs."); 139 140 timestamp1 = System.currentTimeMillis(); 141 iterateOver(hs); 142 timestamp2 = System.currentTimeMillis(); 143 System.out.println(i + ". Iterating through HighlightsSequence took " + (timestamp2 - timestamp1) + " msecs."); 144 } 145 } 146 147 private void iterateOver(TokenSequence ts, HashMap <String , Integer > distro, HashMap <String , Integer > flyweightDistro) { 148 for( ; ts.moveNext(); ) { 149 String name = ts.token().id().name(); 150 assertNotNull("Token name must not be null", name); 151 152 if (distro != null) { 153 String tokenId = ts.languagePath().mimePath() + ":" + name; 154 { 155 Integer freq = distro.get(tokenId); 156 if (freq == null) { 157 freq = 1; 158 } else { 159 freq++; 160 } 161 distro.put(tokenId, freq); 162 } 163 164 if (ts.token().isFlyweight()) { 165 Integer freq = flyweightDistro.get(tokenId); 166 if (freq == null) { 167 freq = 1; 168 } else { 169 freq++; 170 } 171 flyweightDistro.put(tokenId, freq); 172 } 173 174 } 181 182 TokenSequence embedded = ts.embedded(); 183 if (embedded != null) { 184 iterateOver(embedded, distro, flyweightDistro); 185 } 186 } 187 } 188 189 private void iterateOver(HighlightsSequence hs) { 190 for( ; hs.moveNext(); ) { 191 AttributeSet attribs = hs.getAttributes(); 192 assertNotNull("AttributeSet must not be null", attribs); 193 } 194 } 195 196 public void testLexerEmbedding() { 197 long timestamp1, timestamp2; 198 TokenHierarchy th = TokenHierarchy.get(document); 199 200 TokenSequence embeddedSeq = null; 201 TokenSequence ts = th.tokenSequence(); 202 for( ; ts.moveNext(); ) { 203 String name = ts.token().id().name(); 204 assertNotNull("Token name must not be null", name); 205 206 timestamp1 = System.currentTimeMillis(); 207 embeddedSeq = ts.embedded(); 208 timestamp2 = System.currentTimeMillis(); 209 if (embeddedSeq != null) { 210 System.out.println("First TS.embedded() took " + (timestamp2 - timestamp1) + " msecs."); 211 break; 213 } 214 } 215 216 assertNotNull("Can't find embedded sequence", embeddedSeq); 217 218 timestamp1 = System.currentTimeMillis(); 219 TokenSequence embeddedSeq2 = ts.embedded(); 220 timestamp2 = System.currentTimeMillis(); 221 System.out.println("Second TS.embedded() took " + (timestamp2 - timestamp1) + " msecs."); 222 223 assertNotNull("Second call to TS.embedded() produced null", embeddedSeq2); 224 } 225 226 public void testDistro() { 227 TokenHierarchy th = TokenHierarchy.get(document); 228 TokenSequence ts = th.tokenSequence(); 229 HashMap <String , Integer > distro = new HashMap <String , Integer >(); 230 HashMap <String , Integer > flyweightDistro = new HashMap <String , Integer >(); 231 iterateOver(ts, distro, flyweightDistro); 232 233 { 234 long totalTokens = 0; 235 236 System.out.println("\nAll tokens sorted by names:"); 237 ArrayList <String > names = new ArrayList <String >(distro.keySet()); 238 Collections.sort(names); 239 for(String tokenId : names) { 240 Integer freq = distro.get(tokenId); 241 System.out.println(tokenId + " -> " + freq); 242 totalTokens += freq; 243 } 244 System.out.println(" Total tokens count: " + totalTokens); 245 246 totalTokens = 0; 247 System.out.println("\nAll tokens sorted by freq:"); 248 ArrayList <Map.Entry <String , Integer >> entries = new ArrayList <Map.Entry <String , Integer >>(distro.entrySet()); 249 Collections.sort(entries, new FreqCmp()); 250 for(Map.Entry <String , Integer > entry : entries) { 251 System.out.println(entry.getKey() + " -> " + entry.getValue()); 252 totalTokens += entry.getValue(); 253 } 254 System.out.println(" Total tokens count: " + totalTokens); 255 } 256 257 { 258 long totalFlyweightTokens = 0; 259 260 System.out.println("\nFlyweight tokens sorted by names:"); 261 ArrayList <String > names = new ArrayList <String >(flyweightDistro.keySet()); 262 Collections.sort(names); 263 for(String tokenId : names) { 264 Integer freq = flyweightDistro.get(tokenId); 265 System.out.println(tokenId + " -> " + freq); 266 totalFlyweightTokens += freq; 267 } 268 System.out.println(" Total tokens count: " + totalFlyweightTokens); 269 270 271 totalFlyweightTokens = 0; 272 System.out.println("\nFlyweight tokens sorted by freq:"); 273 ArrayList <Map.Entry <String , Integer >> entries = new ArrayList <Map.Entry <String , Integer >>(flyweightDistro.entrySet()); 274 Collections.sort(entries, new FreqCmp()); 275 for(Map.Entry <String , Integer > entry : entries) { 276 System.out.println(entry.getKey() + " -> " + entry.getValue()); 277 totalFlyweightTokens += entry.getValue(); 278 } 279 System.out.println(" Total flyweight tokens count: " + totalFlyweightTokens); 280 } 281 282 System.out.println("\nChecking flywights:"); 283 for(String tokenId : flyweightDistro.keySet()) { 284 Integer freq = distro.get(tokenId); 285 Integer flyweightFreq = flyweightDistro.get(tokenId); 286 assertNotNull("No freq for " + tokenId, freq); 287 assertNotNull("No flyweightDistro freq for " + tokenId, flyweightFreq); 288 if (freq.intValue() != flyweightFreq.intValue()) { 289 System.out.println(tokenId + " : freq = " + freq + " is different from flyweightFreq = " + flyweightFreq); 290 } 291 } 292 System.out.println("Check done!"); 293 } 294 295 private static final class FreqCmp implements Comparator <Map.Entry <String , Integer >> { 296 public int compare(Entry<String , Integer > e1, 297 Entry<String , Integer > e2 298 ) { 299 Integer freq1 = e1.getValue(); 300 Integer freq2 = e2.getValue(); 301 int f1 = freq1 == null ? 0 : freq1.intValue(); 302 int f2 = freq2 == null ? 0 : freq2.intValue(); 303 return f1 - f2; 304 } 305 } 306 } 307 | Popular Tags |