KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > RenderingBenchmarks


1 package test;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.Font JavaDoc;
6 import java.awt.Graphics JavaDoc;
7 import java.awt.Graphics2D JavaDoc;
8 import java.awt.RenderingHints JavaDoc;
9 import java.awt.Toolkit JavaDoc;
10 import java.awt.font.GlyphVector JavaDoc;
11 import java.awt.geom.Line2D JavaDoc;
12 import java.awt.geom.Rectangle2D JavaDoc;
13 import java.awt.geom.RoundRectangle2D JavaDoc;
14 import java.util.logging.Logger JavaDoc;
15
16 import javax.swing.JComponent JavaDoc;
17 import javax.swing.JFrame JavaDoc;
18
19 /**
20  * RenderingBenchmarks
21  *
22  * @author <a HREF="http://jheer.org">jeffrey heer</a>
23  */

24 public class RenderingBenchmarks extends JComponent JavaDoc {
25
26     private static final Logger JavaDoc s_logger
27         = Logger.getLogger(RenderingBenchmarks.class.getName());
28     
29     private StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
30     private String JavaDoc testSuite;
31     private String JavaDoc curTest;
32     private int numItems;
33     private long timein;
34     private int fps = 20;
35     
36     public RenderingBenchmarks() {
37         this.setPreferredSize(new Dimension JavaDoc(500,500));
38     }
39     
40     private void startTest(String JavaDoc name, int numItems) {
41         if ( curTest != null )
42             throw new IllegalStateException JavaDoc("In the middle of a test!");
43         this.curTest = name;
44         this.numItems = numItems;
45         Toolkit JavaDoc tk = Toolkit.getDefaultToolkit();
46         tk.sync();
47         this.timein = System.currentTimeMillis();
48     }
49     
50     private void endTest(boolean print) {
51         if ( print ) {
52             long t = System.currentTimeMillis() - timein;
53             double pps = 1000*((double)numItems)/t;
54             double ppf = pps/fps;
55             sbuf.append(curTest).append(" ")
56                 .append(curTest.length() > 14 ? "\t" : "\t\t")
57                 .append(numItems).append(" \t")
58                 .append(t/1000.0).append("s\t")
59                 .append(((int)(pps*100))/100.0).append(" pr/s\t")
60                 .append(((int)(ppf*100))/100.0).append(" pr/fr")
61                 .append('\n');
62         }
63         curTest = null;
64     }
65     
66     public void printHeader() {
67         sbuf.append("PRIMITIVE\t\tCOUNT\tTIME\tPRIMITIVES/SEC\tPRIMITIVES/FRAME @ ");
68         sbuf.append(fps).append("fps").append('\n');
69     }
70     
71     public void paintComponent(Graphics JavaDoc g) {
72         Graphics2D JavaDoc g2 = (Graphics2D JavaDoc)g;
73         
74         int x = 0, y = 0, w = 100, h = 100, c=10;
75         float xf = 0f, yf = 0f, wf = 100f, hf = 100f, cf=10f;
76         int n;
77         
78         g2.setColor(Color.BLACK);
79         boolean print = false;
80         
81         for ( int j=0; j<3; ++j, print = true ) {
82         
83             if ( j == 1 ) {
84                 print = true;
85                 testSuite = "NORMAL";
86                 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
87             } else if ( j == 2 ) {
88                 testSuite = "ANTI-ALIASING";
89                 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
90             }
91             
92             if ( print ) {
93                 printHeader();
94             }
95             
96             // lines-direct
97
n = 10000;
98             startTest("lines-direct", n);
99             for ( int i=0; i<n; ++i ) {
100                 g2.drawLine(x,y,w,h);
101             }
102             endTest(print);
103             
104             // lines-shape
105
n = 10000;
106             Line2D JavaDoc line = new Line2D.Float JavaDoc(xf,yf,wf,hf);
107             startTest("lines-shape", n);
108             for ( int i=0; i<n; ++i ) {
109                 g2.draw(line);
110             }
111             line = null;
112             endTest(print);
113             
114             // rect-direct-draw
115
n = 10000;
116             startTest("rect-direct-draw", n);
117             for ( int i=0; i<n; ++i ) {
118                 g2.drawRect(x,y,w,h);
119             }
120             endTest(print);
121             
122             // rect-shape-draw
123
n = 10000;
124             Rectangle2D JavaDoc rect = new Rectangle2D.Float JavaDoc(xf,yf,wf,hf);
125             startTest("rect-shape-draw", n);
126             for ( int i=0; i<n; ++i ) {
127                 g2.draw(rect);
128             }
129             rect = null;
130             endTest(print);
131             
132             // rect-direct-fill
133
n = 10000;
134             startTest("rect-direct-fill", n);
135             for ( int i=0; i<n; ++i ) {
136                 g2.fillRect(x,y,w,h);
137             }
138             endTest(print);
139             
140             // rect-shape-fill
141
rect = new Rectangle2D.Float JavaDoc(xf,yf,wf,hf);
142             startTest("rect-shape-fill", n);
143             for ( int i=0; i<n; ++i ) {
144                 g2.fill(rect);
145             }
146             rect = null;
147             endTest(print);
148
149             // rrect-direct-draw
150
startTest("rrect-direct-draw", n);
151             for ( int i=0; i<n; ++i ) {
152                 g2.drawRoundRect(x,y,w,h,c,c);
153             }
154             endTest(print);
155             
156             // rrect-shape-draw
157
RoundRectangle2D JavaDoc rrect = new RoundRectangle2D.Float JavaDoc(xf,yf,wf,hf,cf,cf);
158             startTest("rrect-shape-draw", n);
159             for ( int i=0; i<n; ++i ) {
160                 g2.draw(rrect);
161             }
162             rrect = null;
163             endTest(print);
164             
165             // rrect-direct-fill
166
startTest("rrect-direct-fill", n);
167             for ( int i=0; i<n; ++i ) {
168                 g2.fillRoundRect(x,y,w,h,c,c);
169             }
170             endTest(print);
171             
172             // rrect-shape-fill
173
rrect = new RoundRectangle2D.Float JavaDoc(xf,yf,wf,hf,cf,cf);
174             startTest("rrect-shape-fill", n);
175             for ( int i=0; i<n; ++i ) {
176                 g2.fill(rrect);
177             }
178             rrect = null;
179             endTest(print);
180             
181             // text-direct-int
182
String JavaDoc text = "This is some sample text.";
183             startTest("text-direct-int", n);
184             for ( int i=0; i<n; ++i ) {
185                 g2.drawString(text, x+2, h/2);
186             }
187             endTest(print);
188             
189             // text-direct-float
190
startTest("text-direct-float", n);
191             for ( int i=0; i<n; ++i ) {
192                 g2.drawString(text, xf+2, hf/2);
193             }
194             endTest(print);
195             
196             // text-glyph-vector
197
Font JavaDoc f = g2.getFont();
198             GlyphVector JavaDoc gvec
199                 = f.createGlyphVector(g2.getFontRenderContext(), text);
200             startTest("text-glyph-vector", n);
201             for ( int i=0; i<n; ++i ) {
202                 g2.drawGlyphVector(gvec, xf+2, hf/2);
203             }
204             endTest(print);
205             
206             if ( print ) {
207                 s_logger.info("Rendering Benchmarks: "+testSuite+'\n'
208                         +sbuf.toString());
209                 sbuf.replace(0, sbuf.length(), "");
210             }
211         }
212         System.exit(0);
213     }
214     
215     public static void main(String JavaDoc[] args) {
216         JFrame JavaDoc f = new JFrame JavaDoc("Rendering Test");
217         f.setSize(500, 500);
218         f.getContentPane().add(new RenderingBenchmarks());
219         f.pack();
220         f.setVisible(true);
221     }
222     
223 }
224
Popular Tags