KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > format > test > ConfigurationPerformanceTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.config.format.test;
19
20 import java.util.Date JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24 import org.sape.carbon.core.config.Config;
25 import org.sape.carbon.core.config.Configuration;
26 import org.sape.carbon.core.config.ConfigurationStoreException;
27 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService;
28 import org.sape.carbon.core.util.classify.SeverityEnum;
29
30 import junit.extensions.ActiveTestSuite;
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35 /**
36  * <p>Tests the speed of the configuration service by creating configurations
37  * and repeatedly setting values, reading them from disk and writing them
38  * to disk. Also contains a test that creates a larger than 1 Megabyte config
39  * file to test large file handling.</p>
40  *
41  * Copyright 2003 Sapient
42  * @since carbon 2.0
43  * @author Greg Hinkle, March 2003
44  * @version $Revision: 1.4 $($Author: dvoet $ / $Date: 2003/05/05 21:21:17 $)
45  */

46 public class ConfigurationPerformanceTest extends TestCase {
47
48     public static final String JavaDoc TEST_STORE_LOCATION = "/core/config/test/PerformanceTest";
49     public static final String JavaDoc TEST_LARGE_DOCUMENT = "/core/config/test/LargeDocument";
50     public ConfigurationPerformanceTest(String JavaDoc name) {
51         super(name);
52     }
53
54     /**
55      * Runs some basic speed tests on the configuration service:
56      * <UL>
57      * <LI>Read a document 1000 times - Test the node traversal, but gets a cached document</LI>
58      * <LI>Clone a document 1000 times - Tests org.jdom.Document cloning</LI>
59      * <LI>Write a document 1000 times - Test the JDOM XML serializer's speed</LI>
60      * <LI>Read a Double attribute 10000 times - Test jdom access and DoubleTypeFormat</LI>
61      * <LI>Read an Array with two attributes 10000 times - Tests Proxy construction for sub document</LI>
62      * <LI>Write a double attribute 10000 times - Test the storage of a double into Document and DoubleTypeFormat</LI>
63      * <LI>Write an array attribute 10000 times - Tests the array setting functionality</LI>
64      * <li>Set a map of five config objects 1000 times</li>
65      * <li>Set 1000 individual map attributes</li>
66      * </UL>
67      *
68      */

69     public void testSpeed() throws Exception JavaDoc {
70
71         DefaultConfigurationFormatService formatService =
72             new DefaultConfigurationFormatService();
73
74         TestConfig
75             config =
76             (TestConfig)
77             formatService.newConfiguration(TestConfig.class);
78
79         fillTestConfig(config);
80
81
82         long start;
83         long iterations = 1000;
84
85
86         start = System.currentTimeMillis();
87         for (int i=0; i < iterations;i++) {
88             Config.getInstance().storeConfiguration(
89                 TEST_STORE_LOCATION,
90                 config);
91         }
92         System.out.println("Write document " + iterations + " times took: " + (System.currentTimeMillis() - start));
93
94         start = System.currentTimeMillis();
95         for (int i=0; i < iterations;i++) {
96             TestConfig newC =
97                 (TestConfig)
98                 Config.getInstance().fetchConfiguration(TEST_STORE_LOCATION);
99         }
100         System.out.println("Read document " + iterations + " times took: " + (System.currentTimeMillis() - start));
101
102
103         start = System.currentTimeMillis();
104         for (int i=0; i < iterations;i++) {
105             TestConfig clone =
106                 (TestConfig)
107                 config.clone();
108         }
109         System.out.println("Clone document " + iterations + " times took: " + (System.currentTimeMillis() - start));
110
111
112
113         iterations = 10000;
114
115         start = System.currentTimeMillis();
116         for (int i=0; i < iterations;i++) {
117             config.getMyDouble();
118         }
119         System.out.println("Read attribute " + iterations + " times took: " + (System.currentTimeMillis() - start));
120
121         start = System.currentTimeMillis();
122         for (int i=0; i < iterations;i++) {
123             config.getMyArray();
124         }
125         System.out.println("Read array " + iterations + " times took: " + (System.currentTimeMillis() - start));
126
127         start = System.currentTimeMillis();
128         for (int i=0; i < iterations;i++) {
129             config.setMyDouble(4.594554D);
130         }
131         System.out.println("Write attribute " + iterations + " times took: " + (System.currentTimeMillis() - start));
132
133
134         String JavaDoc[] strArray = new String JavaDoc[] { "a", "b", "c", "d" };
135         start = System.currentTimeMillis();
136         for (int i=0; i < iterations;i++) {
137             config.setStringArray(strArray);
138         }
139         System.out.println("Write string array " + iterations + " times took: " + (System.currentTimeMillis() - start));
140
141
142         // Test the speed of setting an entire map
143
Map JavaDoc testConfigMap = new HashMap JavaDoc();
144         for (int i = 0; i < 5; i++) {
145             TestConfig testConfig =
146                 (TestConfig)
147                 Config.getInstance().createConfiguration(TestConfig.class);
148             testConfig.setMyString("Sub-map-child " + i);
149             testConfigMap.put("key-"+i, testConfig);
150         }
151         start = System.currentTimeMillis();
152         for (int i=0; i < iterations;i++) {
153             config.setTestConfigMap(testConfigMap);
154         }
155         System.out.println("Setting a complete Config Map " + iterations + " times took: " + (System.currentTimeMillis() - start));
156
157
158         // Tests setting map attributes individually
159
TestConfig testConfig =
160             (TestConfig)
161             Config.getInstance().createConfiguration(TestConfig.class);
162         testConfig.setMyString("Sub-map-child generic");
163
164         start = System.currentTimeMillis();
165         for (int i=0; i < iterations;i++) {
166             config.setTestConfigMap(String.valueOf(i%5), testConfig);
167         }
168         System.out.println("Setting individual Config Map " + iterations + " times took: " + (System.currentTimeMillis() - start));
169
170     }
171
172     private void fillTestConfig(TestConfig config) {
173         // Setup values
174
config.setMyBool(true);
175         config.setMyByte((byte)0x12);
176         config.setMyShort((short)32);
177         config.setMyInt(42);
178         config.setMyLong(5535443L);
179         config.setMyFloat(39522343.32F);
180         config.setMyDouble(92325223.92343242D);
181         config.setMyString("Hello, Carbon World!");
182         config.setMyClass(this.getClass());
183         config.setMyDate(new Date JavaDoc());
184
185         for (int i = 0; i < 5; i++) {
186             TestConfig testConfig =
187                 (TestConfig)
188                 Config.getInstance().createConfiguration(TestConfig.class);
189             testConfig.setMyString("Sub-array-child " + i);
190             config.addMyArray(testConfig);
191         }
192
193         for (int i = 0; i < 5; i++) {
194             config.addIntArray(i*2);
195         }
196
197         for (int i = 0; i < 5; i++) {
198             config.addStringArray("String-array-child " + i);
199         }
200
201         for (int i = 0; i < 5; i++) {
202             config.setStringMap("key-" + String.valueOf(i),"String-map-child " + i);
203         }
204
205         for (int i = 0; i < 5; i++) {
206             TestConfig testConfig =
207                 (TestConfig)
208                 Config.getInstance().createConfiguration(TestConfig.class);
209             testConfig.setMyString("Sub-map-child " + i);
210             config.setTestConfigMap("key-" + String.valueOf(i), testConfig);
211         }
212
213         config.setEnum(SeverityEnum.FATAL);
214
215         Configuration childConfig =
216             Config.getInstance().createConfiguration(TestConfig.class);
217
218         config.setSubConfiguration(childConfig);
219     }
220
221     /**
222      * Created a file over 1 Megabyte to test large document support.
223      * @throws ConfigurationStoreException
224      */

225     public void testLargeDocument() throws ConfigurationStoreException {
226         TestConfig testConfig =
227             (TestConfig)
228             Config.getInstance().createConfiguration(TestConfig.class);
229
230         testConfig.setMyString("This is a very large document test");
231
232         int collectionsSize = 200;
233
234         for (int i = 0; i < collectionsSize; i++) {
235             TestConfig child =
236                 (TestConfig)
237                 Config.getInstance().createConfiguration(TestConfig.class);
238             child.setMyString("Sub-array-child " + i);
239             fillTestConfig(child);
240             testConfig.addMyArray(child);
241         }
242
243         for (int i = 0; i < collectionsSize; i++) {
244             TestConfig child =
245                 (TestConfig)
246                 Config.getInstance().createConfiguration(TestConfig.class);
247             child.setMyString("Sub-map-child " + i);
248             fillTestConfig(child);
249             testConfig.setTestConfigMap("key-"+i, child);
250         }
251
252         Config.getInstance().storeConfiguration(
253             TEST_LARGE_DOCUMENT, testConfig);
254     }
255
256
257     public static interface TestConfig extends Configuration {
258
259         // Primitives and simple object types
260
boolean MyBool = true;
261         boolean getMyBool();
262         void setMyBool(boolean value);
263
264         byte MyByte = (byte)0x12;
265         byte getMyByte();
266         void setMyByte(byte value);
267
268         short MyShort = 42;
269         short getMyShort();
270         void setMyShort(short value);
271
272         int MyInt = 495;
273         int getMyInt();
274         void setMyInt(int value);
275
276         long MyLong = 255433L;
277         long getMyLong();
278         void setMyLong(long value);
279
280         float MyFloat = 39522343.32F;
281         float getMyFloat();
282         void setMyFloat(float value);
283
284         double MyDouble = 92343.234323523D;
285         double getMyDouble();
286         void setMyDouble(double value);
287
288         String JavaDoc MyString = "Default String";
289         String JavaDoc getMyString();
290         void setMyString(String JavaDoc value);
291
292
293         Class JavaDoc MyClass = TestConfig.class;
294         Class JavaDoc getMyClass();
295         void setMyClass(Class JavaDoc value);
296
297         Date JavaDoc MyDate = new Date JavaDoc();
298         Date JavaDoc getMyDate();
299         void setMyDate(Date JavaDoc value);
300
301         // Array types
302
TestConfig[] getMyArray();
303         TestConfig getMyArray(int index);
304         void setMyArray(TestConfig[] value);
305         void addMyArray(TestConfig value);
306         void setMyArray(int index, TestConfig value);
307
308         int[] getIntArray();
309         int getIntArray(int index);
310         void setIntArray(int[] value);
311         void addIntArray(int value);
312         void setIntArray(int index, int value);
313
314         String JavaDoc[] getStringArray();
315         String JavaDoc getStringArray(int index);
316         void setStringArray(String JavaDoc[] value);
317         void addStringArray(String JavaDoc value);
318         void setStringArray(int index, String JavaDoc value);
319
320         String JavaDoc[] getEmptyArray();
321         void setEmptyArray(String JavaDoc[] value);
322
323
324         // Map Types
325
Map JavaDoc getStringMap();
326         String JavaDoc getStringMap(String JavaDoc key);
327         void setStringMap(String JavaDoc key, String JavaDoc value);
328         void setStringMap(Map JavaDoc stringMap);
329
330         Map JavaDoc getTestConfigMap();
331         TestConfig getTestConfigMap(String JavaDoc key);
332         void setTestConfigMap(Map JavaDoc testConfigMap);
333         void setTestConfigMap(String JavaDoc key, TestConfig value);
334
335
336         // Other tests
337
SeverityEnum Enum = SeverityEnum.FATAL;
338         SeverityEnum getEnum();
339         void setEnum(SeverityEnum value);
340
341         String JavaDoc getNullValue();
342         //void setNullValue(String value);
343

344         int getNullPrimitive();
345
346         String JavaDoc DefaultNullValue = null;
347         String JavaDoc getDefaultNullValue();
348
349         Configuration getSubConfiguration();
350         void setSubConfiguration(Configuration config);
351
352         Configuration getSubConfigurationRef();
353         void setSubConfigurationRef(Configuration config);
354
355     }
356
357
358
359     /**
360      * Method called by jUnit to get all the tests in this test case.
361      * @return Test the suite of tests in this test case
362      */

363     public static Test suite() {
364         TestSuite masterSuite = new TestSuite();
365
366         // add single threaded tests
367
Test singleThreadedTests = getSingleThreadedTests();
368         if (singleThreadedTests != null) {
369             masterSuite.addTest(singleThreadedTests);
370         }
371
372         // add multi threaded tests
373
Test multiThreadedTests = getMultiThreadedTests();
374         if (multiThreadedTests != null) {
375             masterSuite.addTest(multiThreadedTests);
376         }
377
378         return masterSuite;
379     }
380
381     /**
382      * This method is used within the suite method to get all of the single
383      * threaded tests.
384      *
385      * Add all your single threaded tests in this method with a line like:
386      * suite.addTest(new ConfigurationPerformanceTest("testFunction1"));
387      *
388      * @return Test the suite of single threaded tests in this test case
389      */

390     private static Test getSingleThreadedTests() {
391         TestSuite suite = new TestSuite();
392
393         suite.addTest(new ConfigurationPerformanceTest("testSpeed"));
394         suite.addTest(new ConfigurationPerformanceTest("testLargeDocument"));
395         return suite;
396     }
397
398     /**
399      * This method is used within the suite method to get all of the multi
400      * threaded tests.
401      *
402      * Add all your multi threaded tests in this method with a line like:
403      * addTest(suite, "testFunction1", 5);
404      *
405      * @return Test the suite of multi-threaded tests in this test case
406      */

407     private static Test getMultiThreadedTests() {
408         TestSuite suite = new ActiveTestSuite();
409         /*
410          * add your tests here following these examples:
411          *
412          * addTest(suite, "testFunction1", 5);
413          * addTest(suite, "testFunction2", 10);
414          */

415         return suite;
416     }
417
418     /**
419      * This method will add the give test to the give suite the specified
420      * number of times. This is best used for multi-threaded tests where
421      * suite is an instance of ActiveTestSuite and you want to run the same
422      * test in multiple threads.
423      *
424      * @param suite the suite to add the test to.
425      * @param testName the name of the test to add.
426      * @param number the number of times to add the test to the suite
427      */

428     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
429         for (int count = 0; count < number; count++) {
430             suite.addTest(new ConfigurationPerformanceTest(testName));
431         }
432     }
433 }
Popular Tags