KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > JapexDriverBase


1 /*
2  * Japex ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * This Software is distributed under the following terms:
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, is permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistribution in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based names,
19  * nor the names of contributors may be used to endorse or promote products
20  * derived from this Software without specific prior written permission.
21  *
22  * The Software is provided "AS IS," without a warranty of any kind. ALL
23  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
24  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25  * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS
26  * SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE
27  * AS A RESULT OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE
28  * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE
29  * LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
30  * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED
31  * AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
32  * INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGES.
34  *
35  * You acknowledge that the Software is not designed, licensed or intended
36  * for use in the design, construction, operation or maintenance of any
37  * nuclear facility.
38  */

39     
40 package com.sun.japex;
41
42 import java.util.Properties JavaDoc;
43
44 public class JapexDriverBase implements JapexDriver {
45     
46     private TestSuite _testSuite;
47     private TestCase _testCase;
48     
49     public void setTestSuite(TestSuite testSuite) {
50         _testSuite = testSuite;
51     }
52     
53     public void setTestCase(TestCase testCase) {
54         _testCase = testCase;
55     }
56     
57     protected TestSuite getTestSuite() {
58         return _testSuite;
59     }
60     
61     /**
62      * Execute prepare and warmup phases. Even in multi-threaded test, this
63      * method will only be executed in single-threaded mode, so there's no
64      * need for additional synchronization.
65      */

66     public void prepareAndWarmup() {
67         long millis, nanos;
68         TestCase tc = _testCase;
69         
70         // -- Prepare ----------------------------------------
71

72         nanos = Util.currentTimeNanos();
73         prepare(tc);
74         nanos = Util.currentTimeNanos() - nanos;
75         tc.setDoubleParam(Constants.ACTUAL_PREPARE_TIME,
76                           Util.nanosToMillis(nanos));
77
78         // -- Warmup -----------------------------------------
79

80         int warmupIterations = 0;
81         String JavaDoc warmupTime = tc.getParam(Constants.WARMUP_TIME);
82         if (warmupTime != null) {
83             // Calculate end time
84
long startTime = millis = Util.currentTimeMillis();
85             long endTime = startTime + Util.parseDuration(warmupTime);
86
87             while (endTime > millis) {
88                 warmup(tc); // Call warmup
89
warmupIterations++;
90                 millis = Util.currentTimeMillis();
91             }
92
93             // Set actual number of millis
94
millis -= startTime;
95
96             // Set actual number of iterations
97
tc.setIntParam(Constants.ACTUAL_WARMUP_ITERATIONS, warmupIterations);
98         }
99         else {
100             // Adjust warmup iterations based on number of threads
101
warmupIterations = tc.getIntParam(Constants.WARMUP_ITERATIONS);
102             
103             nanos = Util.currentTimeNanos();
104             for (int i = 0; i < warmupIterations; i++) {
105                 warmup(tc); // Call warmup
106
}
107             nanos = Util.currentTimeNanos() - nanos;
108
109             // Set actual warmup time
110
tc.setDoubleParam(Constants.ACTUAL_WARMUP_TIME,
111                               Util.nanosToMillis(nanos));
112         }
113     }
114     
115     // -- Runnable interface ------------------------------------------
116

117     /**
118      * Execute the run phase. This method can be executed concurrently
119      * by multiple threads. Care should be taken to ensure proper
120      * synchronization. Note that parameter getters and setters are
121      * already synchronized.
122      */

123     public void run() {
124         long millis, nanos;
125         TestCase tc = _testCase;
126         
127         // Force GC before running test
128
System.gc();
129         
130         // Get number of threads to adjust iterations
131
int nOfThreads = tc.getIntParam(Constants.NUMBER_OF_THREADS);
132         
133         int runIterations = 0;
134         String JavaDoc runTime = tc.getParam(Constants.RUN_TIME);
135         if (runTime != null) {
136             // Calculate end time
137
long startTime = Util.currentTimeMillis();
138             long endTime = startTime + Util.parseDuration(runTime);
139
140             // Run phase
141
do {
142                 run(tc); // Call run
143
runIterations++;
144                 millis = Util.currentTimeMillis();
145             } while (endTime >= millis);
146
147             // Set actual number of millis
148
millis -= startTime;
149         }
150         else {
151             // Adjust runIterations based on number of threads
152
runIterations = tc.getIntParam(Constants.RUN_ITERATIONS) / nOfThreads;
153
154             // Run phase
155
nanos = Util.currentTimeNanos();
156             for (int i = 0; i < runIterations; i++) {
157                 run(tc); // Call run
158
}
159             nanos = Util.currentTimeNanos() - nanos;
160             
161         }
162         
163         // Accumulate actual number of iterations
164
synchronized (tc) {
165             int actualRunIterations =
166                 tc.hasParam(Constants.ACTUAL_RUN_ITERATIONS) ?
167                     tc.getIntParam(Constants.ACTUAL_RUN_ITERATIONS) : 0;
168             tc.setIntParam(Constants.ACTUAL_RUN_ITERATIONS,
169                            actualRunIterations + runIterations);
170         }
171     }
172     
173     // -- JapexDriver interface ------------------------------------------
174

175     /**
176      * Called once when the class is loaded.
177      */

178     public void initializeDriver() {
179     }
180     
181     /**
182      * Called exactly once for every test, before calling warmup.
183      */

184     public void prepare(TestCase testCase) {
185     }
186     
187     /**
188      * Called once or more for every test, before calling run.
189      */

190     public void warmup(TestCase testCase) {
191     }
192     
193     /**
194      * Called once or more for every test to obtain perf data.
195      */

196     public void run(TestCase testCase) {
197     }
198     
199     /**
200      * Called exactly once after calling run.
201      */

202     public void finish(TestCase testCase) {
203     }
204     
205     /**
206      * Called after all tests are completed.
207      */

208     public void terminateDriver() {
209     }
210     
211 }
212
Popular Tags