1 43 44 package de.susebox.jtopas; 45 46 import java.io.StringReader ; 50 import java.text.MessageFormat ; 51 import java.util.Date ; 52 53 import junit.framework.Test; 54 import junit.framework.TestCase; 55 import junit.framework.TestSuite; 56 import junit.framework.Assert; 57 58 import de.susebox.java.lang.ExtRuntimeException; 59 60 import de.susebox.TestUtilities; 61 62 63 67 75 public class TestTokenizerCleanup extends TestCase { 76 77 81 84 public static void main(String [] args) { 85 String [] tests = { TestTokenizerCleanup.class.getName() }; 86 87 TestUtilities.run(tests, args); 88 } 89 90 91 95 101 public static Test suite() { 102 TestSuite suite = new TestSuite(TestTokenizerCleanup.class.getName()); 103 104 suite.addTest(new TestTokenizerCleanup("testMemoryUsage")); 105 return suite; 106 } 107 108 109 113 117 public TestTokenizerCleanup(String test) { 118 super(test); 119 } 120 121 122 126 130 protected void setUp() throws Exception { 131 _properties = new StandardTokenizerProperties(); 133 134 _properties.setParseFlags(Flags.F_KEEP_DATA | Flags.F_RETURN_WHITESPACES); 135 136 String source = "This is just a source with lots of data:\n" 138 + "when it is multiplied, see below!\n\n" 139 + "There is >>nothing<< special here, since only the\n" 140 + "memory usage is of interest.\n"; 141 StringBuffer buffer = new StringBuffer (1024); 142 143 while (buffer.length() < 1024) { 144 buffer.append(source); 145 } 146 _source = buffer.toString(); 147 } 148 149 150 154 protected void tearDown() throws Exception { 155 _properties = null; 156 } 157 158 162 163 167 public void testMemoryUsage() throws Throwable { 168 long start = System.currentTimeMillis(); 169 Runtime runtime = Runtime.getRuntime(); 170 double totalMemoryStart = runtime.totalMemory(); 171 double freeMemoryStart = runtime.freeMemory(); 172 int currIndex = 0; 173 174 System.out.println( MessageFormat.format("Total/free memory on start (MB): {0}/{1}", 175 new Object [] { new Double (totalMemoryStart / (1024 * 1024)), 176 new Double (freeMemoryStart / (1024 * 1024)) } )); 177 178 for (int counter = 0; counter < 1000; ++counter) { 179 double totalMemory = runtime.totalMemory(); 180 double freeMemory = runtime.freeMemory(); 181 182 System.out.println( MessageFormat.format("{2}: Total/free memory before tokenizing (MB): {0}/{1}", 183 new Object [] { new Double (totalMemory / (1024 * 1024)), 184 new Double (freeMemory / (1024 * 1024)), 185 new Long (System.currentTimeMillis()) } )); 186 187 Tokenizer tokenizer = new StandardTokenizer(_properties); 189 190 try { 191 tokenizer.setSource(new ReaderSource(new StringReader (_source))); 192 193 while (tokenizer.hasMoreToken()) { 194 tokenizer.nextToken(); 195 tokenizer.currentImage(); 196 } 197 System.out.println( MessageFormat.format("{2}: Total/free memory after tokenizing (MB): {0}/{1}", 198 new Object [] { new Double (totalMemory / (1024 * 1024)), 199 new Double (freeMemory / (1024 * 1024)), 200 new Long (System.currentTimeMillis()) } )); 201 } finally { 202 tokenizer.close(); 204 totalMemory = runtime.totalMemory(); 205 freeMemory = runtime.freeMemory(); 206 System.out.println( MessageFormat.format("{2}: Total/free memory after closing (MB): {0}/{1}", 207 new Object [] { new Double (totalMemory / (1024 * 1024)), 208 new Double (freeMemory / (1024 * 1024)), 209 new Long (System.currentTimeMillis()) } )); 210 211 for (int ii = 0; ii < 5; ++ii) { 213 tokenizer.close(); 214 } 215 216 synchronized(this) { 218 try { 219 wait(10); 220 } catch (InterruptedException ex) {} 221 } 222 } 223 } 224 225 long diff = System.currentTimeMillis() - start; 226 System.out.println("Finished after " + diff + " milliseconds"); 227 } 228 229 230 protected volatile TokenizerProperties _properties = null; 234 protected volatile String _source = null; 235 } 236 | Popular Tags |