KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > java > lexer > JavaLexerPerformanceTest


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.lib.java.lexer;
21
22 import java.io.File JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.nio.CharBuffer 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.lib.lexer.test.ModificationTextDocument;
31
32 /**
33  * This is a test that scans the source that is a copy of javax.swing.JComponent
34  * and reports the times of creation of all the tokens. It exists
35  * mainly because it's easy to run profiler over it.
36  *
37  * @author mmetelka
38  */

39 public class JavaLexerPerformanceTest extends NbTestCase {
40     
41     public JavaLexerPerformanceTest(String JavaDoc testName) {
42         super(testName);
43     }
44     
45     protected void setUp() throws java.lang.Exception JavaDoc {
46         // Set-up testing environment
47
// Disable for performance testing
48
// LexerTestUtilities.setTesting(true);
49
}
50
51     protected void tearDown() throws java.lang.Exception JavaDoc {
52     }
53
54     public void testString() throws Exception JavaDoc {
55         String JavaDoc text = readJComponentFile();
56         TokenHierarchy hi = TokenHierarchy.create(text, JavaTokenId.language());
57         TokenSequence ts = hi.tokenSequence();
58         // Initial pass - force all the tokens to be initialized
59
while (ts.moveNext()) { }
60
61         // Create the token hierarchy again and measure time
62
hi = TokenHierarchy.create(text, JavaTokenId.language());
63         ts = hi.tokenSequence();
64         long tm = System.currentTimeMillis();
65         // Force all the tokens to be initialized
66
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 JavaDoc {
72         String JavaDoc 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         // Create the document and token hierarchy again and measure time
81
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         // Force all the tokens to be initialized
88
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 JavaDoc readJComponentFile() throws Exception JavaDoc {
95         File JavaDoc testJComponentFile = new File JavaDoc(getDataDir() + "/testfiles/JComponent.java.txt");
96         FileReader JavaDoc r = new FileReader JavaDoc(testJComponentFile);
97         int fileLen = (int)testJComponentFile.length();
98         CharBuffer JavaDoc cb = CharBuffer.allocate(fileLen);
99         r.read(cb);
100         cb.rewind();
101         return cb.toString();
102     }
103     
104 }
105
Popular Tags