KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > test > internal > performance > db > Scenario


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.test.internal.performance.db;
12
13 import java.io.PrintStream JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21 import junit.framework.Assert;
22
23 import org.eclipse.test.internal.performance.data.DataPoint;
24 import org.eclipse.test.internal.performance.data.Dim;
25 import org.eclipse.test.internal.performance.eval.StatisticsSession;
26
27 /**
28  * A Scenario contains a series of data points for a single scenario.
29  * The axis of the data points can be specified when creating a scenario.
30  * Typical examples are:
31  * - datapoints corresponding to different builds
32  * - datapoints corresponding to different OSes
33  * - datapoints corresponding to different JVMs
34  * @since 3.1
35  */

36 public class Scenario {
37     
38     private final static boolean DEBUG= false;
39     
40     public static class SharedState {
41         
42         private Variations fVariations;
43         private String JavaDoc fSeriesKey;
44         private Set JavaDoc fQueryDimensions;
45         private String JavaDoc fScenarioPattern;
46         private Map JavaDoc fMessages;
47         
48       
49         SharedState(Variations variations, String JavaDoc scenarioPattern, String JavaDoc seriesKey, Dim[] dimensions) {
50             fVariations= variations;
51             fScenarioPattern= scenarioPattern;
52             fSeriesKey= seriesKey;
53             if (dimensions != null && dimensions.length > 0) {
54                 fQueryDimensions= new HashSet JavaDoc();
55                 for (int i= 0; i < dimensions.length; i++)
56                     fQueryDimensions.add(dimensions[i]);
57             }
58         }
59         
60         String JavaDoc[] getFailures(String JavaDoc[] names, String JavaDoc scenarioId) {
61             if (fMessages == null) {
62                 fMessages= new HashMap JavaDoc();
63                 Variations v= (Variations) fVariations.clone();
64                 for (int i= 0; i < names.length; i++) {
65                     v.put(fSeriesKey, names[i]);
66                     Map JavaDoc map= DB.queryFailure(fScenarioPattern, v);
67                     fMessages.put(names[i], map);
68                 }
69             }
70             String JavaDoc[] result= new String JavaDoc[names.length];
71             for (int i= 0; i < names.length; i++) {
72                 Map JavaDoc messages= (Map JavaDoc) fMessages.get(names[i]);
73                 if (messages != null)
74                     result[i]= (String JavaDoc) messages.get(scenarioId);
75             }
76             return result;
77         }
78     }
79
80     private SharedState fSharedState;
81     private String JavaDoc fScenarioName;
82     private String JavaDoc[] fSeriesNames;
83     private StatisticsSession[] fSessions;
84     private Map JavaDoc fSeries= new HashMap JavaDoc();
85     private Dim[] fDimensions;
86    
87     /**
88      * @param scenario
89      * @param variations
90      * @param seriesKey
91      * @param dimensions
92      * @deprecated
93      */

94     public Scenario(String JavaDoc scenario, Variations variations, String JavaDoc seriesKey, Dim[] dimensions) {
95         Assert.assertFalse(scenario.indexOf('%') >= 0);
96         fScenarioName= scenario;
97         fSharedState= new SharedState(variations, scenario, seriesKey, dimensions);
98     }
99
100     /**
101      * @param scenario
102      * @param sharedState
103      */

104     public Scenario(String JavaDoc scenario, SharedState sharedState) {
105         Assert.assertFalse(scenario.indexOf('%') >= 0);
106         fScenarioName= scenario;
107         fSharedState= sharedState;
108     }
109
110     public String JavaDoc getScenarioName() {
111         return fScenarioName;
112     }
113
114     public Dim[] getDimensions() {
115         loadSessions();
116         if (fDimensions == null)
117             return new Dim[0];
118         return fDimensions;
119     }
120     
121     public String JavaDoc[] getTimeSeriesLabels() {
122         loadSeriesNames();
123         if (fSeriesNames == null)
124             return new String JavaDoc[0];
125         return fSeriesNames;
126     }
127     
128     public String JavaDoc[] getFailureMessages() {
129         loadSeriesNames();
130         return fSharedState.getFailures(fSeriesNames, fScenarioName);
131     }
132
133     public TimeSeries getTimeSeries(Dim dim) {
134         loadSessions();
135         TimeSeries ts= (TimeSeries) fSeries.get(dim);
136         if (ts == null) {
137             double[] ds= new double[fSessions.length];
138             double[] sd= new double[fSessions.length];
139             for (int i= 0; i < ds.length; i++) {
140                 ds[i]= fSessions[i].getAverage(dim);
141                 sd[i]= fSessions[i].getStddev(dim);
142             }
143             ts= new TimeSeries(fSeriesNames, ds, sd);
144             fSeries.put(dim, ts);
145         }
146         return ts;
147     }
148     
149     public void dump(PrintStream JavaDoc ps, String JavaDoc key) {
150         ps.println("Scenario: " + getScenarioName()); //$NON-NLS-1$
151
Report r= new Report(2);
152         
153         String JavaDoc[] timeSeriesLabels= getTimeSeriesLabels();
154         r.addCell(key + ":"); //$NON-NLS-1$
155
for (int j= 0; j < timeSeriesLabels.length; j++)
156             r.addCellRight(timeSeriesLabels[j]);
157         r.nextRow();
158                     
159         Dim[] dimensions= getDimensions();
160         for (int i= 0; i < dimensions.length; i++) {
161             Dim dim= dimensions[i];
162             r.addCell(dim.getName() + ':');
163             
164             TimeSeries ts= getTimeSeries(dim);
165             int n= ts.getLength();
166             for (int j= 0; j < n; j++) {
167                 String JavaDoc stddev= ""; //$NON-NLS-1$
168
double stddev2= ts.getStddev(j);
169                 if (stddev2 != 0.0)
170                     stddev= " [" + dim.getDisplayValue(stddev2) + "]"; //$NON-NLS-1$ //$NON-NLS-2$
171
r.addCellRight(dim.getDisplayValue(ts.getValue(j)) + stddev);
172             }
173             r.nextRow();
174         }
175         r.print(ps);
176         ps.println();
177     }
178     
179     //---- private
180

181     private void loadSeriesNames() {
182         if (fSeriesNames == null) {
183             long start;
184             if (DEBUG) start= System.currentTimeMillis();
185             fSeriesNames= DB.querySeriesValues(fScenarioName, fSharedState.fVariations, fSharedState.fSeriesKey);
186             if (DEBUG) System.err.println("names: " + (System.currentTimeMillis()-start)); //$NON-NLS-1$
187
}
188     }
189     
190     private void loadSessions() {
191         if (fSessions != null)
192             return;
193         
194         loadSeriesNames();
195         
196         long start;
197         Variations v= (Variations) fSharedState.fVariations.clone();
198         if (DEBUG) start= System.currentTimeMillis();
199         ArrayList JavaDoc sessions= new ArrayList JavaDoc();
200         ArrayList JavaDoc names2= new ArrayList JavaDoc();
201         Set JavaDoc dims= new HashSet JavaDoc();
202         for (int t= 0; t < fSeriesNames.length; t++) {
203             v.put(fSharedState.fSeriesKey, fSeriesNames[t]);
204             DataPoint[] dps= DB.queryDataPoints(v, fScenarioName, fSharedState.fQueryDimensions);
205             if (DEBUG) System.err.println(" dps length: " + dps.length); //$NON-NLS-1$
206
if (dps.length > 0) {
207                 dims.addAll(dps[0].getDimensions2());
208                 sessions.add(new StatisticsSession(dps));
209                 names2.add(fSeriesNames[t]);
210             }
211         }
212         if (DEBUG) System.err.println("data: " + (System.currentTimeMillis()-start)); //$NON-NLS-1$
213

214         fSessions= (StatisticsSession[]) sessions.toArray(new StatisticsSession[sessions.size()]);
215         fSeriesNames= (String JavaDoc[]) names2.toArray(new String JavaDoc[sessions.size()]);
216         
217         fDimensions= (Dim[]) dims.toArray(new Dim[dims.size()]);
218         Arrays.sort(fDimensions,
219         new Comparator JavaDoc() {
220                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
221                     Dim d1= (Dim)o1;
222                     Dim d2= (Dim)o2;
223                     return d1.getName().compareTo(d2.getName());
224                 }
225             }
226         );
227     }
228 }
229
Popular Tags