KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > susebox > jtopas > TestTokenizerCleanup


1 /*
2  * TestTokenizerCleanup.java: JUnit test for the StandardTokenizer
3  *
4  * Copyright (C) 2003 Heiko Blau
5  *
6  * This file belongs to the JTopas test suite.
7  * The JTopas test suite is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or (at your option)
10  * any later version.
11  *
12  * This software is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License along
18  * with the JTopas test suite. If not, write to the
19  *
20  * Free Software Foundation, Inc.
21  * 59 Temple Place, Suite 330,
22  * Boston, MA 02111-1307
23  * USA
24  *
25  * or check the Internet: http://www.fsf.org
26  *
27  * The JTopas test suite uses the test framework JUnit by Kent Beck and Erich Gamma.
28  * You should have received a copy of their JUnit licence agreement along with
29  * the JTopas test suite.
30  *
31  * We do NOT provide the JUnit archive junit.jar nessecary to compile and run
32  * our tests, since we assume, that You either have it already or would like
33  * to get the current release Yourself.
34  * Please visit either:
35  * http://sourceforge.net/projects/junit
36  * or
37  * http://junit.org
38  * to obtain JUnit.
39  *
40  * Contact:
41  * email: heiko@susebox.de
42  */

43
44 package de.susebox.jtopas;
45
46 //-----------------------------------------------------------------------------
47
// Imports
48
//
49
import java.io.StringReader JavaDoc;
50 import java.text.MessageFormat JavaDoc;
51 import java.util.Date JavaDoc;
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 //-----------------------------------------------------------------------------
64
// Class TestTokenizerCleanup
65
//
66

67 /**<p>
68  * This test suite checks memory usage, registration end other things related
69  * to cleanup operations of a {@link Tokenizer}.
70  *</p>
71  *
72  * @see Tokenizer
73  * @author Heiko Blau
74  */

75 public class TestTokenizerCleanup extends TestCase {
76   
77   //---------------------------------------------------------------------------
78
// main method
79
//
80

81   /**
82    * call this method to invoke the tests
83    */

84   public static void main(String JavaDoc[] args) {
85     String JavaDoc[] tests = { TestTokenizerCleanup.class.getName() };
86
87     TestUtilities.run(tests, args);
88   }
89   
90
91   //---------------------------------------------------------------------------
92
// suite method
93
//
94

95   /**
96    * Implementation of the JUnit method <code>suite</code>. For each set of test
97    * properties one or more tests are instantiated.
98    *
99    * @return a test suite
100    */

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   //---------------------------------------------------------------------------
110
// Constructor
111
//
112

113   /**
114    * Default constructor. Standard input {@link java.lang.System#in} is used
115    * to construct the input stream reader.
116    */

117   public TestTokenizerCleanup(String JavaDoc test) {
118     super(test);
119   }
120
121   
122   //---------------------------------------------------------------------------
123
// Fixture setup and release
124
//
125

126     /**
127      * Sets up the fixture, for example, open a network connection.
128      * This method is called before a test is executed.
129      */

130   protected void setUp() throws Exception JavaDoc {
131     // empty properties
132
_properties = new StandardTokenizerProperties();
133
134     _properties.setParseFlags(Flags.F_KEEP_DATA | Flags.F_RETURN_WHITESPACES);
135     
136     // create a large source
137
String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc(1024);
142     
143     while (buffer.length() < 1024) {
144       buffer.append(source);
145     }
146     _source = buffer.toString();
147   }
148
149   
150     /**
151      * Tears down the fixture, for example, close a network connection.
152      * This method is called after a test is executed.
153      */

154     protected void tearDown() throws Exception JavaDoc {
155     _properties = null;
156     }
157   
158   //---------------------------------------------------------------------------
159
// test cases
160
//
161

162   
163   /**
164    * The method creates a huge amount of tokenizers and dismisses them again.
165    * Memory usage should be fairly constant.
166    */

167   public void testMemoryUsage() throws Throwable JavaDoc {
168     long start = System.currentTimeMillis();
169     Runtime JavaDoc 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 JavaDoc[] { new Double JavaDoc(totalMemoryStart / (1024 * 1024)),
176                                        new Double JavaDoc(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 JavaDoc[] { new Double JavaDoc(totalMemory / (1024 * 1024)),
184                                          new Double JavaDoc(freeMemory / (1024 * 1024)),
185                                          new Long JavaDoc(System.currentTimeMillis()) } ));
186       
187       // create tokenizer and parse the source
188
Tokenizer tokenizer = new StandardTokenizer(_properties);
189
190       try {
191         tokenizer.setSource(new ReaderSource(new StringReader JavaDoc(_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 JavaDoc[] { new Double JavaDoc(totalMemory / (1024 * 1024)),
199                                            new Double JavaDoc(freeMemory / (1024 * 1024)),
200                                            new Long JavaDoc(System.currentTimeMillis()) } ));
201       } finally {
202         // close the tokenizer and see what happens to memory
203
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 JavaDoc[] { new Double JavaDoc(totalMemory / (1024 * 1024)),
208                                            new Double JavaDoc(freeMemory / (1024 * 1024)),
209                                            new Long JavaDoc(System.currentTimeMillis()) } ));
210         
211         // try to close it a few times
212
for (int ii = 0; ii < 5; ++ii) {
213           tokenizer.close();
214         }
215                                            
216         // dont block the system :-)
217
synchronized(this) {
218           try {
219             wait(10);
220           } catch (InterruptedException JavaDoc ex) {}
221         }
222       }
223     }
224
225     long diff = System.currentTimeMillis() - start;
226     System.out.println("Finished after " + diff + " milliseconds");
227   }
228   
229   
230   //---------------------------------------------------------------------------
231
// Members
232
//
233
protected volatile TokenizerProperties _properties = null;
234   protected volatile String JavaDoc _source = null;
235 }
236
Popular Tags