1 18 package org.apache.batik.test; 19 20 import java.util.Vector ; 21 22 37 public abstract class PerformanceTest extends AbstractTest { 38 41 protected double referenceScore = -1; 42 43 46 protected double allowedScoreDeviation = 0.1; 47 48 51 protected double lastScore = -1; 52 53 public double getLastScore() { 54 return lastScore; 55 } 56 57 public double getReferenceScore() { 58 return referenceScore; 59 } 60 61 public void setReferenceScore(double referenceScore) { 62 this.referenceScore = referenceScore; 63 } 64 65 public double getAllowedScoreDeviation() { 66 return allowedScoreDeviation; 67 } 68 69 public void setAllowedScoreDeviation(double allowedScoreDeviation) { 70 this.allowedScoreDeviation = allowedScoreDeviation; 71 } 72 73 77 public final TestReport run() { 78 return super.run(); 79 } 80 81 85 public final boolean runImplBasic() throws Exception { 86 return false; 88 } 89 90 100 public final TestReport runImpl() throws Exception { 101 int iter = 50; 102 103 double refUnit = 0; 104 long refStart = 0; 105 long refEnd = 0; 106 long opEnd = 0; 107 long opStart = 0; 108 double opLength = 0; 109 110 runRef(); 112 runOp(); 113 115 double[] scores = new double[iter]; 116 117 for (int i=0; i<iter; i++) { 118 if ( i%2 == 0) { 119 refStart = System.currentTimeMillis(); 120 runRef(); 121 refEnd = System.currentTimeMillis(); 122 runOp(); 123 opEnd = System.currentTimeMillis(); 124 refUnit = refEnd - refStart; 125 opLength = opEnd - refEnd; 126 } else { 127 opStart = System.currentTimeMillis(); 128 runOp(); 129 opEnd = System.currentTimeMillis(); 130 runRef(); 131 refEnd = System.currentTimeMillis(); 132 refUnit = refEnd - opEnd; 133 opLength = opEnd - opStart; 134 } 135 136 scores[i] = opLength / refUnit; 137 System.err.println("."); 138 System.gc(); 140 } 141 142 System.err.println(); 143 144 sort(scores); 146 147 double score = 0; 150 int trim = 5; 151 for (int i=trim; i<scores.length-trim; i++) { 152 score += scores[i]; 153 } 154 155 score /= (iter - 2*trim); 156 157 this.lastScore = score; 159 160 if (referenceScore == -1) { 162 TestReport report = reportError("no.reference.score.set"); 163 report.addDescriptionEntry("computed.score", "" + score); 164 return report; 165 } else { 166 double scoreMin = referenceScore*(1-allowedScoreDeviation); 167 double scoreMax = referenceScore*(1+allowedScoreDeviation); 168 if (score > scoreMax) { 169 TestReport report = reportError("performance.regression"); 170 report.addDescriptionEntry("reference.score", "" + referenceScore); 171 report.addDescriptionEntry("computed.score", "" + score); 172 report.addDescriptionEntry("score.deviation", "" + 100*((score-referenceScore)/referenceScore)); 173 return report; 174 } else if (score < scoreMin) { 175 TestReport report = reportError("unexpected.performance.improvement"); 176 report.addDescriptionEntry("reference.score", "" + referenceScore); 177 report.addDescriptionEntry("computed.score", "" + score); 178 report.addDescriptionEntry("score.deviation", "" + 100*((score-referenceScore)/referenceScore)); 179 return report; 180 } else { 181 return reportSuccess(); 182 } 183 } 184 } 185 186 protected void sort(double a[]) throws Exception { 187 for (int i = a.length - 1; i>=0; i--) { 188 boolean swapped = false; 189 for (int j = 0; j<i; j++) { 190 if (a[j] > a[j+1]) { 191 double d = a[j]; 192 a[j] = a[j+1]; 193 a[j+1] = d; 194 swapped = true; 195 } 196 } 197 if (!swapped) 198 return; 199 } 200 } 201 202 207 protected void runRef() { 208 Vector v = new Vector (); 209 for (int i=0; i<10000; i++) { 210 v.addElement("" + i); 211 } 212 213 for (int i=0; i<10000; i++) { 214 if (v.contains("" + i)) { 215 v.remove("" + i); 216 } 217 } 218 } 219 220 223 protected abstract void runOp() throws Exception ; 224 } 225 | Popular Tags |