1 package org.lobobrowser.html.test; 2 3 import java.io.InputStream ; 4 import java.net.HttpURLConnection ; 5 import java.net.URL ; 6 import java.net.URLConnection ; 7 8 import org.lobobrowser.html.*; 9 import org.lobobrowser.html.domimpl.*; 10 import org.lobobrowser.html.gui.*; 11 import org.lobobrowser.html.parser.DocumentBuilderImpl; 12 import org.lobobrowser.html.parser.InputSourceImpl; 13 import org.lobobrowser.html.renderer.*; 14 import org.lobobrowser.util.io.IORoutines; 15 import org.w3c.dom.Document ; 16 17 import java.util.Collection ; 18 import java.util.logging.*; 19 import java.io.*; 20 import javax.swing.*; 21 import java.awt.*; 22 23 26 public class MemoryTest { 27 31 private static final Logger logger = Logger.getLogger(MemoryTest.class.getName()); 32 33 36 public static void main(String [] args) throws Exception { 37 MemoryTest mt = new MemoryTest(); 38 mt.testRendererGUILoop(); 41 } 42 43 private static String TEST_URL = "file:c:\\temp\\html\\long.html"; 44 45 public void testParserLoop() throws Exception { 46 URL url = new URL (TEST_URL); 47 URLConnection connection = url.openConnection(); 48 connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;) Cobra/0.96.1+"); 49 connection.setRequestProperty("Cookie", ""); 50 if(connection instanceof HttpURLConnection ) { 51 HttpURLConnection hc = (HttpURLConnection ) connection; 52 hc.setInstanceFollowRedirects(true); 53 int responseCode = hc.getResponseCode(); 54 logger.info("process(): HTTP response code: " + responseCode); 55 } 56 InputStream in = connection.getInputStream(); 57 byte[] content; 58 try { 59 content = IORoutines.load(in, 8192); 60 } finally { 61 in.close(); 62 } 63 logger.info("Content size: " + content.length + " bytes."); 66 UserAgentContext context = new SimpleUserAgentContext(); 67 DocumentBuilderImpl builder = new DocumentBuilderImpl(context); 68 for(int i = 0; i < 200; i++) { 69 logger.info("Starting parse # " + i + ": freeMemory=" + Runtime.getRuntime().freeMemory()); 70 InputStream bin = new ByteArrayInputStream(content); 71 Document document = builder.parse(new InputSourceImpl(bin, url.toExternalForm(), "ISO-8859-1")); 72 logger.info("Finished parsing: freeMemory=" + Runtime.getRuntime().freeMemory() + ",document=" + document); 73 document = null; 74 System.gc(); 75 logger.info("After GC: freeMemory=" + Runtime.getRuntime().freeMemory()); 76 Thread.sleep(2); 77 } 78 } 79 80 public void testRendererLoop() throws Exception { 81 URL url = new URL (TEST_URL); 82 URLConnection connection = url.openConnection(); 83 connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;) Cobra/0.96.1+"); 84 connection.setRequestProperty("Cookie", ""); 85 if(connection instanceof HttpURLConnection ) { 86 HttpURLConnection hc = (HttpURLConnection ) connection; 87 hc.setInstanceFollowRedirects(true); 88 int responseCode = hc.getResponseCode(); 89 logger.info("process(): HTTP response code: " + responseCode); 90 } 91 InputStream in = connection.getInputStream(); 92 byte[] content; 93 try { 94 content = IORoutines.load(in, 8192); 95 } finally { 96 in.close(); 97 } 98 logger.info("Content size: " + content.length + " bytes."); 101 final UserAgentContext ucontext = new SimpleUserAgentContext(); 102 final HtmlPanel panel = new HtmlPanel(); 103 final HtmlRendererContext rcontext = new SimpleHtmlRendererContext(panel); 104 DocumentBuilderImpl builder = new DocumentBuilderImpl(ucontext, rcontext); 105 InputStream bin = new ByteArrayInputStream(content); 106 final FrameContext frameContext = new LocalFrameContext(); 107 final RenderableContainer renderableContainer = new LocalRenderableContainer(); 108 for(int i = 0; i < 100; i++) { 109 logger.info("Starting parse # " + i + ": freeMemory=" + Runtime.getRuntime().freeMemory()); 110 bin = new ByteArrayInputStream(content); 111 Document document = builder.parse(new InputSourceImpl(bin, url.toExternalForm(), "ISO-8859-1")); 112 logger.info("Finished parsing: freeMemory=" + Runtime.getRuntime().freeMemory()); 113 { 114 final Document doc = document; 115 EventQueue.invokeAndWait(new Runnable () { 116 public void run() { 117 RBlock block = new RBlock((NodeImpl) doc, 0, rcontext.getUserAgentContext(), rcontext, frameContext, renderableContainer, RBlock.OVERFLOW_NONE); 118 block.layout(100, 100); 119 } 120 }); 121 Thread.sleep(50); 123 } 125 document = null; 126 System.gc(); 127 logger.info("After GC: freeMemory=" + Runtime.getRuntime().freeMemory()); 128 Thread.sleep(2); 129 } 130 } 131 132 public void testRendererGUILoop() throws Exception { 133 URL url = new URL (TEST_URL); 134 URLConnection connection = url.openConnection(); 135 connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;) Cobra/0.96.1+"); 136 connection.setRequestProperty("Cookie", ""); 137 if(connection instanceof HttpURLConnection ) { 138 HttpURLConnection hc = (HttpURLConnection ) connection; 139 hc.setInstanceFollowRedirects(true); 140 int responseCode = hc.getResponseCode(); 141 logger.info("process(): HTTP response code: " + responseCode); 142 } 143 InputStream in = connection.getInputStream(); 144 byte[] content; 145 try { 146 content = IORoutines.load(in, 8192); 147 } finally { 148 in.close(); 149 } 150 logger.info("Content size: " + content.length + " bytes."); 153 final UserAgentContext ucontext = new SimpleUserAgentContext(); 154 final HtmlPanel panel = new HtmlPanel(); 155 final HtmlRendererContext rcontext = new SimpleHtmlRendererContext(panel); 156 DocumentBuilderImpl builder = new DocumentBuilderImpl(ucontext, rcontext); 157 JFrame testFrame = new JFrame("Testing..."); 158 testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 159 testFrame.getContentPane().setLayout(new java.awt.BorderLayout ()); 160 testFrame.getContentPane().add(panel, BorderLayout.CENTER); 161 InputStream bin = new ByteArrayInputStream(content); 162 testFrame.setSize(new java.awt.Dimension (600, 400)); 163 testFrame.setVisible(true); 164 for(int i = 0; i < 20; i++) { 165 logger.info("Starting parse # " + i + ": freeMemory=" + Runtime.getRuntime().freeMemory()); 166 bin = new ByteArrayInputStream(content); 167 Document document = builder.parse(new InputSourceImpl(bin, url.toExternalForm(), "ISO-8859-1")); 168 logger.info("Finished parsing: freeMemory=" + Runtime.getRuntime().freeMemory()); 169 panel.setDocument(document, rcontext); 170 EventQueue.invokeAndWait(new Runnable () { public void run() {} }); 171 Thread.sleep(3000); 173 panel.clearDocument(); 174 Thread.sleep(1000); 175 document = null; 176 System.gc(); 177 logger.info("After GC: freeMemory=" + Runtime.getRuntime().freeMemory()); 178 Thread.sleep(2000); 179 } 180 } 181 182 private class LocalRenderableContainer implements RenderableContainer { 183 public void invalidateLayoutUpTree() { 184 } 186 187 public Component add(Component component) { 188 return null; 190 } 191 192 public void remove(Component c) { 193 } 195 196 public Color getPaintedBackgroundColor() { 197 return Color.BLACK; 198 } 199 200 public Insets getInsets() { 201 return new Insets(0, 0, 0, 0); 202 } 203 204 public void repaint(int x, int y, int width, int height) { 205 } 206 207 public void relayout() { 208 } 210 211 public void updateAllWidgetBounds() { 212 } 214 215 public Point getGUIPoint(int clientX, int clientY) { 216 return new Point(clientX, clientY); 217 } 218 219 public void focus() { 220 } 222 223 public void addDelayedPair(DelayedPair pair) { 224 } 226 227 public RenderableContainer getParentContainer() { 228 return null; 229 } 230 231 public Collection getDelayedPairs() { 232 return null; 233 } 234 235 public void clearDelayedPairs() { 236 } 237 } 238 239 private class LocalFrameContext implements FrameContext { 240 public void expandSelection(RenderableSpot rpoint) { 241 } 242 243 public void resetSelection(RenderableSpot rpoint) { 244 } 245 246 public void delayedRelayout(NodeImpl node) { 247 } 248 } 249 } 250 | Popular Tags |