OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / awt / AicasGraphicsBenchmark.java
1 /* AnimationApplet.java -- An example of an old-style AWT applet
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath examples.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA. */
20
21 package gnu.classpath.examples.awt;
22
23 import java.awt.BorderLayout;
24 import java.awt.Canvas;
25 import java.awt.Color;
26 import java.awt.Dimension;
27 import java.awt.Frame;
28 import java.awt.Graphics;
29 import java.awt.Image;
30 import java.awt.Insets;
31 import java.awt.Label;
32 import java.awt.Panel;
33 import java.awt.Toolkit;
34 import java.awt.event.WindowAdapter;
35 import java.awt.event.WindowEvent;
36 import java.net.URL;
37 import java.util.Iterator;
38 import java.util.Map;
39 import java.util.StringTokenizer;
40 import java.util.TreeMap;
41 import java.util.logging.Level;
42 import java.util.logging.Logger;
43
44 public class AicasGraphicsBenchmark extends Panel
45 {
46   /**
47    * Default number of test-iterations.
48    */
49   private static final int DEFAULT_TEST_SIZE = 1000;
50
51   /**
52    * Default screen size.
53    */
54   private static final int DEFAULT_SCREEN_WIDTH  = 320;
55   private static final int DEFAULT_SCREEN_HEIGHT = 240;
56
57   /**
58    * AWT tests.
59    */
60   private static final int AWTTEST_LINES = 1 << 0;
61   private static final int AWTTEST_RECT = 1 << 1;
62   private static final int AWTTEST_POLYLINE = 1 << 2;
63   private static final int AWTTEST_POLYGON = 1 << 3;
64   private static final int AWTTEST_ARC = 1 << 4;
65   private static final int AWTTEST_OVAL = 1 << 5;
66   private static final int AWTTEST_ROUNDRECT = 1 << 6;
67   private static final int AWTTEST_STRING = 1 << 7;
68   private static final int AWTTEST_TRANSPARENTIMAGE = 1 << 8;
69   private static final int AWTTEST_IMAGE = 1 << 9;
70
71   private static final int AWTTEST_NONE = 0;
72   private static final int AWTTEST_ALL  =   AWTTEST_LINES
73                                           | AWTTEST_RECT
74                                           | AWTTEST_POLYLINE
75                                           | AWTTEST_POLYGON
76                                           | AWTTEST_ARC
77                                           | AWTTEST_OVAL
78                                           | AWTTEST_ROUNDRECT
79                                           | AWTTEST_STRING
80                                           | AWTTEST_TRANSPARENTIMAGE
81                                           | AWTTEST_IMAGE
82                                           ;
83
84   int iterations = 1;
85   private int screenWidth = DEFAULT_SCREEN_WIDTH;  
86   private int screenHeight = DEFAULT_SCREEN_HEIGHT;  
87   boolean doubleBufferFlag = true;
88   private int awtTests = AWTTEST_ALL;
89
90   private Label testLabel;
91
92   private String testContext = "";
93
94   Logger logger = Logger.getLogger("AicasGraphicsBenchmark");
95
96   private Image pngTestImage;
97   private Image gifTestImage;
98
99   private TestSet testSetMap = new TestSet();
100
101   public AicasGraphicsBenchmark()
102   {
103     pngTestImage = loadImage("../icons/aicas.png");
104     gifTestImage = loadImage("../icons/palme.gif");
105
106     setLayout(new BorderLayout());
107     testLabel = new Label();
108     add(testLabel,BorderLayout.NORTH);
109     add(new GraphicsTest(),BorderLayout.CENTER);
110   }
111
112   void setTestContext(String testName)
113   {
114     logger.logp(Level.INFO, "AicasGraphicsBenchmark", "recordTest",
115                 "--- Starting new test context: " + testName);
116     testContext = testName;
117     testLabel.setText(testName);
118   }
119
120   private void recordTest(String testName, long time)
121   {
122     logger.logp(Level.INFO, "AicasGraphicsBenchmark", "recordTest",
123                 testContext + ": " + testName + " duration (ms): " + time);
124     TestRecorder recorder = testSetMap.getTest(testName);
125     if (recorder == null)
126       {
127         recorder = new TestRecorder(testName);
128         testSetMap.putTest(testName,recorder);
129       }
130     recorder.addRun(time);
131   }
132
133   void printReport()
134   {
135     for (Iterator i = testSetMap.testIterator(); i.hasNext(); )
136     {
137       TestRecorder recorder = testSetMap.getTest((String)i.next());
138       System.out.println("TEST " + recorder.getTestName() + ": average "
139                          + recorder.getAverage() + "ms ["
140                          + recorder.getMinTime() + "-" + recorder.getMaxTime()
141                          + "]");
142     }
143   }
144
145   public static void main(String[] args)
146   {
147     int awtTests;
148     int i;
149     boolean endOfOptionsFlag;
150     AicasGraphicsBenchmark speed= new AicasGraphicsBenchmark();
151
152     // Parse arguments.
153     i = 0;
154     endOfOptionsFlag = false;
155     awtTests = AWTTEST_NONE;
156     while (i < args.length)
157       {
158         if (!endOfOptionsFlag)
159           {
160             if (args[i].equals("--help") || args[i].equals("-help")
161                 || args[i].equals("-h"))
162               {
163                 System.out.println("Usage: AicasGraphicsBenchmark [<options>] [<test>  ...]");
164                 System.out.println("");
165                 System.out.println("Options: -i|--iterations=<n|-1> - number of iterations (-1 is infinite)");
166                 System.out.println("         -w|--width=<n>         - screen width; default "+DEFAULT_SCREEN_WIDTH);
167                 System.out.println("         -h|--height=<n>        - screen height; default "+DEFAULT_SCREEN_HEIGHT);
168                 System.out.println("         -n|--noDoubleBuffer    - disable double-buffering test");
169                 System.out.println("");
170                 System.out.println("Tests: line");
171                 System.out.println("       rect");
172                 System.out.println("       polyline");
173                 System.out.println("       polygon");
174                 System.out.println("       arc");
175                 System.out.println("       oval");
176                 System.out.println("       roundrect");
177                 System.out.println("       string");
178                 System.out.println("       transparentimage");
179                 System.out.println("       image");
180                 System.exit(1);
181               }
182             else if ((args[i].startsWith("-i=")
183                 || args[i].startsWith("--iterations=")))
184               {
185                 speed.iterations =
186                   Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
187                 i += 1;
188                 continue;
189               }
190             else if ((args[i].equals("-i") || args[i].equals("--iterations")))
191               {
192                 if ((i + 1) >= args.length)
193                   {
194                     System.err.println("ERROR: No argument given for option '"
195                                        + args[i] + "'!");
196                     System.exit(2);
197                   }
198                 speed.iterations = Integer.parseInt(args[i + 1]);
199                 i += 2;
200                 continue;
201               }
202             else if ((args[i].startsWith("-w=")
203                 || args[i].startsWith("--width=")))
204               {
205                 speed.screenWidth =
206                   Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
207                 i += 1;
208                 continue;
209               }
210             else if ((args[i].equals("-w") || args[i].equals("--width")))
211               {
212                 if ((i + 1) >= args.length)
213                   {
214                     System.err.println("ERROR: No argument given for option '"
215                                        + args[i] + "'!");
216                     System.exit(2);
217                   }
218                 speed.screenWidth = Integer.parseInt(args[i + 1]);
219                 i += 2;
220                 continue;
221               }
222             else if ((args[i].startsWith("-h=")
223                 || args[i].startsWith("--height=")))
224               {
225                 speed.screenHeight =
226                   Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
227                 i+=1;
228                 continue;
229               }
230             else if ((args[i].equals("-h") || args[i].equals("--height")))
231               {
232                 if ((i+1) >= args.length)
233                   {
234                     System.err.println("ERROR: No argument given for option '"
235                                        + args[i] + "'!");
236                     System.exit(2);
237                   }
238                 speed.screenHeight = Integer.parseInt(args[i + 1]);
239                 i += 2;
240                 continue;
241               }
242             else if ((args[i].equals("-n")
243                 || args[i].equals("--noDoubleBuffer")))
244               {
245                 speed.doubleBufferFlag = false;
246                 i += 1;
247                 continue;
248               }
249             else if (args[i].equals("--"))
250               {
251                 endOfOptionsFlag = true;
252                 i += 1;
253                 continue;
254               }
255             else if (args[i].startsWith("-"))
256               {
257                 System.err.println("ERROR: Unknown option '" + args[i] + "'!");
258                 System.exit(2);
259               }
260           }
261         StringTokenizer tokenizer = new StringTokenizer(args[i], " +,");
262         while (tokenizer.hasMoreTokens())
263           {
264             String s = tokenizer.nextToken().toLowerCase();
265             if (s.equals("line"))
266               awtTests |= AWTTEST_LINES;
267             else if (s.equals("rect"))
268               awtTests |= AWTTEST_RECT;
269             else if (s.equals("polyline"))
270               awtTests |= AWTTEST_POLYLINE;
271             else if (s.equals("polygon"))
272               awtTests |= AWTTEST_POLYGON;
273             else if (s.equals("arc"))
274               awtTests |= AWTTEST_ARC;
275             else if (s.equals("oval"))
276               awtTests |= AWTTEST_OVAL;
277             else if (s.equals("roundrect"))
278               awtTests |= AWTTEST_ROUNDRECT;
279             else if (s.equals("string"))
280               awtTests |= AWTTEST_STRING;
281             else if (s.equals("transparentimage"))
282               awtTests |= AWTTEST_TRANSPARENTIMAGE;
283             else if (s.equals("image"))
284               awtTests |= AWTTEST_IMAGE;
285             else
286               {
287                 System.err.println("Unknown AWT test '" + s + "'!");
288                 System.exit(2);
289               }
290           }
291         i += 1;
292       }
293     if (awtTests != AWTTEST_NONE)
294       speed.awtTests = awtTests;
295
296     // Create graphics.
297     final Frame frame = new Frame("AicasGraphicsBenchmark");
298
299     frame.addWindowListener(new WindowAdapter()
300     {
301       public void windowClosing(WindowEvent e)
302       {
303         frame.setVisible(false);
304         System.exit(0);
305       }
306     });
307
308     frame.add(speed,BorderLayout.CENTER);
309     frame.setSize(speed.screenWidth,speed.screenHeight);
310     frame.setVisible(true);
311
312     // Insets are correctly set only after the native peer was created.
313     Insets insets = frame.getInsets();
314     // The internal size of the frame should be 320x240.
315     frame.setSize(320 + insets.right + insets.left,
316                   240 + insets.top + insets.bottom);
317   }
318
319   private Image loadImage(String imageName)
320   {
321     Image result = null;
322     logger.logp(Level.INFO, "AicasGraphicsBenchmark", "loadImage",
323                 "Loading image: " + imageName);
324     URL url = getClass().getResource(imageName);
325     if (url != null)
326       {
327         result = Toolkit.getDefaultToolkit().getImage(url);
328         prepareImage(result, this);
329       }
330     else
331       {
332         logger.logp(Level.WARNING, "AicasGraphicsBenchmark", "loadImage",
333                     "Could not locate image resource in class path: "
334                     + imageName);
335       }
336     return result;
337   }
338
339   /**
340    * Executes the test methods.
341    *
342    * @param g The Graphics object that is used to paint.
343    * @param size The size of the canvas.
344    */
345   void runTestSet(Graphics g, Dimension size)
346   {
347     if ((awtTests & AWTTEST_LINES) != 0)
348       test_drawLine(g, size);
349     if ((awtTests & AWTTEST_RECT) != 0)
350       test_drawRect(g, size);
351     if ((awtTests & AWTTEST_RECT) != 0)
352       test_fillRect(g, size);
353     if ((awtTests & AWTTEST_POLYLINE) != 0)
354       test_drawPolyline(g, size);
355     if ((awtTests & AWTTEST_POLYGON) != 0)
356       test_drawPolygon(g, size);
357     if ((awtTests & AWTTEST_POLYGON) != 0)
358       test_fillPolygon(g,size);
359     if ((awtTests & AWTTEST_ARC) != 0)
360       test_drawArc(g,size);
361     if ((awtTests & AWTTEST_ARC) != 0)
362       test_fillArc(g,size);
363     if ((awtTests & AWTTEST_OVAL) != 0)
364       test_drawOval(g, size);
365     if ((awtTests & AWTTEST_OVAL) != 0)
366       test_fillOval(g, size);
367     if ((awtTests & AWTTEST_ROUNDRECT) != 0)
368       test_fillRoundRect(g, size);
369     if ((awtTests & AWTTEST_STRING) != 0)
370       test_drawString(g, size);
371     if ((awtTests & AWTTEST_TRANSPARENTIMAGE) != 0)
372       test_drawTransparentImage(g,size);
373     if ((awtTests & AWTTEST_IMAGE) != 0)
374       test_drawImage(g,size);
375   }
376
377   /**
378    * Gets a new random Color.
379    *
380    * @returna new random Color
381    */
382   private Color getNextColor()
383   {
384     return new Color((int) (Math.random() * 254) + 1,
385                      (int) (Math.random() * 254) + 1,
386                      (int) (Math.random() * 254) + 1);
387   }
388
389   /**
390    * Draws random lines within the given dimensions.
391    *
392    * @param g The Graphics object that is used to paint.
393    * @param size The size of the canvas.
394    */
395   private void test_drawLine(Graphics g, Dimension size)
396   {
397     int maxTests = DEFAULT_TEST_SIZE;
398     int minSize = 10;
399     long startTime = System.currentTimeMillis();
400     for (int i=0; i < maxTests; i += 1)
401       {
402         g.setColor(getNextColor());
403         int x1 = (int) (Math.random() * (size.width-minSize));
404         int y1 = (int) (Math.random() * (size.height-minSize));
405         int x2 = (int) (Math.random() * (size.width-minSize));
406         int y2 = (int) (Math.random() * (size.height-minSize));
407         g.drawLine(x1, y1, x2, y2);
408       }
409     long endTime = System.currentTimeMillis();
410     recordTest("drawLine " + maxTests + " times", (endTime-startTime));
411   }
412
413   /**
414    * Draws random rectangles within the given dimensions.
415    *
416    * @param g The Graphics object that is used to paint.
417    * @param size The size of the canvas.
418    */
419   private void test_drawRect(Graphics g, Dimension size)
420   {
421     int maxTests = DEFAULT_TEST_SIZE;
422     int minSize = 10;
423     long startTime = System.currentTimeMillis();
424     for (int i=0; i < maxTests; i += 1)
425       {
426         g.setColor(getNextColor());
427         int x1 = (int) (Math.random() * (size.width-minSize));
428         int y1 = (int) (Math.random() * (size.height-minSize));
429         int x2 = (int) (Math.random() * (size.width-minSize));
430         int y2 = (int) (Math.random() * (size.height-minSize));
431         g.drawRect(x1, y1, x2, y2);
432       }
433     long endTime = System.currentTimeMillis();
434     recordTest("drawRect " + maxTests + " times", (endTime-startTime));
435   }
436
437   /**
438    * Draws random rectangles within the given dimensions.
439    *
440    * @param g The Graphics object that is used to paint.
441    * @param size The size of the canvas.
442    */
443   private void test_fillRect(Graphics g, Dimension size)
444   {
445     int maxTests = DEFAULT_TEST_SIZE;
446     int minSize = 10;
447     long startTime = System.currentTimeMillis();
448     for (int i = 0; i < maxTests; i += 1)
449       {
450         g.setColor(getNextColor());
451         int x1 = (int) (Math.random() * (size.width-minSize));
452         int y1 = (int) (Math.random() * (size.height-minSize));
453         int x2 = (int) (Math.random() * (size.width-minSize));
454         int y2 = (int) (Math.random() * (size.height-minSize));
455         g.fillRect(x1, y1, x2, y2);
456       }
457     long endTime = System.currentTimeMillis();
458     recordTest("fillRect " + maxTests + " times", (endTime-startTime));
459   }
460
461   /**
462    * Draws random polylines within the given dimensions.
463    *
464    * @param g The Graphics object that is used to paint.
465    * @param size The size of the canvas.
466    */
467   private void test_drawPolyline(Graphics g, Dimension size)
468   {
469     int maxTests = DEFAULT_TEST_SIZE;
470     long startTime = System.currentTimeMillis();
471     for (int i=0; i < maxTests; i += 1)
472       {
473         g.setColor(getNextColor());
474         int points = (int)(Math.random() * 6) + 3;
475         int[] x_coords = new int[points];
476         int[] y_coords = new int[points];
477         for (int j = 0; j < points; j+=1)
478           {
479             x_coords[j] = (int)(Math.random() * (size.width));
480             y_coords[j] = (int)(Math.random() * (size.height));
481           }
482         g.drawPolyline(x_coords,y_coords, points);
483       }
484     long endTime = System.currentTimeMillis();
485     recordTest("drawPolyline " + maxTests + " times", (endTime-startTime));
486   }
487
488   /**
489    * Draws random polygons within the given dimensions.
490    * @param g The Graphics object that is used to paint.
491    * @param size The size of the canvas.
492    */
493   private void test_drawPolygon(Graphics g, Dimension size)
494   {
495     int maxTests = DEFAULT_TEST_SIZE;
496     long startTime = System.currentTimeMillis();
497     for (int i=0; i < maxTests; i += 1)
498       {
499         g.setColor(getNextColor());
500         int points = (int) (Math.random() * 6) + 3;
501         int[] xcoords = new int[points];
502         int[] ycoords = new int[points];
503         for(int j = 0; j < points; j+=1)
504           {
505             xcoords[j] = (int) (Math.random() * (size.width));
506             ycoords[j] = (int) (Math.random() * (size.height));
507           }
508         g.drawPolygon(xcoords, ycoords, points);
509       }
510     long endTime = System.currentTimeMillis();
511     recordTest("drawPolygon " + maxTests + " times", (endTime-startTime));
512   }
513
514   /**
515    * Draws random filled polygons within the given dimensions.
516    *
517    * @param g The Graphics object that is used to paint.
518    * @param size The size of the canvas.
519    */
520   private void test_fillPolygon(Graphics g, Dimension size)
521   {
522     int maxTests = DEFAULT_TEST_SIZE;
523     long startTime = System.currentTimeMillis();
524     for (int i=0; i < maxTests; i += 1)
525       {
526         g.setColor(getNextColor());
527         int points = (int) (Math.random() * 6) + 3;
528         int[] xcoords = new int[points];
529         int[] ycoords = new int[points];
530         for (int j = 0; j < points; j+=1)
531           {
532             xcoords[j] = (int) (Math.random() * (size.width));
533             ycoords[j] = (int) (Math.random() * (size.height));
534           }
535         g.fillPolygon(xcoords, ycoords, points);
536       }
537     long endTime = System.currentTimeMillis();
538     recordTest("fillPolygon " + maxTests + " times", (endTime-startTime));
539   }
540
541   /**
542    * Draws random arcs within the given dimensions.
543    *
544    * @param g The Graphics object that is used to paint.
545    * @param size The size of the canvas.
546    */
547   private void test_drawArc(Graphics g, Dimension size)
548   {
549     int maxTests = DEFAULT_TEST_SIZE;
550     int minSize;
551     long startTime;
552     long endTime;
553     minSize = 10;
554     startTime = System.currentTimeMillis();
555     for (int i=0; i < maxTests; i += 1)
556       {
557         g.setColor(getNextColor());
558         int x = (int) (Math.random() * (size.width - minSize + 1));
559         int y = (int) (Math.random() * (size.height - minSize + 1));
560         int width = (int) (Math.random() * (size.width - x - minSize) + minSize);
561         int height = (int) (Math.random() * (size.height - y - minSize) + minSize);
562         int startAngle = (int) (Math.random() * 360);
563         int arcAngle = (int) (Math.random() * 360 - startAngle);
564         g.drawArc(x, y, width, height, startAngle, arcAngle);
565       }
566     endTime = System.currentTimeMillis();
567     recordTest("drawArc " + maxTests + " times", (endTime-startTime));
568   }
569
570   /**
571    * Draws random filled arcs within the given dimensions.
572    * @param g The Graphics object that is used to paint.
573    * @param size The size of the canvas.
574    */
575   private void test_fillArc(Graphics g, Dimension size)
576   {
577     int maxTests = DEFAULT_TEST_SIZE;
578     int minSize;
579     long startTime;
580     long endTime;
581     minSize = 10;
582     startTime = System.currentTimeMillis();
583     for (int i = 0; i < maxTests; i += 1)
584     {
585       g.setColor(getNextColor());
586       int x = (int) (Math.random() * (size.width - minSize + 1));
587       int y = (int) (Math.random() * (size.height - minSize + 1));
588       int width = (int)(Math.random() * (size.width - x - minSize) + minSize);
589       int height = (int)(Math.random() * (size.height - y - minSize) + minSize);
590       int startAngle = (int)(Math.random() * 360);
591       int arcAngle = (int)(Math.random() * 360);
592       g.fillArc(x, y, width, height, startAngle, arcAngle);
593
594     }
595     endTime = System.currentTimeMillis();
596     recordTest("fillArc " + maxTests + " times", (endTime - startTime));
597   }
598
599   /**
600    * Draws random ovals within the given dimensions.
601    *
602    * @param g The Graphics object that is used to paint.
603    * @param size The size of the canvas.
604    */
605   private void test_drawOval(Graphics g, Dimension size)
606   {
607     int maxTests = DEFAULT_TEST_SIZE;
608     int minSize;
609     long startTime;
610     long endTime;
611     minSize = 10;
612     startTime = System.currentTimeMillis();
613     for (int i = 0; i < maxTests; i += 1)
614       {
615         g.setColor(getNextColor());
616         int x = (int)(Math.random() * (size.width - minSize + 1));
617         int y = (int)(Math.random() * (size.height - minSize + 1));
618         int width = (int)(Math.random() * (size.width - x - minSize) + minSize);
619         int height = (int)(Math.random() * (size.height - y - minSize) + minSize);
620         g.drawOval(x, y, Math.min(width, height), Math.min(width, height));
621       }
622     endTime = System.currentTimeMillis();
623     recordTest("drawOval " + maxTests + " times", (endTime-startTime));
624   }
625
626   /**
627    * Draws random filled ovals within the given dimensions.
628    *
629    * @param g The Graphics object that is used to paint.
630    * @param size The size of the canvas.
631    */
632   private void test_fillOval(Graphics g, Dimension size)
633   {
634     int maxTests = DEFAULT_TEST_SIZE;
635     int minSize;
636     long startTime;
637     long endTime;
638     minSize = 10;
639     startTime = System.currentTimeMillis();
640     for (int i = 0; i < maxTests; i += 1)
641       {
642         g.setColor(getNextColor());
643         int x = (int) (Math.random() * (size.width - minSize + 1));
644         int y = (int) (Math.random() * (size.height - minSize + 1));
645         int width = (int) (Math.random() * (size.width - x - minSize) + minSize);
646         int height = (int) (Math.random() * (size.height - y - minSize) + minSize);
647         g.fillOval(x, y, width,height);
648       }
649     endTime = System.currentTimeMillis();
650     recordTest("fillOval " + maxTests + " times", (endTime-startTime));
651   }
652
653   /**
654    * Draws random filled rounded rectangles within the given dimensions.
655    *
656    * @param g The Graphics object that is used to paint.
657    * @param size The size of the canvas.
658    */
659   private void test_fillRoundRect(Graphics g, Dimension size)
660   {
661     int maxTests = DEFAULT_TEST_SIZE;
662     int minSize;
663     long startTime;
664     long endTime;
665     minSize = 10;
666     startTime = System.currentTimeMillis();
667     for (int i=0; i < maxTests; i+=1)
668       {
669         g.setColor(getNextColor());
670         int x = (int) (Math.random() * (size.width - minSize + 1));
671         int y = (int) (Math.random() * (size.height - minSize + 1));
672         int width = (int) (Math.random() * (size.width - x - minSize) + minSize);
673         int height = (int) (Math.random() * (size.height - y - minSize) + minSize);
674         int arcWidth = (int) (Math.random() * (width - 1) + 1);
675         int arcHeight = (int) (Math.random() * (height - 1) + 5);
676         g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
677       }
678     endTime = System.currentTimeMillis();
679     recordTest("fillRoundRect " + maxTests + " times", (endTime-startTime));
680   }
681
682   /**
683    * Draws random images within the given dimensions.
684    *
685    * @param g The Graphics object that is used to paint.
686    * @param size The size of the canvas.
687    */
688   private void test_drawImage(Graphics g, Dimension size)
689   {
690     if (gifTestImage == null)
691       {
692         logger.logp(Level.WARNING, "AicasGraphicsBenchmark", "runTestSet",
693                     "Skipping 'test_drawImage' due to missing resource.");
694         return;
695       }
696     
697     int maxTests = DEFAULT_TEST_SIZE / 2;
698     if(maxTests == 0)
699       maxTests = 1;
700     int imageWidth = gifTestImage.getWidth(this);
701     int imageHeight = gifTestImage.getHeight(this);
702     long  startTime = System.currentTimeMillis();
703     for (int i = 0; i < maxTests; i += 1)
704       {
705         g.setColor(getNextColor());
706         int x = (int) (Math.random() * (size.width - imageWidth + 1));
707         int y = (int) (Math.random() * (size.height - imageHeight + 1));
708         g.drawImage(gifTestImage, x, y, this);
709       }
710     long endTime = System.currentTimeMillis();
711     recordTest("drawImage " + maxTests + " times", (endTime-startTime));
712   }
713
714   /**
715    * Draws random transparent images within the given dimensions.
716    *
717    * @param g The Graphics object that is used to paint.
718    * @param size The size of the canvas.
719    */
720   private void test_drawTransparentImage(Graphics g, Dimension size)
721   {
722     if (pngTestImage == null)
723       {
724         logger.logp(Level.WARNING, "AicasGraphicsBenchmark", "runTestSet",
725                     "Skipping 'test_drawTransparentImage' due to missing resource.");
726         return;
727       }
728
729     
730     int maxTests = DEFAULT_TEST_SIZE / 5;
731     if(maxTests == 0)
732       maxTests = 1;
733     int imageWidth = pngTestImage.getWidth(this);
734     int imageHeight = pngTestImage.getHeight(this);
735     long  startTime = System.currentTimeMillis();
736     for (int i = 0; i < maxTests; i += 1)
737       {
738         g.setColor(getNextColor());
739         int x = (int) (Math.random() * (size.width - imageWidth + 1));
740         int y = (int) (Math.random() * (size.height - imageHeight + 1));
741         g.drawImage(pngTestImage, x, y, this);
742       }
743     long endTime = System.currentTimeMillis();
744     recordTest("draw transparent image " + maxTests + " times",
745                (endTime-startTime));
746   }
747
748   /**
749    * Draws random strings within the given dimensions.
750    *
751    * @param g The Graphics object that is used to paint.
752    * @param size The size of the canvas.
753    */
754   private void test_drawString(Graphics g, Dimension size)
755   {
756       int maxTests = DEFAULT_TEST_SIZE;
757       String testString = "HelloWorld";
758       int stringWidth = g.getFontMetrics().stringWidth(testString);
759       int stringHeight = g.getFontMetrics().getHeight();
760       
761       long startTime = System.currentTimeMillis();
762       for(int i = 0; i < maxTests; i += 1)
763         {
764           g.setColor(getNextColor());
765           g.drawString(testString, (int) (Math.random() * (size.width - stringWidth + 1)),(int)(Math.random() * (size.height - stringHeight + 1)) + stringHeight);
766         }
767       long endTime = System.currentTimeMillis();
768       recordTest("drawString " + maxTests + " times", (endTime-startTime));
769   }
770
771   private class GraphicsTest extends Canvas implements Runnable
772   {
773     Thread paintThread;
774     boolean done = false;
775     boolean doPaint = false;
776     boolean withClipping = false;
777
778     public GraphicsTest()
779     {
780       paintThread = new Thread(this);
781       paintThread.start();
782     }
783
784     public void run()
785     {
786       int runCount = 0;
787       while (!done)
788         {
789           runCount++;
790
791           try
792             {
793               synchronized (this)
794                 {
795                   while (!doPaint)
796                     {
797                       try
798                         {
799                           wait(200);
800                         }
801                       catch (InterruptedException exception)
802                         {
803                           return;
804                         }
805                     }
806                 }
807
808               if (iterations != 0)
809                 System.out.println("--- run...(" + runCount + "/" + iterations
810                                    + ") ------------------------------------------------------");
811
812               Graphics g = getGraphics();
813               Dimension size = getSize();
814               logger.logp(Level.INFO, "AicasGraphicsBenchmark.GraphicsTest", "run",
815               "Start testing non-double-buffered drawing");
816               runSet_noClipping(g,size);
817               runSet_zeroClipping(g, size);
818               runSet_withClipping(g, size);
819               g.dispose();
820
821               if (doubleBufferFlag)
822                 {
823                   logger.logp(Level.INFO, "AicasGraphicsBenchmark.GraphicsTest",
824                               "run", "Start testing double-buffered drawing");
825                   Graphics canvas = getGraphics();
826                   Image doublebuffer = createImage(size.width,size.height);
827                   g = doublebuffer.getGraphics();
828                   runSet_noClipping(g,size);
829                   g.dispose();
830                   canvas.drawImage(doublebuffer, 0, 0, this);
831
832                   g = doublebuffer.getGraphics();
833                   runSet_withClipping(g, size);
834                   g.dispose();
835                   canvas.drawImage(doublebuffer, 0, 0, this);
836
837                   g = doublebuffer.getGraphics();
838                   runSet_zeroClipping(g, size);
839                   g.dispose();
840                   canvas.drawImage(doublebuffer, 0, 0, this);
841                   canvas.dispose();
842                 }
843
844               printReport();
845
846               if (iterations != 0)
847                 {
848                   if (iterations != -1)
849                     iterations--;
850                 }
851               else
852                 {
853                   System.out.println("--- done --------------------------------------------------------");
854                   synchronized (this)
855                     {
856                       doPaint = false;
857                     }
858                   done = true;
859                 }
860             }
861           catch (Error error)
862             {
863               System.err.println("Error: " + error);
864               System.exit(129);
865             }
866         }
867       System.exit(0);
868     }
869
870     private void runSet_zeroClipping(Graphics g, Dimension size)
871     {
872       int clipped_width;
873       int clipped_height;
874       int clipped_x;
875       int clipped_y;
876
877       clipped_width = 0;
878       clipped_height = 0;
879       clipped_x = (size.width) / 2;
880       clipped_y = (size.height) / 2;
881       g.setClip(0, 0, size.width, size.height);
882       g.setColor(Color.BLACK);
883       g.fillRect(0, 0, size.width, size.height);
884       g.setColor(Color.WHITE);
885       g.drawRect(0, 0, size.width - 1, size.height - 1);
886       g.fillRect(clipped_x - 1, clipped_y - 1, clipped_width + 2, clipped_height + 2);
887
888       g.clipRect(clipped_x, clipped_y, clipped_width, clipped_height);
889       g.setColor(Color.BLACK);
890       g.fillRect(0, 0, size.width, size.height);
891
892       setTestContext("clipping to zero");
893
894       runTestSet(g, size);
895     }
896
897     private void runSet_withClipping(Graphics g, Dimension size)
898     {
899       int clipped_width = 2 * size.width / 3;
900       int clipped_height = 2 * size.height / 3;
901       int clipped_x = (size.width - clipped_width) / 2;
902       int clipped_y = (size.height - clipped_height) / 2;
903
904       g.setClip(0,0,size.width,size.height);
905
906       g.setColor(Color.BLACK);
907       g.fillRect(0, 0, size.width, size.height);
908       g.setColor(Color.GREEN);
909       g.drawRect(0, 0, size.width - 1, size.height - 1);
910       g.setColor(Color.WHITE);
911       g.fillRect(clipped_x - 1, clipped_y - 1, clipped_width + 2, clipped_height + 2);
912
913       g.clipRect(clipped_x, clipped_y, clipped_width, clipped_height);
914       g.setColor(Color.BLACK);
915       g.fillRect(0, 0, size.width, size.height);
916
917       setTestContext("with clipping");
918
919       runTestSet(g, size);
920     }
921
922     public void runSet_noClipping(Graphics g, Dimension size)
923     {
924       g.setColor(Color.BLACK);
925       g.fillRect(0, 0, size.width, size.height);
926
927       setTestContext("without clipping");
928
929       runTestSet(g, size);
930     }
931
932     public void paint(Graphics g)
933     {
934       synchronized(this)
935         {
936           doPaint=true;
937           notify();
938         }
939     }
940   }
941 }
942
943 class TestContext
944 {
945 }
946
947 class TestSet
948 {
949   private Map testsMap = new TreeMap();
950
951   public void putTest(String testName, TestRecorder recoder)
952   {
953     testsMap.put(testName,recoder);
954   }
955
956   public TestRecorder getTest(String testName)
957   {
958     return (TestRecorder)testsMap.get(testName);
959   }
960
961   public Iterator testIterator()
962   {
963     return testsMap.keySet().iterator();
964   }
965 }
966
967 class TestRecorder
968 {
969   String test;
970   long   totalTime = 0;
971   long   minTime   = Long.MAX_VALUE;
972   long   maxTime   = Long.MIN_VALUE;
973   int    runCount  = 0;
974
975   /**
976    * @return Returns the maxTime.
977    */
978   public final long getMaxTime()
979   {
980     return maxTime;
981   }
982
983   /**
984    * @return Returns the minTime.
985    */
986   public final long getMinTime()
987   {
988     return minTime;
989   }
990
991   /**
992    * @return Returns the test name.
993    */
994   public final String getTestName()
995   {
996     return test;
997   }
998
999   public final double getAverage()
1000   {
1001     return ((double)totalTime) / ((double)runCount);
1002   }
1003
1004   public TestRecorder(String testName)
1005   {
1006     test = testName;
1007   }
1008
1009   public void addRun(long time)
1010   {
1011     totalTime += time;
1012     if(minTime > time)
1013       minTime = time;
1014     if(maxTime < time)
1015       maxTime = time;
1016     runCount += 1;
1017   }
1018 }