KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > harness > T_MultiIterations


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.harness.T_MultiIterations
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.harness;
23
24 import org.apache.derby.iapi.services.monitor.Monitor;
25 import org.apache.derby.iapi.services.property.PropertyUtil;
26
27 import java.util.Properties JavaDoc;
28
29 /**
30     Abstract class which executes T_Generic. This splits the running
31     of a test into two parts, the test setup and running the test.
32     This allows the setup to be performed once, and then the
33     test itself to be run for a number of iterations. The number
34     iterations is set by the property derby.unittests.iterations
35     and defaults to 1.
36     <P>
37     Statistics are provided about each iteration in the error log. The statistics
38     are time for each iteration, used and total memory changes per iteration.
39
40     @see T_Generic
41 */

42 public abstract class T_MultiIterations extends T_Generic
43 {
44     protected T_MultiIterations()
45     {
46         super();
47     }
48
49     /*
50     ** methods required by T_Generic
51     */

52
53     
54     /**
55       Run the test. The test should raise an exception if it
56       fails. runTests should return if the tests pass.
57
58       @exception T_Fail Test code throws these
59       */

60     protected void runTests() throws T_Fail {
61
62         setupTest();
63
64         int iterations = 1;
65
66         /*
67         ** The property name for the number of iterations is
68         ** derby.className.iterations. For example, if the test
69         ** class is derby.com.package.to.test.T_Tester,
70         ** the property name is derby.T_Tester.iterations.
71         */

72         String JavaDoc myClass = this.getClass().getName();
73         String JavaDoc noPackage = myClass.substring(myClass.lastIndexOf('.') + 1);
74         String JavaDoc propertyName = "derby." + noPackage + ".iterations";
75
76         String JavaDoc iter = PropertyUtil.getSystemProperty(propertyName);
77         if (iter != null) {
78             try {
79                 iterations = Integer.parseInt(iter);
80             } catch (NumberFormatException JavaDoc nfe) {
81                 // leave at one
82
}
83             if (iterations <= 0)
84                 iterations = 1;
85         }
86
87         for (int i = 0; i < iterations; i++) {
88             Runtime.getRuntime().gc();
89             long btm = Runtime.getRuntime().totalMemory();
90             long bfm = Runtime.getRuntime().freeMemory();
91             long bum = btm - bfm;
92
93             long start = System. currentTimeMillis();
94
95             runTestSet();
96
97             long end = System. currentTimeMillis();
98
99             Runtime.getRuntime().gc();
100             long atm = Runtime.getRuntime().totalMemory();
101             long afm = Runtime.getRuntime().freeMemory();
102             long aum = atm - afm;
103
104             out.println("Iteration " + i + " took " + (end - start) + "ms");
105             out.println("Total memory increased by " + (atm - btm) + " is " + atm);
106             out.println("Used memory increased by " + (aum - bum) + " is " + aum);
107         }
108     }
109
110     /*
111     ** Abstract methods to implement for your test.
112     */

113
114     /**
115         Run once to set up the test.
116
117         @exception T_Fail Test code throws these
118     */

119     protected abstract void setupTest() throws T_Fail;
120
121     /**
122         Run once per-iteration to run the actual test.
123
124         @exception T_Fail Test code throws these
125     */

126     protected abstract void runTestSet() throws T_Fail;
127 }
128
Popular Tags