1 11 package org.eclipse.test.internal.performance.db; 12 13 import java.io.PrintStream ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Comparator ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 import java.util.Map ; 20 import java.util.Set ; 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 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 fSeriesKey; 44 private Set fQueryDimensions; 45 private String fScenarioPattern; 46 private Map fMessages; 47 48 49 SharedState(Variations variations, String scenarioPattern, String seriesKey, Dim[] dimensions) { 50 fVariations= variations; 51 fScenarioPattern= scenarioPattern; 52 fSeriesKey= seriesKey; 53 if (dimensions != null && dimensions.length > 0) { 54 fQueryDimensions= new HashSet (); 55 for (int i= 0; i < dimensions.length; i++) 56 fQueryDimensions.add(dimensions[i]); 57 } 58 } 59 60 String [] getFailures(String [] names, String scenarioId) { 61 if (fMessages == null) { 62 fMessages= new HashMap (); 63 Variations v= (Variations) fVariations.clone(); 64 for (int i= 0; i < names.length; i++) { 65 v.put(fSeriesKey, names[i]); 66 Map map= DB.queryFailure(fScenarioPattern, v); 67 fMessages.put(names[i], map); 68 } 69 } 70 String [] result= new String [names.length]; 71 for (int i= 0; i < names.length; i++) { 72 Map messages= (Map ) fMessages.get(names[i]); 73 if (messages != null) 74 result[i]= (String ) messages.get(scenarioId); 75 } 76 return result; 77 } 78 } 79 80 private SharedState fSharedState; 81 private String fScenarioName; 82 private String [] fSeriesNames; 83 private StatisticsSession[] fSessions; 84 private Map fSeries= new HashMap (); 85 private Dim[] fDimensions; 86 87 94 public Scenario(String scenario, Variations variations, String seriesKey, Dim[] dimensions) { 95 Assert.assertFalse(scenario.indexOf('%') >= 0); 96 fScenarioName= scenario; 97 fSharedState= new SharedState(variations, scenario, seriesKey, dimensions); 98 } 99 100 104 public Scenario(String scenario, SharedState sharedState) { 105 Assert.assertFalse(scenario.indexOf('%') >= 0); 106 fScenarioName= scenario; 107 fSharedState= sharedState; 108 } 109 110 public String 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 [] getTimeSeriesLabels() { 122 loadSeriesNames(); 123 if (fSeriesNames == null) 124 return new String [0]; 125 return fSeriesNames; 126 } 127 128 public String [] 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 ps, String key) { 150 ps.println("Scenario: " + getScenarioName()); Report r= new Report(2); 152 153 String [] timeSeriesLabels= getTimeSeriesLabels(); 154 r.addCell(key + ":"); 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 stddev= ""; double stddev2= ts.getStddev(j); 169 if (stddev2 != 0.0) 170 stddev= " [" + dim.getDisplayValue(stddev2) + "]"; r.addCellRight(dim.getDisplayValue(ts.getValue(j)) + stddev); 172 } 173 r.nextRow(); 174 } 175 r.print(ps); 176 ps.println(); 177 } 178 179 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)); } 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 sessions= new ArrayList (); 200 ArrayList names2= new ArrayList (); 201 Set dims= new HashSet (); 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); 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)); 214 fSessions= (StatisticsSession[]) sessions.toArray(new StatisticsSession[sessions.size()]); 215 fSeriesNames= (String []) names2.toArray(new String [sessions.size()]); 216 217 fDimensions= (Dim[]) dims.toArray(new Dim[dims.size()]); 218 Arrays.sort(fDimensions, 219 new Comparator () { 220 public int compare(Object o1, Object 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 |