Path: blob/master/test/jdk/sun/java2d/loops/CopyAreaSpeed.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 418907025* @summary This test prints out the time it takes for a certain amount of26* copyArea calls to be completed. Because the performance measurement is27* relative, this code only provides a benchmark to run with different releases28* to compare the outcomes.29* @run applet/manual=done CopyAreaSpeed.html30*/3132import java.applet.Applet;33import java.awt.*;34import java.awt.event.*;35import java.util.*;3637public class CopyAreaSpeed extends Applet implements Runnable {38int top = 0;3940public void init() {41}4243public CopyAreaSpeed()44{45super();46String[] instructions =47{48"This test prints out the time it takes for a certain amount ",49"of copyArea calls to be completed. Because the performance ",50"measurement is relative, this code only provides a benchmark ",51"to run with different releases to compare the outcomes."52};53Sysout.createDialogWithInstructions( instructions );54(new Thread(this)).start();55Button bt = new Button("Hello");56bt.setBounds(50, 10, 50, 22);57bt.setVisible(false);58add(bt);59}6061public void update(Graphics g)62{63paint(g);64}6566public void paint(Graphics g)67{68synchronized(this) {69Rectangle rct = g.getClipBounds();70g.setColor(Color.white);71g.fillRect(rct.x, rct.y, rct.width, rct.height);72g.setFont(getFont());73g.setColor(Color.black);7475Dimension dm = getSize();76for (int y = 0; y <= (dm.height + 10); y += 20) {77if (y > rct.y) {78int z = y / 20 + top;79g.drawString("" + z, 10, y);80} /* endif */81} // endfor82}83}8485static long millsec(Date s, Date e) {86long ts = s.getTime();87long te = e.getTime();88return te-ts;89}9091public void run()92{93int count = 1000;94int loops = count;95Date start;96Date end;9798start = new Date();99while (count-- > 0) {100Dimension dm = getSize();101if (dm != null && dm.width != 0 && dm.height != 0) {102synchronized(this) {103top++;104Graphics g = getGraphics();105g.copyArea(0, 20, dm.width, dm.height - 20, 0, -20);106g.setClip(0, dm.height - 20, dm.width, 20);107paint(g);108g.dispose();109}110}111try {112Thread.sleep(1);113} catch(Exception ex) {114ex.printStackTrace();115}116}117end = new Date();118Sysout.println("copyArea X "+loops+" = "+ millsec(start, end) + " msec");119}120121public static void main(String args[]) {122Frame frm = new Frame("CopyAreaSpeed");123frm.add(new CopyAreaSpeed());124frm.addWindowListener(new WindowAdapter() {125public void windowClosing(WindowEvent ev) {126System.exit(0);127}128});129frm.setSize(500, 500);130frm.show();131}132}133/****************************************************134Standard Test Machinery135DO NOT modify anything below -- it's a standard136chunk of code whose purpose is to make user137interaction uniform, and thereby make it simpler138to read and understand someone else's test.139****************************************************/140141/**142This is part of the standard test machinery.143It creates a dialog (with the instructions), and is the interface144for sending text messages to the user.145To print the instructions, send an array of strings to Sysout.createDialog146WithInstructions method. Put one line of instructions per array entry.147To display a message for the tester to see, simply call Sysout.println148with the string to be displayed.149This mimics System.out.println but works within the test harness as well150as standalone.151*/152class Sysout153{154private static TestDialog dialog;155156public static void createDialogWithInstructions( String[] instructions )157{158dialog = new TestDialog( new Frame(), "Instructions" );159dialog.printInstructions( instructions );160dialog.show();161println( "Any messages for the tester will display here." );162}163164public static void createDialog( )165{166dialog = new TestDialog( new Frame(), "Instructions" );167String[] defInstr = { "Instructions will appear here. ", "" } ;168dialog.printInstructions( defInstr );169dialog.show();170println( "Any messages for the tester will display here." );171}172173174public static void printInstructions( String[] instructions )175{176dialog.printInstructions( instructions );177}178179180public static void println( String messageIn )181{182dialog.displayMessage( messageIn );183}184185}// Sysout class186187/**188This is part of the standard test machinery. It provides a place for the189test instructions to be displayed, and a place for interactive messages190to the user to be displayed.191To have the test instructions displayed, see Sysout.192To have a message to the user be displayed, see Sysout.193Do not call anything in this dialog directly.194*/195class TestDialog extends Dialog196{197198TextArea instructionsText;199TextArea messageText;200int maxStringLength = 80;201202//DO NOT call this directly, go through Sysout203public TestDialog( Frame frame, String name )204{205super( frame, name );206int scrollBoth = TextArea.SCROLLBARS_BOTH;207instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );208add( "North", instructionsText );209210messageText = new TextArea( "", 5, maxStringLength, scrollBoth );211add("South", messageText);212213pack();214215show();216}// TestDialog()217218//DO NOT call this directly, go through Sysout219public void printInstructions( String[] instructions )220{221//Clear out any current instructions222instructionsText.setText( "" );223224//Go down array of instruction strings225226String printStr, remainingStr;227for( int i=0; i < instructions.length; i++ )228{229//chop up each into pieces maxSringLength long230remainingStr = instructions[ i ];231while( remainingStr.length() > 0 )232{233//if longer than max then chop off first max chars to print234if( remainingStr.length() >= maxStringLength )235{236//Try to chop on a word boundary237int posOfSpace = remainingStr.238lastIndexOf( ' ', maxStringLength - 1 );239240if( posOfSpace <= 0 ) {241posOfSpace = maxStringLength - 1;242}243244printStr = remainingStr.substring( 0, posOfSpace + 1 );245remainingStr = remainingStr.substring( posOfSpace + 1 );246}247else //else just print248{249printStr = remainingStr;250remainingStr = "";251}252253instructionsText.append( printStr + "\n" );254255}// while256257}// for258259}//printInstructions()260261//DO NOT call this directly, go through Sysout262public void displayMessage( String messageIn )263{264messageText.append( messageIn + "\n" );265}266267}// TestDialog class268269270