1 19 20 package org.netbeans.lib.java.lexer; 21 22 import java.io.File ; 23 import java.io.FileReader ; 24 import java.nio.CharBuffer ; 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.lib.lexer.test.ModificationTextDocument; 31 32 39 public class JavaLexerPerformanceTest extends NbTestCase { 40 41 public JavaLexerPerformanceTest(String testName) { 42 super(testName); 43 } 44 45 protected void setUp() throws java.lang.Exception { 46 } 50 51 protected void tearDown() throws java.lang.Exception { 52 } 53 54 public void testString() throws Exception { 55 String text = readJComponentFile(); 56 TokenHierarchy hi = TokenHierarchy.create(text, JavaTokenId.language()); 57 TokenSequence ts = hi.tokenSequence(); 58 while (ts.moveNext()) { } 60 61 hi = TokenHierarchy.create(text, JavaTokenId.language()); 63 ts = hi.tokenSequence(); 64 long tm = System.currentTimeMillis(); 65 while (ts.moveNext()) { } 67 tm = System.currentTimeMillis() - tm; 68 System.err.println("TH over String: all tokens created in " + tm + " ms."); 69 } 70 71 public void testDocument() throws Exception { 72 String text = readJComponentFile(); 73 ModificationTextDocument doc = new ModificationTextDocument(); 74 doc.insertString(0, text, null); 75 doc.putProperty(Language.class, JavaTokenId.language()); 76 TokenHierarchy hi = TokenHierarchy.get(doc); 77 TokenSequence ts = hi.tokenSequence(); 78 while (ts.moveNext()) { } 79 80 doc = new ModificationTextDocument(); 82 doc.insertString(0, text, null); 83 doc.putProperty(Language.class, JavaTokenId.language()); 84 hi = TokenHierarchy.get(doc); 85 ts = hi.tokenSequence(); 86 long tm = System.currentTimeMillis(); 87 while (ts.moveNext()) { } 89 tm = System.currentTimeMillis() - tm; 90 System.err.println("TH over Swing Document: all tokens created in " + tm + " ms."); 91 92 } 93 94 private String readJComponentFile() throws Exception { 95 File testJComponentFile = new File (getDataDir() + "/testfiles/JComponent.java.txt"); 96 FileReader r = new FileReader (testJComponentFile); 97 int fileLen = (int)testJComponentFile.length(); 98 CharBuffer cb = CharBuffer.allocate(fileLen); 99 r.read(cb); 100 cb.rewind(); 101 return cb.toString(); 102 } 103 104 } 105 | Popular Tags |