001: package test;
002:
003: import java.awt.Color;
004: import java.awt.Dimension;
005: import java.awt.Font;
006: import java.awt.Graphics;
007: import java.awt.Graphics2D;
008: import java.awt.RenderingHints;
009: import java.awt.Toolkit;
010: import java.awt.font.GlyphVector;
011: import java.awt.geom.Line2D;
012: import java.awt.geom.Rectangle2D;
013: import java.awt.geom.RoundRectangle2D;
014: import java.util.logging.Logger;
015:
016: import javax.swing.JComponent;
017: import javax.swing.JFrame;
018:
019: /**
020: * RenderingBenchmarks
021: *
022: * @author <a href="http://jheer.org">jeffrey heer</a>
023: */
024: public class RenderingBenchmarks extends JComponent {
025:
026: private static final Logger s_logger = Logger
027: .getLogger(RenderingBenchmarks.class.getName());
028:
029: private StringBuffer sbuf = new StringBuffer();
030: private String testSuite;
031: private String curTest;
032: private int numItems;
033: private long timein;
034: private int fps = 20;
035:
036: public RenderingBenchmarks() {
037: this .setPreferredSize(new Dimension(500, 500));
038: }
039:
040: private void startTest(String name, int numItems) {
041: if (curTest != null)
042: throw new IllegalStateException("In the middle of a test!");
043: this .curTest = name;
044: this .numItems = numItems;
045: Toolkit tk = Toolkit.getDefaultToolkit();
046: tk.sync();
047: this .timein = System.currentTimeMillis();
048: }
049:
050: private void endTest(boolean print) {
051: if (print) {
052: long t = System.currentTimeMillis() - timein;
053: double pps = 1000 * ((double) numItems) / t;
054: double ppf = pps / fps;
055: sbuf.append(curTest).append(" ").append(
056: curTest.length() > 14 ? "\t" : "\t\t").append(
057: numItems).append(" \t").append(t / 1000.0).append(
058: "s\t").append(((int) (pps * 100)) / 100.0).append(
059: " pr/s\t").append(((int) (ppf * 100)) / 100.0)
060: .append(" pr/fr").append('\n');
061: }
062: curTest = null;
063: }
064:
065: public void printHeader() {
066: sbuf
067: .append("PRIMITIVE\t\tCOUNT\tTIME\tPRIMITIVES/SEC\tPRIMITIVES/FRAME @ ");
068: sbuf.append(fps).append("fps").append('\n');
069: }
070:
071: public void paintComponent(Graphics g) {
072: Graphics2D g2 = (Graphics2D) g;
073:
074: int x = 0, y = 0, w = 100, h = 100, c = 10;
075: float xf = 0f, yf = 0f, wf = 100f, hf = 100f, cf = 10f;
076: int n;
077:
078: g2.setColor(Color.BLACK);
079: boolean print = false;
080:
081: for (int j = 0; j < 3; ++j, print = true) {
082:
083: if (j == 1) {
084: print = true;
085: testSuite = "NORMAL";
086: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
087: RenderingHints.VALUE_ANTIALIAS_OFF);
088: } else if (j == 2) {
089: testSuite = "ANTI-ALIASING";
090: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
091: RenderingHints.VALUE_ANTIALIAS_ON);
092: }
093:
094: if (print) {
095: printHeader();
096: }
097:
098: // lines-direct
099: n = 10000;
100: startTest("lines-direct", n);
101: for (int i = 0; i < n; ++i) {
102: g2.drawLine(x, y, w, h);
103: }
104: endTest(print);
105:
106: // lines-shape
107: n = 10000;
108: Line2D line = new Line2D.Float(xf, yf, wf, hf);
109: startTest("lines-shape", n);
110: for (int i = 0; i < n; ++i) {
111: g2.draw(line);
112: }
113: line = null;
114: endTest(print);
115:
116: // rect-direct-draw
117: n = 10000;
118: startTest("rect-direct-draw", n);
119: for (int i = 0; i < n; ++i) {
120: g2.drawRect(x, y, w, h);
121: }
122: endTest(print);
123:
124: // rect-shape-draw
125: n = 10000;
126: Rectangle2D rect = new Rectangle2D.Float(xf, yf, wf, hf);
127: startTest("rect-shape-draw", n);
128: for (int i = 0; i < n; ++i) {
129: g2.draw(rect);
130: }
131: rect = null;
132: endTest(print);
133:
134: // rect-direct-fill
135: n = 10000;
136: startTest("rect-direct-fill", n);
137: for (int i = 0; i < n; ++i) {
138: g2.fillRect(x, y, w, h);
139: }
140: endTest(print);
141:
142: // rect-shape-fill
143: rect = new Rectangle2D.Float(xf, yf, wf, hf);
144: startTest("rect-shape-fill", n);
145: for (int i = 0; i < n; ++i) {
146: g2.fill(rect);
147: }
148: rect = null;
149: endTest(print);
150:
151: // rrect-direct-draw
152: startTest("rrect-direct-draw", n);
153: for (int i = 0; i < n; ++i) {
154: g2.drawRoundRect(x, y, w, h, c, c);
155: }
156: endTest(print);
157:
158: // rrect-shape-draw
159: RoundRectangle2D rrect = new RoundRectangle2D.Float(xf, yf,
160: wf, hf, cf, cf);
161: startTest("rrect-shape-draw", n);
162: for (int i = 0; i < n; ++i) {
163: g2.draw(rrect);
164: }
165: rrect = null;
166: endTest(print);
167:
168: // rrect-direct-fill
169: startTest("rrect-direct-fill", n);
170: for (int i = 0; i < n; ++i) {
171: g2.fillRoundRect(x, y, w, h, c, c);
172: }
173: endTest(print);
174:
175: // rrect-shape-fill
176: rrect = new RoundRectangle2D.Float(xf, yf, wf, hf, cf, cf);
177: startTest("rrect-shape-fill", n);
178: for (int i = 0; i < n; ++i) {
179: g2.fill(rrect);
180: }
181: rrect = null;
182: endTest(print);
183:
184: // text-direct-int
185: String text = "This is some sample text.";
186: startTest("text-direct-int", n);
187: for (int i = 0; i < n; ++i) {
188: g2.drawString(text, x + 2, h / 2);
189: }
190: endTest(print);
191:
192: // text-direct-float
193: startTest("text-direct-float", n);
194: for (int i = 0; i < n; ++i) {
195: g2.drawString(text, xf + 2, hf / 2);
196: }
197: endTest(print);
198:
199: // text-glyph-vector
200: Font f = g2.getFont();
201: GlyphVector gvec = f.createGlyphVector(g2
202: .getFontRenderContext(), text);
203: startTest("text-glyph-vector", n);
204: for (int i = 0; i < n; ++i) {
205: g2.drawGlyphVector(gvec, xf + 2, hf / 2);
206: }
207: endTest(print);
208:
209: if (print) {
210: s_logger.info("Rendering Benchmarks: " + testSuite
211: + '\n' + sbuf.toString());
212: sbuf.replace(0, sbuf.length(), "");
213: }
214: }
215: System.exit(0);
216: }
217:
218: public static void main(String[] args) {
219: JFrame f = new JFrame("Rendering Test");
220: f.setSize(500, 500);
221: f.getContentPane().add(new RenderingBenchmarks());
222: f.pack();
223: f.setVisible(true);
224: }
225:
226: }
|