1 11 package org.eclipse.test.performance; 12 13 import junit.framework.TestCase; 14 15 import org.eclipse.core.runtime.Platform; 16 17 import org.eclipse.test.internal.performance.InternalDimensions; 18 import org.eclipse.test.internal.performance.InternalPerformanceMeter; 19 import org.eclipse.test.internal.performance.NullPerformanceMeter; 20 import org.eclipse.test.internal.performance.OSPerformanceMeterFactory; 21 import org.eclipse.test.internal.performance.PerformanceMeterFactory; 22 import org.eclipse.test.internal.performance.PerformanceTestPlugin; 23 import org.eclipse.test.internal.performance.data.Dim; 24 import org.eclipse.test.internal.performance.eval.AbsoluteBandChecker; 25 import org.eclipse.test.internal.performance.eval.AssertChecker; 26 import org.eclipse.test.internal.performance.eval.Evaluator; 27 import org.eclipse.test.internal.performance.eval.IEvaluator; 28 import org.eclipse.test.internal.performance.eval.RelativeBandChecker; 29 import org.osgi.framework.Bundle; 30 31 39 public class Performance { 40 41 44 public static final int EXPLAINS_DEGRADATION_COMMENT= 1; 45 46 private static final String PERFORMANCE_METER_FACTORY= "/option/performanceMeterFactory"; private static final String PERFORMANCE_METER_FACTORY_PROPERTY= "PerformanceMeterFactory"; 49 private static Performance fgDefault; 50 51 private PerformanceMeterFactory fPerformanceMeterFactory; 52 private IEvaluator fDefaultEvaluator; 53 54 55 private NullPerformanceMeter fNullPeformanceMeter; 56 57 58 61 private Performance() { 62 } 64 65 70 public static Performance getDefault() { 71 if (fgDefault == null) 72 fgDefault= new Performance(); 73 return fgDefault; 74 } 75 76 83 public void assertPerformance(PerformanceMeter performanceMeter) { 84 if (fDefaultEvaluator == null) { 85 fDefaultEvaluator= new Evaluator(); 86 fDefaultEvaluator.setAssertCheckers(new AssertChecker[] { 87 new RelativeBandChecker(InternalDimensions.ELAPSED_PROCESS, 0.0f, 1.10f), 88 }); 93 } 94 fDefaultEvaluator.evaluate(performanceMeter); 95 } 96 97 108 public void assertPerformanceInRelativeBand(PerformanceMeter performanceMeter, Dimension dim, int lowerPercentage, int upperPercentage) { 109 Evaluator e= new Evaluator(); 110 e.setAssertCheckers(new AssertChecker[] { 111 new RelativeBandChecker((Dim) dim, 1.0+(lowerPercentage / 100.0), 1.0+(upperPercentage / 100.0)), 112 }); 113 e.evaluate(performanceMeter); 114 } 115 116 127 public void assertPerformanceInAbsoluteBand(PerformanceMeter performanceMeter, Dimension dim, int lowerBand, int upperBand) { 128 Evaluator e= new Evaluator(); 129 e.setAssertCheckers(new AssertChecker[] { 130 new AbsoluteBandChecker((Dim) dim, lowerBand, upperBand), 131 }); 132 e.evaluate(performanceMeter); 133 } 134 135 143 public PerformanceMeter createPerformanceMeter(String scenarioId) { 144 return getPeformanceMeterFactory().createPerformanceMeter(scenarioId); 145 } 146 147 152 public PerformanceMeter getNullPerformanceMeter() { 153 if (fNullPeformanceMeter == null) 154 fNullPeformanceMeter= new NullPerformanceMeter(); 155 return fNullPeformanceMeter; 156 } 157 158 166 public String getDefaultScenarioId(TestCase test) { 167 return test.getClass().getName() + '#' + test.getName() + "()"; } 169 170 180 public String getDefaultScenarioId(TestCase test, String id) { 181 return getDefaultScenarioId(test) + '-' + id; 182 } 183 184 private PerformanceMeterFactory getPeformanceMeterFactory() { 185 if (fPerformanceMeterFactory == null) 186 fPerformanceMeterFactory= createPerformanceMeterFactory(); 187 return fPerformanceMeterFactory; 188 } 189 190 private PerformanceMeterFactory createPerformanceMeterFactory() { 191 PerformanceMeterFactory factory; 192 factory= tryInstantiate(System.getProperty(PERFORMANCE_METER_FACTORY_PROPERTY)); 193 if (factory != null) 194 return factory; 195 196 factory= tryInstantiate(Platform.getDebugOption(PerformanceTestPlugin.PLUGIN_ID + PERFORMANCE_METER_FACTORY)); 197 if (factory != null) 198 return factory; 199 200 return createDefaultPerformanceMeterFactory(); 201 } 202 203 private PerformanceMeterFactory tryInstantiate(String className) { 204 PerformanceMeterFactory instance= null; 205 if (className != null && className.length() > 0) { 206 try { 207 int separator= className.indexOf(':'); 208 Bundle bundle= null; 209 if (separator == -1) { 210 bundle= PerformanceTestPlugin.getDefault().getBundle(); 211 } else { 212 String bundleName= className.substring(0, separator); 213 className= className.substring(separator + 1); 214 bundle= Platform.getBundle(bundleName); 215 } 216 Class c= bundle.loadClass(className); 217 instance= (PerformanceMeterFactory) c.newInstance(); 218 } catch (ClassNotFoundException e) { 219 PerformanceTestPlugin.log(e); 220 } catch (InstantiationException e) { 221 PerformanceTestPlugin.log(e); 222 } catch (IllegalAccessException e) { 223 PerformanceTestPlugin.log(e); 224 } catch (ClassCastException e) { 225 PerformanceTestPlugin.log(e); 226 } 227 } 228 return instance; 229 } 230 231 private PerformanceMeterFactory createDefaultPerformanceMeterFactory() { 232 return new OSPerformanceMeterFactory(); 233 } 234 235 244 public void tagAsGlobalSummary(PerformanceMeter pm, String shortName, Dimension dimension) { 245 tagAsGlobalSummary(pm, shortName, new Dimension[] { dimension } ); 246 } 247 248 257 public void tagAsGlobalSummary(PerformanceMeter pm, String shortName, Dimension[] dimensions) { 258 if (pm instanceof InternalPerformanceMeter) { 259 InternalPerformanceMeter ipm= (InternalPerformanceMeter) pm; 260 ipm.tagAsSummary(true, shortName, dimensions); 261 } 262 } 263 264 265 274 public void tagAsSummary(PerformanceMeter pm, String shortName, Dimension dimension) { 275 tagAsSummary(pm, shortName, new Dimension[] { dimension } ); 276 } 277 278 287 public void tagAsSummary(PerformanceMeter pm, String shortName, Dimension[] dimensions) { 288 if (pm instanceof InternalPerformanceMeter) { 289 InternalPerformanceMeter ipm= (InternalPerformanceMeter) pm; 290 ipm.tagAsSummary(false, shortName, dimensions); 291 } 292 } 293 294 304 public void setComment(PerformanceMeter pm, int commentKind, String commentText) { 305 if (commentKind == EXPLAINS_DEGRADATION_COMMENT) { 306 if (pm instanceof InternalPerformanceMeter) { 307 InternalPerformanceMeter ipm= (InternalPerformanceMeter) pm; 308 ipm.setComment(commentKind, commentText); 309 } 310 } 311 } 312 } 313 | Popular Tags |