KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.*;
44 import java.io.File JavaDoc;
45
46 public class DriverInfo extends Params {
47     
48     String JavaDoc _name;
49     JapexClassLoader _classLoader;
50     Class JavaDoc _class = null;
51     boolean _isNormal = false;
52     boolean _computeMeans = true;
53     
54     TestCaseArrayList[] _testCases;
55     TestCaseArrayList _aggregateTestCases;
56     
57     int _runsPerDriver;
58     
59     static class JapexClassLoader extends URLClassLoader {
60         public JapexClassLoader(URL[] urls) {
61             super(urls);
62         }
63         public Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
64             return super.findClass(name);
65         }
66         public void addURL(URL url) {
67             super.addURL(url);
68         }
69     }
70        
71     public DriverInfo(String JavaDoc name, boolean isNormal, int runsPerDriver,
72         Properties params)
73     {
74         super(params);
75         _name = name;
76         _isNormal = isNormal;
77         _classLoader = newJapexClassLoader();
78         
79         _runsPerDriver = runsPerDriver;
80     }
81     
82     public void setTestCases(TestCaseArrayList testCases) {
83         _testCases = new TestCaseArrayList[_runsPerDriver];
84         for (int i = 0; i < _runsPerDriver; i++) {
85             _testCases[i] = (TestCaseArrayList) testCases.clone();
86         }
87         
88         _aggregateTestCases = (TestCaseArrayList) testCases.clone();
89     }
90         
91     private void computeMeans() {
92         final int runsPerDriver = _testCases.length;
93         
94         // Avoid re-computing the driver's aggregates
95
if (_computeMeans) {
96             final int nOfTests = _testCases[0].size();
97
98             for (int n = 0; n < nOfTests; n++) {
99                 double avgRunsResult = 0.0;
100
101                 double[] results = new double[runsPerDriver];
102                 
103                 // Collect all results
104
for (int i = 0; i < runsPerDriver; i++) {
105                     TestCase tc = (TestCase) _testCases[i].get(n);
106                     results[i] = tc.getDoubleParam(Constants.RESULT_VALUE);
107                 }
108                 
109                 TestCase tc = (TestCase) _aggregateTestCases.get(n);
110                 tc.setDoubleParam(Constants.RESULT_VALUE, Util.arithmeticMean(results));
111                 if (runsPerDriver > 1) {
112                     tc.setDoubleParam(Constants.RESULT_VALUE_STDDEV,
113                             runsPerDriver > 1 ? Util.standardDev(results) : 0.0);
114                 }
115             }
116             
117             // geometric mean = (sum{i,n} x_i) / n
118
double geomMeanresult = 1.0;
119             // arithmetic mean = (prod{i,n} x_i)^(1/n)
120
double aritMeanresult = 0.0;
121             // harmonic mean inverse = sum{i,n} 1/(n * x_i)
122
double harmMeanresultInverse = 0.0;
123             
124             // Re-compute means based on averages for all runs
125
Iterator tci = _aggregateTestCases.iterator();
126             while (tci.hasNext()) {
127                 TestCase tc = (TestCase) tci.next();
128                 double result = tc.getDoubleParam(Constants.RESULT_VALUE);
129                 
130                 // Compute running means
131
aritMeanresult += result / nOfTests;
132                 geomMeanresult *= Math.pow(result, 1.0 / nOfTests);
133                 harmMeanresultInverse += 1.0 / (nOfTests * result);
134             }
135             
136             // Set driver-specific params
137
setDoubleParam(Constants.RESULT_ARIT_MEAN, aritMeanresult);
138             setDoubleParam(Constants.RESULT_GEOM_MEAN, geomMeanresult);
139             setDoubleParam(Constants.RESULT_HARM_MEAN, 1.0 / harmMeanresultInverse);
140             
141             // Avoid re-computing these means
142
_computeMeans = false;
143             
144             // If number of runs is just 1, we're done
145
if (runsPerDriver == 1) {
146                 return;
147             }
148             
149             // geometric mean = (sum{i,n} x_i) / n
150
geomMeanresult = 1.0;
151             // arithmetic mean = (prod{i,n} x_i)^(1/n)
152
aritMeanresult = 0.0;
153             // harmonic mean inverse = sum{i,n} 1/(n * x_i)
154
harmMeanresultInverse = 0.0;
155             
156             // Re-compute means based on averages for all runs
157
tci = _aggregateTestCases.iterator();
158             while (tci.hasNext()) {
159                 TestCase tc = (TestCase) tci.next();
160                 double result = tc.getDoubleParam(Constants.RESULT_VALUE_STDDEV);
161                 
162                 // Compute running means
163
aritMeanresult += result / nOfTests;
164                 geomMeanresult *= Math.pow(result, 1.0 / nOfTests);
165                 harmMeanresultInverse += 1.0 / (nOfTests * result);
166             }
167             
168             // Set driver-specific params
169
setDoubleParam(Constants.RESULT_ARIT_MEAN_STDDEV, aritMeanresult);
170             setDoubleParam(Constants.RESULT_GEOM_MEAN_STDDEV, geomMeanresult);
171             setDoubleParam(Constants.RESULT_HARM_MEAN_STDDEV, 1.0 / harmMeanresultInverse);
172         }
173     }
174     
175     public List getTestCases(int driverRun) {
176         return _testCases[driverRun];
177     }
178     
179     public List getAggregateTestCases() {
180         computeMeans();
181         return _aggregateTestCases;
182     }
183     
184     JapexDriverBase getJapexDriver() throws ClassNotFoundException JavaDoc {
185         String JavaDoc className = getParam(Constants.DRIVER_CLASS);
186         if (_class == null) {
187             _class = _classLoader.findClass(className);
188         }
189         
190         try {
191             Thread.currentThread().setContextClassLoader(_classLoader);
192             return (JapexDriverBase) _class.newInstance();
193         }
194         catch (InstantiationException JavaDoc e) {
195             throw new RuntimeException JavaDoc(e);
196         }
197         catch (IllegalAccessException JavaDoc e) {
198             throw new RuntimeException JavaDoc(e);
199         }
200         catch (ClassCastException JavaDoc e) {
201             throw new RuntimeException JavaDoc("Class '" + className
202                 + "' must extend '" + JapexDriverBase.class.getName() + "'");
203         }
204     }
205     
206     public String JavaDoc getName() {
207         return _name;
208     }
209     
210     public boolean isNormal() {
211         return _isNormal;
212     }
213     
214     public void serialize(StringBuffer JavaDoc report, int spaces) {
215         report.append(Util.getSpaces(spaces)
216             + "<driver name=\"" + _name + "\">\n");
217         
218        /*
219          * Calling getAggregateTestCases() forces call to computeMeans(). This
220          * is necessary before serializing driver params.
221          */

222         Iterator tci = getAggregateTestCases().iterator();
223        
224         // Serialize driver params
225
super.serialize(report, spaces + 2);
226
227         // Serialize each test case
228
while (tci.hasNext()) {
229             TestCase tc = (TestCase) tci.next();
230             tc.serialize(report, spaces + 2);
231         }
232
233         report.append(Util.getSpaces(spaces) + "</driver>\n");
234     }
235     
236     private JapexClassLoader newJapexClassLoader() {
237         JapexClassLoader result = new JapexClassLoader(new URL[0]);
238         String JavaDoc classPath = getParam(Constants.CLASS_PATH);
239         if (classPath == null) {
240             return result;
241         }
242         
243         StringTokenizer tokenizer = new StringTokenizer(classPath,
244             System.getProperty("path.separator"));
245         
246     while (tokenizer.hasMoreTokens()) {
247             String JavaDoc path = tokenizer.nextToken();
248             try {
249                 result.addURL(new File JavaDoc(path).toURL());
250             }
251             catch (MalformedURLException e) {
252                 // ignore
253
}
254         }
255         return result;
256     }
257 }
258
Popular Tags