1 25 package testsuite.perf; 26 27 import testsuite.BaseTestCase; 28 29 import java.text.NumberFormat ; 30 31 import java.util.ArrayList ; 32 import java.util.List ; 33 34 39 public abstract class BasePerfTest extends BaseTestCase { 40 43 46 private static final double[] T95 = { 12.706, 4.303, 3.182, 2.776, 2.571, 47 2.447, 2.365, 2.306, 2.262, 2.228, 2.201, 2.179, 2.160, 2.145, 48 2.131, 2.120, 2.110, 2.101, 2.093, 2.086, 2.080, 2.074, 2.069, 49 2.064, 2.060, 2.056, 2.052, 2.048, 2.045, 2.042 }; 50 51 54 private static final double[] T99 = { 63.657, 9.925, 5.841, 4.604, 4.032, 55 3.707, 3.499, 3.355, 3.250, 3.169, 3.106, 3.055, 3.012, 2.977, 56 2.947, 2.921, 2.898, 2.878, 2.861, 2.845, 2.831, 2.819, 2.807, 57 2.797, 2.787, 2.779, 2.771, 2.763, 2.756, 2.750 }; 58 59 static NumberFormat numberFormatter = NumberFormat.getInstance(); 60 61 static { 62 numberFormatter.setMaximumFractionDigits(4); 63 numberFormatter.setMinimumFractionDigits(4); 64 } 65 66 69 72 private List testValuesList = new ArrayList (); 73 74 private double confidenceLevel = 95; 76 private double confidenceValue = 0; 77 78 private double intervalWidth = 0.1; 79 80 private double meanValue = 0; 81 82 private double squareSumValue = 0; 83 84 private double sumValue = 0; 85 86 private double variationValue = 0; 87 88 91 private int numIterations = 0; 92 93 96 102 public BasePerfTest(String name) { 103 super(name); 104 } 105 106 109 114 public double getMeanValue() { 115 return this.meanValue; 116 } 117 118 124 protected abstract void doOneIteration() throws Exception ; 125 126 131 protected double getCurrentConfidence() { 132 return (this.intervalWidth - this.confidenceValue) * 100; 133 } 134 135 140 protected double getMarginOfError() { 141 return getConfidenceLookup() 142 * (getStandardDeviationP() / Math.sqrt(this.numIterations)); 143 } 144 145 150 protected double getStandardDeviationP() { 151 if (this.numIterations < 1) { 152 return 0; 153 } 154 155 return Math 156 .sqrt(((this.numIterations * this.squareSumValue) - (this.sumValue * this.sumValue)) 157 / (this.numIterations * this.numIterations)); 158 } 159 160 167 protected void addResult(double value) { 168 this.numIterations++; 169 this.testValuesList.add(new Double (value)); 170 171 this.sumValue += value; 172 this.squareSumValue += (value * value); 173 this.meanValue = this.sumValue / this.numIterations; 174 this.variationValue = (this.squareSumValue / this.numIterations) 175 - (this.meanValue * this.meanValue); 176 177 if (this.numIterations > 1) { 180 this.confidenceValue = this.intervalWidth 181 - ((2.0 * getConfidenceLookup() * Math 182 .sqrt(this.variationValue 183 / (this.numIterations - 1.0))) / this.meanValue); 184 } 185 } 186 187 196 protected void doIterations(int numIterations) throws Exception { 197 for (int i = 0; i < numIterations; i++) { 198 doOneIteration(); 199 } 200 } 201 202 209 protected synchronized void reportResults(String additionalMessage) { 210 StringBuffer messageBuf = new StringBuffer (); 211 212 if (additionalMessage != null) { 213 messageBuf.append(additionalMessage); 214 messageBuf.append(": "); 215 } 216 217 messageBuf.append(" mean: "); 218 messageBuf.append(numberFormatter.format(this.meanValue)); 219 messageBuf.append(" stdevp: "); 220 messageBuf.append(numberFormatter.format(getStandardDeviationP())); 221 messageBuf.append(" m-o-e: "); 222 messageBuf.append(numberFormatter.format(getMarginOfError())); 223 224 System.out.println(messageBuf.toString()); 225 } 226 227 private double getConfidenceLookup() { 228 if (this.confidenceLevel == 95) { 229 return T95[this.numIterations - 1]; 230 } else if (this.confidenceLevel == 99) { 231 return T99[this.numIterations - 1]; 232 } else { 233 throw new IllegalArgumentException ( 234 "Confidence level must be 95 or 99"); 235 } 236 } 237 } 238 | Popular Tags |