KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
45 import com.sun.japex.testsuite.*;
46
47 import java.awt.Paint JavaDoc;
48 import java.awt.Color JavaDoc;
49 import java.awt.BorderLayout JavaDoc;
50 import java.awt.event.ActionEvent JavaDoc;
51 import java.awt.event.ActionListener JavaDoc;
52
53 import javax.swing.JFrame JavaDoc;
54 import javax.swing.JButton JavaDoc;
55 import javax.swing.JPanel JavaDoc;
56 import javax.swing.JSlider JavaDoc;
57 import javax.swing.JToolBar JavaDoc;
58 import javax.swing.event.ChangeEvent JavaDoc;
59 import javax.swing.event.ChangeListener JavaDoc;
60
61 import org.jfree.chart.*;
62 import org.jfree.data.category.DefaultCategoryDataset;
63 import org.jfree.ui.ApplicationFrame;
64 import org.jfree.ui.RefineryUtilities;
65 import org.jfree.chart.plot.CategoryPlot;
66 import org.jfree.chart.renderer.category.CategoryItemRenderer;
67 import org.jfree.chart.renderer.category.BarRenderer;
68 import org.jfree.chart.plot.PlotOrientation;
69
70 public class TestSuite extends Params {
71     
72     String JavaDoc _name;
73     List _driverInfo = new ArrayList();
74     
75     /**
76      * Creates a new instance of TestSuite from a JAXB-generated
77      * object. In essence, this constructor implements a mapping
78      * between the JAXB object model and the internal object model
79      * used in Japex.
80      */

81     public TestSuite(com.sun.japex.testsuite.TestSuite ts) {
82         _name = ts.getName();
83         
84         // Set global properties by traversing JAXB's model
85
List params = ts.getParam();
86         final String JavaDoc pathSep = System.getProperty("path.separator");
87         List classPathURLs = new ArrayList();
88         
89         if (params != null) {
90             Iterator it = params.iterator();
91             while (it.hasNext()) {
92                 ParamType pt = (ParamType) it.next();
93                 String JavaDoc name = pt.getName();
94                 String JavaDoc value = pt.getValue();
95                 String JavaDoc oldValue = getParam(name);
96                 
97                 // If japex.classPath, append to existing value
98
setParam(name,
99                     name.equals(Constants.CLASS_PATH) && oldValue != null ?
100                     (oldValue + pathSep + value) : value);
101             }
102         }
103         
104         // Set default global params if necessary
105
if (!hasParam(Constants.WARMUP_TIME) &&
106             !hasParam(Constants.WARMUP_ITERATIONS))
107         {
108             setParam(Constants.WARMUP_ITERATIONS,
109                      Constants.DEFAULT_WARMUP_ITERATIONS);
110         }
111         if (!hasParam(Constants.RUN_TIME) &&
112             !hasParam(Constants.RUN_ITERATIONS))
113         {
114             setParam(Constants.RUN_ITERATIONS,
115                      Constants.DEFAULT_RUN_ITERATIONS);
116         }
117         
118         // Check output directory
119
if (!hasParam(Constants.REPORTS_DIRECTORY)) {
120             setParam(Constants.REPORTS_DIRECTORY,
121                      Constants.DEFAULT_REPORTS_DIRECTORY);
122         }
123         
124         // Check number of threads
125
if (!hasParam(Constants.NUMBER_OF_THREADS)) {
126             setParam(Constants.NUMBER_OF_THREADS,
127                      Constants.DEFAULT_NUMBER_OF_THREADS);
128         }
129         else {
130             int nOfThreads = getIntParam(Constants.NUMBER_OF_THREADS);
131             if (nOfThreads < 1) {
132                 throw new RuntimeException JavaDoc(
133                     "Parameter 'japex.numberOfThreads' must be at least 1");
134             }
135         }
136         
137         // Check runs per driver
138
if (!hasParam(Constants.RUNS_PER_DRIVER)) {
139             setParam(Constants.RUNS_PER_DRIVER,
140                      Constants.DEFAULT_RUNS_PER_DRIVER);
141         }
142         else {
143             int runsPerDriver = getIntParam(Constants.RUNS_PER_DRIVER);
144             if (runsPerDriver < 1) {
145                 throw new RuntimeException JavaDoc(
146                     "Parameter 'japex.runsPerDriver' must be at least 1");
147             }
148         }
149         
150         // Set other global params
151
setParam(Constants.VERSION, Constants.VERSION_VALUE);
152         setParam(Constants.OS_NAME, System.getProperty("os.name"));
153         setParam(Constants.OS_ARCHITECTURE, System.getProperty("os.arch"));
154         DateFormat df = new SimpleDateFormat("dd MMM yyyy/HH:mm:ss z");
155         setParam(Constants.DATE_TIME, df.format(Japex.TODAY));
156         setParam(Constants.VM_INFO,
157             System.getProperty("java.vendor") + " " +
158             System.getProperty("java.vm.version"));
159         
160         // Create and populate list of drivers
161
Iterator it = ts.getDriver().iterator();
162         while (it.hasNext()) {
163             TestSuiteType.DriverType dt = (TestSuiteType.DriverType) it.next();
164             
165             Properties driverParams = new Properties(getParams());
166             Iterator driverParamsIt = dt.getParam().iterator();
167             while (driverParamsIt.hasNext()) {
168                 ParamType pt = (ParamType) driverParamsIt.next();
169                 driverParams.setProperty(pt.getName(), pt.getValue());
170             }
171
172             // If japex.driverClass not specified, use the driver's name
173
if (driverParams.getProperty(Constants.DRIVER_CLASS) == null) {
174                 driverParams.setProperty(Constants.DRIVER_CLASS, dt.getName());
175             }
176             _driverInfo.add(
177                 new DriverInfo(dt.getName(), dt.isNormal(),
178                                getIntParam(Constants.RUNS_PER_DRIVER),
179                                driverParams));
180         }
181         
182         // Create and populate list of test cases
183
TestCaseArrayList testCases = new TestCaseArrayList();
184         it = ts.getTestCase().iterator();
185         while (it.hasNext()) {
186             TestSuiteType.TestCaseType tc =
187                 (TestSuiteType.TestCaseType) it.next();
188             
189             Properties localParams = new Properties(getParams());
190             Iterator itParams = tc.getParam().iterator();
191             while (itParams.hasNext()) {
192                 ParamType pt = (ParamType) itParams.next();
193                 localParams.setProperty(pt.getName(), pt.getValue());
194             }
195             testCases.add(new TestCase(tc.getName(), localParams));
196         }
197         
198         // Set list of test cases and number of runs on each driver
199
it = _driverInfo.iterator();
200         while (it.hasNext()) {
201             DriverInfo di = (DriverInfo) it.next();
202             di.setTestCases(testCases);
203         }
204     }
205     
206     public String JavaDoc getName() {
207         return _name;
208     }
209     
210     public List getDriverInfoList() {
211         return _driverInfo;
212     }
213     
214     public void serialize(StringBuffer JavaDoc report) {
215         report.append("<testSuiteReport name=\"" + _name
216             + "\" xmlns=\"http://www.sun.com/japex/testSuiteReport\">\n");
217
218         serialize(report, 2);
219         
220         // Iterate through each class (aka driver)
221
Iterator jdi = _driverInfo.iterator();
222         while (jdi.hasNext()) {
223             DriverInfo di = (DriverInfo) jdi.next();
224             di.serialize(report, 2);
225         }
226                     
227         report.append("</testSuiteReport>\n");
228     }
229        
230     public void generateDriverChart(String JavaDoc fileName) {
231         try {
232             DefaultCategoryDataset dataset = new DefaultCategoryDataset();
233             String JavaDoc resultUnit = getParam(Constants.RESULT_UNIT);
234             
235             // Find first normalizer driver (if any) and adjust unit
236
DriverInfo normalizerDriver = null;
237             Iterator jdi = _driverInfo.iterator();
238             while (jdi.hasNext()) {
239                 DriverInfo di = (DriverInfo) jdi.next();
240                 if (di.isNormal()) {
241                     normalizerDriver = di;
242                     resultUnit = "% of " + resultUnit;
243                     break;
244                 }
245             }
246             
247             // Generate charts
248
jdi = _driverInfo.iterator();
249             while (jdi.hasNext()) {
250                 DriverInfo di = (DriverInfo) jdi.next();
251                               
252                 if (normalizerDriver != null) {
253                     dataset.addValue(
254                         normalizerDriver == di ? 100.0 :
255                         (100.0 * di.getDoubleParam(Constants.RESULT_ARIT_MEAN) /
256                          normalizerDriver.getDoubleParam(Constants.RESULT_ARIT_MEAN)),
257                         di.getName(),
258                         "Arithmetic Mean");
259                     dataset.addValue(
260                         normalizerDriver == di ? 100.0 :
261                         (100.0 * di.getDoubleParam(Constants.RESULT_GEOM_MEAN) /
262                          normalizerDriver.getDoubleParam(Constants.RESULT_GEOM_MEAN)),
263                         di.getName(),
264                         "Geometric Mean");
265                     dataset.addValue(
266                         normalizerDriver == di ? 100.0 :
267                         (100.0 * di.getDoubleParam(Constants.RESULT_HARM_MEAN) /
268                          normalizerDriver.getDoubleParam(Constants.RESULT_HARM_MEAN)),
269                         di.getName(),
270                         "Harmonic Mean");
271                 }
272                 else {
273                     dataset.addValue(
274                         di.getDoubleParam(Constants.RESULT_ARIT_MEAN),
275                         di.getName(),
276                         "Arithmetic Mean");
277                     dataset.addValue(
278                         di.getDoubleParam(Constants.RESULT_GEOM_MEAN),
279                         di.getName(),
280                         "Geometric Mean");
281                     dataset.addValue(
282                         di.getDoubleParam(Constants.RESULT_HARM_MEAN),
283                         di.getName(),
284                         "Harmonic Mean");
285                 }
286             }
287             
288             JFreeChart chart = ChartFactory.createBarChart3D(
289                 "Result Summary (" + resultUnit + ")",
290                 "", resultUnit,
291                 dataset,
292                 PlotOrientation.VERTICAL,
293                 true, true, false);
294             chart.setAntiAlias(true);
295             
296             ChartUtilities.saveChartAsJPEG(new File JavaDoc(fileName), chart, 600, 450);
297         }
298         catch (Exception JavaDoc e) {
299             e.printStackTrace();
300         }
301     }
302     
303     public int generateTestCaseCharts(String JavaDoc baseName, String JavaDoc extension) {
304         int nOfFiles = 0;
305         final int maxGroupSize = 5;
306         
307         // Get number of tests from first driver
308
final int nOfTests =
309             ((DriverInfo) _driverInfo.get(0)).getAggregateTestCases().size();
310             
311         int groupSizesIndex = 0;
312         int[] groupSizes = calculateGroupSizes(nOfTests, maxGroupSize);
313         
314         try {
315             String JavaDoc resultUnit = getParam(Constants.RESULT_UNIT);
316             
317             // Find first normalizer driver (if any)
318
DriverInfo normalizerDriver = null;
319             
320             Iterator jdi = _driverInfo.iterator();
321             while (jdi.hasNext()) {
322                 DriverInfo di = (DriverInfo) jdi.next();
323                 if (di.isNormal()) {
324                     normalizerDriver = di;
325                     resultUnit = "% of " + resultUnit;
326                     break;
327                 }
328             }
329             
330             // Generate charts
331
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
332             
333             int i = 0, thisGroupSize = 0;
334             for (; i < nOfTests; i++) {
335                 jdi = _driverInfo.iterator();
336                 
337                 while (jdi.hasNext()) {
338                     DriverInfo di = (DriverInfo) jdi.next();
339                     TestCase tc = (TestCase) di.getAggregateTestCases().get(i);
340             
341                     // User normalizer driver if defined
342
if (normalizerDriver != null) {
343                         TestCase normalTc =
344                             (TestCase) normalizerDriver.getAggregateTestCases().get(i);
345                         dataset.addValue(normalizerDriver == di ? 100.0 :
346                                 (100.0 * tc.getDoubleParam(Constants.RESULT_VALUE) /
347                                  normalTc.getDoubleParam(Constants.RESULT_VALUE)),
348                                 di.getName(),
349                                 tc.getTestName());
350                     }
351                     else {
352                         dataset.addValue(
353                             tc.getDoubleParam(Constants.RESULT_VALUE),
354                             di.getName(),
355                             tc.getTestName());
356                     }
357                 }
358                 
359                 thisGroupSize++;
360                         
361                 // Generate chart for this group if complete
362
if (thisGroupSize == groupSizes[groupSizesIndex]) {
363                     JFreeChart chart = ChartFactory.createBarChart3D(
364                         "Results per Test (" + resultUnit + ")",
365                         "", resultUnit,
366                         dataset,
367                         PlotOrientation.VERTICAL,
368                         true, true, false);
369                     chart.setAntiAlias(true);
370                     ChartUtilities.saveChartAsJPEG(
371                         new File JavaDoc(baseName + Integer.toString(nOfFiles) + extension),
372                         chart, 600, 450);
373                     
374                     nOfFiles++;
375                     groupSizesIndex++;
376                     thisGroupSize = 0;
377                     dataset = new DefaultCategoryDataset();
378                 }
379             }
380         }
381         catch (Exception JavaDoc e) {
382             e.printStackTrace();
383         }
384         
385         return nOfFiles;
386     }
387     
388     /**
389      * Calculate group sizes for tests to avoid a very small final group.
390      * For example, calculateGroupSizes(21, 5) return { 5,5,5,3,3 } instead
391      * of { 5,5,5,5,1 }.
392      */

393     private static int[] calculateGroupSizes(int nOfTests, int maxGroupSize) {
394         if (nOfTests <= maxGroupSize) {
395             return new int[] { nOfTests };
396         }
397         
398         int[] result = new int[nOfTests / maxGroupSize +
399                                ((nOfTests % maxGroupSize > 0) ? 1 : 0)];
400         
401         // Var m1 represents the number of groups of size maxGroupSize
402
int m1 = (nOfTests - maxGroupSize) / maxGroupSize;
403         for (int i = 0; i < m1; i++) {
404             result[i] = maxGroupSize;
405         }
406         
407         // Var m2 represents the number of tests not allocated into groups
408
int m2 = nOfTests - m1 * maxGroupSize;
409         if (m2 <= maxGroupSize) {
410             result[result.length - 1] = m2;
411         }
412         else {
413             // Allocate last two groups
414
result[result.length - 2] = (int) Math.ceil(m2 / 2.0);
415             result[result.length - 1] = m2 - result[result.length - 2];
416         }
417         return result;
418     }
419
420 }
421
Popular Tags