KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
43 import java.text.*;
44 import java.net.*;
45 import java.io.File JavaDoc;
46
47 public class Engine {
48     
49     public Engine() {
50     }
51     
52     public TestSuite start(String JavaDoc configFile) {
53         TestSuite testSuite = null;
54         Boolean JavaDoc computeResult = null;
55                 
56         try {
57             // Load config file
58
ConfigFileLoader cfl = new ConfigFileLoader(configFile);
59             testSuite = cfl.getTestSuite();
60             
61             // Iterate through each driver
62
Iterator jdi = testSuite.getDriverInfoList().iterator();
63             while (jdi.hasNext()) {
64                 DriverInfo di = (DriverInfo) jdi.next();
65                 
66                 // Display driver's name
67
System.out.print(" " + di.getName());
68                 
69                 int runsPerDriver = testSuite.getIntParam(Constants.RUNS_PER_DRIVER);
70
71                 // Allocate a matrix of nOfThreads * runPerDriver size and initialize each instance
72
int nOfThreads = testSuite.getIntParam(Constants.NUMBER_OF_THREADS);
73                 JapexDriverBase drivers[][] = new JapexDriverBase[nOfThreads][runsPerDriver];
74                 for (int i = 0; i < nOfThreads; i++) {
75                     for (int j = 0; j < runsPerDriver; j++) {
76                         drivers[i][j] = di.getJapexDriver(); // returns fresh copy
77
drivers[i][j].setTestSuite(testSuite);
78                         drivers[i][j].initializeDriver();
79                     }
80                 }
81                 
82                 for (int driverRun = 0; driverRun < runsPerDriver; driverRun++) {
83                     System.out.print("\n Run " + (driverRun + 1) + ": ");
84                     
85                     // Get list of tests
86
List tcList = di.getTestCases(driverRun);
87                     int nOfTests = tcList.size();
88                     
89                     // geometric mean = (sum{i,n} x_i) / n
90
double geomMeanresult = 1.0;
91                     // arithmetic mean = (prod{i,n} x_i)^(1/n)
92
double aritMeanresult = 0.0;
93                     // harmonic mean inverse = sum{i,n} 1/(n * x_i)
94
double harmMeanresultInverse = 0.0;
95                                 
96                     // Iterate through list of test cases
97
Iterator tci = tcList.iterator();
98                     while (tci.hasNext()) {
99                         long runTime = 0L;
100                         TestCase tc = (TestCase) tci.next();
101
102                         System.out.print(tc.getTestName() + ",");
103
104                         // If nOfThreads == 1, re-use this thread
105
if (nOfThreads == 1) {
106                             drivers[0][driverRun].setTestCase(tc); // tc is shared!
107
drivers[0][driverRun].prepareAndWarmup();
108
109                             // Start timer
110
runTime = Util.currentTimeMillis();
111
112                             drivers[0][driverRun].run();
113                         }
114                         else { // nOfThreads > 1
115
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
116
117                             // Create one thread for each driver instance
118
Thread JavaDoc threads[] = new Thread JavaDoc[nOfThreads];
119                             for (int i = 0; i < nOfThreads; i++) {
120                                 threads[i] = new Thread JavaDoc(drivers[i][driverRun]);
121                                 threads[i].setPriority(Thread.MAX_PRIORITY);
122                             }
123
124                             // Initialize driver instance with test case object
125
// and do prepare and warmup phases
126
for (int i = 0; i < nOfThreads; i++) {
127                                 drivers[i][driverRun].setTestCase(tc); // tc is shared!
128
drivers[i][driverRun].prepareAndWarmup();
129                             }
130
131                             // Start timer
132
runTime = Util.currentTimeMillis();
133
134                             // Fork all threads
135
for (int i = 0; i < nOfThreads; i++) {
136                                 threads[i].start();
137                             }
138
139                             // Wait for all threads to finish
140
for (int i = 0; i < nOfThreads; i++) {
141                                 threads[i].join();
142                             }
143                         }
144
145                         // Stop timer
146
runTime = Util.currentTimeMillis() - runTime;
147
148                         // Set japex.actualRunTime output param
149
if (!testSuite.hasParam(Constants.RUN_TIME)) {
150                             tc.setDoubleParam(Constants.ACTUAL_RUN_TIME, runTime);
151                         }
152
153                         // Do finish phase
154
for (int i = 0; i < nOfThreads; i++) {
155                             drivers[i][driverRun].finish(tc);
156                         }
157
158                         double result = 0.0;
159
160                         if (computeResult == null) {
161                             if (!tc.hasParam(Constants.RESULT_VALUE)) {
162                                 result =
163                                     tc.getIntParam(Constants.ACTUAL_RUN_ITERATIONS) /
164                                         (runTime / 1000.0);
165                                 testSuite.setParam(Constants.RESULT_UNIT, "TPS");
166
167                                 computeResult = Boolean.TRUE;
168                                 tc.setDoubleParam(Constants.RESULT_VALUE, result);
169                             }
170                             else {
171                                 result = tc.getDoubleParam(Constants.RESULT_VALUE);
172                                 computeResult = Boolean.FALSE;
173                             }
174                         }
175                         else if (computeResult == Boolean.TRUE
176                                  && !tc.hasParam(Constants.RESULT_VALUE))
177                         {
178                             result =
179                                 tc.getIntParam(Constants.ACTUAL_RUN_ITERATIONS) /
180                                     (runTime / 1000.0);
181                             tc.setDoubleParam(Constants.RESULT_VALUE, result);
182                         }
183                         else if (computeResult == Boolean.FALSE
184                                  && tc.hasParam(Constants.RESULT_VALUE))
185                         {
186                             result = tc.getDoubleParam(Constants.RESULT_VALUE);
187                         }
188                         else {
189                             throw new RuntimeException JavaDoc(
190                                "The output parameter '" + Constants.RESULT_VALUE
191                                + "' must be computed by either all or none of "
192                                + "the drivers for the results to be comparable");
193                         }
194
195                         // Compute running means
196
aritMeanresult += result / nOfTests;
197                         geomMeanresult *= Math.pow(result, 1.0 / nOfTests);
198                         harmMeanresultInverse += 1.0 / (nOfTests * result);
199                     
200                         // Display results for this test
201
System.out.print(tc.getDoubleParam(Constants.RESULT_VALUE) + ",");
202                         System.out.flush();
203                     }
204                 
205                 
206                     System.out.print(
207                         "aritmean," + Util.formatDouble(aritMeanresult) +
208                         ",geommean," + Util.formatDouble(geomMeanresult) +
209                         ",harmmean," + Util.formatDouble(1.0 / harmMeanresultInverse));
210                     
211                     // Call terminate on all driver instances
212
for (int i = 0; i < nOfThreads; i++) {
213                         drivers[i][driverRun].terminateDriver();
214                     }
215                 }
216                 
217                 if (runsPerDriver > 1) {
218                     // Print average for all runs
219
System.out.print("\n Avgs: ");
220                     Iterator tci = di.getAggregateTestCases().iterator();
221                     while (tci.hasNext()) {
222                         TestCase tc = (TestCase) tci.next();
223                         System.out.print(tc.getTestName() + ",");
224                         System.out.print(tc.getDoubleParam(Constants.RESULT_VALUE) + ",");
225                     }
226                     System.out.print(
227                         "aritmean," +
228                         di.getDoubleParam(Constants.RESULT_ARIT_MEAN) +
229                         ",geommean," +
230                         di.getDoubleParam(Constants.RESULT_GEOM_MEAN) +
231                         ",harmmean," +
232                         di.getDoubleParam(Constants.RESULT_HARM_MEAN));
233
234                     // Print standardDevs for all runs
235
System.out.print("\n Stdev: ");
236                     tci = di.getAggregateTestCases().iterator();
237                     while (tci.hasNext()) {
238                         TestCase tc = (TestCase) tci.next();
239                         System.out.print(tc.getTestName() + ",");
240                         System.out.print(tc.getDoubleParam(Constants.RESULT_VALUE_STDDEV) + ",");
241                     }
242                     System.out.println(
243                         "aritmean," +
244                         di.getDoubleParam(Constants.RESULT_ARIT_MEAN_STDDEV) +
245                         ",geommean," +
246                         di.getDoubleParam(Constants.RESULT_GEOM_MEAN_STDDEV) +
247                         ",harmmean," +
248                         di.getDoubleParam(Constants.RESULT_HARM_MEAN_STDDEV));
249                 }
250                 else {
251                     System.out.println("");
252                 }
253             }
254         }
255         catch (Exception JavaDoc e) {
256             e.printStackTrace();
257             throw new RuntimeException JavaDoc(e);
258         }
259
260         return testSuite;
261     }
262 }
263
Popular Tags