KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > editor > highlighting > performance > SyntaxHighlightingTest


1 /*
2  * SyntaxHighlightingTest.java
3  *
4  * Created on January 18, 2007, 9:47 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.spi.editor.highlighting.performance;
11
12 import java.io.FileInputStream JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Map.Entry;
21 import javax.swing.text.AttributeSet JavaDoc;
22 import javax.swing.text.Document JavaDoc;
23 import javax.swing.text.PlainDocument JavaDoc;
24 import javax.swing.text.SimpleAttributeSet JavaDoc;
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 /**
34  *
35  * @author vita
36  */

37 public class SyntaxHighlightingTest extends NbTestCase {
38
39     public static void main(String JavaDoc [] args) {
40         junit.textui.TestRunner.run(SyntaxHighlightingTest.class);
41     }
42
43     /** Creates a new instance of SyntaxHighlightingTest */
44     public SyntaxHighlightingTest(String JavaDoc name) {
45         super(name);
46     }
47
48     private Document JavaDoc document;
49     private TokenHierarchy hierarchy;
50     private SyntaxHighlighting layer;
51     
52     protected void setUp() {
53         // Create and load document
54
Document JavaDoc d = new PlainDocument JavaDoc();
55         try {
56             File JavaDoc dataFolder = new File JavaDoc(getClass().getResource("data").toURI());
57             InputStream JavaDoc io = new FileInputStream JavaDoc(new File JavaDoc(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 JavaDoc s = new String JavaDoc(buffer, 0, size);
64                     d.insertString(d.getLength(), s, SimpleAttributeSet.EMPTY);
65                 }
66             } finally {
67                 io.close();
68             }
69         } catch (Exception JavaDoc 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 JavaDoc<String JavaDoc, Integer JavaDoc> distro, HashMap JavaDoc<String JavaDoc, Integer JavaDoc> flyweightDistro) {
148         for( ; ts.moveNext(); ) {
149             String JavaDoc name = ts.token().id().name();
150             assertNotNull("Token name must not be null", name);
151             
152             if (distro != null) {
153                 String JavaDoc tokenId = ts.languagePath().mimePath() + ":" + name;
154                 {
155                     Integer JavaDoc 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 JavaDoc freq = flyweightDistro.get(tokenId);
166                     if (freq == null) {
167                         freq = 1;
168                     } else {
169                         freq++;
170                     }
171                     flyweightDistro.put(tokenId, freq);
172                 }
173                 
174 // if (ts.token().id() == JavadocTokenId.IDENT) {
175
// System.out.println("Javadoc IDENT: '" + ts.token().text() + "'");
176
// }
177
// if (ts.token().id() == JavadocTokenId.OTHER_TEXT) {
178
// System.out.println("Javadoc OTHER_TEXT: '" + ts.token().text() + "'");
179
// }
180
}
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 JavaDoc 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 JavaDoc 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                 // found embedded
212
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 JavaDoc<String JavaDoc, Integer JavaDoc> distro = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc>();
230         HashMap JavaDoc<String JavaDoc, Integer JavaDoc> flyweightDistro = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc>();
231         iterateOver(ts, distro, flyweightDistro);
232         
233         {
234             long totalTokens = 0;
235             
236             System.out.println("\nAll tokens sorted by names:");
237             ArrayList JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>(distro.keySet());
238             Collections.sort(names);
239             for(String JavaDoc tokenId : names) {
240                 Integer JavaDoc 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 JavaDoc<Map.Entry JavaDoc<String JavaDoc, Integer JavaDoc>> entries = new ArrayList JavaDoc<Map.Entry JavaDoc<String JavaDoc, Integer JavaDoc>>(distro.entrySet());
249             Collections.sort(entries, new FreqCmp());
250             for(Map.Entry JavaDoc<String JavaDoc, Integer JavaDoc> 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 JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>(flyweightDistro.keySet());
262             Collections.sort(names);
263             for(String JavaDoc tokenId : names) {
264                 Integer JavaDoc 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 JavaDoc<Map.Entry JavaDoc<String JavaDoc, Integer JavaDoc>> entries = new ArrayList JavaDoc<Map.Entry JavaDoc<String JavaDoc, Integer JavaDoc>>(flyweightDistro.entrySet());
274             Collections.sort(entries, new FreqCmp());
275             for(Map.Entry JavaDoc<String JavaDoc, Integer JavaDoc> 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 JavaDoc tokenId : flyweightDistro.keySet()) {
284             Integer JavaDoc freq = distro.get(tokenId);
285             Integer JavaDoc 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 JavaDoc<Map.Entry JavaDoc<String JavaDoc, Integer JavaDoc>> {
296         public int compare(Entry<String JavaDoc, Integer JavaDoc> e1,
297                            Entry<String JavaDoc, Integer JavaDoc> e2
298         ) {
299             Integer JavaDoc freq1 = e1.getValue();
300             Integer JavaDoc 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