KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.AlphaComposite JavaDoc;
21 import java.awt.Color JavaDoc;
22 import java.awt.Graphics2D JavaDoc;
23 import java.awt.geom.AffineTransform JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25
26 /**
27  * Validates the operation of the <code>PerformanceTest</code> class.
28  *
29  * @author <a HREF="mailto:vincent.hardy@sun.com">Vincent Hardy</a>
30  * @version $Id: PerformanceTestValidator.java,v 1.5 2004/08/18 07:16:57 vhardy Exp $
31  */

32 public class PerformanceTestValidator extends AbstractTest {
33     public TestReport runImpl() throws Exception JavaDoc {
34         // First, work with SimplePerformanceTest to check the life
35
// cycle of using a performance test.
36
// ===========================================================
37
SimplePerformanceTest p = new SimplePerformanceTest();
38         TestReport r = p.run();
39         assertTrue(!r.hasPassed());
40         assertTrue(r.getErrorCode().equals("no.reference.score.set"));
41         p.setReferenceScore(p.getLastScore());
42         p.run();
43         p.setReferenceScore(p.getLastScore());
44         p.run();
45
46         double score = p.getLastScore();
47         p.setReferenceScore(score);
48         r = p.run();
49
50         if (!r.hasPassed()) {
51             TestReport result = reportError("unexpected.performance.test.failure");
52             result.addDescriptionEntry("error.code", r.getErrorCode());
53             result.addDescriptionEntry("expected.score", "" + score);
54             result.addDescriptionEntry("actual.score", "" + p.getLastScore());
55             result.addDescriptionEntry("regression.percentage", "" + 100*(score - p.getLastScore())/p.getLastScore());
56             return result;
57         }
58
59         // Now, check that performance changes are detected
60
// ===========================================================
61
p.setReferenceScore(score*0.5);
62         r = p.run();
63         assertTrue(!r.hasPassed());
64         if (!r.getErrorCode().equals("performance.regression")) {
65             TestReport result = reportError("unexpected.performance.test.error.code");
66             result.addDescriptionEntry("expected.code", "performance.regression");
67             result.addDescriptionEntry("actual.code", r.getErrorCode());
68             result.addDescriptionEntry("expected.score", "" + score);
69             result.addDescriptionEntry("actual.score", "" + p.getLastScore());
70             result.addDescriptionEntry("regression.percentage", "" + 100*(score - p.getLastScore())/p.getLastScore());
71             return result;
72         }
73
74         p.setReferenceScore(score*2);
75         r = p.run();
76         assertTrue(!r.hasPassed());
77         if (!r.getErrorCode().equals("unexpected.performance.improvement")) {
78             TestReport result = reportError("unexpected.performance.test.error.code");
79             result.addDescriptionEntry("expected.code", "unexpected.performance.improvement");
80             result.addDescriptionEntry("actual.code", r.getErrorCode());
81             result.addDescriptionEntry("expected.score", "" + score);
82             result.addDescriptionEntry("actual.score", "" + p.getLastScore());
83             result.addDescriptionEntry("regression.percentage", "" + 100*(score - p.getLastScore())/p.getLastScore());
84             return result;
85         }
86
87         return reportSuccess();
88     }
89
90     static class SimplePerformanceTest extends PerformanceTest {
91         public void runOp() {
92             // runRef();
93
BufferedImage JavaDoc buf = new BufferedImage JavaDoc(200, 200, BufferedImage.TYPE_INT_ARGB);
94             Graphics2D JavaDoc g = buf.createGraphics();
95             AffineTransform JavaDoc txf = new AffineTransform JavaDoc();
96             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
97             g.setPaint(new Color JavaDoc(30, 100, 200));
98             
99             for (int j=0; j<20; j++) {
100                 txf.setToIdentity();
101                 txf.translate(-100, -100);
102                 txf.rotate(j*Math.PI/100);
103                 txf.translate(100, 100);
104                 g.setTransform(txf);
105                 g.drawRect(30, 30, 140, 140);
106             }
107             /*
108             Vector v = new Vector();
109             for (int i=0; i<5000; i++) {
110                 v.addElement("" + i);
111             }
112             
113             for (int i=0; i<5000; i++) {
114                 if (v.contains("" + i)) {
115                     v.remove("" + i);
116                 }
117                 }*/

118         }
119     }
120 }
121
Popular Tags