KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > test > PerformanceTest


1 /*
2
3    Copyright 2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.test;
19
20 import java.util.Vector JavaDoc;
21
22 /**
23  * This abstract <code>Test</code> implementation instruments performance
24  * testing.
25  *
26  * Derived classes need only implement the <code>runOp</code> and,
27  * optionally, the <code>runRef</code> methods.
28  *
29  * The <code>setReferenceScore</code> method is used to specify
30  * the last recorded score for the performance test and the
31  * <code>setAllowedScoreDeviation</code> method is used to specify
32  * the allowed deviation from the reference score.
33  *
34  * @author <a HREF="mailto:vincent.hardy@sun.com">Vincent Hardy</a>
35  * @version $Id: PerformanceTest.java,v 1.5 2004/08/18 07:16:57 vhardy Exp $
36  */

37 public abstract class PerformanceTest extends AbstractTest {
38     /**
39      * Reference score. -1 means there is no reference score
40      */

41     protected double referenceScore = -1;
42
43     /**
44      * Allowed deviation from the reference score. 10% by default
45      */

46     protected double allowedScoreDeviation = 0.1;
47
48     /**
49      * Score during last run
50      */

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     /**
74      * Force implementations to only implement <code>runOp</code>
75      * and other performance specific methods.
76      */

77     public final TestReport run() {
78         return super.run();
79     }
80
81     /**
82      * Force implementations to only implement <code>runOp</code>
83      * and other performance specific methods.
84      */

85     public final boolean runImplBasic() throws Exception JavaDoc {
86         // Should never be called for a PerformanceTest
87
return false;
88     }
89
90     /**
91      * This implementation of runImpl runs the reference
92      * operation (with <code>runRef</code>), then runs
93      * the operation (with <code>runOp</code>) and checks whether
94      * or not the score is within the allowed deviation of the
95      * reference score.
96      *
97      * @see #runRef
98      * @see #runOp
99      */

100     public final TestReport runImpl() throws Exception JavaDoc {
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         // Run once to remove class load time from timing.
111
runRef();
112         runOp();
113         // System.gc();
114

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.err.println(">>>>>>>> scores[" + i + "] = " + scores[i] + " (" + refUnit + " / " + opLength + ")");
139
System.gc();
140         }
141
142         System.err.println();
143
144         // Now, sort the scores
145
sort(scores);
146
147         // Compute the mean score based on the scores, not accounting
148
// for the lowest and highest scores
149
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         // Compute the score
158
this.lastScore = score;
159
160         // Compare to the reference score
161
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 JavaDoc {
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     /**
203      * Runs the reference operation.
204      * By default, this runs the same BufferedImage drawing
205      * operation 10000 times
206      */

207     protected void runRef() {
208         Vector JavaDoc v = new Vector JavaDoc();
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     /**
221      * Runs the tested operation
222      */

223     protected abstract void runOp() throws Exception JavaDoc;
224 }
225
Popular Tags