1 19 20 package org.netbeans.spi.editor.highlighting.performance; 21 22 import java.lang.reflect.Method ; 23 import java.util.Collections ; 24 import javax.swing.text.AttributeSet ; 25 import javax.swing.text.Document ; 26 import javax.swing.text.PlainDocument ; 27 import javax.swing.text.Position ; 28 import javax.swing.text.SimpleAttributeSet ; 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 39 public class PositionsBagMemoryTest extends NbTestCase { 40 41 private static final int CNT = 1000; 42 43 44 public PositionsBagMemoryTest(String 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 (), 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 (), 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 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 ("unchecked") 129 private void compact(PositionsBag bag) { 130 try { 131 Method m = bag.getClass().getDeclaredMethod("getMarks"); 132 m.setAccessible(true); 133 GapList<Position > marks = (GapList<Position >) m.invoke(bag); 134 marks.trimToSize(); 135 136 m = bag.getClass().getDeclaredMethod("getAttributes"); 137 m.setAccessible(true); 138 GapList<Position > attributes = (GapList<Position >) m.invoke(bag); 139 attributes.trimToSize(); 140 } catch (Exception e) { 141 AssertionError ae = new AssertionError (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 obj) { 149 if (Position .class.isAssignableFrom(obj.getClass()) || 150 AttributeSet .class.isAssignableFrom(obj.getClass()) || 151 Document .class.isAssignableFrom(obj.getClass())) 152 { 153 return true; 154 } else { 155 return false; 156 } 157 } 158 } } 160 | Popular Tags |