1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  * @test
  25  * @bug 4189070
  26  * @summary This test prints out the time it takes for a certain amount of 
  27  * copyArea calls to be completed. Because the performance measurement is 
  28  * relative, this code only provides a benchmark to run with different releases 
  29  * to compare the outcomes.
  30  * @run applet/manual=done CopyAreaSpeed.html
  31  */
  32 
  33 import java.applet.Applet;
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 import java.util.*;
  37 
  38 public class CopyAreaSpeed extends Applet implements Runnable {
  39     int top = 0;
  40 
  41     public void init() {
  42     }
  43 
  44     public CopyAreaSpeed()
  45     {
  46         super();
  47         String[] instructions =
  48         {
  49             "This test prints out the time it takes for a certain amount ",
  50             "of copyArea calls to be completed. Because the performance ",
  51             "measurement is relative, this code only provides a benchmark ",
  52             "to run with different releases to compare the outcomes."
  53         };
  54         Sysout.createDialogWithInstructions( instructions );
  55         (new Thread(this)).start();
  56         Button bt = new Button("Hello");
  57         bt.setBounds(50, 10, 50, 22);
  58         bt.setVisible(false);
  59         add(bt);
  60     }
  61 
  62     public void update(Graphics g)
  63     {
  64         paint(g);
  65     }
  66 
  67     public void paint(Graphics g)
  68     {
  69         synchronized(this) {
  70             Rectangle rct = g.getClipBounds();
  71             g.setColor(Color.white);
  72             g.fillRect(rct.x, rct.y, rct.width, rct.height);
  73             g.setFont(getFont());
  74             g.setColor(Color.black);
  75 
  76             Dimension dm = getSize();
  77             for (int y = 0; y <= (dm.height + 10); y += 20) {
  78                 if (y > rct.y) {
  79                     int z = y / 20 + top;
  80                     g.drawString("" + z, 10, y);
  81                 }               /* endif */
  82             }                   // endfor
  83         }
  84     }
  85 
  86     static long millsec(Date s, Date e) {
  87         long ts = s.getTime();
  88         long te = e.getTime();
  89         return te-ts;
  90     }
  91 
  92     public void run()
  93     {
  94         int count = 1000;
  95         int loops = count;
  96         Date start;
  97         Date end;
  98 
  99         start = new Date();
 100         while (count-- > 0) {
 101             Dimension dm = getSize();
 102             if (dm != null && dm.width != 0 && dm.height != 0) {
 103                 synchronized(this) {
 104                     top++;
 105                     Graphics g = getGraphics();
 106                     g.copyArea(0, 20, dm.width, dm.height - 20, 0, -20);
 107                     g.setClip(0, dm.height - 20, dm.width, 20);
 108                     paint(g);
 109                     g.dispose();
 110                 }
 111             }
 112             try {
 113                 Thread.sleep(1);
 114             } catch(Exception ex) {
 115                 ex.printStackTrace();
 116             }
 117         }
 118         end = new Date();
 119         Sysout.println("copyArea X "+loops+" = "+ millsec(start, end) + " msec");
 120     }
 121 
 122     public static void main(String args[]) {
 123         Frame frm = new Frame("CopyAreaSpeed");
 124         frm.add(new CopyAreaSpeed());
 125         frm.addWindowListener(new WindowAdapter() {
 126             public void windowClosing(WindowEvent ev) {
 127                 System.exit(0);
 128             }
 129         });
 130         frm.setSize(500, 500);
 131         frm.show();
 132     }
 133 }
 134 /****************************************************
 135  Standard Test Machinery
 136  DO NOT modify anything below -- it's a standard
 137   chunk of code whose purpose is to make user
 138   interaction uniform, and thereby make it simpler
 139   to read and understand someone else's test.
 140  ****************************************************/
 141 
 142 /**
 143  This is part of the standard test machinery.
 144  It creates a dialog (with the instructions), and is the interface
 145   for sending text messages to the user.
 146  To print the instructions, send an array of strings to Sysout.createDialog
 147   WithInstructions method.  Put one line of instructions per array entry.
 148  To display a message for the tester to see, simply call Sysout.println
 149   with the string to be displayed.
 150  This mimics System.out.println but works within the test harness as well
 151   as standalone.
 152  */
 153 class Sysout
 154 {
 155     private static TestDialog dialog;
 156 
 157     public static void createDialogWithInstructions( String[] instructions )
 158     {
 159         dialog = new TestDialog( new Frame(), "Instructions" );
 160         dialog.printInstructions( instructions );
 161         dialog.show();
 162         println( "Any messages for the tester will display here." );
 163     }
 164 
 165     public static void createDialog( )
 166     {
 167         dialog = new TestDialog( new Frame(), "Instructions" );
 168         String[] defInstr = { "Instructions will appear here. ", "" } ;
 169         dialog.printInstructions( defInstr );
 170         dialog.show();
 171         println( "Any messages for the tester will display here." );
 172     }
 173 
 174 
 175     public static void printInstructions( String[] instructions )
 176     {
 177         dialog.printInstructions( instructions );
 178     }
 179 
 180 
 181     public static void println( String messageIn )
 182     {
 183         dialog.displayMessage( messageIn );
 184     }
 185 
 186 }// Sysout  class
 187 
 188 /**
 189   This is part of the standard test machinery.  It provides a place for the
 190    test instructions to be displayed, and a place for interactive messages
 191    to the user to be displayed.
 192   To have the test instructions displayed, see Sysout.
 193   To have a message to the user be displayed, see Sysout.
 194   Do not call anything in this dialog directly.
 195   */
 196 class TestDialog extends Dialog
 197 {
 198 
 199     TextArea instructionsText;
 200     TextArea messageText;
 201     int maxStringLength = 80;
 202 
 203     //DO NOT call this directly, go through Sysout
 204     public TestDialog( Frame frame, String name )
 205     {
 206         super( frame, name );
 207         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 208         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 209         add( "North", instructionsText );
 210 
 211         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 212         add("South", messageText);
 213 
 214         pack();
 215 
 216         show();
 217     }// TestDialog()
 218 
 219     //DO NOT call this directly, go through Sysout
 220     public void printInstructions( String[] instructions )
 221     {
 222         //Clear out any current instructions
 223         instructionsText.setText( "" );
 224 
 225         //Go down array of instruction strings
 226 
 227         String printStr, remainingStr;
 228         for( int i=0; i < instructions.length; i++ )
 229         {
 230             //chop up each into pieces maxSringLength long
 231             remainingStr = instructions[ i ];
 232             while( remainingStr.length() > 0 )
 233             {
 234                 //if longer than max then chop off first max chars to print
 235                 if( remainingStr.length() >= maxStringLength )
 236                 {
 237                     //Try to chop on a word boundary
 238                    int posOfSpace = remainingStr.
 239                    lastIndexOf( ' ', maxStringLength - 1 );
 240 
 241                    if( posOfSpace <= 0 ) {
 242                        posOfSpace = maxStringLength - 1;
 243                    }
 244 
 245                    printStr = remainingStr.substring( 0, posOfSpace + 1 );
 246                    remainingStr = remainingStr.substring( posOfSpace + 1 );
 247                 }
 248                 else //else just print
 249                 {
 250                     printStr = remainingStr;
 251                     remainingStr = "";
 252                 }
 253 
 254                 instructionsText.append( printStr + "\n" );
 255 
 256             }// while
 257 
 258         }// for
 259 
 260     }//printInstructions()
 261 
 262     //DO NOT call this directly, go through Sysout
 263     public void displayMessage( String messageIn )
 264     {
 265         messageText.append( messageIn + "\n" );
 266     }
 267 
 268 }// TestDialog  class