KickJava   Java API By Example, From Geeks To Geeks.

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


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.spi.editor.highlighting.performance;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.Collections JavaDoc;
24 import javax.swing.text.AttributeSet JavaDoc;
25 import javax.swing.text.Document JavaDoc;
26 import javax.swing.text.PlainDocument JavaDoc;
27 import javax.swing.text.Position JavaDoc;
28 import javax.swing.text.SimpleAttributeSet JavaDoc;
29 import org.netbeans.junit.MemoryFilter;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.lib.editor.util.GapList;
32 import org.netbeans.spi.editor.highlighting.*;
33 import org.netbeans.spi.editor.highlighting.support.PositionsBag;
34
35 /**
36  *
37  * @author Vita Stejskal
38  */

39 public class PositionsBagMemoryTest extends NbTestCase {
40
41     private static final int CNT = 1000;
42     
43     /** Creates a new instance of HighlightsBagPerformanceTest */
44     public PositionsBagMemoryTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     public void testMemoryConsumptionBestCase() {
49         checkMemoryConsumption(false, true);
50     }
51     
52     public void testMemoryConsumptionWorstCase() {
53         checkMemoryConsumption(false, false);
54     }
55     
56     public void testIteratorMemoryConsumptionBestCase() {
57         checkIteratorMemoryConsumption(false, true);
58     }
59     
60     public void testIteratorMemoryConsumptionWorstCase() {
61         checkIteratorMemoryConsumption(false, false);
62     }
63     
64     public void testMergingBagMemoryConsumptionBestCase() {
65         checkMemoryConsumption(true, true);
66     }
67     
68     public void testMergingBagMemoryConsumptionWorstCase() {
69         checkMemoryConsumption(true, false);
70     }
71     
72     public void testMergingBagIteratorMemoryConsumptionBestCase() {
73         checkIteratorMemoryConsumption(true, true);
74     }
75     
76     public void testMergingBagIteratorMemoryConsumptionWorst() {
77         checkIteratorMemoryConsumption(true, false);
78     }
79
80     private void checkMemoryConsumption(boolean merging, boolean bestCase) {
81         PositionsBag bag = new PositionsBag(new PlainDocument JavaDoc(), merging);
82
83         for(int i = 0; i < CNT; i++) {
84             if (bestCase) {
85                 bag.addHighlight(new SimplePosition(i * 10), new SimplePosition((i + 1) * 10), SimpleAttributeSet.EMPTY);
86             } else {
87                 bag.addHighlight(new SimplePosition(i * 10), new SimplePosition(i* 10 + 5), SimpleAttributeSet.EMPTY);
88             }
89         }
90
91         compact(bag);
92         
93         assertSize("PositionsBag of " + CNT + " highlights " + (bestCase ? "(best case)" : "(worst case)"),
94             Collections.singleton(bag), bestCase ? 8500 : 16500, new MF());
95     }
96
97     private void checkIteratorMemoryConsumption(boolean merging, boolean bestCase) {
98         PositionsBag bag = new PositionsBag(new PlainDocument JavaDoc(), merging);
99
100         for(int i = 0; i < CNT; i++) {
101             if (bestCase) {
102                 bag.addHighlight(new SimplePosition(i * 10), new SimplePosition((i + 1) * 10), SimpleAttributeSet.EMPTY);
103             } else {
104                 bag.addHighlight(new SimplePosition(i * 10), new SimplePosition(i* 10 + 5), SimpleAttributeSet.EMPTY);
105             }
106         }
107
108         compact(bag);
109         
110         HighlightsSequence sequence = bag.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE);
111
112         // Do not iterate through the whole sequence, otherwise it will discard its contents
113
for(int i = 0; i < CNT - 1; i++) {
114             boolean hasHighlight = sequence.moveNext();
115
116             assertTrue("Wrong number of highlights in the sequence; found only " + i, hasHighlight);
117             assertEquals("Wrong start offset of " + i + ". highlight",
118                 i * 10, sequence.getStartOffset());
119             assertEquals("Wrong end offset of " + i + ". highlight",
120                 bestCase ? (i + 1) * 10 : i * 10 + 5, sequence.getEndOffset());
121             assertSame("Wrong attributes of " + i + ". highlight", SimpleAttributeSet.EMPTY, sequence.getAttributes());
122         }
123         
124         assertSize("HighlightsSequence of " + CNT + " highlights " + (bestCase ? "(best case)" : "(worst case)"),
125             Collections.singleton(sequence), bestCase ? 8500 : 16500, new MF());
126     }
127
128     @SuppressWarnings JavaDoc("unchecked")
129     private void compact(PositionsBag bag) {
130         try {
131             Method JavaDoc m = bag.getClass().getDeclaredMethod("getMarks");
132             m.setAccessible(true);
133             GapList<Position JavaDoc> marks = (GapList<Position JavaDoc>) m.invoke(bag);
134             marks.trimToSize();
135
136             m = bag.getClass().getDeclaredMethod("getAttributes");
137             m.setAccessible(true);
138             GapList<Position JavaDoc> attributes = (GapList<Position JavaDoc>) m.invoke(bag);
139             attributes.trimToSize();
140         } catch (Exception JavaDoc e) {
141             AssertionError JavaDoc ae = new AssertionError JavaDoc(e.getMessage());
142             ae.initCause(e);
143             throw ae;
144         }
145     }
146     
147     private static final class MF implements MemoryFilter {
148         public boolean reject(Object JavaDoc obj) {
149             if (Position JavaDoc.class.isAssignableFrom(obj.getClass()) ||
150                 AttributeSet JavaDoc.class.isAssignableFrom(obj.getClass()) ||
151                 Document JavaDoc.class.isAssignableFrom(obj.getClass()))
152             {
153                 return true;
154             } else {
155                 return false;
156             }
157         }
158     } // End of MF class
159
}
160
Popular Tags