Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Multiscreen/WPanelPeerPerf/WPanelPeerPerf.java
38828 views
/*1* Copyright (c) 2004, 2007, 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*/2223/*24@test25@bug 508562626@summary Exponential performance regression in AWT components (multiple mon)27@run main WPanelPeerPerf28*/2930import java.awt.*;31import java.awt.event.*;32import javax.swing.*;3334/**35* This test must be run on a multi-screen system.36* This test works by moving a Frame back and forth between the screens a few37* times. When the bug is active, the first move will overwhelm the EDT with38* recursive display change calls. The test fails if it takes too long to39* service the setLocation() calls and send componentMoved() events.40*/41public class WPanelPeerPerf {4243private static final int NESTED_PANELS = 25;44private static final int ITERATIONS_PER_SCREEN = 3;45private static final int MAX_WAIT_PER_SCREEN = 2500;46private static final int PAUSE_BETWEEN_MOVES = 500;474849private static Object showLock = new Object();5051private static Counter instance = null;52public static Counter getCounter() {53if (instance == null) {54instance = new Counter();55}56return instance;57}5859private static class Counter {60int counter;61Counter() { counter = 0; }62}6364// This one is very slow!65public static void testAWT() {66// fail if only on one screen67int numScreens = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length;68if (numScreens < 2) {69System.err.println("Test must be run on a multiscreen system");70return;71}72final Frame frame = new Frame("AWT WPanelPeerPerf");73frame.addWindowListener(new WindowAdapter() {74public void windowClosing(WindowEvent ev) {75System.exit(0);76}77public void windowOpened(WindowEvent e) {78synchronized(showLock) {79showLock.notify();80}81}82});83frame.setLayout(new BorderLayout());84Label label = new Label("Hello world");85frame.add(label, BorderLayout.NORTH);86Panel panel = new Panel(new BorderLayout());87Panel currentPanel = panel;88for (int i = 0; i < NESTED_PANELS; i++) {89Panel newPanel = new Panel(new BorderLayout());90currentPanel.add(newPanel, BorderLayout.CENTER);91currentPanel = newPanel;92}93currentPanel.add(new Label("WPanelPeerPerf"));94frame.add(panel, BorderLayout.CENTER);95Button btn = new Button("OK");96frame.add(btn, BorderLayout.SOUTH);97frame.pack();9899frame.addComponentListener(new ComponentAdapter() {100public void componentMoved(ComponentEvent e) {101System.out.println("Frame moved: ");102Counter ctr = getCounter();103synchronized(ctr) {104ctr.counter++;105ctr.notify();106}107}108});109synchronized(showLock) {110try {111frame.setVisible(true);112showLock.wait();113}114catch (InterruptedException e) {115e.printStackTrace();116throw new RuntimeException("Problem with showLock");117}118}119runTest(frame);120}121122public static void runTest(Frame theFrame) {123System.out.println("Running test");124GraphicsDevice[] devs = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();125Point[] points = new Point[devs.length];126127for (int i = 0; i < points.length; i++) {128Rectangle bounds = devs[i].getDefaultConfiguration().getBounds();129points[i] = new Point(bounds.x + (bounds.width / 2),130bounds.y + (bounds.height / 2));131System.out.println("Added point:" + points[i]);132}133134final Frame localFrame = theFrame;135136for (int n = 0; n < ITERATIONS_PER_SCREEN; n++) {137for (int i = 0; i < points.length; i++) {138final Point contextPoint = points[i];139SwingUtilities.invokeLater(new Runnable() {140public void run() {141localFrame.setLocation(contextPoint);142}143});144try {145Thread.sleep(PAUSE_BETWEEN_MOVES);146}147catch (InterruptedException e) {148System.out.println("Interrupted during iteration");149}150}151}152Counter ctr = getCounter();153synchronized(ctr) {154try {155if (ctr.counter < ITERATIONS_PER_SCREEN * devs.length) {156// If test hasn't finished, wait for maximum time157// If we get interrupted, test fails158ctr.wait((long)(ITERATIONS_PER_SCREEN * MAX_WAIT_PER_SCREEN * devs.length));159System.out.println("after wait");160if (ctr.counter < ITERATIONS_PER_SCREEN * devs.length) {161throw new RuntimeException("Waited too long for all the componentMoved()s");162}163}164}165catch(InterruptedException e) {166e.printStackTrace();167throw new RuntimeException("Wait interrupted - ???");168}169System.out.println("Counter reads: " + ctr.counter);170}171172}173public static void main(String[] args) {174testAWT();175}176}177178179