001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.lexer;
043:
044: import java.io.File;
045: import java.io.FileReader;
046: import java.nio.CharBuffer;
047: import org.netbeans.cnd.api.lexer.CppTokenId;
048: import org.netbeans.api.lexer.Language;
049: import org.netbeans.api.lexer.TokenHierarchy;
050: import org.netbeans.api.lexer.TokenSequence;
051: import org.netbeans.junit.NbTestCase;
052: import org.netbeans.lib.lexer.test.ModificationTextDocument;
053:
054: /**
055: * This is a test that scans the source that is a copy of <stdio.h> and <istream>
056: * and reports the times of creation of all the tokens. It exists
057: * mainly because it's easy to run profiler over it.
058: *
059: * based on JavaLexerPerformanceTest
060: * @author Vladimir Voskresensky
061: */
062: public class CppLexerPerformanceTestCase extends NbTestCase {
063:
064: public CppLexerPerformanceTestCase(String testName) {
065: super (testName);
066: }
067:
068: @Override
069: protected void setUp() throws java.lang.Exception {
070: // Set-up testing environment
071: // Disable for performance testing
072: // LexerTestUtilities.setTesting(true);
073: }
074:
075: @Override
076: protected void tearDown() throws java.lang.Exception {
077: }
078:
079: private static final int PREPARE_COUNT = 10;
080:
081: public void testAString() throws Exception {
082: String text = readFile();
083: // Possibly some extra prepare runs
084: int prepareTokenCount = 0;
085: for (int i = 0; i < PREPARE_COUNT; i++) {
086: prepareTokenCount += prepareTestAString(text);
087: }
088:
089: TokenHierarchy<?> hi = TokenHierarchy.create(text, CppTokenId
090: .languageC());
091: TokenSequence<?> ts = hi.tokenSequence();
092: long tm = System.currentTimeMillis();
093: // Force all the tokens to be initialized
094: int tokenCount = 0;
095: while (ts.moveNext()) {
096: tokenCount++;
097: }
098: tm = System.currentTimeMillis() - tm;
099: System.err.println("TH over String: " + tokenCount
100: + " tokens created in " + tm + " ms over text with "
101: + text.length() + " chars; prepareTokenCount="
102: + prepareTokenCount + ".");
103: }
104:
105: public void testDocument() throws Exception {
106: String text = readFile();
107: // Possibly some extra prepare runs
108: int prepareTokenCount = 0;
109: for (int i = 0; i < PREPARE_COUNT; i++) {
110: prepareTokenCount += prepareTestDocument(text);
111: }
112:
113: // Create the document and token hierarchy again and measure time
114: ModificationTextDocument doc = new ModificationTextDocument();
115: doc.insertString(0, text, null);
116: doc.putProperty(Language.class, CppTokenId.languageC());
117: TokenHierarchy<?> hi = TokenHierarchy.get(doc);
118: TokenSequence<?> ts = hi.tokenSequence();
119: long tm = System.currentTimeMillis();
120: int tokenCount = 0;
121: // Force all the tokens to be initialized
122: while (ts.moveNext()) {
123: tokenCount++;
124: }
125: tm = System.currentTimeMillis() - tm;
126: System.err.println("TH over Swing Document: " + tokenCount
127: + " tokens created in " + tm
128: + " ms over document with " + doc.getLength()
129: + " chars; prepareTokenCount=" + prepareTokenCount
130: + ".");
131: }
132:
133: private int prepareTestAString(String text) throws Exception {
134: TokenHierarchy<?> hi = TokenHierarchy.create(text, CppTokenId
135: .languageC());
136: TokenSequence<?> ts = hi.tokenSequence();
137: int tokenCount = 0;
138: while (ts.moveNext()) {
139: tokenCount++;
140: }
141: return tokenCount;
142: }
143:
144: private int prepareTestDocument(String text) throws Exception {
145: ModificationTextDocument doc = new ModificationTextDocument();
146: doc.insertString(0, text, null);
147: doc.putProperty(Language.class, CppTokenId.languageC());
148: TokenHierarchy<?> hi = TokenHierarchy.get(doc);
149: TokenSequence<?> ts = hi.tokenSequence();
150: int tokenCount = 0;
151: while (ts.moveNext()) {
152: tokenCount++;
153: }
154: return tokenCount;
155: }
156:
157: private String readFile() throws Exception {
158: File testJComponentFile = new File(getDataDir()
159: + "/testfiles/stdio.h.txt");
160: FileReader r = new FileReader(testJComponentFile);
161: try {
162: int fileLen = (int) testJComponentFile.length();
163: CharBuffer cb = CharBuffer.allocate(fileLen);
164: r.read(cb);
165: cb.rewind();
166: return cb.toString();
167: } finally {
168: r.close();
169: }
170: }
171:
172: }
|